removed useless import
[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                 Article a;
58
59                 LOG.entering(Pnews.class.getName(), "redirect");
60
61                 try {
62                         redirectURL = getQueryParameter(rq, "url");
63
64                         LOG.info("Request redirection to " + redirectURL);
65
66                         if (redirectURL != null) {
67                                 a = ArticleStore.singleton.get(redirectURL);
68                                 if (a != null)
69                                         a.readCount.incrementAndGet();
70                                 else
71                                         LOG.severe("Cannot find the article " + redirectURL);
72                                 
73                                 rp.setHeader("Location", redirectURL);
74                                 rp.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
75                         } else {
76                                 LOG.severe("No redirection URL");
77                                 rp.setStatus(HttpServletResponse.SC_NOT_FOUND);
78                         }
79
80                 } catch (UnsupportedEncodingException e) {
81                         e.printStackTrace();
82                         LOG.log(Level.SEVERE, "redirect failure", e);
83                         rp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
84                 }
85
86                 LOG.exiting(Pnews.class.getName(), "redirect");
87         }
88
89         private void writeStats(HttpServletResponse rp) throws IOException {
90                 rp.setContentType("text/html;charset=utf-8");
91                 rp.setCharacterEncoding("utf8-8");
92
93                 rp.getWriter().write("" + ArticleStore.singleton);
94         }
95
96         
97         private void writeArticles(Category cat, HttpServletResponse rp) {
98                 String html;
99                 List<Article> articles;
100
101                 try {
102                         articles = provider.getArticles(cat);
103                         if (articles != null) {
104                                 html = HTML.toHTML(articles, cat);
105                                 rp.setContentType("text/html;charset=utf-8");
106                                 rp.getWriter().write(html);
107                                 rp.setCharacterEncoding("utf8-8");
108                         } else {
109                                 LOG.severe("writeArticles cannot retrieve any articles");
110                                 html = HTML.toHTML(new ArrayList<Article>(), cat);
111                                 rp.setContentType("text/html");
112                                 rp.getWriter().write(html);
113                         }
114                 } catch (IOException | IllegalArgumentException | FeedException e) {
115                         LOG.log(Level.SEVERE, "writeArticles failure", e);
116                         rp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
117                 }
118         }
119
120         private void copy(InputStream in, Writer writer) throws IOException {
121                 Reader r;
122                 char[] buf;
123                 int n;
124
125                 buf = new char[1024];
126                 r = new InputStreamReader(in);
127                 while ( (n = r.read(buf, 0, buf.length)) != -1)
128                         writer.write(buf, 0, n);
129         }
130
131         @Override
132         protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
133                 String path;
134                 InputStream in;
135
136                 RequesterLog.singleton.writeRequest(req);
137               
138                 LOG.info("doGet " + req.getRemoteAddr().toString() + " " + req.getRequestURI() + " " + req.getQueryString());
139                                 
140                 path = req.getPathInfo();
141
142                 if (path.equals("/redirect")) {
143                         redirect(req, resp);
144                         return ;
145                 }
146
147                 if (path.equals("/style.css")) {
148                         try {
149                                 in = HTML.class.getClassLoader().getResourceAsStream("style.css");
150                                 copy(in, resp.getWriter());
151                                 resp.setContentType("text/css");
152
153                                 return ;
154                         } catch (IOException e) {
155                                 LOG.log(Level.SEVERE, "doGet failure", e);
156                                 resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
157
158                                 return ;
159                         }
160                 }
161
162                 if (path.equals("/")) {
163                         writeArticles(Category.TOP, resp);
164                         return ;
165                 }
166
167                 try {
168                 
169                         if (path.equals("/stats")) {
170                                 writeStats(resp);
171                                 return ;
172                         }
173                 
174                         for (Category cat: Category.values()) {
175                                 if (path.equals('/' + cat.getId())) {
176                                         writeArticles(cat, resp);
177                                         return ;
178                                 }
179                         }
180                 
181                         resp.getWriter().write("Not found " + req.getPathInfo());
182                         resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
183                 } catch (IOException e) {
184                         LOG.log(Level.SEVERE, "doGet failure", e);
185                         resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
186                 }
187         }
188
189         @Override
190         public void init(ServletConfig config) throws ServletException {
191                 LOG.info("Pnews servlet init " + config.getServletContext().getContextPath());
192
193         }
194 }