Better web app logging and checkstyle update

This commit is contained in:
Sean Owen 2017-10-06 12:09:40 +01:00
parent 88120e298d
commit 5b9ce1cd74
2 changed files with 16 additions and 13 deletions

View file

@ -462,7 +462,7 @@
<dependency> <dependency>
<groupId>com.puppycrawl.tools</groupId> <groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId> <artifactId>checkstyle</artifactId>
<version>6.19</version> <version>8.2</version>
</dependency> </dependency>
</dependencies> </dependencies>
</plugin> </plugin>

View file

@ -168,8 +168,8 @@ public final class DecodeServlet extends HttpServlet {
if (imageURI.getScheme() == null) { if (imageURI.getScheme() == null) {
imageURI = new URI("http://" + imageURIString); imageURI = new URI("http://" + imageURIString);
} }
} catch (URISyntaxException urise) { } catch (URISyntaxException e) {
log.info("URI " + imageURIString + " was not valid: " + urise); log.info("Error " + e + " while parsing URI: " + imageURIString);
errorResponse(request, response, "badurl"); errorResponse(request, response, "badurl");
return; return;
} }
@ -180,10 +180,11 @@ public final class DecodeServlet extends HttpServlet {
try { try {
image = ImageReader.readDataURIImage(imageURI); image = ImageReader.readDataURIImage(imageURI);
} catch (IOException | IllegalStateException e) { } catch (IOException | IllegalStateException e) {
log.info(e.toString()); log.info("Error " + e + " while reading data URI: " + imageURIString);
errorResponse(request, response, "badurl"); errorResponse(request, response, "badurl");
} }
if (image == null) { if (image == null) {
log.info("Couldn't read data URI: " + imageURIString);
errorResponse(request, response, "badimage"); errorResponse(request, response, "badimage");
return; return;
} }
@ -199,19 +200,20 @@ public final class DecodeServlet extends HttpServlet {
try { try {
imageURL = imageURI.toURL(); imageURL = imageURI.toURL();
} catch (MalformedURLException ignored) { } catch (MalformedURLException ignored) {
log.info("URI was not valid: " + imageURIString); log.info("URI is not a URL: " + imageURIString);
errorResponse(request, response, "badurl"); errorResponse(request, response, "badurl");
return; return;
} }
String protocol = imageURL.getProtocol(); String protocol = imageURL.getProtocol();
if (!"http".equalsIgnoreCase(protocol) && !"https".equalsIgnoreCase(protocol)) { if (!"http".equalsIgnoreCase(protocol) && !"https".equalsIgnoreCase(protocol)) {
log.info("URI was not valid: " + imageURIString); log.info("URL protocol was not valid: " + imageURIString);
errorResponse(request, response, "badurl"); errorResponse(request, response, "badurl");
return; return;
} }
if (destHostTracker.isBanned(imageURL.getHost())) { if (destHostTracker.isBanned(imageURL.getHost())) {
log.info("Temporarily not requesting from host: " + imageURIString);
errorResponse(request, response, "badurl"); errorResponse(request, response, "badurl");
} }
@ -219,12 +221,13 @@ public final class DecodeServlet extends HttpServlet {
try { try {
connection = (HttpURLConnection) imageURL.openConnection(); connection = (HttpURLConnection) imageURL.openConnection();
} catch (IllegalArgumentException ignored) { } catch (IllegalArgumentException ignored) {
log.info("URI could not be opened: " + imageURL); log.info("URL could not be opened: " + imageURIString);
errorResponse(request, response, "badurl"); errorResponse(request, response, "badurl");
return; return;
} }
connection.setAllowUserInteraction(false); connection.setAllowUserInteraction(false);
connection.setInstanceFollowRedirects(true);
connection.setReadTimeout(5000); connection.setReadTimeout(5000);
connection.setConnectTimeout(5000); connection.setConnectTimeout(5000);
connection.setRequestProperty(HttpHeaders.USER_AGENT, "zxing.org"); connection.setRequestProperty(HttpHeaders.USER_AGENT, "zxing.org");
@ -238,7 +241,7 @@ public final class DecodeServlet extends HttpServlet {
// javax.net.ssl.SSLPeerUnverifiedException, // javax.net.ssl.SSLPeerUnverifiedException,
// org.apache.http.NoHttpResponseException, // org.apache.http.NoHttpResponseException,
// org.apache.http.client.ClientProtocolException, // org.apache.http.client.ClientProtocolException,
log.info(e.toString()); log.info("Error " + e + " connecting to " + imageURIString);
errorResponse(request, response, "badurl"); errorResponse(request, response, "badurl");
return; return;
} }
@ -246,30 +249,30 @@ public final class DecodeServlet extends HttpServlet {
try (InputStream is = connection.getInputStream()) { try (InputStream is = connection.getInputStream()) {
try { try {
if (connection.getResponseCode() != HttpServletResponse.SC_OK) { if (connection.getResponseCode() != HttpServletResponse.SC_OK) {
log.info("Unsuccessful return code: " + connection.getResponseCode()); log.info("Unsuccessful return code " + connection.getResponseCode() + " from " + imageURIString);
errorResponse(request, response, "badurl"); errorResponse(request, response, "badurl");
return; return;
} }
if (connection.getHeaderFieldInt(HttpHeaders.CONTENT_LENGTH, 0) > MAX_IMAGE_SIZE) { if (connection.getHeaderFieldInt(HttpHeaders.CONTENT_LENGTH, 0) > MAX_IMAGE_SIZE) {
log.info("Too large"); log.info("Too large: " + imageURIString);
errorResponse(request, response, "badimage"); errorResponse(request, response, "badimage");
return; return;
} }
// Assume we'll only handle image/* content types // Assume we'll only handle image/* content types
String contentType = connection.getContentType(); String contentType = connection.getContentType();
if (contentType != null && !contentType.startsWith("image/")) { if (contentType != null && !contentType.startsWith("image/")) {
log.info("Wrong content type: " + contentType); log.info("Wrong content type " + contentType + ": " + imageURIString);
errorResponse(request, response, "badimage"); errorResponse(request, response, "badimage");
return; return;
} }
log.info("Decoding " + imageURL); log.info("Decoding " + imageURIString);
processStream(is, request, response); processStream(is, request, response);
} finally { } finally {
consumeRemainder(is); consumeRemainder(is);
} }
} catch (IOException ioe) { } catch (IOException ioe) {
log.info(ioe.toString()); log.info("Error " + ioe + " processing " + imageURIString);
errorResponse(request, response, "badurl"); errorResponse(request, response, "badurl");
} finally { } finally {
connection.disconnect(); connection.disconnect();