load feeds configuration from json
authorJean-Philippe Orsini <orsinije@fr.ibm.com>
Tue, 24 Oct 2017 21:58:08 +0000 (23:58 +0200)
committerJean-Philippe Orsini <orsinije@fr.ibm.com>
Tue, 24 Oct 2017 21:58:08 +0000 (23:58 +0200)
war/src/main/java/pnews/Category.java
war/src/main/java/pnews/Feed.java [new file with mode: 0644]
war/src/main/java/pnews/servlet/ArticleProvider.java
war/src/main/java/pnews/servlet/Config.java [new file with mode: 0644]
war/src/main/java/pnews/servlet/Pnews.java
war/src/main/resources/feeds.json [new file with mode: 0644]

index b67e7d9..6656ea7 100644 (file)
@@ -1,26 +1,24 @@
 package pnews;
 
 public enum Category {
-        TOP("actualite", "Les actualités à la une"),
-        FRANCE("france", "France"),
-        SPORT("sport", "Sport"),
-        EUROPE("europe", "Europe"),
-        MONDE("monde", "Monde"),
-        ECO("economie", "Economie"),
-        ESSONNE("essonne", "Essonne"),
-        TECHNOLOGIE("technologie", "Technologie"),
-        PEOPLE("people", "People");
+        ACTUALITE("Les actualités à la une"),
+        FRANCE("France"),
+        SPORT("Sport"),
+        EUROPE("Europe"),
+        MONDE("Monde"),
+        ECONOMIE("Economie"),
+        ESSONNE("Essonne"),
+        TECHNOLOGIE("Technologie"),
+        PEOPLE("People");
         
-        private final String id;
         private final String title;
                 
-        private Category(String id, String title) {
-                this.id = id;
+        private Category(String title) {
                 this.title = title;
         }
         
         public String getId() {
-                return id;
+                return name();
         }
         
         public String getTitle() {
diff --git a/war/src/main/java/pnews/Feed.java b/war/src/main/java/pnews/Feed.java
new file mode 100644 (file)
index 0000000..b5e677e
--- /dev/null
@@ -0,0 +1,24 @@
+package pnews;
+
+public class Feed {
+        private final String URL;
+        private final Category category;
+        
+        public Feed(String URL, Category category) {
+                this.URL = URL;
+                this.category = category;
+        }
+        
+        public String getURL() {
+                return URL;
+        }
+        
+        public Category getCategory() {
+                return category;
+        }
+        
+        @Override
+        public String toString() {
+                return getURL() + "[" + category + "]";
+        }
+}
index 1dece09..48c551b 100644 (file)
@@ -47,68 +47,7 @@ public class ArticleProvider {
                 return new SyndFeedInput().build(r);                
         }
         
-        private static Map<Category, String[]> getFeeds() {
-                Map<Category, String[]> result;
-                
-                result = new HashMap<>();
-                
-                result.put(Category.TOP,
-                           new String[] {
-                                           "http://www.europe1.fr/var/export/rss/europe1/actus.xml",
-                                           "http://www.francetvinfo.fr/titres.rss",
-                                           "http://www.rfi.fr/general/rss",
-                                           "http://www.cnews.fr/rss/une",
-                                           "http://www.ladepeche.fr/rss/a-la-une.rss",
-                                           "https://www.franceinter.fr/rss/a-la-une.xml",
-                                           "https://www.francebleu.fr/rss/a-la-une.xml",
-                                           "http://www.bfmtv.com/rss/info/flux-rss/flux-toutes-les-actualites/"
-                           });
-                
-                result.put(Category.SPORT,
-                                new String[] { "http://www.europe1.fr/var/export/rss/europe1/sport.xml",
-                                               "http://www.sportune.fr/feed",
-                                               "http://www.france24.com/fr/sports/rss" });
-                
-                result.put(Category.FRANCE,
-                                new String[] { "http://www.france24.com/fr/france/rss",
-                                               "http://www.francetvinfo.fr/france.rss",
-                                               "http://www.rfi.fr/france/rss"});
-                
-                result.put(Category.EUROPE,
-                                new String[] { "http://www.france24.com/fr/europe/rss" });
-
-                result.put(Category.MONDE, 
-                           new String[] { "http://www.europe1.fr/var/export/rss/europe1/international.xml",
-                                          "http://www.france24.com/fr/actualites/rss" });                                           
-
-                
-                result.put(Category.ECO,
-                                new String[] { "http://www.france24.com/fr/economie/rss",
-                                               "http://www.europe1.fr/var/export/rss/europe1/economie.xml",
-                                               "http://www.rfi.fr/economie/rss" });
-                
-                result.put(Category.ESSONNE,
-                                new String[] { "http://www.tourisme-essonne.com/rss/actus/",
-                                               "http://www.ville-palaiseau.fr/rss/actualites.htm" });
-                
-                result.put(Category.PEOPLE,
-                                new String[] { "http://www.premiere.fr/rss/actu-live",
-                                               "http://www.purepeople.com/rss/news_t0.xml"                                               
-                });
-                
-                result.put(Category.TECHNOLOGIE,
-                                new String[] { "http://www.generation-nt.com/export/rss.xml",
-                                               "http://www.europe1.fr/var/export/rss/europe1/sciences.xml",
-                                               "http://feeds.feedburner.com/lesnumeriques/news",
-                                               "http://www.zdnet.fr/feeds/rss/actualites/",
-                                               "http://www.frandroid.com/feed",
-                                               "http://www.silicon.fr/feed",
-                                               "http://www.fredzone.org/feed",
-                                               "http://www.futura-sciences.com/rss/actualites.xml",
-                                               "https://www-03.ibm.com/press/fr/fr/rssfeed.wss?keyword=null&maxFeed=&feedType=RSS&topic=all"});
-                
-                return result;
-        }
+        
         
         private List<Article> getArticlesForUpdate(Category cat) {
                 List<Article> result;
@@ -214,7 +153,7 @@ public class ArticleProvider {
         private void retrieveArticles(Category cat) throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
                 String[] feeds;
                 
-                feeds = getFeeds().get(cat);
+                feeds = Config.getFeedsByCategory().get(cat);
                 
                 if (feeds != null)
                         for (String str: feeds)
diff --git a/war/src/main/java/pnews/servlet/Config.java b/war/src/main/java/pnews/servlet/Config.java
new file mode 100644 (file)
index 0000000..5c0c134
--- /dev/null
@@ -0,0 +1,119 @@
+package pnews.servlet;
+
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.json.Json;
+import javax.json.JsonArray;
+import javax.json.JsonObject;
+
+import pnews.Category;
+import pnews.Feed;
+
+public class Config {
+        public static Feed[] getFeeds() {
+                Reader r;
+                List<Feed> feeds;
+                JsonObject jfeeds;
+                
+                r = null;
+                try {
+                        r = new InputStreamReader(Config.class.getClassLoader().getResourceAsStream("feeds.json"));
+                        jfeeds = Json.createReader(r).readObject();
+                } finally {
+                        if (r != null)
+                                try { r.close(); } catch (IOException e) { };
+                }
+                
+                feeds = new ArrayList<Feed>(jfeeds.size());
+
+                jfeeds.forEach((k, v)-> {
+                        JsonObject jf;
+                        JsonArray jcategories;
+                        
+                        jf = (JsonObject)v;
+                        jcategories = jf.getJsonArray("categories");
+                        feeds.add(new Feed(k, Category.valueOf(jcategories.getString(0))));
+                });
+                        
+                return feeds.toArray(new Feed[] {});
+        }
+        
+        public static Map<Category, String[]> getFeedsByCategory() {
+                Map<Category, String[]> result;
+                
+                result = new HashMap<>();
+                
+                result.put(Category.ACTUALITE,
+                           new String[] {
+                                           "http://www.europe1.fr/var/export/rss/europe1/actus.xml",
+                                           "http://www.francetvinfo.fr/titres.rss",
+                                           "http://www.rfi.fr/general/rss",
+                                           "http://www.cnews.fr/rss/une",
+                                           "http://www.ladepeche.fr/rss/a-la-une.rss",
+                                           "https://www.franceinter.fr/rss/a-la-une.xml",
+                                           "https://www.francebleu.fr/rss/a-la-une.xml",
+                                           "http://www.bfmtv.com/rss/info/flux-rss/flux-toutes-les-actualites/"
+                           });
+                
+                result.put(Category.SPORT,
+                                new String[] { "http://www.europe1.fr/var/export/rss/europe1/sport.xml",
+                                               "http://www.sportune.fr/feed",
+                                               "http://www.france24.com/fr/sports/rss" });
+                
+                result.put(Category.FRANCE,
+                                new String[] { "http://www.france24.com/fr/france/rss",
+                                               "http://www.francetvinfo.fr/france.rss",
+                                               "http://www.rfi.fr/france/rss"});
+                
+                result.put(Category.EUROPE,
+                                new String[] { "http://www.france24.com/fr/europe/rss" });
+
+                result.put(Category.MONDE, 
+                           new String[] { "http://www.europe1.fr/var/export/rss/europe1/international.xml",
+                                          "http://www.france24.com/fr/actualites/rss" });                                           
+
+                
+                result.put(Category.ECONOMIE,
+                                new String[] { "http://www.france24.com/fr/economie/rss",
+                                               "http://www.europe1.fr/var/export/rss/europe1/economie.xml",
+                                               "http://www.rfi.fr/economie/rss" });
+                
+                result.put(Category.ESSONNE,
+                                new String[] { "http://www.tourisme-essonne.com/rss/actus/",
+                                               "http://www.ville-palaiseau.fr/rss/actualites.htm" });
+                
+                result.put(Category.PEOPLE,
+                                new String[] { "http://www.premiere.fr/rss/actu-live",
+                                               "http://www.purepeople.com/rss/news_t0.xml"                                               
+                });
+                
+                result.put(Category.TECHNOLOGIE,
+                                new String[] { "http://www.generation-nt.com/export/rss.xml",
+                                               "http://www.europe1.fr/var/export/rss/europe1/sciences.xml",
+                                               "http://feeds.feedburner.com/lesnumeriques/news",
+                                               "http://www.zdnet.fr/feeds/rss/actualites/",
+                                               "http://www.frandroid.com/feed",
+                                               "http://www.silicon.fr/feed",
+                                               "http://www.fredzone.org/feed",
+                                               "http://www.futura-sciences.com/rss/actualites.xml",
+                                               "https://www-03.ibm.com/press/fr/fr/rssfeed.wss?keyword=null&maxFeed=&feedType=RSS&topic=all"});
+                
+                return result;
+        }
+
+        public static void main(String[] args) {
+                Feed[] feeds;
+                
+                feeds = getFeeds();
+                
+                System.out.println("Number of feeds: " + feeds.length);
+                for (Feed f: feeds)
+                        System.out.println(f);
+        }
+}
index 1b9a4f7..2e49a7a 100644 (file)
@@ -167,7 +167,7 @@ public class Pnews extends HttpServlet {
                 }
 
                 if (path.equals("/")) {
-                        writeArticles(Category.TOP, resp);
+                        writeArticles(Category.ACTUALITE, resp);
                         return ;
                 }
 
diff --git a/war/src/main/resources/feeds.json b/war/src/main/resources/feeds.json
new file mode 100644 (file)
index 0000000..120bc25
--- /dev/null
@@ -0,0 +1,34 @@
+{
+        "http://www.europe1.fr/var/export/rss/europe1/actus.xml": {
+                "categories": ["ACTUALITE"],
+                "language": "fr"
+        },
+        "http://www.francetvinfo.fr/titres.rss":  {
+                "categories": ["ACTUALITE"],
+                "language": "fr"
+        },
+        "http://www.rfi.fr/general/rss": {
+                "categories": ["ACTUALITE"],
+                "language": "fr"
+        },
+        "http://www.cnews.fr/rss/une": {
+                "categories": ["ACTUALITE"],
+                "language": "fr"
+        },
+        "http://www.ladepeche.fr/rss/a-la-une.rss": {
+                "categories": ["ACTUALITE"],
+                "language": "fr"
+        },
+        "https://www.franceinter.fr/rss/a-la-une.xml": {
+                "categories": ["ACTUALITE"],
+                "language": "fr"
+        },
+        "https://www.francebleu.fr/rss/a-la-une.xml": {
+                "categories": ["ACTUALITE"],
+                "language": "fr"
+        },
+        "http://www.bfmtv.com/rss/info/flux-rss/flux-toutes-les-actualites/": {
+                "categories": ["ACTUALITE"],
+                "language": "fr"
+        }
+}