X-Git-Url: https://wpitchoune.net/gitweb/?a=blobdiff_plain;f=war%2Fsrc%2Fmain%2Fjava%2Fpnews%2Fservlet%2FPnews.java;h=777f7fd08fb7f26ff09d77ddf20605ef088e2992;hb=56c07f5de3319eb61182b7100855801644538e6f;hp=e254a61cac03691f1d5185e3c2d2f062d0ccf2d7;hpb=386f46525e32212ac5f3653135a6539c1b2639eb;p=pnews.git diff --git a/war/src/main/java/pnews/servlet/Pnews.java b/war/src/main/java/pnews/servlet/Pnews.java index e254a61..777f7fd 100644 --- a/war/src/main/java/pnews/servlet/Pnews.java +++ b/war/src/main/java/pnews/servlet/Pnews.java @@ -22,6 +22,7 @@ import com.rometools.rome.io.FeedException; import pnews.Article; import pnews.Category; +import pnews.Language; public class Pnews extends HttpServlet { private static final String CLASS_NAME = Pnews.class.getName(); @@ -90,12 +91,17 @@ public class Pnews extends HttpServlet { 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(config.getCategories())); + rp.getWriter().write(JSON.getStats(provider, config)); } @@ -106,13 +112,13 @@ public class Pnews extends HttpServlet { try { articles = provider.getArticles(cat); if (articles != null) { - html = HTML.toHTML(articles, cat, config.getCategories()); + html = HTML.toHTML(articles, cat, config, provider); 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
(), cat, config.getCategories()); + html = HTML.toHTML(new ArrayList
(), cat, config, provider); rp.setContentType("text/html"); rp.getWriter().write(html); } @@ -168,12 +174,11 @@ public class Pnews extends HttpServlet { } if (path.equals("/")) { - writeArticles(config.getDefaultCategory(), resp); + doTemporaryRedirect(config.getDefaultLanguage().toURL(), resp); return ; } try { - if (path.equals("/stats")) { writeStats(resp); return ; @@ -185,10 +190,17 @@ public class Pnews extends HttpServlet { return ; } } + + for (Language l: config.getLanguages()) { + if (path.equals(l.toURL())) { + doTemporaryRedirect(config.getDefaultCategory(l).getURL(), resp); + return ; + } + } resp.getWriter().write("Not found " + req.getPathInfo()); resp.setStatus(HttpServletResponse.SC_NOT_FOUND); - } catch (IOException e) { + } catch (IOException | RuntimeException e) { LOG.log(Level.SEVERE, "doGet failure", e); resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } @@ -199,7 +211,11 @@ public class Pnews extends HttpServlet { LOG.info("Pnews servlet init " + cfg.getServletContext().getContextPath()); config = new Config(); - config.loadConfig(); + try { + config.loadConfig(); + } catch (UnsupportedEncodingException e) { + throw new ServletException(e); + } provider = new ArticleProvider(config); }