commit 61171791dddc0245355bcc12b204a2c42674d295 Author: Karsten Loesing karsten.loesing@gmx.net Date: Wed Jul 6 11:18:01 2016 +0200
Fix most of the style issues found by checkstyle. --- .../exonerator/ExoneraTorDatabaseImporter.java | 78 ++++++++++-------- .../torproject/exonerator/ExoneraTorServlet.java | 92 +++++++++++----------- 2 files changed, 90 insertions(+), 80 deletions(-)
diff --git a/src/main/java/org/torproject/exonerator/ExoneraTorDatabaseImporter.java b/src/main/java/org/torproject/exonerator/ExoneraTorDatabaseImporter.java index 41751ca..23c009b 100644 --- a/src/main/java/org/torproject/exonerator/ExoneraTorDatabaseImporter.java +++ b/src/main/java/org/torproject/exonerator/ExoneraTorDatabaseImporter.java @@ -1,7 +1,14 @@ -/* Copyright 2011, 2012 The Tor Project +/* Copyright 2011--2016 The Tor Project * See LICENSE for licensing information */ + package org.torproject.exonerator;
+import org.torproject.descriptor.DescriptorCollector; +import org.torproject.descriptor.DescriptorSourceFactory; + +import org.apache.commons.codec.binary.Base64; +import org.apache.commons.codec.binary.Hex; + import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.BufferedWriter; @@ -12,7 +19,6 @@ import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.StringReader; -import java.io.UnsupportedEncodingException; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.DriverManager; @@ -29,12 +35,6 @@ import java.util.Set; import java.util.Stack; import java.util.TimeZone;
-import org.apache.commons.codec.binary.Base64; -import org.apache.commons.codec.binary.Hex; -import org.apache.commons.codec.digest.DigestUtils; -import org.torproject.descriptor.DescriptorCollector; -import org.torproject.descriptor.DescriptorSourceFactory; - /* Import Tor descriptors into the ExoneraTor database. */ public class ExoneraTorDatabaseImporter {
@@ -101,6 +101,7 @@ public class ExoneraTorDatabaseImporter {
/* Callable statements to import data into the database. */ private static CallableStatement insertStatusentryStatement; + private static CallableStatement insertExitlistentryStatement;
/* Prepare statements for importing data into the database. */ @@ -159,7 +160,9 @@ public class ExoneraTorDatabaseImporter { /* Last and next parse histories containing paths of parsed files and * last modified times. */ private static Map<String, Long> - lastImportHistory = new HashMap<String, Long>(), + lastImportHistory = new HashMap<String, Long>(); + + private static Map<String, Long> nextImportHistory = new HashMap<String, Long>();
/* Read stats/exonerator-import-history file from disk and remember @@ -224,8 +227,8 @@ public class ExoneraTorDatabaseImporter { long lastModified = file.lastModified(); String filename = file.getName(); nextImportHistory.put(filename, lastModified); - if (!lastImportHistory.containsKey(filename) || - lastImportHistory.get(filename) < lastModified) { + if (!lastImportHistory.containsKey(filename) + || lastImportHistory.get(filename) < lastModified) { try { FileInputStream fis = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(fis); @@ -264,8 +267,8 @@ public class ExoneraTorDatabaseImporter { String startToken = null; if (line.equals("network-status-version 3")) { startToken = "network-status-version 3"; - } else if (line.startsWith("Downloaded ") || - line.startsWith("ExitNode ")) { + } else if (line.startsWith("Downloaded ") + || line.startsWith("ExitNode ")) { startToken = "ExitNode "; } else { System.out.println("Unknown descriptor type in file '" + file @@ -273,7 +276,8 @@ public class ExoneraTorDatabaseImporter { return; } String splitToken = "\n" + startToken; - int length = bytes.length, start = asciiString.indexOf(startToken); + int length = bytes.length; + int start = asciiString.indexOf(startToken); while (start < length) { int end = asciiString.indexOf(splitToken, start); if (end < 0) { @@ -298,6 +302,7 @@ public class ExoneraTorDatabaseImporter {
/* Date format to parse UTC timestamps. */ private static SimpleDateFormat parseFormat; + static { parseFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); parseFormat.setTimeZone(TimeZone.getTimeZone("UTC")); @@ -308,32 +313,35 @@ public class ExoneraTorDatabaseImporter { try { BufferedReader br = new BufferedReader(new StringReader(new String( bytes, "US-ASCII"))); - String line, fingerprint = null, descriptor = null; + String line; + String fingerprint = null; + String descriptor = null; Set<String> orAddresses = new HashSet<String>(); long validAfterMillis = -1L; StringBuilder rawStatusentryBuilder = null; boolean isRunning = false; while ((line = br.readLine()) != null) { - if (line.startsWith("vote-status ") && - !line.equals("vote-status consensus")) { - System.out.println("File '" + file + "' contains network status " - + "*votes*, not network status *consensuses*. Skipping."); + if (line.startsWith("vote-status ") + && !line.equals("vote-status consensus")) { + System.out.println("File '" + file + "' contains network " + + "status *votes*, not network status *consensuses*. " + + "Skipping."); return; } else if (line.startsWith("valid-after ")) { String validAfterTime = line.substring("valid-after ".length()); try { - validAfterMillis = parseFormat.parse(validAfterTime). - getTime(); + validAfterMillis = parseFormat.parse(validAfterTime) + .getTime(); } catch (ParseException e) { System.out.println("Could not parse valid-after timestamp in " + "'" + file + "'. Skipping."); return; } - } else if (line.startsWith("r ") || - line.equals("directory-footer")) { + } else if (line.startsWith("r ") + || line.equals("directory-footer")) { if (isRunning) { - byte[] rawStatusentry = rawStatusentryBuilder.toString(). - getBytes(); + byte[] rawStatusentry = rawStatusentryBuilder.toString() + .getBytes(); importStatusentry(validAfterMillis, fingerprint, descriptor, orAddresses, rawStatusentry); orAddresses = new HashSet<String>(); @@ -419,10 +427,10 @@ public class ExoneraTorDatabaseImporter { addressHexString = addressHexString.replaceFirst("x", String.format("%" + (33 - addressHexString.length()) + "s", "0")); - if (!addressHexString.contains("x") && - addressHexString.length() == 32) { - orAddress48 = addressHexString.replaceAll(" ", "0"). - toLowerCase().substring(0, 12); + if (!addressHexString.contains("x") + && addressHexString.length() == 32) { + orAddress48 = addressHexString.replaceAll(" ", "0") + .toLowerCase().substring(0, 12); } } if (orAddress48 != null) { @@ -455,8 +463,8 @@ public class ExoneraTorDatabaseImporter { StringBuilder rawExitlistentryBuilder = new StringBuilder(); while (true) { String line = br.readLine(); - if ((line == null || line.startsWith("ExitNode ")) && - fingerprint != null) { + if ((line == null || line.startsWith("ExitNode ")) + && fingerprint != null) { for (String exitAddressLine : exitAddressLines) { String[] parts = exitAddressLine.split(" "); String exitAddress = parts[1]; @@ -481,8 +489,8 @@ public class ExoneraTorDatabaseImporter { + "'" + file + "'. Skipping."); return; } - byte[] rawExitlistentry = rawExitlistentryBuilder.toString(). - getBytes(); + byte[] rawExitlistentry = rawExitlistentryBuilder.toString() + .getBytes(); importExitlistentry(fingerprint, exitAddress24, exitAddress, scannedMillis, rawExitlistentry); } @@ -494,8 +502,8 @@ public class ExoneraTorDatabaseImporter { } rawExitlistentryBuilder.append(line + "\n"); if (line.startsWith("ExitNode ")) { - fingerprint = line.substring("ExitNode ".length()). - toLowerCase(); + fingerprint = line.substring("ExitNode ".length()) + .toLowerCase(); } else if (line.startsWith("ExitAddress ")) { exitAddressLines.add(line); } diff --git a/src/main/java/org/torproject/exonerator/ExoneraTorServlet.java b/src/main/java/org/torproject/exonerator/ExoneraTorServlet.java index 68d79a3..f637150 100644 --- a/src/main/java/org/torproject/exonerator/ExoneraTorServlet.java +++ b/src/main/java/org/torproject/exonerator/ExoneraTorServlet.java @@ -1,7 +1,11 @@ -/* Copyright 2011--2015 The Tor Project +/* Copyright 2011--2016 The Tor Project * See LICENSE for licensing information */ + package org.torproject.exonerator;
+import org.apache.commons.codec.binary.Hex; +import org.apache.commons.lang.StringEscapeUtils; + import java.io.IOException; import java.io.PrintWriter; import java.sql.CallableStatement; @@ -32,9 +36,6 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.sql.DataSource;
-import org.apache.commons.codec.binary.Hex; -import org.apache.commons.lang.StringEscapeUtils; - public class ExoneraTorServlet extends HttpServlet {
private static final long serialVersionUID = 1370088989739567509L; @@ -113,8 +114,8 @@ public class ExoneraTorServlet extends HttpServlet { if (timestampStr != null && timestampStr.length() > 0) { try { timestamp = dateFormat.parse(timestampParameter).getTime(); - if (timestamp < firstAndLastDates[0] || - timestamp > firstAndLastDates[1]) { + if (timestamp < firstAndLastDates[0] + || timestamp > firstAndLastDates[1]) { timestampOutOfRange = true; } } catch (ParseException e) { @@ -123,12 +124,12 @@ public class ExoneraTorServlet extends HttpServlet { }
/* Write form. */ - this.writeForm(out, rb, relayIP, relayIPHasError || - ("".equals(relayIP) && !"".equals(timestampStr)), timestampStr, - !relayIPHasError && - !("".equals(relayIP) && !"".equals(timestampStr)) && - (timestampHasError || timestampOutOfRange || - (!"".equals(relayIP) && "".equals(timestampStr)))); + this.writeForm(out, rb, relayIP, relayIPHasError + || ("".equals(relayIP) && !"".equals(timestampStr)), timestampStr, + !relayIPHasError + && !("".equals(relayIP) && !"".equals(timestampStr)) + && (timestampHasError || timestampOutOfRange + || (!"".equals(relayIP) && "".equals(timestampStr))));
/* If both parameters are empty, don't print any summary and exit. * This is the start page. */ @@ -219,8 +220,8 @@ public class ExoneraTorServlet extends HttpServlet { this.writeSummaryPositive(out, rb, relayIP, timestampStr); this.writeTechnicalDetails(out, rb, relayIP, timestampStr, statusEntries); - } else if (addressesInSameNetwork != null && - !addressesInSameNetwork.isEmpty()) { + } else if (addressesInSameNetwork != null + && !addressesInSameNetwork.isEmpty()) { this.writeSummaryAddressesInSameNetwork(out, rb, relayIP, timestampStr, addressesInSameNetwork); } else { @@ -240,10 +241,10 @@ public class ExoneraTorServlet extends HttpServlet { if (passedIpParameter != null && passedIpParameter.length() > 0) { String ipParameter = passedIpParameter.trim(); Pattern ipv4AddressPattern = Pattern.compile( - "^([01]?\d\d?|2[0-4]\d|25[0-5])\." + - "([01]?\d\d?|2[0-4]\d|25[0-5])\." + - "([01]?\d\d?|2[0-4]\d|25[0-5])\." + - "([01]?\d\d?|2[0-4]\d|25[0-5])$"); + "^([01]?\d\d?|2[0-4]\d|25[0-5])\." + + "([01]?\d\d?|2[0-4]\d|25[0-5])\." + + "([01]?\d\d?|2[0-4]\d|25[0-5])\." + + "([01]?\d\d?|2[0-4]\d|25[0-5])$"); Pattern ipv6AddressPattern = Pattern.compile( "^\[?[0-9a-fA-F:]{3,39}\]?$"); if (ipv4AddressPattern.matcher(ipParameter).matches()) { @@ -278,8 +279,8 @@ public class ExoneraTorServlet extends HttpServlet { addressHexString = addressHexString.replaceFirst("x", String.format("%" + (33 - addressHexString.length()) + "s", "0")); - if (!addressHexString.contains("x") && - addressHexString.length() == 32) { + if (!addressHexString.contains("x") + && addressHexString.length() == 32) { relayIP = ipParameter.toLowerCase(); } } @@ -325,10 +326,10 @@ public class ExoneraTorServlet extends HttpServlet { addressHexString = addressHexString.replaceFirst("x", String.format("%" + (33 - addressHexString.length()) + "s", "0")); - if (!addressHexString.contains("x") && - addressHexString.length() == 32) { - address48 = addressHexString.replaceAll(" ", "0"). - toLowerCase(); + if (!addressHexString.contains("x") + && addressHexString.length() == 32) { + address48 = addressHexString.replaceAll(" ", "0") + .toLowerCase(); } } return address48; @@ -340,8 +341,8 @@ public class ExoneraTorServlet extends HttpServlet { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); dateFormat.setLenient(false); - if (passedTimestampParameter != null && - passedTimestampParameter.length() > 0) { + if (passedTimestampParameter != null + && passedTimestampParameter.length() > 0) { String timestampParameter = passedTimestampParameter.trim(); try { long timestamp = dateFormat.parse(timestampParameter).getTime(); @@ -436,8 +437,8 @@ public class ExoneraTorServlet extends HttpServlet { ResultSet rs = cs.executeQuery(); while (rs.next()) { byte[] rawstatusentry = rs.getBytes(1); - SortedSet<String> addresses = new TreeSet<String>(), - addressesHex = new TreeSet<String>(); + SortedSet<String> addresses = new TreeSet<String>(); + SortedSet<String> addressesHex = new TreeSet<String>(); long validafter = rs.getTimestamp(2, utcCalendar).getTime(); String validAfterString = validAfterTimeFormat.format(validafter); String fingerprint = rs.getString(3).toUpperCase(); @@ -627,8 +628,8 @@ public class ExoneraTorServlet extends HttpServlet { ipValue, timestampHasError ? " has-error" : "", rb.getString("form.timestamp.label"), - timestampStr != null && timestampStr.length() > 0 ? - " value="" + timestampStr + """ : "", + timestampStr != null && timestampStr.length() > 0 + ? " value="" + timestampStr + """ : "", rb.getString("form.search.label")); }
@@ -685,8 +686,8 @@ public class ExoneraTorServlet extends HttpServlet {
private void writeSummaryInvalidIp(PrintWriter out, ResourceBundle rb, String ipParameter) throws IOException { - String escapedIpParameter = ipParameter.length() > 40 ? - StringEscapeUtils.escapeHtml(ipParameter.substring(0, 40)) + String escapedIpParameter = ipParameter.length() > 40 + ? StringEscapeUtils.escapeHtml(ipParameter.substring(0, 40)) + "[...]" : StringEscapeUtils.escapeHtml(ipParameter); this.writeSummary(out, rb.getString("summary.heading"), "panel-danger", @@ -697,10 +698,10 @@ public class ExoneraTorServlet extends HttpServlet {
private void writeSummaryInvalidTimestamp(PrintWriter out, ResourceBundle rb, String timestampParameter) throws IOException { - String escapedTimestampParameter = timestampParameter.length() > 20 ? - StringEscapeUtils.escapeHtml(timestampParameter. - substring(0, 20)) + "[...]" : - StringEscapeUtils.escapeHtml(timestampParameter); + String escapedTimestampParameter = timestampParameter.length() > 20 + ? StringEscapeUtils.escapeHtml(timestampParameter + .substring(0, 20)) + "[...]" + : StringEscapeUtils.escapeHtml(timestampParameter); this.writeSummary(out, rb.getString("summary.heading"), "panel-danger", rb.getString("summary.invalidparams.invalidtimestamp.title"), @@ -727,7 +728,8 @@ public class ExoneraTorServlet extends HttpServlet { Object[][] panelItems = new Object[addressesInSameNetwork.size()][]; for (int i = 0; i < addressesInSameNetwork.size(); i++) { String addressInSameNetwork = addressesInSameNetwork.get(i); - String link, address; + String link; + String address; if (addressInSameNetwork.contains(":")) { link = String.format("/?ip=[%s]×tamp=%s", addressInSameNetwork.replaceAll(":", "%3A"), timestampStr); @@ -748,8 +750,8 @@ public class ExoneraTorServlet extends HttpServlet {
private void writeSummaryPositive(PrintWriter out, ResourceBundle rb, String relayIP, String timestampStr) throws IOException { - String formattedRelayIP = relayIP.contains(":") ? - "[" + relayIP + "]" : relayIP; + String formattedRelayIP = relayIP.contains(":") + ? "[" + relayIP + "]" : relayIP; this.writeSummary(out, rb.getString("summary.heading"), "panel-success", rb.getString("summary.positive.title"), null, rb.getString("summary.positive.body"), formattedRelayIP, @@ -758,8 +760,8 @@ public class ExoneraTorServlet extends HttpServlet {
private void writeSummaryNegative(PrintWriter out, ResourceBundle rb, String relayIP, String timestampStr) throws IOException { - String formattedRelayIP = relayIP.contains(":") ? - "[" + relayIP + "]" : relayIP; + String formattedRelayIP = relayIP.contains(":") + ? "[" + relayIP + "]" : relayIP; this.writeSummary(out, rb.getString("summary.heading"), "panel-warning", rb.getString("summary.negative.title"), null, rb.getString("summary.negative.body"), formattedRelayIP, @@ -797,8 +799,8 @@ public class ExoneraTorServlet extends HttpServlet { private void writeTechnicalDetails(PrintWriter out, ResourceBundle rb, String relayIP, String timestampStr, List<String[]> tableRows) throws IOException { - String formattedRelayIP = relayIP.contains(":") ? - "[" + relayIP + "]" : relayIP; + String formattedRelayIP = relayIP.contains(":") + ? "[" + relayIP + "]" : relayIP; out.printf(" <div class="row">\n" + " <div class="col-xs-12">\n" + " <h2>%s</h2>\n" @@ -853,8 +855,8 @@ public class ExoneraTorServlet extends HttpServlet {
private void writePermanentLink(PrintWriter out, ResourceBundle rb, String relayIP, String timestampStr) throws IOException { - String encodedAddress = relayIP.contains(":") ? - "[" + relayIP.replaceAll(":", "%3A") + "]" : relayIP; + String encodedAddress = relayIP.contains(":") + ? "[" + relayIP.replaceAll(":", "%3A") + "]" : relayIP; out.printf(" <div class="row">\n" + " <div class="col-xs-12">\n" + " <h2>%s</h2>\n"
tor-commits@lists.torproject.org