From 02d3697a59087b87bdae6cb9c85e508f5de90c3d Mon Sep 17 00:00:00 2001 From: Sean Owen Date: Sat, 6 Jun 2015 10:46:18 +0100 Subject: [PATCH] Remove spurious stack trace from singleton exception, and various minor edits --- .../com/google/zxing/ChecksumException.java | 3 ++ .../com/google/zxing/FormatException.java | 3 ++ .../com/google/zxing/NotFoundException.java | 3 ++ .../com/google/zxing/ReaderException.java | 1 + .../zxing/client/result/URIParsedResult.java | 8 ++--- .../RSSExpandedImage2resultTestCase.java | 2 +- .../zxing/qrcode/encoder/QRCodeTestCase.java | 2 +- zxingorg/pom.xml | 2 +- .../com/google/zxing/web/DecodeServlet.java | 33 +++++++------------ 9 files changed, 27 insertions(+), 30 deletions(-) diff --git a/core/src/main/java/com/google/zxing/ChecksumException.java b/core/src/main/java/com/google/zxing/ChecksumException.java index 18cb3a67a..c5acbe3ee 100644 --- a/core/src/main/java/com/google/zxing/ChecksumException.java +++ b/core/src/main/java/com/google/zxing/ChecksumException.java @@ -25,6 +25,9 @@ package com.google.zxing; public final class ChecksumException extends ReaderException { private static final ChecksumException INSTANCE = new ChecksumException(); + static { + INSTANCE.setStackTrace(NO_TRACE); // since it's meaningless + } private ChecksumException() { // do nothing diff --git a/core/src/main/java/com/google/zxing/FormatException.java b/core/src/main/java/com/google/zxing/FormatException.java index 9be232fb8..b046b822d 100644 --- a/core/src/main/java/com/google/zxing/FormatException.java +++ b/core/src/main/java/com/google/zxing/FormatException.java @@ -26,6 +26,9 @@ package com.google.zxing; public final class FormatException extends ReaderException { private static final FormatException INSTANCE = new FormatException(); + static { + INSTANCE.setStackTrace(NO_TRACE); // since it's meaningless + } private FormatException() { } diff --git a/core/src/main/java/com/google/zxing/NotFoundException.java b/core/src/main/java/com/google/zxing/NotFoundException.java index 017d4dda6..863526af4 100644 --- a/core/src/main/java/com/google/zxing/NotFoundException.java +++ b/core/src/main/java/com/google/zxing/NotFoundException.java @@ -25,6 +25,9 @@ package com.google.zxing; public final class NotFoundException extends ReaderException { private static final NotFoundException INSTANCE = new NotFoundException(); + static { + INSTANCE.setStackTrace(NO_TRACE); // since it's meaningless + } private NotFoundException() { // do nothing diff --git a/core/src/main/java/com/google/zxing/ReaderException.java b/core/src/main/java/com/google/zxing/ReaderException.java index a55e59819..5f2c12e43 100644 --- a/core/src/main/java/com/google/zxing/ReaderException.java +++ b/core/src/main/java/com/google/zxing/ReaderException.java @@ -28,6 +28,7 @@ public abstract class ReaderException extends Exception { // disable stack traces when not running inside test units protected static final boolean isStackTrace = System.getProperty("surefire.test.class.path") != null; + protected static final StackTraceElement[] NO_TRACE = new StackTraceElement[0]; ReaderException() { // do nothing diff --git a/core/src/main/java/com/google/zxing/client/result/URIParsedResult.java b/core/src/main/java/com/google/zxing/client/result/URIParsedResult.java index d36c6500b..b360de92e 100644 --- a/core/src/main/java/com/google/zxing/client/result/URIParsedResult.java +++ b/core/src/main/java/com/google/zxing/client/result/URIParsedResult.java @@ -69,11 +69,9 @@ public final class URIParsedResult extends ParsedResult { private static String massageURI(String uri) { uri = uri.trim(); int protocolEnd = uri.indexOf(':'); - if (protocolEnd < 0) { - // No protocol, assume http - uri = "http://" + uri; - } else if (isColonFollowedByPortNumber(uri, protocolEnd)) { - // Found a colon, but it looks like it is after the host, so the protocol is still missing + if (protocolEnd < 0 || isColonFollowedByPortNumber(uri, protocolEnd)) { + // No protocol, or found a colon, but it looks like it is after the host, so the protocol is still missing, + // so assume http uri = "http://" + uri; } return uri; diff --git a/core/src/test/java/com/google/zxing/oned/rss/expanded/RSSExpandedImage2resultTestCase.java b/core/src/test/java/com/google/zxing/oned/rss/expanded/RSSExpandedImage2resultTestCase.java index 42700f517..dbf405cb6 100644 --- a/core/src/test/java/com/google/zxing/oned/rss/expanded/RSSExpandedImage2resultTestCase.java +++ b/core/src/test/java/com/google/zxing/oned/rss/expanded/RSSExpandedImage2resultTestCase.java @@ -1,4 +1,4 @@ -/** +/* * Copyright (C) 2010 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/core/src/test/java/com/google/zxing/qrcode/encoder/QRCodeTestCase.java b/core/src/test/java/com/google/zxing/qrcode/encoder/QRCodeTestCase.java index fab1fcb43..dfee6e357 100644 --- a/core/src/test/java/com/google/zxing/qrcode/encoder/QRCodeTestCase.java +++ b/core/src/test/java/com/google/zxing/qrcode/encoder/QRCodeTestCase.java @@ -1,4 +1,4 @@ -/** +/* * Copyright 2008 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/zxingorg/pom.xml b/zxingorg/pom.xml index 9cbe539b3..eff593df7 100644 --- a/zxingorg/pom.xml +++ b/zxingorg/pom.xml @@ -61,7 +61,7 @@ org.eclipse.jetty jetty-maven-plugin - 9.3.0.RC0 + 9.3.0.RC1 diff --git a/zxingorg/src/main/java/com/google/zxing/web/DecodeServlet.java b/zxingorg/src/main/java/com/google/zxing/web/DecodeServlet.java index fabd413af..b78064ac9 100644 --- a/zxingorg/src/main/java/com/google/zxing/web/DecodeServlet.java +++ b/zxingorg/src/main/java/com/google/zxing/web/DecodeServlet.java @@ -34,7 +34,6 @@ import com.google.zxing.common.HybridBinarizer; import com.google.zxing.multi.GenericMultipleBarcodeReader; import com.google.zxing.multi.MultipleBarcodeReader; -import com.google.common.collect.Lists; import com.google.common.io.Resources; import com.google.common.net.HttpHeaders; import com.google.common.net.MediaType; @@ -51,6 +50,7 @@ import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import java.nio.charset.StandardCharsets; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; @@ -320,7 +320,7 @@ public final class DecodeServlet extends HttpServlet { LuminanceSource source = new BufferedImageLuminanceSource(image); BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source)); - Collection results = Lists.newArrayListWithCapacity(1); + Collection results = new ArrayList<>(1); try { @@ -375,7 +375,15 @@ public final class DecodeServlet extends HttpServlet { } if (results.isEmpty()) { - handleException(savedException, request, response); + try { + throw savedException == null ? NotFoundException.getNotFoundInstance() : savedException; + } catch (FormatException | ChecksumException e) { + log.info(e.getMessage()); + errorResponse(request, response, "format"); + } catch (ReaderException e) { // Including NotFoundException + log.info(e.getMessage()); + errorResponse(request, response, "notfound"); + } return; } @@ -402,25 +410,6 @@ public final class DecodeServlet extends HttpServlet { } } - private static void handleException(ReaderException re, - HttpServletRequest request, - HttpServletResponse response) - throws IOException, ServletException { - if (re instanceof NotFoundException) { - log.info("Not found: " + re); - errorResponse(request, response, "notfound"); - } else if (re instanceof FormatException) { - log.info("Format problem: " + re); - errorResponse(request, response, "format"); - } else if (re instanceof ChecksumException) { - log.info("Checksum problem: " + re); - errorResponse(request, response, "format"); - } else { - log.info("Unknown problem: " + re); - errorResponse(request, response, "notfound"); - } - } - private static void errorResponse(HttpServletRequest request, HttpServletResponse response, String key) throws ServletException, IOException {