Commit bd235118 authored by Karsten Loesing's avatar Karsten Loesing
Browse files

Prepare ExoneraTorServlet for integration into Tor Metrics.

Also add a new ExoneraTorRedirectServlet that redirects to Tor
Metrics, and make it the default.

Implements #23549.
parent 48c44a23
# Changes in version 2.2.0 - 2018-08-??
* Medium changes
- Prepare ExoneraTorServlet for integration into Tor Metrics.
- Add a new ExoneraTorRedirectServlet that redirects to Tor
Metrics, and make it the default.
* Minor changes
- Provide a thin jar file without dependencies.
# Changes in version 2.1.0 - 2018-05-29 # Changes in version 2.1.0 - 2018-05-29
* Medium changes * Medium changes
......
build @ 08514a32
Subproject commit 23c6e0be5fab9463f137615053ef412e4da2315e Subproject commit 08514a32afefbeef848b80f9a338ee840c282604
/* Copyright 2018 The Tor Project
* See LICENSE for licensing information */
package org.torproject.metrics.exonerator;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ExoneraTorRedirectServlet extends HttpServlet {
private static final long serialVersionUID = 526889516976914884L;
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) {
String redirectUrl = "https://metrics.torproject.org/exonerator.html";
if (null != request.getQueryString()) {
redirectUrl += "?" + request.getQueryString();
}
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.setHeader("Location", redirectUrl);
}
}
...@@ -141,6 +141,9 @@ public class ExoneraTorServlet extends HttpServlet { ...@@ -141,6 +141,9 @@ public class ExoneraTorServlet extends HttpServlet {
PrintWriter out = new PrintWriter(so); PrintWriter out = new PrintWriter(so);
this.writeHeader(out, rb, langStr); this.writeHeader(out, rb, langStr);
/* Obtain request URL without query string parameters for links. */
String requestUrl = request.getRequestURL().toString();
/* Write form. */ /* Write form. */
boolean timestampOutOfRange = requestedDate.valid boolean timestampOutOfRange = requestedDate.valid
&& (firstDate.valid && requestedDate.date.isBefore(firstDate.date) && (firstDate.valid && requestedDate.date.isBefore(firstDate.date)
...@@ -155,43 +158,43 @@ public class ExoneraTorServlet extends HttpServlet { ...@@ -155,43 +158,43 @@ public class ExoneraTorServlet extends HttpServlet {
/* If both parameters are empty, don't print any summary and exit. /* If both parameters are empty, don't print any summary and exit.
* This is the start page. */ * This is the start page. */
if ("".equals(relayIp) && requestedDate.empty) { if ("".equals(relayIp) && requestedDate.empty) {
this.writeFooter(out, rb, null, null); this.writeFooter(out, rb, requestUrl, null, null);
/* If only one parameter is empty and the other is not, print summary /* If only one parameter is empty and the other is not, print summary
* with warning message and exit. */ * with warning message and exit. */
} else if ("".equals(relayIp)) { } else if ("".equals(relayIp)) {
this.writeSummaryNoIp(out, rb); this.writeSummaryNoIp(out, rb);
this.writeFooter(out, rb, null, null); this.writeFooter(out, rb, requestUrl, null, null);
} else if (requestedDate.empty) { } else if (requestedDate.empty) {
this.writeSummaryNoTimestamp(out, rb); this.writeSummaryNoTimestamp(out, rb);
this.writeFooter(out, rb, null, null); this.writeFooter(out, rb, requestUrl, null, null);
/* If there's an issue with parsing either of the parameters, print /* If there's an issue with parsing either of the parameters, print
* summary with error message and exit. */ * summary with error message and exit. */
} else if (relayIpHasError) { } else if (relayIpHasError) {
this.writeSummaryInvalidIp(out, rb, ipParameter); this.writeSummaryInvalidIp(out, rb, ipParameter);
this.writeFooter(out, rb, null, null); this.writeFooter(out, rb, requestUrl, null, null);
} else if (!requestedDate.valid) { } else if (!requestedDate.valid) {
this.writeSummaryInvalidTimestamp(out, rb, requestedDate.asRequested); this.writeSummaryInvalidTimestamp(out, rb, requestedDate.asRequested);
this.writeFooter(out, rb, null, null); this.writeFooter(out, rb, requestUrl, null, null);
/* If the timestamp is too recent, print summary with error message and /* If the timestamp is too recent, print summary with error message and
* exit. */ * exit. */
} else if (requestedDate.tooRecent) { } else if (requestedDate.tooRecent) {
this.writeSummaryTimestampTooRecent(out, rb); this.writeSummaryTimestampTooRecent(out, rb);
this.writeFooter(out, rb, null, null); this.writeFooter(out, rb, requestUrl, null, null);
/* If we were unable to connect to the database, /* If we were unable to connect to the database,
* write an error message. */ * write an error message. */
} else if (!successfullyConnectedToBackend) { } else if (!successfullyConnectedToBackend) {
this.writeSummaryUnableToConnectToBackend(out, rb); this.writeSummaryUnableToConnectToBackend(out, rb);
this.writeFooter(out, rb, null, null); this.writeFooter(out, rb, requestUrl, null, null);
/* Similarly, if we found the database to be empty, /* Similarly, if we found the database to be empty,
* write an error message, too. */ * write an error message, too. */
} else if (firstDate.empty || lastDate.empty) { } else if (firstDate.empty || lastDate.empty) {
this.writeSummaryNoData(out, rb); this.writeSummaryNoData(out, rb);
this.writeFooter(out, rb, null, null); this.writeFooter(out, rb, requestUrl, null, null);
/* If the requested date is out of range, tell the user. */ /* If the requested date is out of range, tell the user. */
} else if (timestampOutOfRange) { } else if (timestampOutOfRange) {
...@@ -199,11 +202,11 @@ public class ExoneraTorServlet extends HttpServlet { ...@@ -199,11 +202,11 @@ public class ExoneraTorServlet extends HttpServlet {
this.writeSummaryTimestampOutsideRange(out, rb, requestedDate.asString, this.writeSummaryTimestampOutsideRange(out, rb, requestedDate.asString,
firstDate.asString, lastDate.date.isBefore(dayBeforeYesterday) firstDate.asString, lastDate.date.isBefore(dayBeforeYesterday)
? lastDate.asString : dayBeforeYesterday.format(ISO_LOCAL_DATE)); ? lastDate.asString : dayBeforeYesterday.format(ISO_LOCAL_DATE));
this.writeFooter(out, rb, relayIp, requestedDate.asString); this.writeFooter(out, rb, requestUrl, relayIp, requestedDate.asString);
} else if (noRelevantConsensuses) { } else if (noRelevantConsensuses) {
this.writeSummaryNoDataForThisInterval(out, rb); this.writeSummaryNoDataForThisInterval(out, rb);
this.writeFooter(out, rb, relayIp, requestedDate.asString); this.writeFooter(out, rb, requestUrl, relayIp, requestedDate.asString);
/* Print out result. */ /* Print out result. */
} else { } else {
...@@ -218,15 +221,15 @@ public class ExoneraTorServlet extends HttpServlet { ...@@ -218,15 +221,15 @@ public class ExoneraTorServlet extends HttpServlet {
} else { } else {
this.writeSummaryNegative(out, rb, relayIp, requestedDate.asString); this.writeSummaryNegative(out, rb, relayIp, requestedDate.asString);
} }
this.writePermanentLink(out, rb, relayIp, requestedDate.asString, this.writePermanentLink(out, rb, requestUrl, relayIp,
langStr); requestedDate.asString, langStr);
this.writeFooter(out, rb, relayIp, requestedDate.asString); this.writeFooter(out, rb, requestUrl, relayIp, requestedDate.asString);
} }
/* Forward to the JSP that adds header and footer. */ /* Forward to the JSP that adds header and footer. */
request.setAttribute("lang", langStr); request.setAttribute("lang", langStr);
request.setAttribute("body", so.toString()); request.setAttribute("body", so.toString());
request.getRequestDispatcher("WEB-INF/index.jsp").forward(request, request.getRequestDispatcher("WEB-INF/exonerator.jsp").forward(request,
response); response);
} catch (Throwable th) { } catch (Throwable th) {
logger.error("Some problem in doGet. Returning error.", th); logger.error("Some problem in doGet. Returning error.", th);
...@@ -334,7 +337,7 @@ public class ExoneraTorServlet extends HttpServlet { ...@@ -334,7 +337,7 @@ public class ExoneraTorServlet extends HttpServlet {
+ " <div class=\"col-xs-12\">\n" + " <div class=\"col-xs-12\">\n"
+ " <div class=\"text-center\">\n" + " <div class=\"text-center\">\n"
+ " <div class=\"row vbottom15\">\n" + " <div class=\"row vbottom15\">\n"
+ " <h4>%s</h4>\n" + " <p>%s</p>\n"
+ " </div> <!-- row -->\n" + " </div> <!-- row -->\n"
+ " <form class=\"form-inline\">\n" + " <form class=\"form-inline\">\n"
+ " <div class=\"form-group%s\">\n" + " <div class=\"form-group%s\">\n"
...@@ -605,22 +608,24 @@ public class ExoneraTorServlet extends HttpServlet { ...@@ -605,22 +608,24 @@ public class ExoneraTorServlet extends HttpServlet {
} }
private void writePermanentLink(PrintWriter out, ResourceBundle rb, private void writePermanentLink(PrintWriter out, ResourceBundle rb,
String relayIp, String timestampStr, String langStr) throws IOException { String requestUrl, String relayIp, String timestampStr, String langStr)
throws IOException {
String encodedAddress = relayIp.contains(":") String encodedAddress = relayIp.contains(":")
? "[" + relayIp.replaceAll(":", "%3A") + "]" : relayIp; ? "[" + relayIp.replaceAll(":", "%3A") + "]" : relayIp;
out.printf(" <div class=\"row\">\n" out.printf(" <div class=\"row\">\n"
+ " <div class=\"col-xs-12\">\n" + " <div class=\"col-xs-12\">\n"
+ " <h2>%s</h2>\n" + " <h2>%s</h2>\n"
+ " <pre>https://exonerator.torproject.org/?ip=%s&amp;" + " <pre>%s?ip=%s&amp;"
+ "timestamp=%s&amp;lang=%s</pre>\n" + "timestamp=%s&amp;lang=%s</pre>\n"
+ " </div><!-- col -->\n" + " </div><!-- col -->\n"
+ " </div><!-- row -->\n", + " </div><!-- row -->\n",
rb.getString("permanentlink.heading"), rb.getString("permanentlink.heading"), requestUrl,
encodedAddress, timestampStr, langStr); encodedAddress, timestampStr, langStr);
} }
private void writeFooter(PrintWriter out, ResourceBundle rb, String relayIp, private void writeFooter(PrintWriter out, ResourceBundle rb,
String timestampStr) throws IOException { String requestUrl, String relayIp, String timestampStr)
throws IOException {
out.printf(" </div><!-- container -->\n" out.printf(" </div><!-- container -->\n"
+ " <div class=\"container\">\n" + " <div class=\"container\">\n"
+ " <div class=\"row\">\n" + " <div class=\"row\">\n"
...@@ -651,11 +656,12 @@ public class ExoneraTorServlet extends HttpServlet { ...@@ -651,11 +656,12 @@ public class ExoneraTorServlet extends HttpServlet {
for (Map.Entry<String, String> entry for (Map.Entry<String, String> entry
: this.availableLanguageNames.entrySet()) { : this.availableLanguageNames.entrySet()) {
if (null != relayIp && null != timestampStr) { if (null != relayIp && null != timestampStr) {
out.printf(" <a href=\"/?ip=%s&timestamp=%s&lang=%s\">%s</a>", out.printf(" <a href=\"%s?ip=%s&timestamp=%s&lang=%s\">%s</a>",
relayIp, timestampStr, entry.getKey(), entry.getValue()); requestUrl, relayIp, timestampStr, entry.getKey(),
entry.getValue());
} else { } else {
out.printf(" <a href=\"/?lang=%s\">%s</a>", out.printf(" <a href=\"%s?lang=%s\">%s</a>",
entry.getKey(), entry.getValue()); requestUrl, entry.getKey(), entry.getValue());
} }
} }
out.printf("</p>\n" out.printf("</p>\n"
......
...@@ -6,10 +6,19 @@ ...@@ -6,10 +6,19 @@
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
metadata-complete="false" > metadata-complete="false" >
<!--
Notice: By default, we redirect all requests to:
https://metrics.torproject.org/exonerator.html
If this is not the desired behavior, simply replace the servlet-class below
with org.torproject.metrics.exonerator.ExoneraTorServlet and redeploy.
-->
<servlet> <servlet>
<servlet-name>ExoneraTor</servlet-name> <servlet-name>ExoneraTor</servlet-name>
<servlet-class> <servlet-class>
org.torproject.metrics.exonerator.ExoneraTorServlet org.torproject.metrics.exonerator.ExoneraTorRedirectServlet
</servlet-class> </servlet-class>
</servlet> </servlet>
<servlet-mapping> <servlet-mapping>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment