import pnews.Article;
import pnews.Category;
+import pnews.Language;
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 ArticleProvider provider = ArticleProvider.singleton;
+ private ArticleProvider provider;
+ private Config config;
private static String getQueryParameter(HttpServletRequest rq, String key)
throws UnsupportedEncodingException {
LOG.exiting(Pnews.class.getName(), "redirect");
}
+
+ private static void doTemporaryRedirect(String newURL, HttpServletResponse rp) {
+ rp.setHeader("Location", newURL);
+ rp.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
+ }
private void writeStats(HttpServletResponse rp) throws IOException {
rp.setContentType("application/json;charset=utf-8");
rp.setCharacterEncoding("utf-8");
- rp.getWriter().write(JSON.getStats());
+ rp.getWriter().write(JSON.getStats(provider, config));
}
try {
articles = provider.getArticles(cat);
if (articles != null) {
- html = HTML.toHTML(articles, cat);
+ html = HTML.toHTML(articles, cat, config.getCategories());
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);
+ html = HTML.toHTML(new ArrayList<Article>(), cat, config.getCategories());
rp.setContentType("text/html");
rp.getWriter().write(html);
}
}
if (path.equals("/")) {
- writeArticles(Category.ACTUALITE, resp);
+ doTemporaryRedirect(Language.toURL(config.getDefaultLanguage()), resp);
return ;
}
try {
-
if (path.equals("/stats")) {
writeStats(resp);
return ;
}
- for (Category cat: Category.values()) {
- if (path.equals('/' + cat.getId())) {
+ for (Category cat: config.getCategories()) {
+ if (path.equals(cat.getURL())) {
writeArticles(cat, resp);
return ;
}
}
+
+ for (String l: config.getLanguages()) {
+ if (path.equals("/" + l) || path.equals("/" + l + "/")) {
+ doTemporaryRedirect(config.getDefaultCategory().getURL(), resp);
+ return ;
+ }
+ }
resp.getWriter().write("Not found " + req.getPathInfo());
resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
}
@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();
+ try {
+ config.loadConfig();
+ } catch (UnsupportedEncodingException e) {
+ throw new ServletException(e);
+ }
+
+ provider = new ArticleProvider(config);
}
}