[or-cvs] [metrics-web/master] Fetch consensuses from the database instead of the file system.
karsten at torproject.org
karsten at torproject.org
Fri Nov 12 11:02:54 UTC 2010
Author: Karsten Loesing <karsten.loesing at gmx.net>
Date: Fri, 12 Nov 2010 12:02:05 +0100
Subject: Fetch consensuses from the database instead of the file system.
Commit: d3eda5a44fa6170462c292d35c3939d9ab4ffccc
---
src/org/torproject/ernie/web/ConsensusServlet.java | 109 ++++++++++++--------
1 files changed, 65 insertions(+), 44 deletions(-)
diff --git a/src/org/torproject/ernie/web/ConsensusServlet.java b/src/org/torproject/ernie/web/ConsensusServlet.java
index 97577c8..eade324 100644
--- a/src/org/torproject/ernie/web/ConsensusServlet.java
+++ b/src/org/torproject/ernie/web/ConsensusServlet.java
@@ -1,82 +1,103 @@
package org.torproject.ernie.web;
-import javax.servlet.*;
-import javax.servlet.http.*;
import java.io.*;
-import java.math.*;
+import java.sql.*;
import java.text.*;
import java.util.*;
+import java.util.logging.*;
+
+import javax.naming.*;
+import javax.servlet.*;
+import javax.servlet.http.*;
+import javax.sql.*;
public class ConsensusServlet extends HttpServlet {
- public void doGet(HttpServletRequest request,
- HttpServletResponse response) throws IOException,
- ServletException {
+ private DataSource ds;
- String validAfterParameter = request.getParameter("valid-after");
+ private Logger logger;
- /* Check if we have a descriptors directory. */
- // TODO make this configurable!
- File archiveDirectory = new File("/srv/metrics.torproject.org/ernie/"
- + "directory-archive/consensus");
- if (!archiveDirectory.exists() || !archiveDirectory.isDirectory()) {
- /* Oops, we don't have any descriptors to serve. */
- response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
- return;
+ public void init() {
+
+ /* Initialize logger. */
+ this.logger = Logger.getLogger(ConsensusServlet.class.toString());
+
+ /* Look up data source. */
+ try {
+ Context cxt = new InitialContext();
+ this.ds = (DataSource) cxt.lookup("java:comp/env/jdbc/tordir");
+ this.logger.info("Successfully looked up data source.");
+ } catch (NamingException e) {
+ this.logger.log(Level.WARNING, "Could not look up data source", e);
}
+ }
+
+ public void doGet(HttpServletRequest request,
+ HttpServletResponse response) throws IOException,
+ ServletException {
/* Check valid-after parameter. */
+ String validAfterParameter = request.getParameter("valid-after");
if (validAfterParameter == null ||
validAfterParameter.length() != "yyyy-MM-dd-HH-mm-ss".length()) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
- SimpleDateFormat timeFormat = new SimpleDateFormat(
+ SimpleDateFormat parameterFormat = new SimpleDateFormat(
"yyyy-MM-dd-HH-mm-ss");
- timeFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
- Date parsedTimestamp = null;
+ parameterFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
+ long parsedTimestamp = -1L;
try {
- parsedTimestamp = timeFormat.parse(validAfterParameter);
+ parsedTimestamp = parameterFormat.parse(validAfterParameter).
+ getTime();
} catch (ParseException e) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
- if (parsedTimestamp == null) {
+ if (parsedTimestamp < 0L) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
- String consensusFilename = archiveDirectory.getAbsolutePath()
- + "/" + validAfterParameter.substring(0, 4) + "/"
- + validAfterParameter.substring(5, 7) + "/"
- + validAfterParameter.substring(8, 10) + "/"
- + validAfterParameter + "-consensus";
- File consensusFile = new File(consensusFilename);
- if (!consensusFile.exists()) {
- response.sendError(HttpServletResponse.SC_NOT_FOUND);
+ /* Look up consensus in the database. */
+ SimpleDateFormat databaseFormat = new SimpleDateFormat(
+ "yyyy-MM-dd HH:mm:ss");
+ databaseFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
+ String databaseParameter = databaseFormat.format(parsedTimestamp);
+ byte[] rawDescriptor = null;
+ try {
+ Connection conn = this.ds.getConnection();
+ Statement statement = conn.createStatement();
+ String query = "SELECT rawdesc FROM consensus "
+ + "WHERE validafter = '" + databaseParameter + "'";
+ ResultSet rs = statement.executeQuery(query);
+ if (rs.next()) {
+ rawDescriptor = rs.getBytes(1);
+ }
+ rs.close();
+ statement.close();
+ conn.close();
+ } catch (SQLException e) {
+ response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}
- /* Read file from disk and write it to response. */
- BufferedInputStream input = null;
- BufferedOutputStream output = null;
+ /* Write response. */
+ if (rawDescriptor == null) {
+ response.sendError(HttpServletResponse.SC_NOT_FOUND);
+ return;
+ }
try {
response.setContentType("text/plain");
response.setHeader("Content-Length", String.valueOf(
- consensusFile.length()));
- response.setHeader("Content-Disposition",
- "inline; filename=\"" + consensusFile.getName() + "\"");
- input = new BufferedInputStream(new FileInputStream(consensusFile),
- 1024);
- output = new BufferedOutputStream(response.getOutputStream(), 1024);
- byte[] buffer = new byte[1024];
- int length;
- while ((length = input.read(buffer)) > 0) {
- output.write(buffer, 0, length);
- }
+ rawDescriptor.length));
+ response.setHeader("Content-Disposition", "inline; filename=\""
+ + validAfterParameter + "-consensus\"");
+ BufferedOutputStream output = new BufferedOutputStream(
+ response.getOutputStream());
+ output.write(rawDescriptor);
} finally {
- output.close();
- input.close();
+ /* Nothing to do here. */
}
}
}
--
1.7.1
More information about the tor-commits
mailing list