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