diff options
author | David A. Madore <david+git@madore.org> | 2014-10-07 15:42:07 +0200 |
---|---|---|
committer | David A. Madore <david+git@madore.org> | 2014-10-07 15:42:07 +0200 |
commit | 1b5c19b3e42f4549ebb863cd97d2656e833ce516 (patch) | |
tree | 218145ada75226426b8e2f083c4391acf78f4c97 | |
parent | cfbf81150814c26e733480fa01a2eeb4541c068d (diff) | |
download | damlengine-1b5c19b3e42f4549ebb863cd97d2656e833ce516.tar.gz damlengine-1b5c19b3e42f4549ebb863cd97d2656e833ce516.tar.bz2 damlengine-1b5c19b3e42f4549ebb863cd97d2656e833ce516.zip |
Implement .getLastModified() on WeblogServlet.
-rw-r--r-- | org/madore/damlengine/WeblogServlet.java | 81 |
1 files changed, 72 insertions, 9 deletions
diff --git a/org/madore/damlengine/WeblogServlet.java b/org/madore/damlengine/WeblogServlet.java index f9d23e3..568a9ec 100644 --- a/org/madore/damlengine/WeblogServlet.java +++ b/org/madore/damlengine/WeblogServlet.java @@ -7,6 +7,10 @@ import java.util.Enumeration; import java.io.IOException; import java.io.OutputStream; import java.nio.file.Paths; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; import javax.servlet.ServletConfig; import javax.servlet.ServletException; @@ -30,7 +34,7 @@ import javax.servlet.http.HttpServletResponse; public class WeblogServlet extends HttpServlet { - private static final long serialVersionUID = 2014100701L; + private static final long serialVersionUID = 2014100703L; @Override public void init(ServletConfig cfg) @@ -55,22 +59,67 @@ public class WeblogServlet extends HttpServlet { } } } + static abstract class RequestPath { + public static RequestPath parsePath(String pathInfo) + throws ServletException { + Matcher matcher; + if ( pathInfo == null ) + return new NoSuchPath(); + if ( (matcher=Pattern.compile("/(\\d{1,7})").matcher(pathInfo)).matches() ) { + final int number; + try { + number = Integer.parseInt(matcher.group(1)); + } catch (NumberFormatException e) { + return new NoSuchPath(); + } + try { + final Connection conn = WeblogDatabaseConnection.getConnection(); + final PreparedStatement selSt + = conn.prepareStatement("SELECT id , extract(epoch from mdate) FROM entries WHERE id=?"); + selSt.setInt(1,number); + final ResultSet selRes = selSt.executeQuery(); + if ( selRes.next() ) { + int id = selRes.getInt(1); + double mdate = selRes.getDouble(2); + if ( id != number ) + throw new ServletException("this cannot happen"); + long lastModified = ((long)mdate)*1000; + // O'Reilly recommends not using milliseconds part. + return new SingleBlogEntryPath(number, lastModified); + } else + return new NoSuchPath(); + } catch (SQLException e) { + throw new ServletException(e); + } + } + return new NoSuchPath(); + } + } + + static final class NoSuchPath extends RequestPath { } + + static final class SingleBlogEntryPath extends RequestPath { + public int number; + public long lastModified; + public SingleBlogEntryPath(int number, long lastModified) { + this.number = number; + this.lastModified = lastModified; + } + } + @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { - Matcher matcher; - int number = -1; - if ( (matcher=Pattern.compile("/(\\d{1,7})").matcher(request.getPathInfo())).matches() ) { - try { - number = Integer.parseInt(matcher.group(1)); - } catch (NumberFormatException e) { } - } - if ( number == -1 ) { + final RequestPath rp = RequestPath.parsePath(request.getPathInfo()); + if ( rp instanceof NoSuchPath ) { response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } + if ( ! (rp instanceof SingleBlogEntryPath) ) + throw new ServletException("this cannot happen"); + final int number = ((SingleBlogEntryPath)rp).number; response.setContentType("text/html; charset=utf-8"); response.setCharacterEncoding("UTF-8"); OutputStream out = response.getOutputStream(); @@ -80,6 +129,20 @@ public class WeblogServlet extends HttpServlet { } catch (Exception e) { throw new ServletException("exception during WeblogSelect.fullProcess()", e); } + } + @Override + public long getLastModified(HttpServletRequest request) { + final RequestPath rp; + try { + rp = RequestPath.parsePath(request.getPathInfo()); + } catch (Exception e) { + return -1; + } + if ( rp instanceof SingleBlogEntryPath ) { + final long lastModified = ((SingleBlogEntryPath)rp).lastModified; + return lastModified; + } + return -1; } } |