Add uri blocklist feature

git-svn-id: https://zxing.googlecode.com/svn/trunk@2711 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen@gmail.com 2013-05-05 12:41:41 +00:00
parent d722038cdb
commit ad1a305cf6
4 changed files with 84 additions and 46 deletions

View file

@ -59,6 +59,7 @@
<include name="zxingorg/**"/> <include name="zxingorg/**"/>
<exclude name="zxingorg/build/**"/> <exclude name="zxingorg/build/**"/>
<exclude name="zxingorg/target/**"/> <exclude name="zxingorg/target/**"/>
<exclude name="zxingorg/resources/private/**"/>
</zipfileset> </zipfileset>
</zip> </zip>
<!-- Separate out test data files now to make download more manageable --> <!-- Separate out test data files now to make download more manageable -->

View file

@ -70,6 +70,11 @@
<build> <build>
<sourceDirectory>src</sourceDirectory> <sourceDirectory>src</sourceDirectory>
<outputDirectory>build</outputDirectory> <outputDirectory>build</outputDirectory>
<resources>
<resource>
<directory>resources</directory>
</resource>
</resources>
<plugins> <plugins>
<plugin> <plugin>
<artifactId>maven-enforcer-plugin</artifactId> <artifactId>maven-enforcer-plugin</artifactId>

View file

@ -83,12 +83,6 @@
<version>2.2-SNAPSHOT</version> <version>2.2-SNAPSHOT</version>
<type>javadoc</type> <type>javadoc</type>
</artifactItem> </artifactItem>
<artifactItem>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>2.2-SNAPSHOT</version>
<type>javadoc</type>
</artifactItem>
</artifactItems> </artifactItems>
<outputDirectory>web/w/docs/javadoc</outputDirectory> <outputDirectory>web/w/docs/javadoc</outputDirectory>
<overWriteIfNewer>true</overWriteIfNewer> <overWriteIfNewer>true</overWriteIfNewer>

View file

@ -43,9 +43,11 @@ 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.BufferedReader;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.Writer; import java.io.Writer;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
@ -60,6 +62,7 @@ import java.util.Collection;
import java.util.EnumMap; import java.util.EnumMap;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.Map; import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
@ -98,9 +101,10 @@ public final class DecodeServlet extends HttpServlet {
} }
private DiskFileItemFactory diskFileItemFactory; private DiskFileItemFactory diskFileItemFactory;
private Collection<String> blockedURLSubstrings;
@Override @Override
public void init(ServletConfig servletConfig) { public void init(ServletConfig servletConfig) throws ServletException {
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));
@ -108,6 +112,25 @@ public final class DecodeServlet extends HttpServlet {
FileCleaningTracker fileCleaningTracker = FileCleanerCleanup.getFileCleaningTracker(context); FileCleaningTracker fileCleaningTracker = FileCleanerCleanup.getFileCleaningTracker(context);
diskFileItemFactory = new DiskFileItemFactory(1 << 16, repository); diskFileItemFactory = new DiskFileItemFactory(1 << 16, repository);
diskFileItemFactory.setFileCleaningTracker(fileCleaningTracker); diskFileItemFactory.setFileCleaningTracker(fileCleaningTracker);
blockedURLSubstrings = new ArrayList<String>();
InputStream in = DecodeServlet.class.getResourceAsStream("/private/uri-block-substrings.txt");
if (in != null) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(in, Charset.forName("UTF-8")));
try {
String line;
while ((line = reader.readLine()) != null) {
blockedURLSubstrings.add(line);
}
} finally {
reader.close();
}
} catch (IOException ioe) {
throw new ServletException(ioe);
}
}
log.info("Blocking URIs containing: " + blockedURLSubstrings);
} }
@Override @Override
@ -122,6 +145,13 @@ public final class DecodeServlet extends HttpServlet {
} }
imageURIString = imageURIString.trim(); imageURIString = imageURIString.trim();
for (String substring : blockedURLSubstrings) {
if (imageURIString.contains(substring)) {
log.info("Disallowed URI " + imageURIString);
response.sendRedirect("badurl.jspx");
return;
}
}
URI imageURI; URI imageURI;
try { try {
@ -322,56 +352,64 @@ public final class DecodeServlet extends HttpServlet {
ReaderException savedException = null; ReaderException savedException = null;
try { try {
// Look for multiple barcodes
MultipleBarcodeReader multiReader = new GenericMultipleBarcodeReader(reader);
Result[] theResults = multiReader.decodeMultiple(bitmap, HINTS);
if (theResults != null) {
results.addAll(Arrays.asList(theResults));
}
} catch (ReaderException re) {
savedException = re;
}
if (results.isEmpty()) {
try { try {
// Look for pure barcode // Look for multiple barcodes
Result theResult = reader.decode(bitmap, HINTS_PURE); MultipleBarcodeReader multiReader = new GenericMultipleBarcodeReader(reader);
if (theResult != null) { Result[] theResults = multiReader.decodeMultiple(bitmap, HINTS);
results.add(theResult); if (theResults != null) {
results.addAll(Arrays.asList(theResults));
} }
} catch (ReaderException re) { } catch (ReaderException re) {
savedException = re; savedException = re;
} }
}
if (results.isEmpty()) {
if (results.isEmpty()) { try {
try { // Look for pure barcode
// Look for normal barcode in photo Result theResult = reader.decode(bitmap, HINTS_PURE);
Result theResult = reader.decode(bitmap, HINTS); if (theResult != null) {
if (theResult != null) { results.add(theResult);
results.add(theResult); }
} catch (ReaderException re) {
savedException = re;
} }
} catch (ReaderException re) {
savedException = re;
} }
}
if (results.isEmpty()) {
if (results.isEmpty()) { try {
try { // Look for normal barcode in photo
// Try again with other binarizer Result theResult = reader.decode(bitmap, HINTS);
BinaryBitmap hybridBitmap = new BinaryBitmap(new HybridBinarizer(source)); if (theResult != null) {
Result theResult = reader.decode(hybridBitmap, HINTS); results.add(theResult);
if (theResult != null) { }
results.add(theResult); } catch (ReaderException re) {
savedException = re;
} }
} catch (ReaderException re) {
savedException = re;
} }
}
if (results.isEmpty()) {
try {
// Try again with other binarizer
BinaryBitmap hybridBitmap = new BinaryBitmap(new HybridBinarizer(source));
Result theResult = reader.decode(hybridBitmap, HINTS);
if (theResult != null) {
results.add(theResult);
}
} catch (ReaderException re) {
savedException = re;
}
}
if (results.isEmpty()) {
handleException(savedException, response);
return;
}
if (results.isEmpty()) { } catch (RuntimeException re) {
handleException(savedException, response); // Call out unexpected errors in the log clearly
return; log.log(Level.WARNING, "Unexpected exception from library", re);
throw new ServletException(re);
} }
String fullParameter = request.getParameter("full"); String fullParameter = request.getParameter("full");