[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] [ernie/master 1/3] Make ExoneraTor servlet use ERNIE's directory archive.



Author: Karsten Loesing <karsten.loesing@xxxxxxx>
Date: Mon, 7 Jun 2010 23:01:18 +0200
Subject: Make ExoneraTor servlet use ERNIE's directory archive.
Commit: 14387b4254272b7d424628d11969614316b62202

---
 src/org/torproject/ernie/web/ConsensusServlet.java |   17 ++---
 .../torproject/ernie/web/ExoneraTorServlet.java    |   73 ++++++++++++--------
 .../ernie/web/ServerDescriptorServlet.java         |   71 +++++++++----------
 3 files changed, 85 insertions(+), 76 deletions(-)

diff --git a/src/org/torproject/ernie/web/ConsensusServlet.java b/src/org/torproject/ernie/web/ConsensusServlet.java
index a95df12..7be0600 100644
--- a/src/org/torproject/ernie/web/ConsensusServlet.java
+++ b/src/org/torproject/ernie/web/ConsensusServlet.java
@@ -17,19 +17,18 @@ public class ConsensusServlet extends HttpServlet {
 
     /* Check if we have a descriptors directory. */
     // TODO make this configurable!
-    File archiveDirectory = new File("/srv/metrics.torproject.org/archives");
+    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. */
-// TODO change to internal server error
-      response.setStatus(HttpServletResponse.SC_NOT_FOUND);
+      response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
       return;
     }
 
     /* Check valid-after parameter. */
     if (validAfterParameter == null ||
         validAfterParameter.length() < "yyyy-MM-dd-HH-mm-ss".length()) {
-// TODO is there something like "wrong parameter"?
-      response.setStatus(HttpServletResponse.SC_NOT_FOUND);
+      response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
       return;
     }
     SimpleDateFormat timeFormat = new SimpleDateFormat(
@@ -39,17 +38,15 @@ public class ConsensusServlet extends HttpServlet {
     try {
       parsedTimestamp = timeFormat.parse(validAfterParameter);
     } catch (ParseException e) {
-// TODO is there something like "wrong parameter"?
-      response.setStatus(HttpServletResponse.SC_NOT_FOUND);
+      response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
       return;
     }
     if (parsedTimestamp == null) {
-// TODO is there something like "wrong parameter"?
-      response.setStatus(HttpServletResponse.SC_NOT_FOUND);
+      response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
       return;
     }
     String consensusFilename = archiveDirectory.getAbsolutePath()
-        + "/consensuses-" + validAfterParameter.substring(0, 4) + "-"
+        + "/" + validAfterParameter.substring(0, 4) + "/"
         + validAfterParameter.substring(5, 7) + "/"
         + validAfterParameter.substring(8, 10) + "/"
         + validAfterParameter + "-consensus";
diff --git a/src/org/torproject/ernie/web/ExoneraTorServlet.java b/src/org/torproject/ernie/web/ExoneraTorServlet.java
index c1bfea1..394d5ce 100644
--- a/src/org/torproject/ernie/web/ExoneraTorServlet.java
+++ b/src/org/torproject/ernie/web/ExoneraTorServlet.java
@@ -82,9 +82,13 @@ public class ExoneraTorServlet extends HttpServlet {
   }
 
   // TODO make this configurable!
-  public final String ARCHIVES_DIRECTORY = "/home/karsten/archives";
+  public final String CONSENSUS_DIRECTORY =
+      "/srv/metrics.torproject.org/ernie/directory-archive/consensus";
+  public final String SERVER_DESCRIPTOR_DIRECTORY =
+      "/srv/metrics.torproject.org/ernie/directory-archive/"
+      + "server-descriptor";
 
-  private static final boolean TEST_MODE = false;
+  private static final boolean TEST_MODE = false; // TODO take me out
 
   public void doGet(HttpServletRequest request,
       HttpServletResponse response) throws IOException,
@@ -97,21 +101,14 @@ public class ExoneraTorServlet extends HttpServlet {
     PrintWriter out = response.getWriter();
     writeHeader(out);
 
-    SortedSet<File> consensusDirectories = new TreeSet<File>();
-    SortedSet<File> serverDescriptorDirectories = new TreeSet<File>();
-
     /* Check if we have a descriptors directory. */
-    File archiveDirectory = new File("/srv/metrics.torproject.org/archives");
-    if (!archiveDirectory.exists() || !archiveDirectory.isDirectory()) {
-      /* Leave sets with consensus and server descriptor directories
-         empty. */
-      return;
-    }
-    for (File dir : archiveDirectory.listFiles()) {
-      if (dir.getName().startsWith("consensuses-")) {
-        consensusDirectories.add(dir);
-      } else if (dir.getName().startsWith("server-descriptors-")) {
-        serverDescriptorDirectories.add(dir);
+    File consensusDirectory = new File(CONSENSUS_DIRECTORY);
+    SortedSet<File> consensusDirectories = new TreeSet<File>();
+    if (consensusDirectory.exists() && consensusDirectory.isDirectory()) {
+      for (File yearFile : consensusDirectory.listFiles()) {
+        for (File monthFile : yearFile.listFiles()) {
+          consensusDirectories.add(monthFile);
+        }
       }
     }
 
@@ -124,14 +121,18 @@ public class ExoneraTorServlet extends HttpServlet {
       writeFooter(out);
       return;
     }
-    String firstDay = consensusDirectories.first().getName().
-        substring("consensuses-".length()) + "-" + new TreeSet<File>(
-        Arrays.asList(consensusDirectories.first().listFiles())).
-        first().getName();
-    String lastDay = consensusDirectories.last().getName().
-        substring("consensuses-".length()) + "-" + new TreeSet<File>(
-        Arrays.asList(consensusDirectories.last().listFiles())).
-        last().getName();
+    String firstConsensus = new TreeSet<File>(Arrays.asList(
+        new TreeSet<File>(Arrays.asList(consensusDirectories.first().
+        listFiles())).first().listFiles())).first().getName().substring(0,
+        13);
+    firstConsensus = firstConsensus.substring(0, 10) + " "
+        + firstConsensus.substring(11, 13) + ":00";
+    String lastConsensus = new TreeSet<File>(Arrays.asList(
+        new TreeSet<File>(Arrays.asList(consensusDirectories.last().
+        listFiles())).last().listFiles())).last().getName().substring(0,
+        13);
+    lastConsensus = lastConsensus.substring(0, 10) + " "
+        + lastConsensus.substring(11, 13) + ":00";
 
     out.println("<a id=\"relay\"/><h3>Was there a Tor relay running on "
         + "this IP address?</h3>");
@@ -169,13 +170,13 @@ public class ExoneraTorServlet extends HttpServlet {
     if (timestampParameter != null && timestampParameter.length() > 0) {
       try {
         Date parsedTimestamp = parseTimeFormat.parse(timestampParameter);
-        if (timestampParameter.compareTo(firstDay) >= 0 &&
-            timestampParameter.compareTo(lastDay) <= 0) {
+        if (timestampParameter.compareTo(firstConsensus) >= 0 &&
+            timestampParameter.compareTo(lastConsensus) <= 0) {
           timestamp = parsedTimestamp.getTime();
           timestampStr = parseTimeFormat.format(timestamp);
         } else {
-          timestampWarning = "Please pick a value between \"" + firstDay
-              + " 03:00\" and \"" + lastDay + " 21:00\".";
+          timestampWarning = "Please pick a value between \""
+              + firstConsensus + "\" and \"" + lastConsensus + "\".";
         }
       } catch (ParseException e) {
         /* We have no way to handle this exception, other than leaving
@@ -330,8 +331,8 @@ public class ExoneraTorServlet extends HttpServlet {
     SortedSet<File> relevantConsensuses = new TreeSet<File>();
     SortedSet<File> tooNewConsensuses = new TreeSet<File>();
     for (File consensusMonth : consensusDirectories) {
-      String month = consensusMonth.getName().substring(
-          "consensuses-".length());
+      String month = consensusMonth.getParentFile().getName() + "-"
+          + consensusMonth.getName();
       if (month.compareTo(fromMonth) < 0 ||
           month.compareTo(toMonth) > 0) {
         continue;
@@ -533,6 +534,18 @@ public class ExoneraTorServlet extends HttpServlet {
     out.println("<br/><a id=\"exit\"/><h3>Was this relay configured to "
         + "permit exiting to a given target?</h3>");
 
+    File serverDescriptorDirectory =
+        new File(SERVER_DESCRIPTOR_DIRECTORY);
+    SortedSet<File> serverDescriptorDirectories = new TreeSet<File>();
+    if (serverDescriptorDirectory.exists() &&
+        serverDescriptorDirectory.isDirectory()) {
+      for (File yearFile : serverDescriptorDirectory.listFiles()) {
+        for (File monthFile : yearFile.listFiles()) {
+          serverDescriptorDirectories.add(monthFile);
+        }
+      }
+    }
+
     if (serverDescriptorDirectories.isEmpty()) {
       out.println("<p><font color=\"red\"><b>Warning: </b></font>This "
           + "server doesn't have any relay descriptors available. If "
diff --git a/src/org/torproject/ernie/web/ServerDescriptorServlet.java b/src/org/torproject/ernie/web/ServerDescriptorServlet.java
index 056ce9c..dcf9113 100644
--- a/src/org/torproject/ernie/web/ServerDescriptorServlet.java
+++ b/src/org/torproject/ernie/web/ServerDescriptorServlet.java
@@ -18,62 +18,61 @@ public class ServerDescriptorServlet extends HttpServlet {
 
     /* Check if we have a descriptors directory. */
     // TODO make this configurable!
-    File archiveDirectory = new File("/srv/metrics.torproject.org/archives");
+    File archiveDirectory = new File("/srv/metrics.torproject.org/ernie/"
+        + "directory-archive/server-descriptor");
     if (!archiveDirectory.exists() || !archiveDirectory.isDirectory()) {
       /* Oops, we don't have any descriptors to serve. */
-// TODO change to internal server error
       response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
       return;
     }
 
     /* Check desc-id parameter. */
     if (descIdParameter == null || descIdParameter.length() < 4) {
-// TODO is there something like "wrong parameter"?
       response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
       return;
     }
     Pattern descIdPattern = Pattern.compile("^[0-9a-f]+$");
     Matcher descIdMatcher = descIdPattern.matcher(descIdParameter);
     if (!descIdMatcher.matches()) {
-// TODO is there something like "wrong parameter"?
       response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
       return;
     }
 
-    for (File directory : archiveDirectory.listFiles()) {
-      if (!directory.isDirectory() ||
-          !directory.getName().startsWith("server-descriptors-")) {
-        continue;
-      }
-      File subDirectory = new File(directory.getAbsolutePath() + "/"
-          + descIdParameter.substring(0, 1) + "/"
-          + descIdParameter.substring(1, 2));
-      if (subDirectory.exists()) {
-        for (File serverDescriptorFile : subDirectory.listFiles()) {
-          if (!serverDescriptorFile.getName().startsWith(descIdParameter)) {
-            continue;
-          }
+    for (File yearFile : archiveDirectory.listFiles()) {
+      for (File monthFile : yearFile.listFiles()) {
+        File subDirectory = new File(monthFile.getAbsolutePath() + "/"
+            + descIdParameter.substring(0, 1) + "/"
+            + descIdParameter.substring(1, 2));
+        if (subDirectory.exists()) {
+          for (File serverDescriptorFile : subDirectory.listFiles()) {
+            if (!serverDescriptorFile.getName().startsWith(
+                descIdParameter)) {
+              continue;
+            }
 
-          /* Found it! Read file from disk and write it to response. */
-          BufferedInputStream input = null;
-          BufferedOutputStream output = null;
-          try {
-            response.setContentType("text/plain");
-            response.setHeader("Content-Length", String.valueOf(
-                serverDescriptorFile.length()));
-            response.setHeader("Content-Disposition",
-                "inline; filename=\"" + serverDescriptorFile.getName() + "\"");
-            input = new BufferedInputStream(new FileInputStream(
-                serverDescriptorFile), 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);
+            /* Found it! Read file from disk and write it to response. */
+            BufferedInputStream input = null;
+            BufferedOutputStream output = null;
+            try {
+              response.setContentType("text/plain");
+              response.setHeader("Content-Length", String.valueOf(
+                  serverDescriptorFile.length()));
+              response.setHeader("Content-Disposition",
+                  "inline; filename=\"" + serverDescriptorFile.getName()
+                  + "\"");
+              input = new BufferedInputStream(new FileInputStream(
+                  serverDescriptorFile), 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);
+              }
+            } finally {
+              output.close();
+              input.close();
             }
-          } finally {
-            output.close();
-            input.close();
           }
         }
       }
-- 
1.6.5