cleanup and refactored to move to net.wpitchoune package
[pnews.git] / war / src / main / java / pnews / servlet / Pnews.java
index db93bb3..c51f946 100644 (file)
@@ -9,7 +9,6 @@ import java.io.Writer;
 import java.net.URLDecoder;
 import java.util.ArrayList;
 import java.util.List;
-import java.util.Locale;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -21,33 +20,41 @@ import javax.servlet.http.HttpServletResponse;
 
 import com.rometools.rome.io.FeedException;
 
-import pnews.Article;
-import pnews.Category;
-import pnews.HTML;
+import net.wpitchoune.pnews.Article;
+import net.wpitchoune.pnews.ArticleStore;
+import net.wpitchoune.pnews.Category;
+import net.wpitchoune.pnews.Config;
+import net.wpitchoune.pnews.Language;
 
 public class Pnews extends HttpServlet {
+        private static final String CLASS_NAME = Pnews.class.getName();
         private static final Logger LOG = Logger.getLogger(Pnews.class.getName());
         private static final long serialVersionUID = 1L;
-        private static final ArticleProvider provider = ArticleProvider.singleton;
+        private ArticleProvider provider;
+        private Config config;
 
         private static String getQueryParameter(HttpServletRequest rq, String key)
                         throws UnsupportedEncodingException {
+                final String METHOD_NAME="getQueryParameter";                
                 String[] params;
                 int idx;
                 String q;
+                
+                
+                LOG.entering(CLASS_NAME, METHOD_NAME, new Object[] { rq, key} );
 
                 q = rq.getQueryString();
 
                 if (q == null)
                         return null;
 
-                params = URLDecoder.decode(q, "UTF-8").split("&");
+                params = q.split("&");
 
                 for (String p: params) {
                         idx = p.indexOf('=');
 
                         if (idx > 1 && p.substring(0, idx).equals(key))
-                                return p.substring(idx + 1);
+                                return URLDecoder.decode(p.substring(idx + 1), "UTF-8");
                 }
 
                 return null;
@@ -55,8 +62,9 @@ public class Pnews extends HttpServlet {
 
         private static void redirect(HttpServletRequest rq, HttpServletResponse rp) {
                 String redirectURL;
+                Article a;
 
-                LOG.entering(Pnews.class.getName(), "redirect");
+                LOG.entering(Pnews.class.getName(), "redirect", new Object[] { rq, rp });
 
                 try {
                         redirectURL = getQueryParameter(rq, "url");
@@ -64,6 +72,12 @@ public class Pnews extends HttpServlet {
                         LOG.info("Request redirection to " + redirectURL);
 
                         if (redirectURL != null) {
+                                a = ArticleStore.singleton.get(redirectURL);
+                                if (a != null)
+                                        a.incrementReadCount();
+                                else
+                                        LOG.severe("Cannot find the article " + redirectURL);
+                                
                                 rp.setHeader("Location", redirectURL);
                                 rp.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
                         } else {
@@ -79,21 +93,34 @@ public class Pnews extends HttpServlet {
 
                 LOG.exiting(Pnews.class.getName(), "redirect");
         }
+        
+        private static void doTemporaryRedirect(String newURL, HttpServletResponse rp) {
+                rp.setHeader("Location", newURL);
+                rp.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);             
+        }
+
+        private void writeStats(HttpServletResponse rp) throws IOException {
+                rp.setContentType("application/json;charset=utf-8");
+                rp.setCharacterEncoding("utf-8");
+
+                rp.getWriter().write(JSON.getStats(provider, config));
+        }
 
-        private void writeArticles(Category cat, HttpServletResponse rp) {
+        
+        private void writeArticles(Category cat, String entity, HttpServletResponse rp) {
                 String html;
                 List<Article> articles;
 
                 try {
-                        articles = provider.getArticles(cat);
+                        articles = provider.getArticles(cat, entity);
                         if (articles != null) {
-                                html = HTML.toHTML(articles, cat);
+                                html = HTML.toHTML(articles, cat, entity, config, provider);
                                 rp.setContentType("text/html;charset=utf-8");
                                 rp.getWriter().write(html);
-                                rp.setCharacterEncoding("utf8-8");
+                                rp.setCharacterEncoding("utf-8");
                         } else {
                                 LOG.severe("writeArticles cannot retrieve any articles");
-                                html = HTML.toHTML(new ArrayList<Article>(), cat);
+                                html = HTML.toHTML(new ArrayList<Article>(), cat, entity, config, provider);
                                 rp.setContentType("text/html");
                                 rp.getWriter().write(html);
                         }
@@ -116,6 +143,7 @@ public class Pnews extends HttpServlet {
 
         @Override
         protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
+                final String METHOD_NAME = "doGet";
                 String path;
                 InputStream in;
 
@@ -123,6 +151,8 @@ public class Pnews extends HttpServlet {
               
                 LOG.info("doGet " + req.getRemoteAddr().toString() + " " + req.getRequestURI() + " " + req.getQueryString());
                                 
+                LOG.info(METHOD_NAME + " queryString=" + req.getQueryString());
+                
                 path = req.getPathInfo();
 
                 if (path.equals("/redirect")) {
@@ -146,29 +176,49 @@ public class Pnews extends HttpServlet {
                 }
 
                 if (path.equals("/")) {
-                        writeArticles(Category.TOP, resp);
+                        doTemporaryRedirect(config.getDefaultLanguage().toURL(), resp);
                         return ;
                 }
 
-                for (Category cat: Category.values()) {
-                        if (path.equals('/' + cat.getId())) {
-                                writeArticles(cat, resp);
+                try {
+                        if (path.equals("/stats")) {
+                                writeStats(resp);
                                 return ;
                         }
-                }
-
-                try {
+                
+                        for (Category cat: config.getCategories()) {
+                                if (path.equals(cat.getURL())) {
+                                        writeArticles(cat, getQueryParameter(req, "entity"), resp);
+                                        return ;
+                                }
+                        }
+                        
+                        for (Language l: config.getLanguages()) {
+                                if (path.equals(l.toURL())) {
+                                        doTemporaryRedirect(config.getDefaultCategory(l).getURL(), resp);
+                                        return ;
+                                }
+                        }
+                
                         resp.getWriter().write("Not found " + req.getPathInfo());
                         resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
-                } catch (IOException e) {
+                } catch (IOException | RuntimeException 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());
-
+        public void init(ServletConfig cfg) throws ServletException {
+                LOG.info("Pnews servlet init " + cfg.getServletContext().getContextPath());
+                
+                config = new Config();
+                try {
+                        config.loadConfig();
+                } catch (UnsupportedEncodingException e) {
+                        throw new ServletException(e);
+                }
+                
+                provider = new ArticleProvider(config);
         }
 }