projects
/
pnews.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
read config from json. Many refactoring to prepare multi language support
[pnews.git]
/
war
/
src
/
main
/
java
/
pnews
/
servlet
/
Pnews.java
diff --git
a/war/src/main/java/pnews/servlet/Pnews.java
b/war/src/main/java/pnews/servlet/Pnews.java
index
8a98571
..
e254a61
100644
(file)
--- a/
war/src/main/java/pnews/servlet/Pnews.java
+++ b/
war/src/main/java/pnews/servlet/Pnews.java
@@
-22,31
+22,36
@@
import com.rometools.rome.io.FeedException;
import pnews.Article;
import pnews.Category;
import pnews.Article;
import pnews.Category;
-import pnews.HTML;
public class Pnews extends HttpServlet {
public class Pnews extends HttpServlet {
+ private static final String CLASS_NAME = Pnews.class.getName();
private static final Logger LOG = Logger.getLogger(Pnews.class.getName());
private static final long serialVersionUID = 1L;
private static final Logger LOG = Logger.getLogger(Pnews.class.getName());
private static final long serialVersionUID = 1L;
- private static final ArticleProvider provider = ArticleProvider.singleton;
+ private ArticleProvider provider;
+ private Config config;
private static String getQueryParameter(HttpServletRequest rq, String key)
throws UnsupportedEncodingException {
private static String getQueryParameter(HttpServletRequest rq, String key)
throws UnsupportedEncodingException {
+ final String METHOD_NAME="getQueryParameter";
String[] params;
int idx;
String q;
String[] params;
int idx;
String q;
+
+
+ LOG.entering(CLASS_NAME, METHOD_NAME, new Object[] { rq, key} );
q = rq.getQueryString();
if (q == null)
return null;
q = rq.getQueryString();
if (q == null)
return null;
- params =
URLDecoder.decode(q, "UTF-8")
.split("&");
+ params =
q
.split("&");
for (String p: params) {
idx = p.indexOf('=');
if (idx > 1 && p.substring(0, idx).equals(key))
for (String p: params) {
idx = p.indexOf('=');
if (idx > 1 && p.substring(0, idx).equals(key))
- return
p.substring(idx + 1
);
+ return
URLDecoder.decode(p.substring(idx + 1), "UTF-8"
);
}
return null;
}
return null;
@@
-54,8
+59,9
@@
public class Pnews extends HttpServlet {
private static void redirect(HttpServletRequest rq, HttpServletResponse rp) {
String redirectURL;
private static void redirect(HttpServletRequest rq, HttpServletResponse rp) {
String redirectURL;
+ Article a;
- LOG.entering(Pnews.class.getName(), "redirect");
+ LOG.entering(Pnews.class.getName(), "redirect"
, new Object[] { rq, rp }
);
try {
redirectURL = getQueryParameter(rq, "url");
try {
redirectURL = getQueryParameter(rq, "url");
@@
-63,6
+69,12
@@
public class Pnews extends HttpServlet {
LOG.info("Request redirection to " + redirectURL);
if (redirectURL != null) {
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 {
rp.setHeader("Location", redirectURL);
rp.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
} else {
@@
-79,6
+91,14
@@
public class Pnews extends HttpServlet {
LOG.exiting(Pnews.class.getName(), "redirect");
}
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(config.getCategories()));
+ }
+
+
private void writeArticles(Category cat, HttpServletResponse rp) {
String html;
List<Article> articles;
private void writeArticles(Category cat, HttpServletResponse rp) {
String html;
List<Article> articles;
@@
-86,13
+106,13
@@
public class Pnews extends HttpServlet {
try {
articles = provider.getArticles(cat);
if (articles != null) {
try {
articles = provider.getArticles(cat);
if (articles != null) {
- html = HTML.toHTML(articles, cat);
- rp.setContentType("text/html");
+ html = HTML.toHTML(articles, cat
, config.getCategories()
);
+ rp.setContentType("text/html
;charset=utf-8
");
rp.getWriter().write(html);
rp.getWriter().write(html);
- rp.setCharacterEncoding("
UTF
-8");
+ rp.setCharacterEncoding("
utf
-8");
} else {
LOG.severe("writeArticles cannot retrieve any articles");
} else {
LOG.severe("writeArticles cannot retrieve any articles");
- html = HTML.toHTML(new ArrayList<Article>(), cat);
+ html = HTML.toHTML(new ArrayList<Article>(), cat
, config.getCategories()
);
rp.setContentType("text/html");
rp.getWriter().write(html);
}
rp.setContentType("text/html");
rp.getWriter().write(html);
}
@@
-115,6
+135,7
@@
public class Pnews extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
+ final String METHOD_NAME = "doGet";
String path;
InputStream in;
String path;
InputStream in;
@@
-122,6
+143,8
@@
public class Pnews extends HttpServlet {
LOG.info("doGet " + req.getRemoteAddr().toString() + " " + req.getRequestURI() + " " + req.getQueryString());
LOG.info("doGet " + req.getRemoteAddr().toString() + " " + req.getRequestURI() + " " + req.getQueryString());
+ LOG.info(METHOD_NAME + " queryString=" + req.getQueryString());
+
path = req.getPathInfo();
if (path.equals("/redirect")) {
path = req.getPathInfo();
if (path.equals("/redirect")) {
@@
-145,18
+168,24
@@
public class Pnews extends HttpServlet {
}
if (path.equals("/")) {
}
if (path.equals("/")) {
- writeArticles(
Category.TOP
, resp);
+ writeArticles(
config.getDefaultCategory()
, resp);
return ;
}
return ;
}
- for (Category cat: Category.values()) {
- if (path.equals('/' + cat.getId())) {
- writeArticles(cat, resp);
+ try {
+
+ if (path.equals("/stats")) {
+ writeStats(resp);
return ;
}
return ;
}
- }
-
- try {
+
+ for (Category cat: config.getCategories()) {
+ if (path.equals(cat.getURL())) {
+ writeArticles(cat, resp);
+ return ;
+ }
+ }
+
resp.getWriter().write("Not found " + req.getPathInfo());
resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
} catch (IOException e) {
resp.getWriter().write("Not found " + req.getPathInfo());
resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
} catch (IOException e) {
@@
-166,8
+195,12
@@
public class Pnews extends HttpServlet {
}
@Override
}
@Override
- public void init(ServletConfig config) throws ServletException {
- LOG.info("Pnews servlet init " + config.getServletContext().getContextPath());
-
+ public void init(ServletConfig cfg) throws ServletException {
+ LOG.info("Pnews servlet init " + cfg.getServletContext().getContextPath());
+
+ config = new Config();
+ config.loadConfig();
+
+ provider = new ArticleProvider(config);
}
}
}
}