count the number of hits per articles
authorJean-Philippe Orsini <orsinije@fr.ibm.com>
Tue, 17 Oct 2017 14:50:11 +0000 (16:50 +0200)
committerJean-Philippe Orsini <orsinije@fr.ibm.com>
Tue, 17 Oct 2017 14:50:11 +0000 (16:50 +0200)
war/src/main/java/pnews/Article.java
war/src/main/java/pnews/HTML.java
war/src/main/java/pnews/servlet/ArticleProvider.java
war/src/main/java/pnews/servlet/Pnews.java

index 5d4a8ed..ad42e7e 100644 (file)
@@ -1,6 +1,7 @@
 package pnews;
 
 import java.util.Date;
+import java.util.concurrent.atomic.AtomicLong;
 
 public class Article {
         public final String title;
@@ -9,6 +10,7 @@ public class Article {
         public final String link;
         public final Date publicationDate;
         public final String website;
+        public final AtomicLong readCount = new AtomicLong();
         
         public Article(String link, String title, String description, String thumbnail, Date publicationDate, String website) {
                 this.link = link;
index ee367dc..9478fa2 100644 (file)
@@ -85,6 +85,7 @@ public class HTML {
        
        public static String toHTML(List<Article> articles, Category catActive) {
                StringBuffer buf;
+               int i;
                
                buf = new StringBuffer();
                buf.append("<!DOCTYPE html>\n");
@@ -98,8 +99,14 @@ public class HTML {
                
                appendMenu(buf, catActive);
                
-               for (Article e: articles)
+               i = 0;
+               for (Article e: articles) {
                        append(buf, e);
+                       if (i == 100)
+                               break;
+                       else
+                               i++;
+               }
                
                buf.append("</body>\n");
                buf.append("</html>\n");
index e578d2a..93c9339 100644 (file)
@@ -240,23 +240,11 @@ public class ArticleProvider {
                 }
                 
                 @Override
-                public void run() {
-                        List<Article> articles;
-                        
+                public void run() {                       
                         LOG.info("refresher "+ category.getId());
                         
                         try {
                                 retrieveArticles(category);
-                                
-                                synchronized (articlesByCategory) {
-                                        articles = articlesByCategory.get(category);
-                                        if (articles != null && articles.size() > 100) {
-                                                articlesByCategory.put(category,
-                                                                       articles.subList(0, 100));
-                                                                
-                                        }
-                                        LOG.info("refresher " + category.getId() + " number of articles: " + articles.size());
-                                }
                         } catch (IllegalArgumentException | FeedException | IOException e) {
                                 LOG.log(Level.SEVERE, "refresher failure", e);
                         }                        
index db93bb3..2682b91 100644 (file)
@@ -55,6 +55,7 @@ public class Pnews extends HttpServlet {
 
         private static void redirect(HttpServletRequest rq, HttpServletResponse rp) {
                 String redirectURL;
+                Article a;
 
                 LOG.entering(Pnews.class.getName(), "redirect");
 
@@ -64,6 +65,12 @@ public class Pnews extends HttpServlet {
                         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 {
@@ -80,6 +87,14 @@ public class Pnews extends HttpServlet {
                 LOG.exiting(Pnews.class.getName(), "redirect");
         }
 
+        private void writeStats(HttpServletResponse rp) throws IOException {
+                rp.setContentType("text/html;charset=utf-8");
+                rp.setCharacterEncoding("utf8-8");
+
+                rp.getWriter().write("" + ArticleStore.singleton);
+        }
+
+        
         private void writeArticles(Category cat, HttpServletResponse rp) {
                 String html;
                 List<Article> articles;
@@ -150,14 +165,20 @@ public class Pnews extends HttpServlet {
                         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: Category.values()) {
+                                if (path.equals('/' + cat.getId())) {
+                                        writeArticles(cat, resp);
+                                        return ;
+                                }
+                        }
+                
                         resp.getWriter().write("Not found " + req.getPathInfo());
                         resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
                 } catch (IOException e) {