2 * Copyright 2008 ZXing authors
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.google.zxing.web;
19 import com.google.zxing.DecodeHintType;
20 import com.google.zxing.MultiFormatReader;
21 import com.google.zxing.Reader;
22 import com.google.zxing.ReaderException;
23 import com.google.zxing.Result;
24 import com.google.zxing.client.j2se.BufferedImageMonochromeBitmapSource;
25 import com.google.zxing.client.result.ParsedResult;
26 import com.google.zxing.client.result.ResultParser;
27 import org.apache.commons.fileupload.FileItem;
28 import org.apache.commons.fileupload.FileUploadException;
29 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
30 import org.apache.commons.fileupload.servlet.ServletFileUpload;
31 import org.apache.commons.lang.StringEscapeUtils;
32 import org.apache.http.Header;
33 import org.apache.http.HttpException;
34 import org.apache.http.HttpMessage;
35 import org.apache.http.HttpResponse;
36 import org.apache.http.HttpVersion;
37 import org.apache.http.client.HttpClient;
38 import org.apache.http.client.methods.HttpGet;
39 import org.apache.http.conn.scheme.PlainSocketFactory;
40 import org.apache.http.conn.scheme.Scheme;
41 import org.apache.http.conn.scheme.SchemeRegistry;
42 import org.apache.http.conn.ssl.SSLSocketFactory;
43 import org.apache.http.impl.client.DefaultHttpClient;
44 import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
45 import org.apache.http.params.BasicHttpParams;
46 import org.apache.http.params.HttpParams;
47 import org.apache.http.params.HttpProtocolParams;
49 import javax.imageio.ImageIO;
50 import javax.mail.Authenticator;
51 import javax.servlet.ServletConfig;
52 import javax.servlet.ServletException;
53 import javax.servlet.http.HttpServlet;
54 import javax.servlet.http.HttpServletRequest;
55 import javax.servlet.http.HttpServletResponse;
56 import java.awt.image.BufferedImage;
57 import java.io.IOException;
58 import java.io.InputStream;
59 import java.io.OutputStreamWriter;
60 import java.io.Writer;
61 import java.net.SocketException;
63 import java.net.URISyntaxException;
64 import java.net.UnknownHostException;
65 import java.util.Hashtable;
66 import java.util.List;
67 import java.util.Timer;
68 import java.util.logging.Logger;
71 * {@link HttpServlet} which decodes images containing barcodes. Given a URL, it will
72 * retrieve the image and decode it. It can also process image files uploaded via POST.
74 * @author Sean Owen (srowen@google.com)
76 public final class DecodeServlet extends HttpServlet {
78 private static final long MAX_IMAGE_SIZE = 500000L;
79 private static final long EMAIL_CHECK_INTERVAL = 2L * 60 * 1000;
81 private static final Logger log = Logger.getLogger(DecodeServlet.class.getName());
83 static final Hashtable<DecodeHintType, Object> HINTS;
86 HINTS = new Hashtable<DecodeHintType, Object>(3);
87 HINTS.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
90 private HttpClient client;
91 private DiskFileItemFactory diskFileItemFactory;
92 private Timer emailTimer;
95 public void init(ServletConfig servletConfig) throws ServletException {
97 Logger logger = Logger.getLogger("com.google.zxing");
98 logger.addHandler(new ServletContextLogHandler(servletConfig.getServletContext()));
100 String emailAddress = servletConfig.getInitParameter("emailAddress");
101 String emailPassword = servletConfig.getInitParameter("emailPassword");
102 if (emailAddress == null || emailPassword == null) {
103 throw new ServletException("emailAddress or emailPassword not specified");
106 HttpParams params = new BasicHttpParams();
107 HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
109 SchemeRegistry registry = new SchemeRegistry();
110 registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
111 registry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
113 client = new DefaultHttpClient(new ThreadSafeClientConnManager(params, registry), params);
115 diskFileItemFactory = new DiskFileItemFactory();
117 Authenticator emailAuthenticator = new EmailAuthenticator(emailAddress, emailPassword);
118 emailTimer = new Timer("Email decoder timer", true);
119 emailTimer.schedule(new DecodeEmailTask(emailAddress, emailAuthenticator), 0L, EMAIL_CHECK_INTERVAL);
121 log.info("DecodeServlet configured");
125 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
127 String imageURIString = request.getParameter("u");
128 if (imageURIString == null || imageURIString.length() == 0) {
129 response.sendRedirect("badurl.jspx");
133 if (!(imageURIString.startsWith("http://") || imageURIString.startsWith("https://"))) {
134 imageURIString = "http://" + imageURIString;
139 imageURI = new URI(imageURIString);
140 } catch (URISyntaxException urise) {
141 response.sendRedirect("badurl.jspx");
145 HttpGet getRequest = new HttpGet(imageURI);
148 HttpResponse getResponse = client.execute(getRequest);
149 if (getResponse.getStatusLine().getStatusCode() != HttpServletResponse.SC_OK) {
150 response.sendRedirect("badurl.jspx");
153 if (!isSizeOK(getResponse)) {
154 response.sendRedirect("badimage.jspx");
157 log.info("Decoding " + imageURI);
158 InputStream is = getResponse.getEntity().getContent();
160 processStream(is, request, response);
164 } catch (IllegalArgumentException iae) {
165 // Thrown if hostname is bad or null
167 response.sendRedirect("badurl.jspx");
168 } catch (SocketException se) {
169 // Thrown if hostname is bad or null
171 response.sendRedirect("badurl.jspx");
172 } catch (HttpException he) {
174 response.sendRedirect("badurl.jspx");
175 } catch (UnknownHostException uhe) {
177 response.sendRedirect("badurl.jspx");
183 protected void doPost(HttpServletRequest request, HttpServletResponse response)
184 throws ServletException, IOException {
186 if (!ServletFileUpload.isMultipartContent(request)) {
187 response.sendRedirect("badimage.jspx");
191 ServletFileUpload upload = new ServletFileUpload(diskFileItemFactory);
192 upload.setFileSizeMax(MAX_IMAGE_SIZE);
196 for (FileItem item : (List<FileItem>) upload.parseRequest(request)) {
197 if (!item.isFormField()) {
198 if (item.getSize() <= MAX_IMAGE_SIZE) {
199 log.info("Decoding uploaded file");
200 InputStream is = item.getInputStream();
202 processStream(is, request, response);
207 response.sendRedirect("badimage.jspx");
212 } catch (FileUploadException fue) {
213 response.sendRedirect("badimage.jspx");
218 private static void processStream(InputStream is, HttpServletRequest request, HttpServletResponse response)
219 throws ServletException, IOException {
220 BufferedImage image = ImageIO.read(is);
222 response.sendRedirect("badimage.jspx");
226 Reader reader = new MultiFormatReader();
229 result = reader.decode(new BufferedImageMonochromeBitmapSource(image), HINTS);
230 } catch (ReaderException re) {
231 log.info("DECODE FAILED: " + re.toString());
232 response.sendRedirect("notfound.jspx");
236 if (request.getParameter("full") == null) {
237 response.setContentType("text/plain");
238 response.setCharacterEncoding("UTF8");
239 Writer out = new OutputStreamWriter(response.getOutputStream(), "UTF8");
241 out.write(result.getText());
246 request.setAttribute("result", result);
247 byte[] rawBytes = result.getRawBytes();
248 if (rawBytes != null) {
249 request.setAttribute("rawBytesString", arrayToString(rawBytes));
251 request.setAttribute("rawBytesString", "(Not applicable)");
253 String text = result.getText();
255 request.setAttribute("text", StringEscapeUtils.escapeXml(text));
257 request.setAttribute("text", "(Not applicable)");
259 ParsedResult parsedResult = ResultParser.parseResult(result);
260 request.setAttribute("parsedResult", parsedResult);
261 String displayResult = parsedResult.getDisplayResult();
262 if (displayResult != null) {
263 request.setAttribute("displayResult", StringEscapeUtils.escapeXml(displayResult));
265 request.setAttribute("displayResult", "(Not applicable)");
267 request.getRequestDispatcher("decoderesult.jspx").forward(request, response);
271 private static boolean isSizeOK(HttpMessage getResponse) {
272 Header lengthHeader = getResponse.getLastHeader("Content-Length");
273 if (lengthHeader != null) {
274 long length = Long.parseLong(lengthHeader.getValue());
275 if (length > MAX_IMAGE_SIZE) {
282 private static String arrayToString(byte[] bytes) {
283 int length = bytes.length;
284 StringBuilder result = new StringBuilder(length << 2);
287 int max = Math.min(i + 8, length);
288 for (int j = i; j < max; j++) {
289 int value = bytes[j] & 0xFF;
290 result.append(Integer.toHexString(value / 16));
291 result.append(Integer.toHexString(value % 16));
297 for (int j = i - 8; j < length; j++) {
298 result.append(Integer.toHexString(bytes[j] & 0xFF));
301 return result.toString();
305 public void destroy() {
306 log.config("DecodeServlet shutting down...");