read config from json. Many refactoring to prepare multi language support
[pnews.git] / war / src / main / java / pnews / servlet / Pnews.java
index 2e49a7a..e254a61 100644 (file)
@@ -27,7 +27,8 @@ 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 {
@@ -94,7 +95,7 @@ public class Pnews extends HttpServlet {
                 rp.setContentType("application/json;charset=utf-8");
                 rp.setCharacterEncoding("utf-8");
 
-                rp.getWriter().write(JSON.getStats());
+                rp.getWriter().write(JSON.getStats(config.getCategories()));
         }
 
         
@@ -105,13 +106,13 @@ public class Pnews extends HttpServlet {
                 try {
                         articles = provider.getArticles(cat);
                         if (articles != null) {
-                                html = HTML.toHTML(articles, cat);
+                                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);
                         }
@@ -167,7 +168,7 @@ public class Pnews extends HttpServlet {
                 }
 
                 if (path.equals("/")) {
-                        writeArticles(Category.ACTUALITE, resp);
+                        writeArticles(config.getDefaultCategory(), resp);
                         return ;
                 }
 
@@ -178,8 +179,8 @@ public class Pnews extends HttpServlet {
                                 return ;
                         }
                 
-                        for (Category cat: Category.values()) {
-                                if (path.equals('/' + cat.getId())) {
+                        for (Category cat: config.getCategories()) {
+                                if (path.equals(cat.getURL())) {
                                         writeArticles(cat, resp);
                                         return ;
                                 }
@@ -194,8 +195,12 @@ public class Pnews extends HttpServlet {
         }
 
         @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();
+                config.loadConfig();
+                
+                provider = new ArticleProvider(config);
         }
 }