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;
26 public class Pnews extends HttpServlet {
27 private static final Logger LOG = Logger.getLogger(Pnews.class.getName());
28 private static final long serialVersionUID = 1L;
29 private static final ArticleProvider provider = ArticleProvider.singleton;
31 private static String getQueryParameter(HttpServletRequest rq, String key)
32 throws UnsupportedEncodingException {
37 q = rq.getQueryString();
42 params = URLDecoder.decode(q, "UTF-8").split("&");
44 for (String p: params) {
47 if (idx > 1 && p.substring(0, idx).equals(key))
48 return p.substring(idx + 1);
54 private static void redirect(HttpServletRequest rq, HttpServletResponse rp) {
58 LOG.entering(Pnews.class.getName(), "redirect");
61 redirectURL = getQueryParameter(rq, "url");
63 LOG.info("Request redirection to " + redirectURL);
65 if (redirectURL != null) {
66 a = ArticleStore.singleton.get(redirectURL);
68 a.readCount.incrementAndGet();
70 LOG.severe("Cannot find the article " + redirectURL);
72 rp.setHeader("Location", redirectURL);
73 rp.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
75 LOG.severe("No redirection URL");
76 rp.setStatus(HttpServletResponse.SC_NOT_FOUND);
79 } catch (UnsupportedEncodingException e) {
81 LOG.log(Level.SEVERE, "redirect failure", e);
82 rp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
85 LOG.exiting(Pnews.class.getName(), "redirect");
88 private void writeStats(HttpServletResponse rp) throws IOException {
89 rp.setContentType("application/json;charset=utf-8");
90 rp.setCharacterEncoding("utf-8");
92 rp.getWriter().write(JSON.getStats());
96 private void writeArticles(Category cat, HttpServletResponse rp) {
98 List<Article> articles;
101 articles = provider.getArticles(cat);
102 if (articles != null) {
103 html = HTML.toHTML(articles, cat);
104 rp.setContentType("text/html;charset=utf-8");
105 rp.getWriter().write(html);
106 rp.setCharacterEncoding("utf-8");
108 LOG.severe("writeArticles cannot retrieve any articles");
109 html = HTML.toHTML(new ArrayList<Article>(), cat);
110 rp.setContentType("text/html");
111 rp.getWriter().write(html);
113 } catch (IOException | IllegalArgumentException | FeedException e) {
114 LOG.log(Level.SEVERE, "writeArticles failure", e);
115 rp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
119 private void copy(InputStream in, Writer writer) throws IOException {
124 buf = new char[1024];
125 r = new InputStreamReader(in);
126 while ( (n = r.read(buf, 0, buf.length)) != -1)
127 writer.write(buf, 0, n);
131 protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
132 final String METHOD_NAME = "doGet";
136 RequesterLog.singleton.writeRequest(req);
138 LOG.info("doGet " + req.getRemoteAddr().toString() + " " + req.getRequestURI() + " " + req.getQueryString());
140 LOG.info(METHOD_NAME + " queryString=" + req.getQueryString());
142 path = req.getPathInfo();
144 if (path.equals("/redirect")) {
149 if (path.equals("/style.css")) {
151 in = HTML.class.getClassLoader().getResourceAsStream("style.css");
152 copy(in, resp.getWriter());
153 resp.setContentType("text/css");
156 } catch (IOException e) {
157 LOG.log(Level.SEVERE, "doGet failure", e);
158 resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
164 if (path.equals("/")) {
165 writeArticles(Category.TOP, resp);
171 if (path.equals("/stats")) {
176 for (Category cat: Category.values()) {
177 if (path.equals('/' + cat.getId())) {
178 writeArticles(cat, resp);
183 resp.getWriter().write("Not found " + req.getPathInfo());
184 resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
185 } catch (IOException e) {
186 LOG.log(Level.SEVERE, "doGet failure", e);
187 resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
192 public void init(ServletConfig config) throws ServletException {
193 LOG.info("Pnews servlet init " + config.getServletContext().getContextPath());