X-Git-Url: https://git.wpitchoune.net/gitweb/?p=pnews.git;a=blobdiff_plain;f=war%2Fsrc%2Fmain%2Fjava%2Fpnews%2Fservlet%2FPnews.java;fp=war%2Fsrc%2Fmain%2Fjava%2Fpnews%2Fservlet%2FPnews.java;h=0000000000000000000000000000000000000000;hp=c51f9468d6de242c4a25d5a774de75448ed0fa63;hb=a0c6addfd9ac6ac45f37b4202e787602c40e6bf7;hpb=aff83c8798602b535d13edeaffdb8f4238e2bbf5 diff --git a/war/src/main/java/pnews/servlet/Pnews.java b/war/src/main/java/pnews/servlet/Pnews.java deleted file mode 100644 index c51f946..0000000 --- a/war/src/main/java/pnews/servlet/Pnews.java +++ /dev/null @@ -1,224 +0,0 @@ -package pnews.servlet; - -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.Reader; -import java.io.UnsupportedEncodingException; -import java.io.Writer; -import java.net.URLDecoder; -import java.util.ArrayList; -import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; - -import javax.servlet.ServletConfig; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import com.rometools.rome.io.FeedException; - -import net.wpitchoune.pnews.Article; -import net.wpitchoune.pnews.ArticleStore; -import net.wpitchoune.pnews.Category; -import net.wpitchoune.pnews.Config; -import net.wpitchoune.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 ArticleProvider provider; - private Config config; - - private static String getQueryParameter(HttpServletRequest rq, String key) - throws UnsupportedEncodingException { - final String METHOD_NAME="getQueryParameter"; - String[] params; - int idx; - String q; - - - LOG.entering(CLASS_NAME, METHOD_NAME, new Object[] { rq, key} ); - - q = rq.getQueryString(); - - if (q == null) - return null; - - params = q.split("&"); - - for (String p: params) { - idx = p.indexOf('='); - - if (idx > 1 && p.substring(0, idx).equals(key)) - return URLDecoder.decode(p.substring(idx + 1), "UTF-8"); - } - - return null; - } - - private static void redirect(HttpServletRequest rq, HttpServletResponse rp) { - String redirectURL; - Article a; - - LOG.entering(Pnews.class.getName(), "redirect", new Object[] { rq, rp }); - - try { - redirectURL = getQueryParameter(rq, "url"); - - LOG.info("Request redirection to " + redirectURL); - - if (redirectURL != null) { - a = ArticleStore.singleton.get(redirectURL); - if (a != null) - a.incrementReadCount(); - else - LOG.severe("Cannot find the article " + redirectURL); - - rp.setHeader("Location", redirectURL); - rp.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT); - } else { - LOG.severe("No redirection URL"); - rp.setStatus(HttpServletResponse.SC_NOT_FOUND); - } - - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - LOG.log(Level.SEVERE, "redirect failure", e); - rp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); - } - - 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(provider, config)); - } - - - private void writeArticles(Category cat, String entity, HttpServletResponse rp) { - String html; - List
articles; - - try { - articles = provider.getArticles(cat, entity); - if (articles != null) { - html = HTML.toHTML(articles, cat, entity, 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, entity, config, provider); - rp.setContentType("text/html"); - rp.getWriter().write(html); - } - } catch (IOException | IllegalArgumentException | FeedException e) { - LOG.log(Level.SEVERE, "writeArticles failure", e); - rp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); - } - } - - private void copy(InputStream in, Writer writer) throws IOException { - Reader r; - char[] buf; - int n; - - buf = new char[1024]; - r = new InputStreamReader(in); - while ( (n = r.read(buf, 0, buf.length)) != -1) - writer.write(buf, 0, n); - } - - @Override - protected void doGet(HttpServletRequest req, HttpServletResponse resp) { - final String METHOD_NAME = "doGet"; - String path; - InputStream in; - - 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")) { - redirect(req, resp); - return ; - } - - if (path.equals("/style.css")) { - try { - in = HTML.class.getClassLoader().getResourceAsStream("style.css"); - copy(in, resp.getWriter()); - resp.setContentType("text/css"); - - return ; - } catch (IOException e) { - LOG.log(Level.SEVERE, "doGet failure", e); - resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); - - return ; - } - } - - if (path.equals("/")) { - doTemporaryRedirect(config.getDefaultLanguage().toURL(), resp); - return ; - } - - try { - if (path.equals("/stats")) { - writeStats(resp); - return ; - } - - for (Category cat: config.getCategories()) { - if (path.equals(cat.getURL())) { - writeArticles(cat, getQueryParameter(req, "entity"), resp); - 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 | RuntimeException e) { - LOG.log(Level.SEVERE, "doGet failure", e); - resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); - } - } - - @Override - 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); - } -}