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

Only serve completed dates.

Implements #24580.
parent a8bb4bab
......@@ -15,6 +15,8 @@ import java.io.StringWriter;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
......@@ -76,6 +78,8 @@ public class ExoneraTorServlet extends HttpServlet {
String timestampParameter = request.getParameter("timestamp");
String timestampStr = parseTimestampParameter(timestampParameter);
final boolean timestampHasError = timestampStr == null;
final boolean timestampTooRecent = !timestampHasError
&& checkTimestampTooRecent(timestampStr);
/* Parse lang parameter. */
String langParameter = request.getParameter("lang");
......@@ -96,7 +100,7 @@ public class ExoneraTorServlet extends HttpServlet {
/* Only query, if we received valid user input. */
if (null != relayIp && !relayIp.isEmpty() && null != timestampStr
&& !timestampStr.isEmpty()) {
&& !timestampStr.isEmpty() && !timestampTooRecent) {
QueryResponse queryResponse = this.queryBackend(relayIp, timestampStr);
if (null != queryResponse) {
successfullyConnectedToBackend = true;
......@@ -175,6 +179,12 @@ public class ExoneraTorServlet extends HttpServlet {
this.writeSummaryInvalidTimestamp(out, rb, timestampParameter);
this.writeFooter(out, rb, null, null);
/* If the timestamp is too recent, print summary with error message and
* exit. */
} else if (timestampTooRecent) {
this.writeSummaryTimestampTooRecent(out, rb, timestampStr);
this.writeFooter(out, rb, null, null);
/* If we were unable to connect to the database,
* write an error message. */
} else if (!successfullyConnectedToBackend) {
......@@ -307,6 +317,14 @@ public class ExoneraTorServlet extends HttpServlet {
return timestampStr;
}
/** Return whether the timestamp parameter is too recent, which is the case if
* it matches the day before the current system date (in UTC) or is even
* younger. */
static boolean checkTimestampTooRecent(String timestampParameter) {
return timestampParameter.compareTo(ZonedDateTime.now(ZoneOffset.UTC)
.toLocalDate().minusDays(1).toString()) >= 0;
}
/* Helper method for fetching a query response via URL. */
private QueryResponse queryBackend(String relayIp, String timestampStr) {
......@@ -458,6 +476,14 @@ public class ExoneraTorServlet extends HttpServlet {
escapedTimestampParameter, "\"YYYY-MM-DD\"");
}
private void writeSummaryTimestampTooRecent(PrintWriter out,
ResourceBundle rb, String timestampStr) throws IOException {
this.writeSummary(out, rb.getString("summary.heading"), "panel-danger",
rb.getString("summary.invalidparams.timestamptoorecent.title"),
null, rb.getString("summary.invalidparams.timestamptoorecent.body"),
timestampStr);
}
private void writeSummaryNoDataForThisInterval(PrintWriter out,
ResourceBundle rb) throws IOException {
String contactLink =
......
......@@ -16,6 +16,8 @@ import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
......@@ -90,6 +92,11 @@ public class QueryServlet extends HttpServlet {
"Invalid timestamp parameter.");
return;
}
if (this.checkTimestampTooRecent(timestampParameter)) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST,
"Timestamp too recent.");
return;
}
/* Query the database. */
QueryResponse queryResponse = this.queryDatabase(relayIp, timestamp);
......@@ -228,6 +235,15 @@ public class QueryServlet extends HttpServlet {
return timestamp;
}
/** Return whether the timestamp parameter is too recent, which is the case if
* it matches the day before the current system date (in UTC) or is even
* younger. */
private boolean checkTimestampTooRecent(String timestampParameter) {
return timestampParameter.compareTo(ZonedDateTime.now(ZoneOffset.UTC)
.toLocalDate().minusDays(1).toString()) >= 0;
}
/* Helper methods for querying the database. */
private QueryResponse queryDatabase(String relayIp, long timestamp) {
......
......@@ -19,6 +19,8 @@ summary.invalidparams.invalidip.title=Invalid IP address parameter
summary.invalidparams.invalidip.body=Sorry, %s is not a valid IP address. The expected IP address formats are %s or %s.
summary.invalidparams.invalidtimestamp.title=Invalid date parameter
summary.invalidparams.invalidtimestamp.body=Sorry, %s is not a valid date. The expected date format is %s.
summary.invalidparams.timestamptoorecent.title=Date parameter too recent
summary.invalidparams.timestamptoorecent.body=Sorry, %s is too recent. The database may not yet contain enough data to correctly answer this request.
summary.serverproblem.nodata.title=Server problem
summary.serverproblem.nodata.body.text=The database does not contain any data for the requested date. Please try again later. If this problem persists, please %s!
summary.serverproblem.nodata.body.link=let us know
......
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