package pnews;
import java.util.Date;
+import java.util.concurrent.atomic.AtomicLong;
public class Article {
public final String title;
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;
public static String toHTML(List<Article> articles, Category catActive) {
StringBuffer buf;
+ int i;
buf = new StringBuffer();
buf.append("<!DOCTYPE html>\n");
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");
}
@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);
}
private static void redirect(HttpServletRequest rq, HttpServletResponse rp) {
String redirectURL;
+ Article a;
LOG.entering(Pnews.class.getName(), "redirect");
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 {
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;
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) {