Localize error response and remove redundant JSPXs

This commit is contained in:
Sean Owen 2014-09-11 11:34:08 +01:00
parent e75788e3e0
commit 9bac0584f0
6 changed files with 80 additions and 147 deletions

View file

@ -56,7 +56,9 @@ import java.util.Collection;
import java.util.Collections;
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -64,7 +66,6 @@ import javax.imageio.ImageIO;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
@ -132,15 +133,15 @@ public final class DecodeServlet extends HttpServlet {
String imageURIString = request.getParameter("u");
if (imageURIString == null || imageURIString.isEmpty()) {
log.info("URI was empty");
response.sendRedirect("badurl.jspx");
errorResponse(request, response, "badurl");
return;
}
imageURIString = imageURIString.trim();
for (CharSequence substring : blockedURLSubstrings) {
if (imageURIString.contains(substring)) {
log.info("Disallowed URI " + imageURIString);
response.sendRedirect("badurl.jspx");
log.info("Disallowed URI " + imageURIString);
errorResponse(request, response, "badurl");
return;
}
}
@ -154,7 +155,7 @@ public final class DecodeServlet extends HttpServlet {
}
} catch (URISyntaxException urise) {
log.info("URI " + imageURIString + " was not valid: " + urise);
response.sendRedirect("badurl.jspx");
errorResponse(request, response, "badurl");
return;
}
@ -165,7 +166,7 @@ public final class DecodeServlet extends HttpServlet {
processImage(image, request, response);
} catch (IOException ioe) {
log.info(ioe.toString());
response.sendRedirect("badurl.jspx");
errorResponse(request, response, "badurl");
}
return;
}
@ -175,14 +176,14 @@ public final class DecodeServlet extends HttpServlet {
imageURL = imageURI.toURL();
} catch (MalformedURLException ignored) {
log.info("URI was not valid: " + imageURIString);
response.sendRedirect("badurl.jspx");
errorResponse(request, response, "badurl");
return;
}
String protocol = imageURL.getProtocol();
if (!"http".equalsIgnoreCase(protocol) && !"https".equalsIgnoreCase(protocol)) {
log.info("URI was not valid: " + imageURIString);
response.sendRedirect("badurl.jspx");
errorResponse(request, response, "badurl");
return;
}
@ -191,7 +192,7 @@ public final class DecodeServlet extends HttpServlet {
connection = (HttpURLConnection) imageURL.openConnection();
} catch (IllegalArgumentException ignored) {
log.info("URI could not be opened: " + imageURL);
response.sendRedirect("badurl.jspx");
errorResponse(request, response, "badurl");
return;
}
@ -210,7 +211,7 @@ public final class DecodeServlet extends HttpServlet {
// org.apache.http.NoHttpResponseException,
// org.apache.http.client.ClientProtocolException,
log.info(ioe.toString());
response.sendRedirect("badurl.jspx");
errorResponse(request, response, "badurl");
return;
}
@ -218,12 +219,12 @@ public final class DecodeServlet extends HttpServlet {
try {
if (connection.getResponseCode() != HttpServletResponse.SC_OK) {
log.info("Unsuccessful return code: " + connection.getResponseCode());
response.sendRedirect("badurl.jspx");
errorResponse(request, response, "badurl");
return;
}
if (connection.getHeaderFieldInt(HttpHeaders.CONTENT_LENGTH, 0) > MAX_IMAGE_SIZE) {
log.info("Too large");
response.sendRedirect("badimage.jspx");
errorResponse(request, response, "badimage");
return;
}
@ -234,7 +235,7 @@ public final class DecodeServlet extends HttpServlet {
}
} catch (IOException ioe) {
log.info(ioe.toString());
response.sendRedirect("badurl.jspx");
errorResponse(request, response, "badurl");
} finally {
connection.disconnect();
}
@ -256,10 +257,17 @@ public final class DecodeServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Collection<Part> parts = request.getParts();
Collection<Part> parts;
try {
parts = request.getParts();
} catch (IllegalStateException ise) {
log.info("File upload was too large or invalid");
errorResponse(request, response, "badimage");
return;
}
if (parts.isEmpty()) {
log.info("File upload was not multipart");
response.sendRedirect("badimage.jspx");
errorResponse(request, response, "badimage");
return;
}
for (Part part : parts) {
@ -271,7 +279,7 @@ public final class DecodeServlet extends HttpServlet {
}
private static void processStream(InputStream is,
ServletRequest request,
HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
BufferedImage image;
@ -280,17 +288,17 @@ public final class DecodeServlet extends HttpServlet {
} catch (IOException | CMMException | IllegalArgumentException ioe) {
log.info(ioe.toString());
// Have seen these in some logs
response.sendRedirect("badimage.jspx");
errorResponse(request, response, "badimage");
return;
}
if (image == null) {
response.sendRedirect("badimage.jspx");
errorResponse(request, response, "badimage");
return;
}
if (image.getHeight() <= 1 || image.getWidth() <= 1 ||
image.getHeight() * image.getWidth() > MAX_PIXELS) {
log.info("Dimensions out of bounds: " + image.getWidth() + 'x' + image.getHeight());
response.sendRedirect("badimage.jspx");
errorResponse(request, response, "badimage");
return;
}
@ -298,7 +306,7 @@ public final class DecodeServlet extends HttpServlet {
}
private static void processImage(BufferedImage image,
ServletRequest request,
HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
LuminanceSource source = new BufferedImageLuminanceSource(image);
@ -358,7 +366,7 @@ public final class DecodeServlet extends HttpServlet {
}
if (results.isEmpty()) {
handleException(savedException, response);
handleException(savedException, request, response);
return;
}
@ -385,20 +393,38 @@ public final class DecodeServlet extends HttpServlet {
}
}
private static void handleException(ReaderException re, HttpServletResponse response) throws IOException {
private static void handleException(ReaderException re,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
if (re instanceof NotFoundException) {
log.info("Not found: " + re);
response.sendRedirect("notfound.jspx");
errorResponse(request, response, "notfound");
} else if (re instanceof FormatException) {
log.info("Format problem: " + re);
response.sendRedirect("format.jspx");
errorResponse(request, response, "format");
} else if (re instanceof ChecksumException) {
log.info("Checksum problem: " + re);
response.sendRedirect("format.jspx");
errorResponse(request, response, "format");
} else {
log.info("Unknown problem: " + re);
response.sendRedirect("notfound.jspx");
errorResponse(request, response, "notfound");
}
}
private static void errorResponse(HttpServletRequest request,
HttpServletResponse response,
String key) throws ServletException, IOException {
Locale locale = request.getLocale();
if (locale == null) {
locale = Locale.ENGLISH;
}
ResourceBundle bundle = ResourceBundle.getBundle("Strings", locale);
String title = bundle.getString("response.error." + key + ".title");
String text = bundle.getString("response.error." + key + ".text");
request.setAttribute("title", title);
request.setAttribute("text", text);
request.getRequestDispatcher("response.jspx").forward(request, response);
}
}

View file

@ -0,0 +1,15 @@
response.error.badimage.title=Bad Image
response.error.badimage.text=The image you uploaded could not be decoded, or was too large. \
Go "Back" in your browser and try another image.
response.error.badurl.title=Bad URL
response.error.badurl.text=You didn't specify a URL, or the URL was not valid, or did not return \
an image. Go "Back" in your browser and try again.
response.error.format.title=Barcode Format Problem
response.error.format.text=A barcode was possibly found in this image, but a problem occurred \
while decoding it. The data did not conform to the barcode format. This could be due to a \
misdetection of the barcode, or could indicate a problem with the barcode contents. \
Go "Back" in your browser and try another image.
response.error.notfound.title=No Barcode Found
response.error.notfound.text=No barcode was found in this image. Either it did not contain a \
barcode, or did not contain one in a supported format, or the software was simply unable to \
find it. Go "Back" in your browser and try another image.

View file

@ -1,34 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2008 ZXing authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1">
<jsp:directive.page contentType="text/html" session="false"/>
<jsp:scriptlet>response.setHeader("Cache-Control", "public");</jsp:scriptlet>
<jsp:text><![CDATA[<!DOCTYPE html>]]></jsp:text>
<html>
<head>
<meta charset="UTF-8"/>
<title>Bad URL</title>
<link rel="stylesheet" href="style.css" type="text/css"/>
</head>
<body>
<div id="main">
<div id="header"><h1><img src="zxing-icon.png" height="32" width="32" alt=""/> Bad URL</h1></div>
<p>You didn't specify a URL, or the URL was not valid, or did not return an image. Go "Back" in your browser and try again.</p>
</div>
</body>
</html>
</jsp:root>

View file

@ -1,37 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2008 ZXing authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1">
<jsp:directive.page contentType="text/html" session="false"/>
<jsp:scriptlet>response.setHeader("Cache-Control", "public");</jsp:scriptlet>
<jsp:text><![CDATA[<!DOCTYPE html>]]></jsp:text>
<html>
<head>
<meta charset="UTF-8"/>
<title>No Barcode Found</title>
<link rel="stylesheet" href="style.css" type="text/css"/>
</head>
<body>
<div id="main">
<div id="header"><h1><img src="zxing-icon.png" height="32" width="32" alt=""/> Barcode Format Problem</h1></div>
<p>A barcode was possibly found in this image, but a problem occurred while decoding it. The data did not conform
to the barcode format. This could be due to a misdetection of the barcode, or could indicate a problem
with the barcode contents. Go "Back" in your browser and try another image.
</p>
</div>
</body>
</html>
</jsp:root>

View file

@ -1,36 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2008 ZXing authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- Author: Sean Owen -->
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1">
<jsp:directive.page contentType="text/html" session="false"/>
<jsp:scriptlet>response.setHeader("Cache-Control", "public");</jsp:scriptlet>
<jsp:text><![CDATA[<!DOCTYPE html>]]></jsp:text>
<html>
<head>
<meta charset="UTF-8"/>
<title>No Barcode Found</title>
<link rel="stylesheet" href="style.css" type="text/css"/>
</head>
<body>
<div id="main">
<div id="header"><h1><img src="zxing-icon.png" height="32" width="32" alt=""/> No Barcode Found</h1></div>
<p>No barcode was found in this image. Either it did not contain a barcode, or did not contain one in a
supported format, or the software was simply unable to find it. Go "Back" in your browser and try another image.</p>
</div>
</body>
</html>
</jsp:root>

View file

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2008 ZXing authors
Copyright 2014 ZXing authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@ -14,21 +14,20 @@
See the License for the specific language governing permissions and
limitations under the License.
-->
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1">
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:c="http://java.sun.com/jsp/jstl/core" version="2.1">
<jsp:directive.page contentType="text/html" session="false"/>
<jsp:scriptlet>response.setHeader("Cache-Control", "public");</jsp:scriptlet>
<jsp:text><![CDATA[<!DOCTYPE html>]]></jsp:text>
<html>
<head>
<meta charset="UTF-8"/>
<title>Bad Image</title>
<link rel="stylesheet" href="style.css" type="text/css"/>
</head>
<body>
<div id="main">
<div id="header"><h1><img src="zxing-icon.png" height="32" width="32" alt=""/> Bad Image</h1></div>
<p>The image you uploaded could not be decoded, or was too large. Go "Back" in your browser and try another image.</p>
</div>
</body>
<head>
<meta charset="UTF-8"/>
<title><c:out value="${requestScope['title']}"/></title>
<link rel="stylesheet" href="style.css" type="text/css"/>
</head>
<body>
<div id="main">
<div id="header"><h1><img src="zxing-icon.png" height="32" width="32" alt=""/> <c:out value="${requestScope['title']}"/></h1></div>
<p><c:out value="${requestScope['text']}"/></p>
</div>
</body>
</html>
</jsp:root>