From d00919beaf69c925f6287dbde65ce9fc806f735b Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Sat, 11 Oct 2014 00:26:49 +0200 Subject: Allow for a way to access entries without giving number (redirect). --- org/madore/damlengine/WeblogServlet.java | 64 ++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 4 deletions(-) (limited to 'org/madore') diff --git a/org/madore/damlengine/WeblogServlet.java b/org/madore/damlengine/WeblogServlet.java index 648c12f..444d1ed 100644 --- a/org/madore/damlengine/WeblogServlet.java +++ b/org/madore/damlengine/WeblogServlet.java @@ -34,7 +34,7 @@ import javax.servlet.http.HttpServletResponse; public class WeblogServlet extends HttpServlet { - private static final long serialVersionUID = 2014101001L; + private static final long serialVersionUID = 2014101004L; @Override public void init(ServletConfig cfg) @@ -60,7 +60,7 @@ public class WeblogServlet extends HttpServlet { } static abstract class RequestPath { - public static RequestPath parsePath(String pathInfo) + public static RequestPath parsePath(String pathInfo, boolean inGet) throws ServletException { Matcher matcher; if ( pathInfo == null ) @@ -110,6 +110,31 @@ public class WeblogServlet extends HttpServlet { throw new ServletException(e); } } + if ( inGet && (matcher=Pattern.compile("/d\\.(\\d{4}-\\d{2}-\\d{2})\\.([\\p{Alpha}][\\p{Alnum}\\-]*)\\.html").matcher(pathInfo)).matches() ) { + final String date; + final String extra; + try { + date = matcher.group(1); + extra = matcher.group(2); + } catch (NumberFormatException e) { + return new NoSuchPath(); + } + try { + final Connection conn = WeblogDatabaseConnection.getConnection(); + final PreparedStatement selSt + = conn.prepareStatement("SELECT id FROM entries WHERE edate=? AND do_single_page=?"); + selSt.setString(1,date); + selSt.setString(2,extra); + final ResultSet selRes = selSt.executeQuery(); + if ( selRes.next() ) { + int id = selRes.getInt(1); + return new SingleBlogEntryRedirectPath(date, id, extra); + } else + return new NoSuchPath(); + } catch (SQLException e) { + throw new ServletException(e); + } + } return new NoSuchPath(); } } @@ -125,16 +150,47 @@ public class WeblogServlet extends HttpServlet { } } + static final class SingleBlogEntryRedirectPath extends RequestPath { + public String date; + public int number; + public String extra; + public SingleBlogEntryRedirectPath(String date, int number, String extra) { + this.date = date; + this.number = number; + this.extra = extra; + } + } + @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { - final RequestPath rp = RequestPath.parsePath(request.getPathInfo()); + final RequestPath rp = RequestPath.parsePath(request.getPathInfo(), true); if ( rp instanceof NoSuchPath ) { response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } + if ( rp instanceof SingleBlogEntryRedirectPath ) { + SingleBlogEntryRedirectPath rrp = (SingleBlogEntryRedirectPath)rp; + Matcher matcher = Pattern.compile("(\\d{4})-(\\d{2})-(\\d{2})").matcher(rrp.date); + if ( ! matcher.matches() ) + throw new AssertionError("bad date format"); + WeblogLink lk = new WeblogLink(matcher.group(1), matcher.group(2), + matcher.group(3), + String.format("%04d", rrp.number), + "", rrp.extra); + lk.setTypeStandard(); + String basePath = request.getScheme() + "://" + + request.getServerName() + + ":" + request.getServerPort() + + request.getContextPath() + + "/"; + // response.sendRedirect(lk.getFile(basePath)); + response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); + response.setHeader("Location", lk.getFile(basePath)); + return; + } if ( ! (rp instanceof SingleBlogEntryPath) ) throw new ServletException("this cannot happen"); final int number = ((SingleBlogEntryPath)rp).number; @@ -155,7 +211,7 @@ public class WeblogServlet extends HttpServlet { public long getLastModified(HttpServletRequest request) { final RequestPath rp; try { - rp = RequestPath.parsePath(request.getPathInfo()); + rp = RequestPath.parsePath(request.getPathInfo(), false); } catch (Exception e) { return -1; } -- cgit v1.2.3