From 9eb04809796f6d79d0adf63cd866875431a85848 Mon Sep 17 00:00:00 2001 From: srowen Date: Sun, 11 May 2008 18:01:23 +0000 Subject: [PATCH] Improvements and refinements to web site git-svn-id: http://zxing.googlecode.com/svn/trunk@397 59b500cc-1b3d-0410-9834-0bbf25fbcc57 --- zxingorg/build.xml | 6 +- .../com/google/zxing/web/DecodeEmailTask.java | 145 ++++++++++-------- .../com/google/zxing/web/DecodeServlet.java | 38 +++-- .../src/com/google/zxing/web/DoSFilter.java | 5 +- .../google/zxing/web/EmailAuthenticator.java | 5 + zxingorg/src/com/google/zxing/web/IPTrie.java | 4 +- .../zxing/web/ServletContextLogHandler.java | 4 +- zxingorg/web/analytics.jspx | 29 ++++ zxingorg/web/badimage.jspx | 20 +-- zxingorg/web/badurl.jspx | 20 +-- zxingorg/web/decode.jspx | 33 +--- zxingorg/web/decoderesult.jspx | 58 +++++++ zxingorg/web/index.jspx | 19 +-- zxingorg/web/notfound.jspx | 18 +-- zxingorg/web/zxing-icon.png | Bin 0 -> 1607 bytes 15 files changed, 234 insertions(+), 170 deletions(-) create mode 100644 zxingorg/web/analytics.jspx create mode 100644 zxingorg/web/decoderesult.jspx create mode 100644 zxingorg/web/zxing-icon.png diff --git a/zxingorg/build.xml b/zxingorg/build.xml index 073485b7..5d75636b 100644 --- a/zxingorg/build.xml +++ b/zxingorg/build.xml @@ -67,14 +67,16 @@ + - - + + + diff --git a/zxingorg/src/com/google/zxing/web/DecodeEmailTask.java b/zxingorg/src/com/google/zxing/web/DecodeEmailTask.java index 9105177c..10cbcb1b 100644 --- a/zxingorg/src/com/google/zxing/web/DecodeEmailTask.java +++ b/zxingorg/src/com/google/zxing/web/DecodeEmailTask.java @@ -38,13 +38,18 @@ import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import java.awt.image.BufferedImage; import java.io.UnsupportedEncodingException; +import java.io.IOException; import java.util.Properties; import java.util.TimerTask; import java.util.logging.Level; import java.util.logging.Logger; /** - * @author Sean Owen + * A {@link TimerTask} which repeatedly checks an e-mail account for messages with an attached + * image. When one is found it attempts to decode the image and replies with the decoded messages + * by e-mail. + * + * @author Sean Owen (srowen@google.com) */ final class DecodeEmailTask extends TimerTask { @@ -89,75 +94,85 @@ final class DecodeEmailTask extends TimerTask { @Override public void run() { log.info("Checking email..."); + Session session = Session.getInstance(sessionProperties, emailAuthenticator); + Store store = null; + Folder inbox = null; try { - Session session = Session.getInstance(sessionProperties, emailAuthenticator); - Store store = null; - Folder inbox = null; - try { - store = session.getStore("pop3"); - store.connect(); - inbox = store.getFolder("INBOX"); - inbox.open(Folder.READ_WRITE); - int count = inbox.getMessageCount(); - if (count > 0) { - log.info("Found " + count + " messages"); - } - for (int i = 1; i <= count; i++) { - log.info("Processing message " + i); - Message message = inbox.getMessage(i); - Object content = message.getContent(); - if (content instanceof MimeMultipart) { - MimeMultipart mimeContent = (MimeMultipart) content; - int numParts = mimeContent.getCount(); - for (int j = 0; j < numParts; j++) { - MimeBodyPart part = (MimeBodyPart) mimeContent.getBodyPart(j); - String contentType = part.getContentType(); - if (!contentType.startsWith("image/")) { - continue; - } - BufferedImage image = ImageIO.read(part.getInputStream()); - if (image != null) { - Reader reader = new MultiFormatReader(); - Result result = null; - try { - result = reader.decode(new BufferedImageMonochromeBitmapSource(image), DecodeServlet.HINTS); - } catch (ReaderException re) { - log.info("Decoding FAILED"); - } + store = session.getStore("pop3"); + store.connect(); + inbox = store.getFolder("INBOX"); + inbox.open(Folder.READ_WRITE); + int count = inbox.getMessageCount(); + if (count > 0) { + log.info("Found " + count + " messages"); + } + for (int i = 1; i <= count; i++) { + log.info("Processing message " + i); + Message message = inbox.getMessage(i); + processMessage(session, message); + } + } catch (Throwable t) { + log.log(Level.WARNING, "Unexpected error", t); + } finally { + closeResources(store, inbox); + } + } - Message reply = new MimeMessage(session); - Address sender = message.getFrom()[0]; - reply.setRecipient(Message.RecipientType.TO, sender); - reply.setFrom(fromAddress); - if (result == null) { - reply.setSubject("Decode failed"); - reply.setContent("Sorry, we could not decode that image.", "text/plain"); - } else { - String text = result.getText(); - reply.setSubject("Decode succeeded"); - reply.setContent(text, "text/plain"); - } - log.info("Sending reply"); - Transport.send(reply); - } - } - } - message.setFlag(Flags.Flag.DELETED, true); - } - } finally { + private void processMessage(Session session, Message message) throws MessagingException, IOException { + Object content = message.getContent(); + if (content instanceof MimeMultipart) { + MimeMultipart mimeContent = (MimeMultipart) content; + int numParts = mimeContent.getCount(); + for (int j = 0; j < numParts; j++) { + MimeBodyPart part = (MimeBodyPart) mimeContent.getBodyPart(j); + processMessagePart(session, message, part); + } + } + message.setFlag(Flags.Flag.DELETED, true); + } + + private void processMessagePart(Session session, Message message, MimeBodyPart part) + throws MessagingException, IOException { + String contentType = part.getContentType(); + if (contentType.startsWith("image/")) { + BufferedImage image = ImageIO.read(part.getInputStream()); + if (image != null) { + Reader reader = new MultiFormatReader(); + Result result = null; try { - if (inbox != null) { - inbox.close(true); - } - if (store != null) { - store.close(); - } - } catch (MessagingException me) { - // continue + result = reader.decode(new BufferedImageMonochromeBitmapSource(image), DecodeServlet.HINTS); + } catch (ReaderException re) { + log.info("Decoding FAILED"); + } + + Message reply = new MimeMessage(session); + Address sender = message.getFrom()[0]; + reply.setRecipient(Message.RecipientType.TO, sender); + reply.setFrom(fromAddress); + if (result == null) { + reply.setSubject("Decode failed"); + reply.setContent("Sorry, we could not decode that image.", "text/plain"); + } else { + String text = result.getText(); + reply.setSubject("Decode succeeded"); + reply.setContent(text, "text/plain"); } + log.info("Sending reply"); + Transport.send(reply); } - } catch (Throwable t) { - log.log(Level.WARNING, "Unexpected error", t); + } + } + + private void closeResources(Store store, Folder inbox) { + try { + if (inbox != null) { + inbox.close(true); + } + if (store != null) { + store.close(); + } + } catch (MessagingException me) { + // continue } } diff --git a/zxingorg/src/com/google/zxing/web/DecodeServlet.java b/zxingorg/src/com/google/zxing/web/DecodeServlet.java index 7c69cc51..a50719a7 100644 --- a/zxingorg/src/com/google/zxing/web/DecodeServlet.java +++ b/zxingorg/src/com/google/zxing/web/DecodeServlet.java @@ -22,6 +22,7 @@ import com.google.zxing.Reader; import com.google.zxing.ReaderException; import com.google.zxing.Result; import com.google.zxing.client.j2se.BufferedImageMonochromeBitmapSource; +import com.google.zxing.client.result.ParsedReaderResult; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; @@ -63,7 +64,10 @@ import java.util.Timer; import java.util.logging.Logger; /** - * @author Sean Owen + * {@link HttpServlet} which decodes images containing barcodes. Given a URL, it will + * retrieve the image and decode it. It can also process image files uploaded via POST. + * + * @author Sean Owen (srowen@google.com) */ public final class DecodeServlet extends HttpServlet { @@ -114,7 +118,7 @@ public final class DecodeServlet extends HttpServlet { } @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String imageURIString = request.getParameter("u"); if (imageURIString == null || imageURIString.length() == 0) { @@ -149,7 +153,7 @@ public final class DecodeServlet extends HttpServlet { log.info("Decoding " + imageURI); InputStream is = getResponse.getEntity().getContent(); try { - processStream(is, response); + processStream(is, request, response); } finally { is.close(); } @@ -183,12 +187,12 @@ public final class DecodeServlet extends HttpServlet { log.info("Decoding uploaded file"); InputStream is = item.getInputStream(); try { - processStream(is, response); + processStream(is, request, response); } finally { is.close(); } } else { - throw new ServletException("File is too large: " + item.getSize()); + response.sendRedirect("badimage.jspx"); } break; } @@ -199,7 +203,8 @@ public final class DecodeServlet extends HttpServlet { } - private static void processStream(InputStream is, HttpServletResponse response) throws IOException { + private static void processStream(InputStream is, HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { BufferedImage image = ImageIO.read(is); if (image == null) { response.sendRedirect("badimage.jspx"); @@ -216,13 +221,20 @@ public final class DecodeServlet extends HttpServlet { return; } - response.setContentType("text/plain"); - response.setCharacterEncoding("UTF-8"); - Writer out = new OutputStreamWriter(response.getOutputStream(), "UTF-8"); - try { - out.write(result.getText()); - } finally { - out.close(); + if (request.getParameter("full") == null) { + response.setContentType("text/plain"); + response.setCharacterEncoding("UTF-8"); + Writer out = new OutputStreamWriter(response.getOutputStream(), "UTF-8"); + try { + out.write(result.getText()); + } finally { + out.close(); + } + } else { + request.setAttribute("result", result); + ParsedReaderResult parsedReaderResult = ParsedReaderResult.parseReaderResult(result); + request.setAttribute("parsedReaderResult", parsedReaderResult); + request.getRequestDispatcher("decoderesult.jspx").forward(request, response); } } diff --git a/zxingorg/src/com/google/zxing/web/DoSFilter.java b/zxingorg/src/com/google/zxing/web/DoSFilter.java index bb94cea3..ec287aef 100755 --- a/zxingorg/src/com/google/zxing/web/DoSFilter.java +++ b/zxingorg/src/com/google/zxing/web/DoSFilter.java @@ -35,7 +35,10 @@ import java.util.Timer; import java.util.TimerTask; /** - * @author Sean Owen + * A {@link Filter} that rejects requests from hosts that are sending too many + * requests in too short a time. + * + * @author Sean Owen (srowen@google.com) */ public final class DoSFilter implements Filter { diff --git a/zxingorg/src/com/google/zxing/web/EmailAuthenticator.java b/zxingorg/src/com/google/zxing/web/EmailAuthenticator.java index 7fd79c6a..b5d9b3cb 100644 --- a/zxingorg/src/com/google/zxing/web/EmailAuthenticator.java +++ b/zxingorg/src/com/google/zxing/web/EmailAuthenticator.java @@ -19,6 +19,11 @@ package com.google.zxing.web; import javax.mail.Authenticator; import javax.mail.PasswordAuthentication; +/** + * A simple {@link Authenticator} which replies with a fixed username and password. + * + * @author Sean Owen (srowen@google.com) + */ final class EmailAuthenticator extends Authenticator { private final String emailUsername; diff --git a/zxingorg/src/com/google/zxing/web/IPTrie.java b/zxingorg/src/com/google/zxing/web/IPTrie.java index e9ebc63c..25a01f8a 100755 --- a/zxingorg/src/com/google/zxing/web/IPTrie.java +++ b/zxingorg/src/com/google/zxing/web/IPTrie.java @@ -20,7 +20,9 @@ import java.net.InetAddress; import java.util.Arrays; /** - * @author Sean Owen + * A trie data structure for storing a set of IP addresses efficiently. + * + * @author Sean Owen (srowen@google.com) */ final class IPTrie { diff --git a/zxingorg/src/com/google/zxing/web/ServletContextLogHandler.java b/zxingorg/src/com/google/zxing/web/ServletContextLogHandler.java index c76ca24a..3ee60ea5 100644 --- a/zxingorg/src/com/google/zxing/web/ServletContextLogHandler.java +++ b/zxingorg/src/com/google/zxing/web/ServletContextLogHandler.java @@ -22,7 +22,9 @@ import java.util.logging.Handler; import java.util.logging.LogRecord; /** - * @author Sean Owen + * A {@link Handler} that redirects log messages to the servlet container log. + * + * @author Sean Owen (srowen@google.com) */ final class ServletContextLogHandler extends Handler { diff --git a/zxingorg/web/analytics.jspx b/zxingorg/web/analytics.jspx new file mode 100644 index 00000000..02d0176e --- /dev/null +++ b/zxingorg/web/analytics.jspx @@ -0,0 +1,29 @@ + + + + + + + + diff --git a/zxingorg/web/badimage.jspx b/zxingorg/web/badimage.jspx index 0fed7b1b..f9700b17 100644 --- a/zxingorg/web/badimage.jspx +++ b/zxingorg/web/badimage.jspx @@ -14,6 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. --> + response.setHeader("Cache-Control", "public"); - No Barcode Found + Bad Image -

- Bad URL -

+

Bad Image

The image you uploaded could not be decoded, or was too large. Go "Back" in your browser and try another image.

- - +
diff --git a/zxingorg/web/badurl.jspx b/zxingorg/web/badurl.jspx index 10e8b5cc..30b6f5e2 100644 --- a/zxingorg/web/badurl.jspx +++ b/zxingorg/web/badurl.jspx @@ -14,6 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. --> + response.setHeader("Cache-Control", "public"); - No Barcode Found + Bad URL -

- Bad URL -

+

Bad URL

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.

- - +
diff --git a/zxingorg/web/decode.jspx b/zxingorg/web/decode.jspx index 4a4d4dff..cba8596d 100644 --- a/zxingorg/web/decode.jspx +++ b/zxingorg/web/decode.jspx @@ -14,6 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. --> + ZXing Decoder Online - -

ZXing Decoder Online

- +

ZXing Decoder Online

Under Construction: This is a simple page that will let you decode a 1D or 2D barcode found in an image online. Enter a URL below.

-

- - + &nbsp; +

-

Or try uploading a file:

-

- - + &nbsp; +

-

See the project page for details.

- - - - +

Copyright (C) 2008 Google Inc.

+
diff --git a/zxingorg/web/decoderesult.jspx b/zxingorg/web/decoderesult.jspx new file mode 100644 index 00000000..152d3751 --- /dev/null +++ b/zxingorg/web/decoderesult.jspx @@ -0,0 +1,58 @@ + + + + + + + response.setHeader("Cache-Control", "no-cache"); + + + + + Decode Succeeded + + + +

Decode Succeeded

+ + + + + + + + + + + + + + + + + +
Raw text${result.text}
Barcode format${result.barcodeFormat}
Parsed Result Type${parsedReaderResult.type}
Parsed Result${parsedReaderResult.displayResult}
+

Copyright (C) 2008 Google Inc.

+ + + +
diff --git a/zxingorg/web/index.jspx b/zxingorg/web/index.jspx index 9d670fd2..0e8a68bb 100644 --- a/zxingorg/web/index.jspx +++ b/zxingorg/web/index.jspx @@ -14,6 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. --> + Download ZXing Reader -

- Welcome to zxing! -

+

Download ZXing Reader

Download the ZXing Barcode Reader. @@ -47,18 +46,8 @@ Download the Android client.

- - +

Copyright (C) 2008 Google Inc.

+
diff --git a/zxingorg/web/notfound.jspx b/zxingorg/web/notfound.jspx index cdec2079..54b61937 100644 --- a/zxingorg/web/notfound.jspx +++ b/zxingorg/web/notfound.jspx @@ -14,6 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. --> + No Barcode Found -

- No Barcode Found -

+

No Barcode Found

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.

- - +
diff --git a/zxingorg/web/zxing-icon.png b/zxingorg/web/zxing-icon.png new file mode 100644 index 0000000000000000000000000000000000000000..7be1aa4564b152ee8d5aebaa201b068aebf55531 GIT binary patch literal 1607 zcmV-N2Dtf&P)T zr#32fPR?hE)zww_Kk)wjdysVvo&_}eHDO^1EEU0YO8`^&p^t=BwZ!h-yT#F?N9`JW z`0ycP&z?OZolblFoA8BTVvxy+Zv;Po9Lq!9moOq0F{2+nFybf21@zJ3FDZwH5OD zJRi9wtDO-Fh4>YG`0xSBDo&KjWhj+PP%4$6jF_97gQlh?&%JT+V9^lm?(UWe(3j%M zr839*d_G7flhE1WU4Xj|XaQ%=oMB$ZmOvDX1t=7YP%IQ7o6Y^>9;hdvv48)5mT3V$ z8{=c+P{et~*RNlJP(Ta}48WZ`cc7!AW5Xnb+1XjRcI_G*K74r71<;^a_X5a1K0eO7 z-?wicJbwHbo;-QNdYV_{b2%X7a?X)8E`WxXC?%lh`!L&#{p&yIS@AujAg9i^_ zcz76&A3qM}0d~Z-$+dV%Rrn>rzQ~jwFcL#4IFmhqOyQ=&kBOeV=UXlOQCdtSF(qy`8Us#&j&E zYzdBGP#ns3tcv5>o0*vb4SYthbLUQoL?Ya7ckI{!H*emAgtEuuafsmr)HC7;9oN>@ z=2?LBneieaemOgXWJ>yZ zIt5cxgeWh7jwL#!bZDb-vFjqsw!Q(>qz)ZA1P2Zr0Ltg&+ze5VEFzyj!)Jy5H*etX z-Md@^QWxRf(M$LfPlI(4iHPDO2(r_6g!+J;k9{I6M^vte@{j0vgfQ{3kL-28tF119hqiPD@KMj&87zzBeW)CdG1 zz%Ue5dbG2Tq3niX+n5?oj({QK4YoUR?m7Pk009600|2x>W_>Q3frJ16002ovPDHLk FV1ixX41@px literal 0 HcmV?d00001 -- 2.20.1