mirror of
https://github.com/zxing/zxing.git
synced 2025-02-02 05:41:08 -08:00
Move to servlet 3.0 file upload handling and annotations
This commit is contained in:
parent
aa0b4c93c0
commit
482e2fb3ac
|
@ -40,11 +40,6 @@
|
||||||
<artifactId>commons-io</artifactId>
|
<artifactId>commons-io</artifactId>
|
||||||
<version>2.4</version>
|
<version>2.4</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>commons-fileupload</groupId>
|
|
||||||
<artifactId>commons-fileupload</artifactId>
|
|
||||||
<version>1.3.1</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.google.guava</groupId>
|
<groupId>com.google.guava</groupId>
|
||||||
<artifactId>guava</artifactId>
|
<artifactId>guava</artifactId>
|
||||||
|
@ -66,6 +61,11 @@
|
||||||
<warSourceDirectory>src/web</warSourceDirectory>
|
<warSourceDirectory>src/web</warSourceDirectory>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.eclipse.jetty</groupId>
|
||||||
|
<artifactId>jetty-maven-plugin</artifactId>
|
||||||
|
<version>9.2.3.v20140905</version>
|
||||||
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
|
|
|
@ -28,6 +28,7 @@ import com.google.zxing.qrcode.QRCodeWriter;
|
||||||
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
|
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
|
||||||
|
|
||||||
import javax.servlet.ServletRequest;
|
import javax.servlet.ServletRequest;
|
||||||
|
import javax.servlet.annotation.WebServlet;
|
||||||
import javax.servlet.http.HttpServlet;
|
import javax.servlet.http.HttpServlet;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
@ -46,6 +47,7 @@ import java.util.Map;
|
||||||
*
|
*
|
||||||
* @author Sean Owen
|
* @author Sean Owen
|
||||||
*/
|
*/
|
||||||
|
@WebServlet("/w/chart")
|
||||||
public final class ChartServlet extends HttpServlet {
|
public final class ChartServlet extends HttpServlet {
|
||||||
|
|
||||||
private static final int MAX_DIMENSION = 4096;
|
private static final int MAX_DIMENSION = 4096;
|
||||||
|
|
|
@ -38,16 +38,9 @@ import com.google.zxing.common.HybridBinarizer;
|
||||||
|
|
||||||
import com.google.zxing.multi.GenericMultipleBarcodeReader;
|
import com.google.zxing.multi.GenericMultipleBarcodeReader;
|
||||||
import com.google.zxing.multi.MultipleBarcodeReader;
|
import com.google.zxing.multi.MultipleBarcodeReader;
|
||||||
import org.apache.commons.fileupload.FileItem;
|
|
||||||
import org.apache.commons.fileupload.FileUploadException;
|
|
||||||
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
|
||||||
import org.apache.commons.fileupload.servlet.FileCleanerCleanup;
|
|
||||||
import org.apache.commons.fileupload.servlet.ServletFileUpload;
|
|
||||||
import org.apache.commons.io.FileCleaningTracker;
|
|
||||||
|
|
||||||
import java.awt.color.CMMException;
|
import java.awt.color.CMMException;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStreamWriter;
|
import java.io.OutputStreamWriter;
|
||||||
|
@ -72,9 +65,12 @@ import javax.servlet.ServletConfig;
|
||||||
import javax.servlet.ServletContext;
|
import javax.servlet.ServletContext;
|
||||||
import javax.servlet.ServletException;
|
import javax.servlet.ServletException;
|
||||||
import javax.servlet.ServletRequest;
|
import javax.servlet.ServletRequest;
|
||||||
|
import javax.servlet.annotation.MultipartConfig;
|
||||||
|
import javax.servlet.annotation.WebServlet;
|
||||||
import javax.servlet.http.HttpServlet;
|
import javax.servlet.http.HttpServlet;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import javax.servlet.http.Part;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link HttpServlet} which decodes images containing barcodes. Given a URL, it will
|
* {@link HttpServlet} which decodes images containing barcodes. Given a URL, it will
|
||||||
|
@ -82,6 +78,12 @@ import javax.servlet.http.HttpServletResponse;
|
||||||
*
|
*
|
||||||
* @author Sean Owen
|
* @author Sean Owen
|
||||||
*/
|
*/
|
||||||
|
@MultipartConfig(
|
||||||
|
maxFileSize = 10_000_000,
|
||||||
|
maxRequestSize = 10_000_000,
|
||||||
|
fileSizeThreshold = 1_000_000,
|
||||||
|
location = "/tmp")
|
||||||
|
@WebServlet("/w/decode")
|
||||||
public final class DecodeServlet extends HttpServlet {
|
public final class DecodeServlet extends HttpServlet {
|
||||||
|
|
||||||
private static final Logger log = Logger.getLogger(DecodeServlet.class.getName());
|
private static final Logger log = Logger.getLogger(DecodeServlet.class.getName());
|
||||||
|
@ -102,7 +104,6 @@ public final class DecodeServlet extends HttpServlet {
|
||||||
HINTS_PURE.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
|
HINTS_PURE.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
private DiskFileItemFactory diskFileItemFactory;
|
|
||||||
private Iterable<String> blockedURLSubstrings;
|
private Iterable<String> blockedURLSubstrings;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -110,10 +111,6 @@ public final class DecodeServlet extends HttpServlet {
|
||||||
Logger logger = Logger.getLogger("com.google.zxing");
|
Logger logger = Logger.getLogger("com.google.zxing");
|
||||||
ServletContext context = servletConfig.getServletContext();
|
ServletContext context = servletConfig.getServletContext();
|
||||||
logger.addHandler(new ServletContextLogHandler(context));
|
logger.addHandler(new ServletContextLogHandler(context));
|
||||||
File repository = (File) context.getAttribute("javax.servlet.context.tempdir");
|
|
||||||
FileCleaningTracker fileCleaningTracker = FileCleanerCleanup.getFileCleaningTracker(context);
|
|
||||||
diskFileItemFactory = new DiskFileItemFactory(1 << 16, repository);
|
|
||||||
diskFileItemFactory.setFileCleaningTracker(fileCleaningTracker);
|
|
||||||
|
|
||||||
URL blockURL = context.getClassLoader().getResource("/private/uri-block-substrings.txt");
|
URL blockURL = context.getClassLoader().getResource("/private/uri-block-substrings.txt");
|
||||||
if (blockURL == null) {
|
if (blockURL == null) {
|
||||||
|
@ -259,37 +256,18 @@ public final class DecodeServlet extends HttpServlet {
|
||||||
@Override
|
@Override
|
||||||
protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
||||||
throws ServletException, IOException {
|
throws ServletException, IOException {
|
||||||
|
Collection<Part> parts = request.getParts();
|
||||||
if (!ServletFileUpload.isMultipartContent(request)) {
|
if (parts.isEmpty()) {
|
||||||
log.info("File upload was not multipart");
|
log.info("File upload was not multipart");
|
||||||
response.sendRedirect("badimage.jspx");
|
response.sendRedirect("badimage.jspx");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
for (Part part : parts) {
|
||||||
ServletFileUpload upload = new ServletFileUpload(diskFileItemFactory);
|
log.info("Decoding uploaded file");
|
||||||
upload.setFileSizeMax(MAX_IMAGE_SIZE);
|
try (InputStream is = part.getInputStream()) {
|
||||||
|
processStream(is, request, response);
|
||||||
// Parse the request
|
|
||||||
try {
|
|
||||||
for (FileItem item : upload.parseRequest(request)) {
|
|
||||||
if (!item.isFormField()) {
|
|
||||||
if (item.getSize() <= MAX_IMAGE_SIZE) {
|
|
||||||
log.info("Decoding uploaded file");
|
|
||||||
try (InputStream is = item.getInputStream()) {
|
|
||||||
processStream(is, request, response);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
log.info("Too large");
|
|
||||||
response.sendRedirect("badimage.jspx");
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (FileUploadException fue) {
|
|
||||||
log.info(fue.toString());
|
|
||||||
response.sendRedirect("badimage.jspx");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void processStream(InputStream is,
|
private static void processStream(InputStream is,
|
||||||
|
|
|
@ -19,10 +19,12 @@ package com.google.zxing.web;
|
||||||
import com.google.common.base.Preconditions;
|
import com.google.common.base.Preconditions;
|
||||||
import com.google.common.net.HttpHeaders;
|
import com.google.common.net.HttpHeaders;
|
||||||
|
|
||||||
|
import javax.servlet.annotation.WebServlet;
|
||||||
import javax.servlet.http.HttpServlet;
|
import javax.servlet.http.HttpServlet;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
@WebServlet("/w/docs/javadoc/*")
|
||||||
public final class LegacyJavadocRedirectServlet extends HttpServlet {
|
public final class LegacyJavadocRedirectServlet extends HttpServlet {
|
||||||
|
|
||||||
private static final String PREFIX = "/w/docs/javadoc";
|
private static final String PREFIX = "/w/docs/javadoc";
|
||||||
|
|
|
@ -23,39 +23,6 @@
|
||||||
|
|
||||||
<distributable/>
|
<distributable/>
|
||||||
|
|
||||||
<listener>
|
|
||||||
<listener-class>org.apache.commons.fileupload.servlet.FileCleanerCleanup</listener-class>
|
|
||||||
</listener>
|
|
||||||
|
|
||||||
<servlet>
|
|
||||||
<servlet-name>DecodeServlet</servlet-name>
|
|
||||||
<servlet-class>com.google.zxing.web.DecodeServlet</servlet-class>
|
|
||||||
<load-on-startup>1</load-on-startup>
|
|
||||||
</servlet>
|
|
||||||
<servlet>
|
|
||||||
<servlet-name>ChartServlet</servlet-name>
|
|
||||||
<servlet-class>com.google.zxing.web.ChartServlet</servlet-class>
|
|
||||||
<load-on-startup>1</load-on-startup>
|
|
||||||
</servlet>
|
|
||||||
<servlet>
|
|
||||||
<servlet-name>LegacyJavadocRedirectServlet</servlet-name>
|
|
||||||
<servlet-class>com.google.zxing.web.LegacyJavadocRedirectServlet</servlet-class>
|
|
||||||
<load-on-startup>1</load-on-startup>
|
|
||||||
</servlet>
|
|
||||||
|
|
||||||
<servlet-mapping>
|
|
||||||
<servlet-name>DecodeServlet</servlet-name>
|
|
||||||
<url-pattern>/w/decode</url-pattern>
|
|
||||||
</servlet-mapping>
|
|
||||||
<servlet-mapping>
|
|
||||||
<servlet-name>ChartServlet</servlet-name>
|
|
||||||
<url-pattern>/w/chart</url-pattern>
|
|
||||||
</servlet-mapping>
|
|
||||||
<servlet-mapping>
|
|
||||||
<servlet-name>LegacyJavadocRedirectServlet</servlet-name>
|
|
||||||
<url-pattern>/w/docs/javadoc/*</url-pattern>
|
|
||||||
</servlet-mapping>
|
|
||||||
|
|
||||||
<welcome-file-list>
|
<welcome-file-list>
|
||||||
<welcome-file>index.jspx</welcome-file>
|
<welcome-file>index.jspx</welcome-file>
|
||||||
</welcome-file-list>
|
</welcome-file-list>
|
||||||
|
|
Loading…
Reference in a new issue