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;
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;
32 private static String getQueryParameter(HttpServletRequest rq, String key)
33 throws UnsupportedEncodingException {
38 q = rq.getQueryString();
43 params = URLDecoder.decode(q, "UTF-8").split("&");
45 for (String p: params) {
48 if (idx > 1 && p.substring(0, idx).equals(key))
49 return p.substring(idx + 1);
55 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 rp.setHeader("Location", redirectURL);
67 rp.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
69 LOG.severe("No redirection URL");
70 rp.setStatus(HttpServletResponse.SC_NOT_FOUND);
73 } catch (UnsupportedEncodingException e) {
75 LOG.log(Level.SEVERE, "redirect failure", e);
76 rp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
79 LOG.exiting(Pnews.class.getName(), "redirect");
82 private void writeArticles(Category cat, HttpServletResponse rp) {
84 List<Article> articles;
87 articles = provider.getArticles(cat);
88 if (articles != null) {
89 html = HTML.toHTML(articles, cat);
90 rp.setContentType("text/html");
91 rp.getWriter().write(html);
93 LOG.severe("writeArticles cannot retrieve any articles");
94 html = HTML.toHTML(new ArrayList<Article>(), cat);
95 rp.setContentType("text/html");
96 rp.getWriter().write(html);
98 } catch (IOException | IllegalArgumentException | FeedException e) {
99 LOG.log(Level.SEVERE, "writeArticles failure", e);
100 rp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
104 private void copy(InputStream in, Writer writer) throws IOException {
109 buf = new char[1024];
110 r = new InputStreamReader(in);
111 while ( (n = r.read(buf, 0, buf.length)) != -1)
112 writer.write(buf, 0, n);
116 protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
120 RequesterLog.singleton.writeRequest(req);
122 LOG.info("doGet " + req.getRemoteAddr().toString() + " " + req.getRequestURI() + " " + req.getQueryString());
124 path = req.getPathInfo();
126 if (path.equals("/redirect")) {
131 if (path.equals("/style.css")) {
133 in = HTML.class.getClassLoader().getResourceAsStream("style.css");
134 copy(in, resp.getWriter());
135 resp.setContentType("text/css");
138 } catch (IOException e) {
139 LOG.log(Level.SEVERE, "doGet failure", e);
140 resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
146 if (path.equals("/")) {
147 writeArticles(Category.TOP, resp);
151 for (Category cat: Category.values()) {
152 if (path.equals('/' + cat.getId())) {
153 writeArticles(cat, resp);
159 resp.getWriter().write("Not found " + req.getPathInfo());
160 resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
161 } catch (IOException e) {
162 LOG.log(Level.SEVERE, "doGet failure", e);
163 resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
168 public void init(ServletConfig config) throws ServletException {
169 LOG.info("Pnews servlet init " + config.getServletContext().getContextPath());