Some small improvements in error handling based on exceptions observed at zxing.org
[zxing.git] / zxingorg / src / com / google / zxing / web / DecodeServlet.java
1 /*
2  * Copyright 2008 ZXing authors
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.google.zxing.web;
18
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;
48
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;
62 import java.net.URI;
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;
69
70 /**
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.
73  * 
74  * @author Sean Owen (srowen@google.com)
75  */
76 public final class DecodeServlet extends HttpServlet {
77
78   private static final long MAX_IMAGE_SIZE = 500000L;
79   private static final long EMAIL_CHECK_INTERVAL = 2L * 60 * 1000;
80
81   private static final Logger log = Logger.getLogger(DecodeServlet.class.getName());
82
83   static final Hashtable<DecodeHintType, Object> HINTS;
84
85   static {
86     HINTS = new Hashtable<DecodeHintType, Object>(3);
87     HINTS.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
88   }
89
90   private HttpClient client;
91   private DiskFileItemFactory diskFileItemFactory;
92   private Timer emailTimer;
93
94   @Override
95   public void init(ServletConfig servletConfig) throws ServletException {
96
97     Logger logger = Logger.getLogger("com.google.zxing");
98     logger.addHandler(new ServletContextLogHandler(servletConfig.getServletContext()));
99
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");
104     }
105
106     HttpParams params = new BasicHttpParams();
107     HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
108
109     SchemeRegistry registry = new SchemeRegistry();
110     registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
111     registry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
112
113     client = new DefaultHttpClient(new ThreadSafeClientConnManager(params, registry), params);
114
115     diskFileItemFactory = new DiskFileItemFactory();
116
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);
120
121     log.info("DecodeServlet configured");
122   }
123
124   @Override
125   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
126
127     String imageURIString = request.getParameter("u");
128     if (imageURIString == null || imageURIString.length() == 0) {
129       response.sendRedirect("badurl.jspx");
130       return;
131     }
132
133     if (!(imageURIString.startsWith("http://") || imageURIString.startsWith("https://"))) {
134       imageURIString = "http://" + imageURIString;
135     }
136
137     URI imageURI;
138     try {
139       imageURI = new URI(imageURIString);
140     } catch (URISyntaxException urise) {
141       response.sendRedirect("badurl.jspx");
142       return;
143     }
144
145     HttpGet getRequest = new HttpGet(imageURI);
146
147     try {
148       HttpResponse getResponse = client.execute(getRequest);
149       if (getResponse.getStatusLine().getStatusCode() != HttpServletResponse.SC_OK) {
150         response.sendRedirect("badurl.jspx");
151         return;
152       }
153       if (!isSizeOK(getResponse)) {
154         response.sendRedirect("badimage.jspx");
155         return;
156       }
157       log.info("Decoding " + imageURI);
158       InputStream is = getResponse.getEntity().getContent();
159       try {
160         processStream(is, request, response);
161       } finally {
162         is.close();
163       }
164     } catch (IllegalArgumentException iae) {
165       // Thrown if hostname is bad or null
166       getRequest.abort();
167       response.sendRedirect("badurl.jspx");
168     } catch (SocketException se) {
169       // Thrown if hostname is bad or null
170       getRequest.abort();
171       response.sendRedirect("badurl.jspx");
172     } catch (HttpException he) {
173       getRequest.abort();
174       response.sendRedirect("badurl.jspx");
175     } catch (UnknownHostException uhe) {
176       getRequest.abort();
177       response.sendRedirect("badurl.jspx");
178     }
179
180   }
181
182   @Override
183   protected void doPost(HttpServletRequest request, HttpServletResponse response)
184           throws ServletException, IOException {
185
186     if (!ServletFileUpload.isMultipartContent(request)) {
187       response.sendRedirect("badimage.jspx");
188       return;
189     }
190
191     ServletFileUpload upload = new ServletFileUpload(diskFileItemFactory);
192     upload.setFileSizeMax(MAX_IMAGE_SIZE);
193
194     // Parse the request
195     try {
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();
201             try {
202               processStream(is, request, response);
203             } finally {
204               is.close();
205             }
206           } else {
207             response.sendRedirect("badimage.jspx");
208           }
209           break;
210         }
211       }
212     } catch (FileUploadException fue) {
213       response.sendRedirect("badimage.jspx");
214     }
215
216   }
217
218   private static void processStream(InputStream is, HttpServletRequest request, HttpServletResponse response)
219       throws ServletException, IOException {
220     BufferedImage image = ImageIO.read(is);
221     if (image == null) {
222       response.sendRedirect("badimage.jspx");
223       return;
224     }
225
226     Reader reader = new MultiFormatReader();
227     Result result;
228     try {
229       result = reader.decode(new BufferedImageMonochromeBitmapSource(image), HINTS);
230     } catch (ReaderException re) {
231       log.info("DECODE FAILED: " + re.toString());
232       response.sendRedirect("notfound.jspx");
233       return;
234     }
235
236     if (request.getParameter("full") == null) {
237       response.setContentType("text/plain");
238       response.setCharacterEncoding("UTF8");
239       Writer out = new OutputStreamWriter(response.getOutputStream(), "UTF8");
240       try {
241         out.write(result.getText());
242       } finally {
243         out.close();
244       }
245     } else {
246       request.setAttribute("result", result);
247       byte[] rawBytes = result.getRawBytes();
248       if (rawBytes != null) {
249         request.setAttribute("rawBytesString", arrayToString(rawBytes));
250       } else {
251         request.setAttribute("rawBytesString", "(Not applicable)");
252       }
253       String text = result.getText();
254       if (text != null) {
255         request.setAttribute("text", StringEscapeUtils.escapeXml(text));
256       } else {
257         request.setAttribute("text", "(Not applicable)");
258       }
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));
264       } else {
265         request.setAttribute("displayResult", "(Not applicable)");
266       }
267       request.getRequestDispatcher("decoderesult.jspx").forward(request, response);
268     }
269   }
270
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) {
276         return false;
277       }
278     }
279     return true;
280   }
281
282   private static String arrayToString(byte[] bytes) {
283     int length = bytes.length;
284     StringBuilder result = new StringBuilder(length << 2);
285     int i = 0;
286     while (i < length) {
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));
292         result.append(' ');
293       }
294       result.append('\n');
295       i += 8;
296     }
297     for (int j = i - 8; j < length; j++) {
298       result.append(Integer.toHexString(bytes[j] & 0xFF));
299       result.append(' ');
300     }
301     return result.toString();
302   }
303
304   @Override
305   public void destroy() {
306     log.config("DecodeServlet shutting down...");
307     emailTimer.cancel();
308   }
309
310 }