removed useless import
[pnews.git] / war / src / main / java / pnews / servlet / Pnews.java
index f7175a1..dcb86ef 100644 (file)
@@ -1,8 +1,14 @@
 package pnews.servlet;
 
 import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
 import java.io.UnsupportedEncodingException;
+import java.io.Writer;
 import java.net.URLDecoder;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -12,45 +18,62 @@ import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import com.rometools.rome.io.FeedException;
+
+import pnews.Article;
+import pnews.Category;
+import pnews.HTML;
+
 public class Pnews extends HttpServlet {
         private static final Logger LOG = Logger.getLogger(Pnews.class.getName());
         private static final long serialVersionUID = 1L;
+        private static final ArticleProvider provider = ArticleProvider.singleton;
 
         private static String getQueryParameter(HttpServletRequest rq, String key)
                         throws UnsupportedEncodingException {
                 String[] params;
                 int idx;
                 String q;
-                
+
                 q = rq.getQueryString();
-                
+
                 if (q == null)
                         return null;
-                
+
                 params = URLDecoder.decode(q, "UTF-8").split("&");
-                
-                for (String p: params) {                        
+
+                for (String p: params) {
                         idx = p.indexOf('=');
-                                                
+
                         if (idx > 1 && p.substring(0, idx).equals(key))
                                 return p.substring(idx + 1);
                 }
-                
+
                 return null;
         }
-        
+
         private static void redirect(HttpServletRequest rq, HttpServletResponse rp) {
                 String redirectURL;
-                
+                Article a;
+
+                LOG.entering(Pnews.class.getName(), "redirect");
+
                 try {
                         redirectURL = getQueryParameter(rq, "url");
-                                               
+
                         LOG.info("Request redirection to " + redirectURL);
-                        
+
                         if (redirectURL != null) {
+                                a = ArticleStore.singleton.get(redirectURL);
+                                if (a != null)
+                                        a.readCount.incrementAndGet();
+                                else
+                                        LOG.severe("Cannot find the article " + redirectURL);
+                                
                                 rp.setHeader("Location", redirectURL);
                                 rp.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
                         } else {
+                                LOG.severe("No redirection URL");
                                 rp.setStatus(HttpServletResponse.SC_NOT_FOUND);
                         }
 
@@ -58,32 +81,114 @@ public class Pnews extends HttpServlet {
                         e.printStackTrace();
                         LOG.log(Level.SEVERE, "redirect failure", e);
                         rp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
-                }                
+                }
+
+                LOG.exiting(Pnews.class.getName(), "redirect");
         }
+
+        private void writeStats(HttpServletResponse rp) throws IOException {
+                rp.setContentType("text/html;charset=utf-8");
+                rp.setCharacterEncoding("utf8-8");
+
+                rp.getWriter().write("" + ArticleStore.singleton);
+        }
+
         
+        private void writeArticles(Category cat, HttpServletResponse rp) {
+                String html;
+                List<Article> articles;
+
+                try {
+                        articles = provider.getArticles(cat);
+                        if (articles != null) {
+                                html = HTML.toHTML(articles, cat);
+                                rp.setContentType("text/html;charset=utf-8");
+                                rp.getWriter().write(html);
+                                rp.setCharacterEncoding("utf8-8");
+                        } else {
+                                LOG.severe("writeArticles cannot retrieve any articles");
+                                html = HTML.toHTML(new ArrayList<Article>(), cat);
+                                rp.setContentType("text/html");
+                                rp.getWriter().write(html);
+                        }
+                } catch (IOException | IllegalArgumentException | FeedException e) {
+                        LOG.log(Level.SEVERE, "writeArticles failure", e);
+                        rp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+                }
+        }
+
+        private void copy(InputStream in, Writer writer) throws IOException {
+                Reader r;
+                char[] buf;
+                int n;
+
+                buf = new char[1024];
+                r = new InputStreamReader(in);
+                while ( (n = r.read(buf, 0, buf.length)) != -1)
+                        writer.write(buf, 0, n);
+        }
+
         @Override
-        protected void  doGet(HttpServletRequest req, HttpServletResponse resp) {
+        protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
                 String path;
-                
-                LOG.info("doGet " + req.getRequestURI());
+                InputStream in;
 
+                RequesterLog.singleton.writeRequest(req);
+              
+                LOG.info("doGet " + req.getRemoteAddr().toString() + " " + req.getRequestURI() + " " + req.getQueryString());
+                                
                 path = req.getPathInfo();
-                
+
                 if (path.equals("/redirect")) {
                         redirect(req, resp);
-                } else {
+                        return ;
+                }
+
+                if (path.equals("/style.css")) {
                         try {
-                                resp.getWriter().write("Not found " + req.getPathInfo());
-                                resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
+                                in = HTML.class.getClassLoader().getResourceAsStream("style.css");
+                                copy(in, resp.getWriter());
+                                resp.setContentType("text/css");
+
+                                return ;
                         } catch (IOException e) {
                                 LOG.log(Level.SEVERE, "doGet failure", e);
+                                resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+
+                                return ;
                         }
                 }
+
+                if (path.equals("/")) {
+                        writeArticles(Category.TOP, resp);
+                        return ;
+                }
+
+                try {
+                
+                        if (path.equals("/stats")) {
+                                writeStats(resp);
+                                return ;
+                        }
+                
+                        for (Category cat: Category.values()) {
+                                if (path.equals('/' + cat.getId())) {
+                                        writeArticles(cat, resp);
+                                        return ;
+                                }
+                        }
+                
+                        resp.getWriter().write("Not found " + req.getPathInfo());
+                        resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
+                } catch (IOException e) {
+                        LOG.log(Level.SEVERE, "doGet failure", e);
+                        resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+                }
         }
-        
+
         @Override
         public void init(ServletConfig config) throws ServletException {
                 LOG.info("Pnews servlet init " + config.getServletContext().getContextPath());
-                
+
         }
 }