3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.InputStreamReader;
7 import java.io.UnsupportedEncodingException;
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;
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;
21 import com.rometools.rome.io.FeedException;
24 import pnews.Category;
25 import pnews.Language;
27 public class Pnews extends HttpServlet {
28 private static final String CLASS_NAME = Pnews.class.getName();
29 private static final Logger LOG = Logger.getLogger(Pnews.class.getName());
30 private static final long serialVersionUID = 1L;
31 private ArticleProvider provider;
32 private Config config;
34 private static String getQueryParameter(HttpServletRequest rq, String key)
35 throws UnsupportedEncodingException {
36 final String METHOD_NAME="getQueryParameter";
42 LOG.entering(CLASS_NAME, METHOD_NAME, new Object[] { rq, key} );
44 q = rq.getQueryString();
49 params = q.split("&");
51 for (String p: params) {
54 if (idx > 1 && p.substring(0, idx).equals(key))
55 return URLDecoder.decode(p.substring(idx + 1), "UTF-8");
61 private static void redirect(HttpServletRequest rq, HttpServletResponse rp) {
65 LOG.entering(Pnews.class.getName(), "redirect", new Object[] { rq, rp });
68 redirectURL = getQueryParameter(rq, "url");
70 LOG.info("Request redirection to " + redirectURL);
72 if (redirectURL != null) {
73 a = ArticleStore.singleton.get(redirectURL);
75 a.readCount.incrementAndGet();
77 LOG.severe("Cannot find the article " + redirectURL);
79 rp.setHeader("Location", redirectURL);
80 rp.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
82 LOG.severe("No redirection URL");
83 rp.setStatus(HttpServletResponse.SC_NOT_FOUND);
86 } catch (UnsupportedEncodingException e) {
88 LOG.log(Level.SEVERE, "redirect failure", e);
89 rp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
92 LOG.exiting(Pnews.class.getName(), "redirect");
95 private static void doTemporaryRedirect(String newURL, HttpServletResponse rp) {
96 rp.setHeader("Location", newURL);
97 rp.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
100 private void writeStats(HttpServletResponse rp) throws IOException {
101 rp.setContentType("application/json;charset=utf-8");
102 rp.setCharacterEncoding("utf-8");
104 rp.getWriter().write(JSON.getStats(provider, config));
108 private void writeArticles(Category cat, HttpServletResponse rp) {
110 List<Article> articles;
113 articles = provider.getArticles(cat);
114 if (articles != null) {
115 html = HTML.toHTML(articles, cat, config);
116 rp.setContentType("text/html;charset=utf-8");
117 rp.getWriter().write(html);
118 rp.setCharacterEncoding("utf-8");
120 LOG.severe("writeArticles cannot retrieve any articles");
121 html = HTML.toHTML(new ArrayList<Article>(), cat, config);
122 rp.setContentType("text/html");
123 rp.getWriter().write(html);
125 } catch (IOException | IllegalArgumentException | FeedException e) {
126 LOG.log(Level.SEVERE, "writeArticles failure", e);
127 rp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
131 private void copy(InputStream in, Writer writer) throws IOException {
136 buf = new char[1024];
137 r = new InputStreamReader(in);
138 while ( (n = r.read(buf, 0, buf.length)) != -1)
139 writer.write(buf, 0, n);
143 protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
144 final String METHOD_NAME = "doGet";
148 RequesterLog.singleton.writeRequest(req);
150 LOG.info("doGet " + req.getRemoteAddr().toString() + " " + req.getRequestURI() + " " + req.getQueryString());
152 LOG.info(METHOD_NAME + " queryString=" + req.getQueryString());
154 path = req.getPathInfo();
156 if (path.equals("/redirect")) {
161 if (path.equals("/style.css")) {
163 in = HTML.class.getClassLoader().getResourceAsStream("style.css");
164 copy(in, resp.getWriter());
165 resp.setContentType("text/css");
168 } catch (IOException e) {
169 LOG.log(Level.SEVERE, "doGet failure", e);
170 resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
176 if (path.equals("/")) {
177 doTemporaryRedirect(config.getDefaultLanguage().toURL(), resp);
182 if (path.equals("/stats")) {
187 for (Category cat: config.getCategories()) {
188 if (path.equals(cat.getURL())) {
189 writeArticles(cat, resp);
194 for (Language l: config.getLanguages()) {
195 if (path.equals(l.toURL())) {
196 doTemporaryRedirect(config.getDefaultCategory(l).getURL(), resp);
201 resp.getWriter().write("Not found " + req.getPathInfo());
202 resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
203 } catch (IOException | RuntimeException e) {
204 LOG.log(Level.SEVERE, "doGet failure", e);
205 resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
210 public void init(ServletConfig cfg) throws ServletException {
211 LOG.info("Pnews servlet init " + cfg.getServletContext().getContextPath());
213 config = new Config();
216 } catch (UnsupportedEncodingException e) {
217 throw new ServletException(e);
220 provider = new ArticleProvider(config);