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) {
59 LOG.entering(Pnews.class.getName(), "redirect");
62 redirectURL = getQueryParameter(rq, "url");
64 LOG.info("Request redirection to " + redirectURL);
66 if (redirectURL != null) {
67 rp.setHeader("Location", redirectURL);
68 rp.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
70 LOG.severe("No redirection URL");
71 rp.setStatus(HttpServletResponse.SC_NOT_FOUND);
74 } catch (UnsupportedEncodingException e) {
76 LOG.log(Level.SEVERE, "redirect failure", e);
77 rp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
80 LOG.exiting(Pnews.class.getName(), "redirect");
83 private void writeArticles(Category cat, HttpServletResponse rp) {
85 List<Article> articles;
88 articles = provider.getArticles(cat);
89 if (articles != null) {
90 html = HTML.toHTML(articles, cat);
91 rp.setContentType("text/html;charset=utf-8");
92 rp.getWriter().write(html);
93 rp.setCharacterEncoding("utf8-8");
95 LOG.severe("writeArticles cannot retrieve any articles");
96 html = HTML.toHTML(new ArrayList<Article>(), cat);
97 rp.setContentType("text/html");
98 rp.getWriter().write(html);
100 } catch (IOException | IllegalArgumentException | FeedException e) {
101 LOG.log(Level.SEVERE, "writeArticles failure", e);
102 rp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
106 private void copy(InputStream in, Writer writer) throws IOException {
111 buf = new char[1024];
112 r = new InputStreamReader(in);
113 while ( (n = r.read(buf, 0, buf.length)) != -1)
114 writer.write(buf, 0, n);
118 protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
122 RequesterLog.singleton.writeRequest(req);
124 LOG.info("doGet " + req.getRemoteAddr().toString() + " " + req.getRequestURI() + " " + req.getQueryString());
126 path = req.getPathInfo();
128 if (path.equals("/redirect")) {
133 if (path.equals("/style.css")) {
135 in = HTML.class.getClassLoader().getResourceAsStream("style.css");
136 copy(in, resp.getWriter());
137 resp.setContentType("text/css");
140 } catch (IOException e) {
141 LOG.log(Level.SEVERE, "doGet failure", e);
142 resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
148 if (path.equals("/")) {
149 writeArticles(Category.TOP, resp);
153 for (Category cat: Category.values()) {
154 if (path.equals('/' + cat.getId())) {
155 writeArticles(cat, resp);
161 resp.getWriter().write("Not found " + req.getPathInfo());
162 resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
163 } catch (IOException e) {
164 LOG.log(Level.SEVERE, "doGet failure", e);
165 resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
170 public void init(ServletConfig config) throws ServletException {
171 LOG.info("Pnews servlet init " + config.getServletContext().getContextPath());