import pnews.Article;
import pnews.Category;
-import pnews.HTML;
public class Pnews extends HttpServlet {
private static final Logger LOG = Logger.getLogger(Pnews.class.getName());
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("application/json;charset=utf-8");
+ rp.setCharacterEncoding("utf-8");
+
+ rp.getWriter().write(JSON.getStats());
+ }
+
+
private void writeArticles(Category cat, HttpServletResponse rp) {
String html;
List<Article> articles;
articles = provider.getArticles(cat);
if (articles != null) {
html = HTML.toHTML(articles, cat);
- rp.setContentType("text/html");
+ 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);
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
+ final String METHOD_NAME = "doGet";
String path;
InputStream in;
- LOG.info("doGet " + req.getRequestURI());
-
+ RequesterLog.singleton.writeRequest(req);
+
+ LOG.info("doGet " + req.getRemoteAddr().toString() + " " + req.getRequestURI() + " " + req.getQueryString());
+
+ LOG.info(METHOD_NAME + " queryString=" + req.getQueryString());
+
path = req.getPathInfo();
if (path.equals("/redirect")) {
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) {