6a64c3edb53c25433e3cb206e6bf6d4b7e0b9e85
[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.BarcodeFormat;
20 import com.google.zxing.BinaryBitmap;
21 import com.google.zxing.ChecksumException;
22 import com.google.zxing.DecodeHintType;
23 import com.google.zxing.FormatException;
24 import com.google.zxing.LuminanceSource;
25 import com.google.zxing.MultiFormatReader;
26 import com.google.zxing.NotFoundException;
27 import com.google.zxing.Reader;
28 import com.google.zxing.ReaderException;
29 import com.google.zxing.Result;
30 import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
31 import com.google.zxing.common.GlobalHistogramBinarizer;
32 import com.google.zxing.common.HybridBinarizer;
33
34 import com.google.zxing.multi.GenericMultipleBarcodeReader;
35 import com.google.zxing.multi.MultipleBarcodeReader;
36 import org.apache.commons.fileupload.FileItem;
37 import org.apache.commons.fileupload.FileUploadException;
38 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
39 import org.apache.commons.fileupload.servlet.ServletFileUpload;
40 import org.apache.http.Header;
41 import org.apache.http.HttpMessage;
42 import org.apache.http.HttpResponse;
43 import org.apache.http.HttpVersion;
44 import org.apache.http.HttpEntity;
45 import org.apache.http.client.HttpClient;
46 import org.apache.http.client.methods.HttpGet;
47 import org.apache.http.client.methods.HttpUriRequest;
48 import org.apache.http.conn.scheme.PlainSocketFactory;
49 import org.apache.http.conn.scheme.Scheme;
50 import org.apache.http.conn.scheme.SchemeRegistry;
51 import org.apache.http.conn.ssl.SSLSocketFactory;
52 import org.apache.http.conn.ClientConnectionManager;
53 import org.apache.http.impl.client.DefaultHttpClient;
54 import org.apache.http.impl.conn.SingleClientConnManager;
55 import org.apache.http.params.BasicHttpParams;
56 import org.apache.http.params.HttpParams;
57 import org.apache.http.params.HttpProtocolParams;
58
59 import java.awt.color.CMMException;
60 import java.awt.image.BufferedImage;
61 import java.io.IOException;
62 import java.io.InputStream;
63 import java.io.OutputStreamWriter;
64 import java.io.Writer;
65 import java.net.URI;
66 import java.net.URISyntaxException;
67 import java.util.ArrayList;
68 import java.util.Arrays;
69 import java.util.Collection;
70 import java.util.Hashtable;
71 import java.util.List;
72 import java.util.Vector;
73 import java.util.logging.Logger;
74
75 import javax.imageio.ImageIO;
76 import javax.servlet.ServletConfig;
77 import javax.servlet.ServletException;
78 import javax.servlet.ServletRequest;
79 import javax.servlet.http.HttpServlet;
80 import javax.servlet.http.HttpServletRequest;
81 import javax.servlet.http.HttpServletResponse;
82
83 /**
84  * {@link HttpServlet} which decodes images containing barcodes. Given a URL, it will
85  * retrieve the image and decode it. It can also process image files uploaded via POST.
86  *
87  * @author Sean Owen
88  */
89 public final class DecodeServlet extends HttpServlet {
90
91   // No real reason to let people upload more than a 2MB image
92   private static final long MAX_IMAGE_SIZE = 2000000L;
93   // No real reason to deal with more than maybe 2 megapixels
94   private static final int MAX_PIXELS = 1 << 21;
95
96   private static final Logger log = Logger.getLogger(DecodeServlet.class.getName());
97
98   static final Hashtable<DecodeHintType, Object> HINTS;
99   static final Hashtable<DecodeHintType, Object> HINTS_PURE;
100
101   static {
102     HINTS = new Hashtable<DecodeHintType, Object>(5);
103     HINTS.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
104     Collection<BarcodeFormat> possibleFormats = new Vector<BarcodeFormat>(17);
105     possibleFormats.add(BarcodeFormat.UPC_A);
106     possibleFormats.add(BarcodeFormat.UPC_E);
107     possibleFormats.add(BarcodeFormat.EAN_8);
108     possibleFormats.add(BarcodeFormat.EAN_13);
109     possibleFormats.add(BarcodeFormat.CODE_39);
110     possibleFormats.add(BarcodeFormat.CODE_93);
111     possibleFormats.add(BarcodeFormat.CODE_128);
112     //possibleFormats.add(BarcodeFormat.CODABAR);
113     possibleFormats.add(BarcodeFormat.ITF);
114     possibleFormats.add(BarcodeFormat.RSS14);
115     possibleFormats.add(BarcodeFormat.QR_CODE);
116     possibleFormats.add(BarcodeFormat.DATA_MATRIX);
117     possibleFormats.add(BarcodeFormat.PDF417);
118     HINTS.put(DecodeHintType.POSSIBLE_FORMATS, possibleFormats);
119     HINTS_PURE = new Hashtable<DecodeHintType, Object>(HINTS);
120     HINTS_PURE.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
121   }
122
123   private HttpParams params;
124   private SchemeRegistry registry;
125   private DiskFileItemFactory diskFileItemFactory;
126
127   @Override
128   public void init(ServletConfig servletConfig) {
129
130     Logger logger = Logger.getLogger("com.google.zxing");
131     logger.addHandler(new ServletContextLogHandler(servletConfig.getServletContext()));
132
133     params = new BasicHttpParams();
134     HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
135
136     registry = new SchemeRegistry();
137     registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
138     registry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
139
140     diskFileItemFactory = new DiskFileItemFactory();
141
142     log.info("DecodeServlet configured");
143   }
144
145   @Override
146   protected void doGet(HttpServletRequest request, HttpServletResponse response)
147       throws ServletException, IOException {
148     String imageURIString = request.getParameter("u");
149     if (imageURIString == null || imageURIString.length() == 0) {
150       log.fine("URI was empty");
151       response.sendRedirect("badurl.jspx");
152       return;
153     }
154
155     imageURIString = imageURIString.trim();
156
157     if (!(imageURIString.startsWith("http://") || imageURIString.startsWith("https://"))) {
158       imageURIString = "http://" + imageURIString;
159     }
160
161     URI imageURI;
162     try {
163       imageURI = new URI(imageURIString);
164     } catch (URISyntaxException urise) {
165       log.fine("URI was not valid: " + imageURIString);
166       response.sendRedirect("badurl.jspx");
167       return;
168     }
169
170     ClientConnectionManager connectionManager = new SingleClientConnManager(params, registry);
171     HttpClient client = new DefaultHttpClient(connectionManager, params);
172
173     HttpUriRequest getRequest = new HttpGet(imageURI);
174     getRequest.addHeader("Connection", "close"); // Avoids CLOSE_WAIT socket issue?
175
176     try {
177
178       HttpResponse getResponse;
179       try {
180         getResponse = client.execute(getRequest);
181       } catch (IllegalArgumentException iae) {
182         // Thrown if hostname is bad or null
183         log.fine(iae.toString());
184         getRequest.abort();
185         response.sendRedirect("badurl.jspx");
186         return;
187       } catch (IOException ioe) {
188         // Encompasses lots of stuff, including
189         //  java.net.SocketException, java.net.UnknownHostException,
190         //  javax.net.ssl.SSLPeerUnverifiedException,
191         //  org.apache.http.NoHttpResponseException,
192         //  org.apache.http.client.ClientProtocolException,
193         log.fine(ioe.toString());
194         getRequest.abort();
195         response.sendRedirect("badurl.jspx");
196         return;
197       }
198
199       if (getResponse.getStatusLine().getStatusCode() != HttpServletResponse.SC_OK) {
200         log.fine("Unsuccessful return code: " + getResponse.getStatusLine().getStatusCode());
201         response.sendRedirect("badurl.jspx");
202         return;
203       }
204       if (!isSizeOK(getResponse)) {
205         log.fine("Too large");
206         response.sendRedirect("badimage.jspx");
207         return;
208       }
209
210       log.info("Decoding " + imageURI);
211       HttpEntity entity = getResponse.getEntity();
212       InputStream is = entity.getContent();
213       try {
214         processStream(is, request, response);
215       } finally {
216         entity.consumeContent();
217         is.close();
218       }
219
220     } finally {
221       connectionManager.shutdown();
222     }
223
224   }
225
226   @Override
227   protected void doPost(HttpServletRequest request, HttpServletResponse response)
228           throws ServletException, IOException {
229
230     if (!ServletFileUpload.isMultipartContent(request)) {
231       log.fine("File upload was not multipart");
232       response.sendRedirect("badimage.jspx");
233       return;
234     }
235
236     ServletFileUpload upload = new ServletFileUpload(diskFileItemFactory);
237     upload.setFileSizeMax(MAX_IMAGE_SIZE);
238
239     // Parse the request
240     try {
241       for (FileItem item : (List<FileItem>) upload.parseRequest(request)) {
242         if (!item.isFormField()) {
243           if (item.getSize() <= MAX_IMAGE_SIZE) {
244             log.info("Decoding uploaded file");
245             InputStream is = item.getInputStream();
246             try {
247               processStream(is, request, response);
248             } finally {
249               is.close();
250             }
251           } else {
252             log.fine("Too large");
253             response.sendRedirect("badimage.jspx");
254           }
255           break;
256         }
257       }
258     } catch (FileUploadException fue) {
259       log.fine(fue.toString());
260       response.sendRedirect("badimage.jspx");
261     }
262
263   }
264
265   private static void processStream(InputStream is, ServletRequest request,
266       HttpServletResponse response) throws ServletException, IOException {
267
268     BufferedImage image;
269     try {
270       image = ImageIO.read(is);
271     } catch (IOException ioe) {
272       log.fine(ioe.toString());
273       // Includes javax.imageio.IIOException
274       response.sendRedirect("badimage.jspx");
275       return;
276     } catch (CMMException cmme) {
277       log.fine(cmme.toString());
278       // Have seen this in logs
279       response.sendRedirect("badimage.jspx");
280       return;
281     } catch (IllegalArgumentException iae) {
282       log.fine(iae.toString());
283       // Have seen this in logs for some JPEGs
284       response.sendRedirect("badimage.jspx");
285       return;
286     }
287     if (image == null) {
288       response.sendRedirect("badimage.jspx");
289       return;
290     }
291     if (image.getHeight() <= 1 || image.getWidth() <= 1 ||
292         image.getHeight() * image.getWidth() > MAX_PIXELS) {
293       log.fine("Dimensions too large: " + image.getWidth() + 'x' + image.getHeight());
294       response.sendRedirect("badimage.jspx");
295       return;
296     }
297
298     Reader reader = new MultiFormatReader();
299     LuminanceSource source = new BufferedImageLuminanceSource(image);
300     BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));
301     Collection<Result> results = new ArrayList<Result>(1);
302     ReaderException savedException = null;
303
304     try {
305       // Look for multiple barcodes
306       MultipleBarcodeReader multiReader = new GenericMultipleBarcodeReader(reader);
307       Result[] theResults = multiReader.decodeMultiple(bitmap, HINTS);
308       if (theResults != null) {
309         results.addAll(Arrays.asList(theResults));
310       }
311     } catch (ReaderException re) {
312       savedException = re;
313     }
314
315     if (results.isEmpty()) {
316       try {
317         // Look for pure barcode
318         Result theResult = reader.decode(bitmap, HINTS_PURE);
319         if (theResult != null) {
320           results.add(theResult);
321         }
322       } catch (ReaderException re) {
323         savedException = re;
324       }
325     }
326
327     if (results.isEmpty()) {
328       try {
329         // Look for normal barcode in photo
330         Result theResult = reader.decode(bitmap, HINTS);
331         if (theResult != null) {
332           results.add(theResult);
333         }
334       } catch (ReaderException re) {
335         savedException = re;
336       }
337     }
338
339     if (results.isEmpty()) {
340       try {
341         // Try again with other binarizer
342         BinaryBitmap hybridBitmap = new BinaryBitmap(new HybridBinarizer(source));
343         Result theResult = reader.decode(hybridBitmap, HINTS);
344         if (theResult != null) {
345           results.add(theResult);
346         }
347       } catch (ReaderException re) {
348         savedException = re;
349       }
350     }
351
352     if (results.isEmpty()) {
353       handleException(savedException, response);
354       return;
355     }
356
357     if (request.getParameter("full") == null) {
358       response.setContentType("text/plain");
359       response.setCharacterEncoding("UTF8");
360       Writer out = new OutputStreamWriter(response.getOutputStream(), "UTF8");
361       try {
362         for (Result result : results) {
363           out.write(result.getText());
364           out.write('\n');
365         }
366       } finally {
367         out.close();
368       }
369     } else {
370       request.setAttribute("results", results);
371       request.getRequestDispatcher("decoderesult.jspx").forward(request, response);
372     }
373   }
374
375   private static void handleException(ReaderException re, HttpServletResponse response) throws IOException {
376     if (re instanceof NotFoundException) {
377       log.info("Not found: " + re);
378       response.sendRedirect("notfound.jspx");
379     } else if (re instanceof FormatException) {
380       log.info("Format problem: " + re);
381       response.sendRedirect("format.jspx");
382     } else if (re instanceof ChecksumException) {
383       log.info("Checksum problem: " + re);
384       response.sendRedirect("format.jspx");
385     } else {
386       log.info("Unknown problem: " + re);
387       response.sendRedirect("notfound.jspx");
388     }
389   }
390
391   private static boolean isSizeOK(HttpMessage getResponse) {
392     Header lengthHeader = getResponse.getLastHeader("Content-Length");
393     if (lengthHeader != null) {
394       long length = Long.parseLong(lengthHeader.getValue());
395       if (length > MAX_IMAGE_SIZE) {
396         return false;
397       }
398     }
399     return true;
400   }
401
402   @Override
403   public void destroy() {
404     log.config("DecodeServlet shutting down...");
405   }
406
407 }