sanity fixes about the encodingY
[pnews.git] / war / src / main / java / pnews / servlet / Pnews.java
1 package pnews.servlet;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.InputStreamReader;
6 import java.io.Reader;
7 import java.io.UnsupportedEncodingException;
8 import java.io.Writer;
9 import java.net.URLDecoder;
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.logging.Level;
13 import java.util.logging.Logger;
14
15 import javax.servlet.ServletConfig;
16 import javax.servlet.ServletException;
17 import javax.servlet.http.HttpServlet;
18 import javax.servlet.http.HttpServletRequest;
19 import javax.servlet.http.HttpServletResponse;
20
21 import com.rometools.rome.io.FeedException;
22
23 import pnews.Article;
24 import pnews.Category;
25 import pnews.HTML;
26
27 public class Pnews extends HttpServlet {
28         private static final Logger LOG = Logger.getLogger(Pnews.class.getName());
29         private static final long serialVersionUID = 1L;
30         private static final ArticleProvider provider = ArticleProvider.singleton;
31
32         private static String getQueryParameter(HttpServletRequest rq, String key)
33                         throws UnsupportedEncodingException {
34                 String[] params;
35                 int idx;
36                 String q;
37
38                 q = rq.getQueryString();
39
40                 if (q == null)
41                         return null;
42
43                 params = URLDecoder.decode(q, "UTF-8").split("&");
44
45                 for (String p: params) {
46                         idx = p.indexOf('=');
47
48                         if (idx > 1 && p.substring(0, idx).equals(key))
49                                 return p.substring(idx + 1);
50                 }
51
52                 return null;
53         }
54
55         private static void redirect(HttpServletRequest rq, HttpServletResponse rp) {
56                 String redirectURL;
57
58                 LOG.entering(Pnews.class.getName(), "redirect");
59
60                 try {
61                         redirectURL = getQueryParameter(rq, "url");
62
63                         LOG.info("Request redirection to " + redirectURL);
64
65                         if (redirectURL != null) {
66                                 rp.setHeader("Location", redirectURL);
67                                 rp.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
68                         } else {
69                                 LOG.severe("No redirection URL");
70                                 rp.setStatus(HttpServletResponse.SC_NOT_FOUND);
71                         }
72
73                 } catch (UnsupportedEncodingException e) {
74                         e.printStackTrace();
75                         LOG.log(Level.SEVERE, "redirect failure", e);
76                         rp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
77                 }
78
79                 LOG.exiting(Pnews.class.getName(), "redirect");
80         }
81
82         private void writeArticles(Category cat, HttpServletResponse rp) {
83                 String html;
84                 List<Article> articles;
85
86                 try {
87                         articles = provider.getArticles(cat);
88                         if (articles != null) {
89                                 html = HTML.toHTML(articles, cat);
90                                 rp.setContentType("text/html");
91                                 rp.getWriter().write(html);
92                                 rp.setCharacterEncoding("UTF-8");
93                         } else {
94                                 LOG.severe("writeArticles cannot retrieve any articles");
95                                 html = HTML.toHTML(new ArrayList<Article>(), cat);
96                                 rp.setContentType("text/html");
97                                 rp.getWriter().write(html);
98                         }
99                 } catch (IOException | IllegalArgumentException | FeedException e) {
100                         LOG.log(Level.SEVERE, "writeArticles failure", e);
101                         rp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
102                 }
103         }
104
105         private void copy(InputStream in, Writer writer) throws IOException {
106                 Reader r;
107                 char[] buf;
108                 int n;
109
110                 buf = new char[1024];
111                 r = new InputStreamReader(in);
112                 while ( (n = r.read(buf, 0, buf.length)) != -1)
113                         writer.write(buf, 0, n);
114         }
115
116         @Override
117         protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
118                 String path;
119                 InputStream in;
120
121                 RequesterLog.singleton.writeRequest(req);
122               
123                 LOG.info("doGet " + req.getRemoteAddr().toString() + " " + req.getRequestURI() + " " + req.getQueryString());
124                                 
125                 path = req.getPathInfo();
126
127                 if (path.equals("/redirect")) {
128                         redirect(req, resp);
129                         return ;
130                 }
131
132                 if (path.equals("/style.css")) {
133                         try {
134                                 in = HTML.class.getClassLoader().getResourceAsStream("style.css");
135                                 copy(in, resp.getWriter());
136                                 resp.setContentType("text/css");
137
138                                 return ;
139                         } catch (IOException e) {
140                                 LOG.log(Level.SEVERE, "doGet failure", e);
141                                 resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
142
143                                 return ;
144                         }
145                 }
146
147                 if (path.equals("/")) {
148                         writeArticles(Category.TOP, resp);
149                         return ;
150                 }
151
152                 for (Category cat: Category.values()) {
153                         if (path.equals('/' + cat.getId())) {
154                                 writeArticles(cat, resp);
155                                 return ;
156                         }
157                 }
158
159                 try {
160                         resp.getWriter().write("Not found " + req.getPathInfo());
161                         resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
162                 } catch (IOException e) {
163                         LOG.log(Level.SEVERE, "doGet failure", e);
164                         resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
165                 }
166         }
167
168         @Override
169         public void init(ServletConfig config) throws ServletException {
170                 LOG.info("Pnews servlet init " + config.getServletContext().getContextPath());
171
172         }
173 }