read categories information from the configuration
authorJean-Philippe Orsini <orsinije@fr.ibm.com>
Thu, 26 Oct 2017 21:41:31 +0000 (23:41 +0200)
committerJean-Philippe Orsini <orsinije@fr.ibm.com>
Thu, 26 Oct 2017 21:41:31 +0000 (23:41 +0200)
war/src/main/java/pnews/Category.java
war/src/main/java/pnews/servlet/ArticleProvider.java
war/src/main/java/pnews/servlet/Config.java
war/src/main/java/pnews/servlet/HTML.java
war/src/main/java/pnews/servlet/JSON.java
war/src/main/java/pnews/servlet/Pnews.java
war/src/main/resources/feeds.json

index 1156acf..ab63a05 100644 (file)
@@ -1,26 +1,36 @@
 package pnews;
 
 public class Category {
 package pnews;
 
 public class Category {
-        private final String name;
-                
-        public Category(String name) {
-                this.name = name;
+        private final String id;
+        private final String label;
+        private final String title;
+        private final String language;
+
+        public Category(String id, String label, String title, String language) {
+                this.id = id;
+                this.label = label;
+                this.title = title;
+                this.language = language;
         }
                 
         public String getTitle() {
         }
                 
         public String getTitle() {
-                return name;
+                return title;
         }
         
         public String getURL() {
         }
         
         public String getURL() {
-                return "/fr/" + name.toLowerCase();
+                return "/" + language + "/" + id.toLowerCase();
         }
         
         }
         
-        public String getName() {
-                return name;
+        public String getLabel() {
+                return label;
         }        
         
         }        
         
+        public String getId() {
+                return id;
+        }
+        
         @Override
         public String toString() {
         @Override
         public String toString() {
-                return getName();
+                return getLabel();
         }
 }
         }
 }
index 3e90d11..3bef20e 100644 (file)
@@ -117,7 +117,7 @@ public class ArticleProvider {
                 
                 feedTitle = feed.getTitle().trim();
                 
                 
                 feedTitle = feed.getTitle().trim();
                 
-                LOG.info("addArticles " + cat.getName() + " " + feedTitle + " number of articles: " + feed.getEntries().size());
+                LOG.info("addArticles " + cat.getLabel() + " " + feedTitle + " number of articles: " + feed.getEntries().size());
                 
                 for (SyndEntry entry: feed.getEntries()) {
                         String link = entry.getLink().trim();
                 
                 for (SyndEntry entry: feed.getEntries()) {
                         String link = entry.getLink().trim();
@@ -147,7 +147,7 @@ public class ArticleProvider {
                         }
                 }          
                 
                         }
                 }          
                 
-                LOG.info("addArticles done " + cat.getName());
+                LOG.info("addArticles done " + cat.getLabel());
         }
              
         private void retrieveArticles(Category cat) throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
         }
              
         private void retrieveArticles(Category cat) throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
@@ -161,7 +161,7 @@ public class ArticleProvider {
                                         addArticles(cat, getSyndFeed(f.getURL()));
                                 } catch (Throwable e) {
                                         LOG.log(Level.SEVERE,
                                         addArticles(cat, getSyndFeed(f.getURL()));
                                 } catch (Throwable e) {
                                         LOG.log(Level.SEVERE,
-                                                "retrieveArticles failure " + cat.getName() + " " + f.toString(),
+                                                "retrieveArticles failure " + cat.getLabel() + " " + f.toString(),
                                                 e);
                                 }
                 else
                                                 e);
                                 }
                 else
@@ -193,7 +193,7 @@ public class ArticleProvider {
                 
                 @Override
                 public void run() {                       
                 
                 @Override
                 public void run() {                       
-                        LOG.info("refresher "+ category.getName());
+                        LOG.info("refresher "+ category.getLabel());
                         
                         try {
                                 retrieveArticles(category);
                         
                         try {
                                 retrieveArticles(category);
@@ -201,7 +201,7 @@ public class ArticleProvider {
                                 LOG.log(Level.SEVERE, "refresher failure", e);
                         }                        
                         
                                 LOG.log(Level.SEVERE, "refresher failure", e);
                         }                        
                         
-                        LOG.info("refresher "+ category.getName() + " done");
+                        LOG.info("refresher "+ category.getLabel() + " done");
                 }                
         }
 }
                 }                
         }
 }
index a77eb88..7e2b642 100644 (file)
@@ -3,14 +3,17 @@ package pnews.servlet;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.Reader;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.Reader;
+import java.io.UnsupportedEncodingException;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.logging.Logger;
 
 import javax.json.Json;
 import javax.json.JsonArray;
 import javax.json.JsonObject;
 
 import javax.json.Json;
 import javax.json.JsonArray;
 import javax.json.JsonObject;
+import javax.json.JsonValue;
 
 import pnews.Category;
 import pnews.Feed;
 
 import pnews.Category;
 import pnews.Feed;
@@ -18,26 +21,55 @@ import pnews.Feed;
 public class Config {
         private Feed[] feeds;
         private Category[] categories;
 public class Config {
         private Feed[] feeds;
         private Category[] categories;
+        private static final Logger LOG = Logger.getLogger(Config.class.getName());
                 
                 
-        public void loadConfig() {
+        private void loadCategories(JsonArray jcats) {
+                List<Category> cats;
+                JsonObject jcat;
+                String id, label, title, language;
+                
+                cats = new ArrayList<>(jcats.size());
+                
+                for (JsonValue v: jcats) {
+                        jcat = (JsonObject)v;
+                        id = jcat.getString("id");
+                        label = jcat.getString("label");
+                        title = jcat.getString("title");
+                        language = jcat.getString("language");
+                        cats.add(new Category(id, label, title, language));
+                }
+                
+                categories = cats.toArray(new Category[0]);
+        }
+        
+        private Category getCategory(String id) {
+                for (Category c: categories)
+                        if (c.getId().equals(id))
+                                return c;
+                return null;
+        }
+        
+        public void loadConfig() throws UnsupportedEncodingException {
                 Reader r;
                 JsonObject jfeeds, jroot;
                 List<Feed> feedList;
                 Reader r;
                 JsonObject jfeeds, jroot;
                 List<Feed> feedList;
-                Map<String, Category> cats;
                 
                 r = null;
                 try {
                 
                 r = null;
                 try {
-                        r = new InputStreamReader(Config.class.getClassLoader().getResourceAsStream("feeds.json"));
+                        r = new InputStreamReader(Config.class.getClassLoader().getResourceAsStream("feeds.json"),
+                                                  "UTF-8");
                         jroot = Json.createReader(r).readObject();
                 } finally {
                         if (r != null)
                                 try { r.close(); } catch (IOException e) { };
                 }
                 
                         jroot = Json.createReader(r).readObject();
                 } finally {
                         if (r != null)
                                 try { r.close(); } catch (IOException e) { };
                 }
                 
+                
+                loadCategories(jroot.getJsonArray("categories"));
+                
                 jfeeds = jroot.getJsonObject("feeds");
                 
                 feedList = new ArrayList<Feed>(jfeeds.size());
                 jfeeds = jroot.getJsonObject("feeds");
                 
                 feedList = new ArrayList<Feed>(jfeeds.size());
-                cats = new HashMap<>();
 
                 jfeeds.forEach((k, v)-> {
                         JsonObject jf;
 
                 jfeeds.forEach((k, v)-> {
                         JsonObject jf;
@@ -49,17 +81,15 @@ public class Config {
                         jcategories = jf.getJsonArray("categories");
                         str = jcategories.getString(0);
                         
                         jcategories = jf.getJsonArray("categories");
                         str = jcategories.getString(0);
                         
-                        cat = cats.get(str);
-                        if (cat == null) {
-                                cat = new Category(str);
-                                cats.put(str, cat);
-                        }
+                        cat = getCategory(str);
                         
                         
-                        feedList.add(new Feed(k, cat));                                           
+                        if (cat != null)
+                                feedList.add(new Feed(k, cat));
+                        else
+                                LOG.severe("Missing category: " + str);
                 });
                 
                 feeds = feedList.toArray(new Feed[0]);
                 });
                 
                 feeds = feedList.toArray(new Feed[0]);
-                categories = cats.values().toArray(new Category[0]);
         }
         
         public Feed[] getFeeds() {
         }
         
         public Feed[] getFeeds() {
@@ -97,7 +127,7 @@ public class Config {
                 return categories[0];
         }
         
                 return categories[0];
         }
         
-        public static void main(String[] args) {
+        public static void main(String[] args) throws UnsupportedEncodingException {
                 Config cfg;
                 Feed[] feeds;           
                 Category[] cats;
                 Config cfg;
                 Feed[] feeds;           
                 Category[] cats;
index e9aae26..681c84b 100644 (file)
@@ -85,7 +85,7 @@ public class HTML {
                        else
                                cl = null;
                        
                        else
                                cl = null;
                        
-                       appendA(buf, cat.getName(), cat.getURL(), cl);
+                       appendA(buf, cat.getLabel(), cat.getURL(), cl);
                        buf.append("</li>");
                }
                
                        buf.append("</li>");
                }
                
index 8146b78..a36367e 100644 (file)
@@ -34,7 +34,7 @@ public class JSON {
                 
                 for (Category cat: cats)
                         try {
                 
                 for (Category cat: cats)
                         try {
-                                jcategories.addProperty(cat.getName(),
+                                jcategories.addProperty(cat.getLabel(),
                                                         articles.length);
                         } catch (IllegalArgumentException e) {
                                 LOG.log(Level.SEVERE, "Fail to retrieve articles", e);
                                                         articles.length);
                         } catch (IllegalArgumentException e) {
                                 LOG.log(Level.SEVERE, "Fail to retrieve articles", e);
index e254a61..63040c8 100644 (file)
@@ -199,7 +199,11 @@ public class Pnews extends HttpServlet {
                 LOG.info("Pnews servlet init " + cfg.getServletContext().getContextPath());
                 
                 config = new Config();
                 LOG.info("Pnews servlet init " + cfg.getServletContext().getContextPath());
                 
                 config = new Config();
-                config.loadConfig();
+                try {
+                        config.loadConfig();
+                } catch (UnsupportedEncodingException e) {
+                        throw new ServletException(e);
+                }
                 
                 provider = new ArticleProvider(config);
         }
                 
                 provider = new ArticleProvider(config);
         }
index f0e0916..a9569ed 100644 (file)
@@ -1,37 +1,86 @@
 {
 {
+        "categories": [
+                {
+                        "id": "actualite",
+                        "label": "actualité",
+                        "title": "Les actualités à la une",
+                        "language": "fr"
+                }, {
+                        "id": "sport",
+                        "label": "sport",
+                        "title": "Sport",
+                        "language": "fr"
+                }, {
+                        "id": "france",
+                        "label": "France",
+                        "title": "France",
+                        "language": "fr"
+                }, {
+                        "id": "europe",
+                        "label": "Europe",
+                        "title": "Europe",
+                        "language": "fr"
+                }, {
+                        "id": "monde",
+                        "label": "Monde",
+                        "title": "Monde",
+                        "language": "fr"
+                }, {
+                        "id": "essonne",
+                        "label": "Essonne",
+                        "title": "Essonne",
+                        "language": "fr"
+                }, {
+                        "id": "economie",
+                        "label": "Economie",
+                        "title": "Economie",
+                        "language": "fr"
+                }, {
+                        "id": "technologie",
+                        "label": "Technologie",
+                        "title": "Technologie",
+                        "language": "fr"
+                }, {
+                        "id": "people",
+                        "label": "People",
+                        "title": "People",
+                        "language": "fr"
+                }
+
+        ],
         "feeds": {
         "feeds": {
-                "http://www.europe1.fr/var/export/rss/europe1/actus.xml": { "categories": ["ACTUALITE"] },
-                "http://www.francetvinfo.fr/titres.rss":  { "categories": ["ACTUALITE"] },
-                "http://www.rfi.fr/general/rss": { "categories": ["ACTUALITE"] },
-                "http://www.cnews.fr/rss/une": { "categories": ["ACTUALITE"] },
-                "http://www.ladepeche.fr/rss/a-la-une.rss": { "categories": ["ACTUALITE"] },
-                "https://www.franceinter.fr/rss/a-la-une.xml": { "categories": ["ACTUALITE"] },
-                "https://www.francebleu.fr/rss/a-la-une.xml": { "categories": ["ACTUALITE"] },
-                "http://www.bfmtv.com/rss/info/flux-rss/flux-toutes-les-actualites/": { "categories": ["ACTUALITE"] },
-                "http://www.europe1.fr/var/export/rss/europe1/sport.xml": { "categories": ["SPORT"] },
-                "http://www.sportune.fr/feed": { "categories": ["SPORT"] },
-                "http://www.france24.com/fr/sports/rss": { "categories": ["SPORT"] },
-                "http://www.france24.com/fr/france/rss": { "categories": ["FRANCE"] },
-                "http://www.francetvinfo.fr/france.rss": { "categories": ["FRANCE"] },
-                "http://www.rfi.fr/france/rss": { "categories": ["FRANCE"] },
-                "http://www.france24.com/fr/europe/rss": { "categories": ["EUROPE"] },
-                "http://www.europe1.fr/var/export/rss/europe1/international.xml": { "categories": ["MONDE"] },
-                "http://www.france24.com/fr/actualites/rss": { "categories": ["MONDE"] },
-                "http://www.france24.com/fr/economie/rss": { "categories": ["ECONOMIE"] },
-                "http://www.europe1.fr/var/export/rss/europe1/economie.xml": { "categories": ["ECONOMIE"] },
-                "http://www.rfi.fr/economie/rss": { "categories": ["ECONOMIE"] },
-                "http://www.tourisme-essonne.com/rss/actus/": { "categories": ["ESSONNE"] },
-                "http://www.ville-palaiseau.fr/rss/actualites.htm": { "categories": ["ESSONNE"] },
-                "http://www.premiere.fr/rss/actu-live": { "categories": ["PEOPLE"] },
-                "http://www.purepeople.com/rss/news_t0.xml": { "categories": ["PEOPLE"] },
-                "http://www.generation-nt.com/export/rss.xml": { "categories": ["TECHNOLOGIE"] },
-                "http://www.europe1.fr/var/export/rss/europe1/sciences.xml": { "categories": ["TECHNOLOGIE"] },
-                "http://feeds.feedburner.com/lesnumeriques/news": { "categories": ["TECHNOLOGIE"] },
-                "http://www.zdnet.fr/feeds/rss/actualites/": { "categories": ["TECHNOLOGIE"] },
-                "http://www.frandroid.com/feed": { "categories": ["TECHNOLOGIE"] },
-                "http://www.silicon.fr/feed": { "categories": ["TECHNOLOGIE"] },
-                "http://www.fredzone.org/feed": { "categories": ["TECHNOLOGIE"] },
-                "http://www.futura-sciences.com/rss/actualites.xml": { "categories": ["TECHNOLOGIE"] },
-                "https://www-03.ibm.com/press/fr/fr/rssfeed.wss?keyword=null&maxFeed=&feedType=RSS&topic=all": { "categories": ["TECHNOLOGIE"] }
+                "http://www.europe1.fr/var/export/rss/europe1/actus.xml": { "categories": ["actualite"] },
+                "http://www.francetvinfo.fr/titres.rss":  { "categories": ["actualite"] },
+                "http://www.rfi.fr/general/rss": { "categories": ["actualite"] },
+                "http://www.cnews.fr/rss/une": { "categories": ["actualite"] },
+                "http://www.ladepeche.fr/rss/a-la-une.rss": { "categories": ["actualite"] },
+                "https://www.franceinter.fr/rss/a-la-une.xml": { "categories": ["actualite"] },
+                "https://www.francebleu.fr/rss/a-la-une.xml": { "categories": ["actualite"] },
+                "http://www.bfmtv.com/rss/info/flux-rss/flux-toutes-les-actualites/": { "categories": ["actualite"] },
+                "http://www.europe1.fr/var/export/rss/europe1/sport.xml": { "categories": ["sport"] },
+                "http://www.sportune.fr/feed": { "categories": ["sport"] },
+                "http://www.france24.com/fr/sports/rss": { "categories": ["sport"] },
+                "http://www.france24.com/fr/france/rss": { "categories": ["france"] },
+                "http://www.francetvinfo.fr/france.rss": { "categories": ["france"] },
+                "http://www.rfi.fr/france/rss": { "categories": ["france"] },
+                "http://www.france24.com/fr/europe/rss": { "categories": ["europe"] },
+                "http://www.europe1.fr/var/export/rss/europe1/international.xml": { "categories": ["monde"] },
+                "http://www.france24.com/fr/actualites/rss": { "categories": ["monde"] },
+                "http://www.france24.com/fr/economie/rss": { "categories": ["economie"] },
+                "http://www.europe1.fr/var/export/rss/europe1/economie.xml": { "categories": ["economie"] },
+                "http://www.rfi.fr/economie/rss": { "categories": ["economie"] },
+                "http://www.tourisme-essonne.com/rss/actus/": { "categories": ["essonne"] },
+                "http://www.ville-palaiseau.fr/rss/actualites.htm": { "categories": ["essonne"] },
+                "http://www.premiere.fr/rss/actu-live": { "categories": ["people"] },
+                "http://www.purepeople.com/rss/news_t0.xml": { "categories": ["people"] },
+                "http://www.generation-nt.com/export/rss.xml": { "categories": ["technologie"] },
+                "http://www.europe1.fr/var/export/rss/europe1/sciences.xml": { "categories": ["technologie"] },
+                "http://feeds.feedburner.com/lesnumeriques/news": { "categories": ["technologie"] },
+                "http://www.zdnet.fr/feeds/rss/actualites/": { "categories": ["technologie"] },
+                "http://www.frandroid.com/feed": { "categories": ["technologie"] },
+                "http://www.silicon.fr/feed": { "categories": ["technologie"] },
+                "http://www.fredzone.org/feed": { "categories": ["technologie"] },
+                "http://www.futura-sciences.com/rss/actualites.xml": { "categories": ["technologie"] },
+                "https://www-03.ibm.com/press/fr/fr/rssfeed.wss?keyword=null&maxFeed=&feedType=RSS&topic=all": { "categories": ["technologie"] }
         }
 }
         }
 }