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.Locale;
13 import java.util.logging.Level;
14 import java.util.logging.Logger;
16 import javax.servlet.ServletConfig;
17 import javax.servlet.ServletException;
18 import javax.servlet.http.HttpServlet;
19 import javax.servlet.http.HttpServletRequest;
20 import javax.servlet.http.HttpServletResponse;
22 import com.rometools.rome.io.FeedException;
25 import pnews.Category;
28 public class Pnews extends HttpServlet {
29 private static final Logger LOG = Logger.getLogger(Pnews.class.getName());
30 private static final long serialVersionUID = 1L;
31 private static final ArticleProvider provider = ArticleProvider.singleton;
33 private static String getQueryParameter(HttpServletRequest rq, String key)
34 throws UnsupportedEncodingException {
39 q = rq.getQueryString();
44 params = URLDecoder.decode(q, "UTF-8").split("&");
46 for (String p: params) {
49 if (idx > 1 && p.substring(0, idx).equals(key))
50 return p.substring(idx + 1);
56 private static void redirect(HttpServletRequest rq, HttpServletResponse rp) {
60 LOG.entering(Pnews.class.getName(), "redirect");
63 redirectURL = getQueryParameter(rq, "url");
65 LOG.info("Request redirection to " + redirectURL);
67 if (redirectURL != null) {
68 a = ArticleStore.singleton.get(redirectURL);
70 a.readCount.incrementAndGet();
72 LOG.severe("Cannot find the article " + redirectURL);
74 rp.setHeader("Location", redirectURL);
75 rp.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
77 LOG.severe("No redirection URL");
78 rp.setStatus(HttpServletResponse.SC_NOT_FOUND);
81 } catch (UnsupportedEncodingException e) {
83 LOG.log(Level.SEVERE, "redirect failure", e);
84 rp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
87 LOG.exiting(Pnews.class.getName(), "redirect");
90 private void writeStats(HttpServletResponse rp) throws IOException {
91 rp.setContentType("text/html;charset=utf-8");
92 rp.setCharacterEncoding("utf8-8");
94 rp.getWriter().write("" + ArticleStore.singleton);
98 private void writeArticles(Category cat, HttpServletResponse rp) {
100 List<Article> articles;
103 articles = provider.getArticles(cat);
104 if (articles != null) {
105 html = HTML.toHTML(articles, cat);
106 rp.setContentType("text/html;charset=utf-8");
107 rp.getWriter().write(html);
108 rp.setCharacterEncoding("utf8-8");
110 LOG.severe("writeArticles cannot retrieve any articles");
111 html = HTML.toHTML(new ArrayList<Article>(), cat);
112 rp.setContentType("text/html");
113 rp.getWriter().write(html);
115 } catch (IOException | IllegalArgumentException | FeedException e) {
116 LOG.log(Level.SEVERE, "writeArticles failure", e);
117 rp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
121 private void copy(InputStream in, Writer writer) throws IOException {
126 buf = new char[1024];
127 r = new InputStreamReader(in);
128 while ( (n = r.read(buf, 0, buf.length)) != -1)
129 writer.write(buf, 0, n);
133 protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
137 RequesterLog.singleton.writeRequest(req);
139 LOG.info("doGet " + req.getRemoteAddr().toString() + " " + req.getRequestURI() + " " + req.getQueryString());
141 path = req.getPathInfo();
143 if (path.equals("/redirect")) {
148 if (path.equals("/style.css")) {
150 in = HTML.class.getClassLoader().getResourceAsStream("style.css");
151 copy(in, resp.getWriter());
152 resp.setContentType("text/css");
155 } catch (IOException e) {
156 LOG.log(Level.SEVERE, "doGet failure", e);
157 resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
163 if (path.equals("/")) {
164 writeArticles(Category.TOP, resp);
170 if (path.equals("/stats")) {
175 for (Category cat: Category.values()) {
176 if (path.equals('/' + cat.getId())) {
177 writeArticles(cat, resp);
182 resp.getWriter().write("Not found " + req.getPathInfo());
183 resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
184 } catch (IOException e) {
185 LOG.log(Level.SEVERE, "doGet failure", e);
186 resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
191 public void init(ServletConfig config) throws ServletException {
192 LOG.info("Pnews servlet init " + config.getServletContext().getContextPath());