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 String CLASS_NAME = Pnews.class.getName();
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 {
34 final String METHOD_NAME="getQueryParameter";
40 LOG.entering(CLASS_NAME, METHOD_NAME, new Object[] { rq, key} );
42 q = rq.getQueryString();
47 params = q.split("&");
49 for (String p: params) {
52 if (idx > 1 && p.substring(0, idx).equals(key))
53 return URLDecoder.decode(p.substring(idx + 1), "UTF-8");
59 private static void redirect(HttpServletRequest rq, HttpServletResponse rp) {
63 LOG.entering(Pnews.class.getName(), "redirect", new Object[] { rq, rp });
66 redirectURL = getQueryParameter(rq, "url");
68 LOG.info("Request redirection to " + redirectURL);
70 if (redirectURL != null) {
71 a = ArticleStore.singleton.get(redirectURL);
73 a.readCount.incrementAndGet();
75 LOG.severe("Cannot find the article " + redirectURL);
77 rp.setHeader("Location", redirectURL);
78 rp.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
80 LOG.severe("No redirection URL");
81 rp.setStatus(HttpServletResponse.SC_NOT_FOUND);
84 } catch (UnsupportedEncodingException e) {
86 LOG.log(Level.SEVERE, "redirect failure", e);
87 rp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
90 LOG.exiting(Pnews.class.getName(), "redirect");
93 private void writeStats(HttpServletResponse rp) throws IOException {
94 rp.setContentType("application/json;charset=utf-8");
95 rp.setCharacterEncoding("utf-8");
97 rp.getWriter().write(JSON.getStats());
101 private void writeArticles(Category cat, HttpServletResponse rp) {
103 List<Article> articles;
106 articles = provider.getArticles(cat);
107 if (articles != null) {
108 html = HTML.toHTML(articles, cat);
109 rp.setContentType("text/html;charset=utf-8");
110 rp.getWriter().write(html);
111 rp.setCharacterEncoding("utf-8");
113 LOG.severe("writeArticles cannot retrieve any articles");
114 html = HTML.toHTML(new ArrayList<Article>(), cat);
115 rp.setContentType("text/html");
116 rp.getWriter().write(html);
118 } catch (IOException | IllegalArgumentException | FeedException e) {
119 LOG.log(Level.SEVERE, "writeArticles failure", e);
120 rp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
124 private void copy(InputStream in, Writer writer) throws IOException {
129 buf = new char[1024];
130 r = new InputStreamReader(in);
131 while ( (n = r.read(buf, 0, buf.length)) != -1)
132 writer.write(buf, 0, n);
136 protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
137 final String METHOD_NAME = "doGet";
141 RequesterLog.singleton.writeRequest(req);
143 LOG.info("doGet " + req.getRemoteAddr().toString() + " " + req.getRequestURI() + " " + req.getQueryString());
145 LOG.info(METHOD_NAME + " queryString=" + req.getQueryString());
147 path = req.getPathInfo();
149 if (path.equals("/redirect")) {
154 if (path.equals("/style.css")) {
156 in = HTML.class.getClassLoader().getResourceAsStream("style.css");
157 copy(in, resp.getWriter());
158 resp.setContentType("text/css");
161 } catch (IOException e) {
162 LOG.log(Level.SEVERE, "doGet failure", e);
163 resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
169 if (path.equals("/")) {
170 writeArticles(Category.ACTUALITE, resp);
176 if (path.equals("/stats")) {
181 for (Category cat: Category.values()) {
182 if (path.equals('/' + cat.getId())) {
183 writeArticles(cat, resp);
188 resp.getWriter().write("Not found " + req.getPathInfo());
189 resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
190 } catch (IOException e) {
191 LOG.log(Level.SEVERE, "doGet failure", e);
192 resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
197 public void init(ServletConfig config) throws ServletException {
198 LOG.info("Pnews servlet init " + config.getServletContext().getContextPath());