multi languages support
[pnews.git] / war / src / main / java / pnews / servlet / Pnews.java
index 01489f8..fc08d13 100644 (file)
@@ -22,47 +22,59 @@ import com.rometools.rome.io.FeedException;
 
 import pnews.Article;
 import pnews.Category;
-import pnews.HTML;
 
 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;
                 
-                q = rq.getQueryString();
                 
+                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("&");
-                
-                for (String p: params) {                        
+
+                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;
         }
-        
+
         private static void redirect(HttpServletRequest rq, HttpServletResponse rp) {
                 String redirectURL;
-                
-                LOG.entering(Pnews.class.getName(), "redirect");
-                
+                Article a;
+
+                LOG.entering(Pnews.class.getName(), "redirect", new Object[] { rq, rp });
+
                 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 {
@@ -74,24 +86,38 @@ 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 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(config.getCategories()));
+        }
+
+        
         private void writeArticles(Category cat, HttpServletResponse rp) {
                 String html;
                 List<Article> articles;
-                
-                try {   
+
+                try {
                         articles = provider.getArticles(cat);
                         if (articles != null) {
-                                html = HTML.toHTML(articles, cat);
-                                rp.setContentType("text/html");
+                                html = HTML.toHTML(articles, cat, config.getCategories());
+                                rp.setContentType("text/html;charset=utf-8");
                                 rp.getWriter().write(html);
+                                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, config.getCategories());
                                 rp.setContentType("text/html");
                                 rp.getWriter().write(html);
                         }
@@ -100,55 +126,77 @@ public class Pnews extends HttpServlet {
                         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);                
+                        writer.write(buf, 0, n);
         }
-        
+
         @Override
         protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
+                final String METHOD_NAME = "doGet";
                 String path;
                 InputStream in;
-                
-                LOG.info("doGet " + req.getRequestURI());
 
-                path = req.getPathInfo();
+                RequesterLog.singleton.writeRequest(req);
+              
+                LOG.info("doGet " + req.getRemoteAddr().toString() + " " + req.getRequestURI() + " " + req.getQueryString());
+                                
+                LOG.info(METHOD_NAME + " queryString=" + req.getQueryString());
                 
+                path = req.getPathInfo();
+
                 if (path.equals("/redirect")) {
                         redirect(req, resp);
                         return ;
-                } 
-                
+                }
+
                 if (path.equals("/style.css")) {
                         try {
                                 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 ;
                         }
                 }
-                
-                for (Category cat: Category.values()) {
-                        if (path.equals('/' + cat.getId())) {
-                                writeArticles(cat, resp);
+
+                if (path.equals("/")) {
+                        writeArticles(config.getDefaultCategory(), resp);
+                        return ;
+                }
+
+                try {
+                        if (path.equals("/stats")) {
+                                writeStats(resp);
                                 return ;
                         }
-                }
                 
-                try {
+                        for (Category cat: config.getCategories()) {
+                                if (path.equals(cat.getURL())) {
+                                        writeArticles(cat, resp);
+                                        return ;
+                                }
+                        }
+                        
+                        for (String l: config.getLanguages()) {
+                                if (path.equals("/" + l) || path.equals("/" + l + "/")) {
+                                        doTemporaryRedirect(config.getDefaultCategory().getURL(), resp);
+                                        return ;
+                                }
+                        }
+                
                         resp.getWriter().write("Not found " + req.getPathInfo());
                         resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
                 } catch (IOException e) {
@@ -156,10 +204,18 @@ public class Pnews extends HttpServlet {
                         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);
         }
 }