Remove spurious stack trace from singleton exception, and various minor edits

This commit is contained in:
Sean Owen 2015-06-06 10:46:18 +01:00
parent ea9a260f0a
commit 02d3697a59
9 changed files with 27 additions and 30 deletions

View file

@ -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

View file

@ -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() {
}

View file

@ -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

View file

@ -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

View file

@ -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;

View file

@ -1,4 +1,4 @@
/**
/*
* Copyright (C) 2010 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");

View file

@ -1,4 +1,4 @@
/**
/*
* Copyright 2008 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");

View file

@ -61,7 +61,7 @@
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.3.0.RC0</version>
<version>9.3.0.RC1</version>
</plugin>
</plugins>
</build>

View file

@ -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<Result> results = Lists.newArrayListWithCapacity(1);
Collection<Result> 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 {