Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

NUTCH-643 ClassCastException in PDF parser, upgrade to unofficial PDFBox 0.7.4

Andrzej Bialeckid22073f277d5
diff --git a/CHANGES.txt b/CHANGES.txt
index 8202cc163..0013b8ceb 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -333,6 +333,8 @@ Unreleased changes (1.0-dev)
124. NUTCH-671 - JSP errors in Nutch searcher webapp (Edwin Chu via ab)
+125. NUTCH-643 - ClassCastException in PDF parser (Guillaume Smet, ab)
+
Release 0.9 - 2007-04-02
diff --git a/src/plugin/parse-pdf/build.xml b/src/plugin/parse-pdf/build.xml
index 94d2bf225..2c3b97c26 100644
--- a/src/plugin/parse-pdf/build.xml
+++ b/src/plugin/parse-pdf/build.xml
@@ -28,6 +28,10 @@
<!-- for junit test -->
<mkdir dir="${build.test}/data"/>
- <copy file="sample/pdftest.pdf" todir="${build.test}/data"/>
+ <copy todir="${build.test}/data">
+ <fileset dir="sample">
+ <include name="*.pdf"/>
+ </fileset>
+ </copy>
</project>
diff --git a/src/plugin/parse-pdf/lib/FontBox-0.1.0-dev.jar b/src/plugin/parse-pdf/lib/FontBox-0.1.0-dev.jar
deleted file mode 100644
index c9030febd..000000000
Binary files a/src/plugin/parse-pdf/lib/FontBox-0.1.0-dev.jar and /dev/null differ
diff --git a/src/plugin/parse-pdf/lib/FontBox-0.2.0-dev.jar b/src/plugin/parse-pdf/lib/FontBox-0.2.0-dev.jar
new file mode 100644
index 000000000..a0ba4cb0a
Binary files /dev/null and b/src/plugin/parse-pdf/lib/FontBox-0.2.0-dev.jar differ
diff --git a/src/plugin/parse-pdf/lib/JempBox-0.2.0.jar b/src/plugin/parse-pdf/lib/JempBox-0.2.0.jar
new file mode 100644
index 000000000..b7de2c8d9
Binary files /dev/null and b/src/plugin/parse-pdf/lib/JempBox-0.2.0.jar differ
diff --git a/src/plugin/parse-pdf/lib/JempBox-LICENSE.txt b/src/plugin/parse-pdf/lib/JempBox-LICENSE.txt
new file mode 100644
index 000000000..15dfcf5e8
--- /dev/null
+++ b/src/plugin/parse-pdf/lib/JempBox-LICENSE.txt
@@ -0,0 +1,25 @@
+Copyright (c) 2006-2007, www.jempbox.org
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+3. Neither the name of pdfbox; nor the names of its
+ contributors may be used to endorse or promote products derived from this
+ software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/src/plugin/parse-pdf/lib/NOTICE.txt b/src/plugin/parse-pdf/lib/NOTICE.txt
new file mode 100644
index 000000000..d7fc5a8d6
--- /dev/null
+++ b/src/plugin/parse-pdf/lib/NOTICE.txt
@@ -0,0 +1,6 @@
+This plugin uses an unreleased version of PDFBox, obtained from SourceForce
+CVS repository (HEAD revision as of Feb 3 2009), because the last released
+version contains a bug related to document decryption (see NUTCH-643).
+
+The libraries should be updated to a regular PDFBox release whenever that
+becomes available.
diff --git a/src/plugin/parse-pdf/lib/PDFBox-0.7.3.jar b/src/plugin/parse-pdf/lib/PDFBox-0.7.4-dev.jar
similarity index 80%
rename from src/plugin/parse-pdf/lib/PDFBox-0.7.3.jar
rename to src/plugin/parse-pdf/lib/PDFBox-0.7.4-dev.jar
index f821d6447..3a337f576 100644
Binary files a/src/plugin/parse-pdf/lib/PDFBox-0.7.3.jar and b/src/plugin/parse-pdf/lib/PDFBox-0.7.4-dev.jar differ
diff --git a/src/plugin/parse-pdf/lib/bcprov-LICENSE.txt b/src/plugin/parse-pdf/lib/bcprov-LICENSE.txt
new file mode 100644
index 000000000..cf1d6be93
--- /dev/null
+++ b/src/plugin/parse-pdf/lib/bcprov-LICENSE.txt
@@ -0,0 +1,7 @@
+Copyright (c) 2000 - 2008 The Legion Of The Bouncy Castle (http://www.bouncycastle.org)
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/src/plugin/parse-pdf/lib/bcprov-jdk14-132.jar b/src/plugin/parse-pdf/lib/bcprov-jdk14-132.jar
new file mode 100644
index 000000000..552721d0e
Binary files /dev/null and b/src/plugin/parse-pdf/lib/bcprov-jdk14-132.jar differ
diff --git a/src/plugin/parse-pdf/lib/jai_codec.jar b/src/plugin/parse-pdf/lib/jai_codec.jar
new file mode 100644
index 000000000..056ecd3a7
Binary files /dev/null and b/src/plugin/parse-pdf/lib/jai_codec.jar differ
diff --git a/src/plugin/parse-pdf/lib/jai_core.jar b/src/plugin/parse-pdf/lib/jai_core.jar
new file mode 100644
index 000000000..b29b8eed5
Binary files /dev/null and b/src/plugin/parse-pdf/lib/jai_core.jar differ
diff --git a/src/plugin/parse-pdf/plugin.xml b/src/plugin/parse-pdf/plugin.xml
index 117021cbe..7b11558ff 100644
--- a/src/plugin/parse-pdf/plugin.xml
+++ b/src/plugin/parse-pdf/plugin.xml
@@ -26,8 +26,12 @@
<library name="parse-pdf.jar">
<export name="*"/>
</library>
- <library name="PDFBox-0.7.3.jar"/>
- <library name="FontBox-0.1.0-dev.jar"/>
+ <library name="PDFBox-0.7.4-dev.jar"/>
+ <library name="FontBox-0.2.0-dev.jar"/>
+ <library name="JempBox-0.2.0-dev.jar"/>
+ <library name="bcprov-jdk14-132.jar"/>
+ <library name="jai_codec.jar"/>
+ <library name="jai_core.jar"/>
</runtime>
<requires>
diff --git a/src/plugin/parse-pdf/sample/README.txt b/src/plugin/parse-pdf/sample/README.txt
index e13b54b0e..9f68af87a 100644
--- a/src/plugin/parse-pdf/sample/README.txt
+++ b/src/plugin/parse-pdf/sample/README.txt
@@ -1,10 +1,5 @@
-20040710, John Xing
-
-./pdftest.pdf: a small pdf file picked up from internet.
-
-Used for junit test by ./src/test/org/apache/nutch/parse/pdf/TestPdfParser.java
-
-It contains text:
+1. pdftest.pdf:
+A small pdf file picked up from internet. It contains text:
E:\M55\!\1725.fm 2003-01-01 18:15 P Tagg, IPM, University of Liverpool
A VERY SMALL PDF FILE
A VERY SMALL PDF FILE
@@ -12,3 +7,7 @@ A VERY SMALL PDF FILE
A VERY SMALL PDF FILE
A VERY SMALL PDF FILE
A VERY SMALL PDF FILE
+
+2. encrypt.pdf:
+The same file as above, encrypted with owner's password 'owner'
+and empty user's password.
diff --git a/src/plugin/parse-pdf/sample/encrypted.pdf b/src/plugin/parse-pdf/sample/encrypted.pdf
new file mode 100644
index 000000000..383cebbca
Binary files /dev/null and b/src/plugin/parse-pdf/sample/encrypted.pdf differ
diff --git a/src/plugin/parse-pdf/src/java/org/apache/nutch/parse/pdf/PdfParser.java b/src/plugin/parse-pdf/src/java/org/apache/nutch/parse/pdf/PdfParser.java
index c01697fca..695b87de4 100644
--- a/src/plugin/parse-pdf/src/java/org/apache/nutch/parse/pdf/PdfParser.java
+++ b/src/plugin/parse-pdf/src/java/org/apache/nutch/parse/pdf/PdfParser.java
@@ -17,14 +17,14 @@
package org.apache.nutch.parse.pdf;
-import org.pdfbox.encryption.DocumentEncryption;
import org.pdfbox.pdfparser.PDFParser;
import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.pdmodel.PDDocumentInformation;
+import org.pdfbox.pdmodel.encryption.BadSecurityHandlerException;
+import org.pdfbox.pdmodel.encryption.StandardDecryptionMaterial;
import org.pdfbox.util.PDFTextStripper;
import org.pdfbox.exceptions.CryptographyException;
-import org.pdfbox.exceptions.InvalidPasswordException;
// Commons Logging imports
import org.apache.commons.logging.Log;
@@ -93,9 +93,8 @@ public class PdfParser implements Parser {
pdf = parser.getPDDocument();
if (pdf.isEncrypted()) {
- DocumentEncryption decryptor = new DocumentEncryption(pdf);
//Just try using the default password and move on
- decryptor.decryptDocument("");
+ pdf.openProtection(new StandardDecryptionMaterial(""));
}
// collect text
@@ -106,7 +105,7 @@ public class PdfParser implements Parser {
PDDocumentInformation info = pdf.getDocumentInformation();
title = info.getTitle();
// more useful info, currently not used. please keep them for future use.
- metadata.add(Metadata.PAGE_COUNT, String.valueOf(pdf.getPageCount()));
+ metadata.add(Metadata.PAGE_COUNT, String.valueOf(pdf.getNumberOfPages()));
metadata.add(Metadata.AUTHOR, info.getAuthor());
metadata.add(Metadata.SUBJECT, info.getSubject());
metadata.add(Metadata.KEYWORDS, info.getKeywords());
@@ -122,9 +121,9 @@ public class PdfParser implements Parser {
} catch (CryptographyException e) {
return new ParseStatus(ParseStatus.FAILED,
"Error decrypting document. " + e).getEmptyParseResult(content.getUrl(), getConf());
- } catch (InvalidPasswordException e) {
+ } catch (BadSecurityHandlerException e) {
return new ParseStatus(ParseStatus.FAILED,
- "Can't decrypt document - invalid password. " + e).getEmptyParseResult(content.getUrl(), getConf());
+ "Error decrypting document. " + e).getEmptyParseResult(content.getUrl(), getConf());
} catch (Exception e) { // run time exception
if (LOG.isWarnEnabled()) {
LOG.warn("General exception in PDF parser: "+e.getMessage());
diff --git a/src/plugin/parse-pdf/src/test/org/apache/nutch/parse/pdf/TestPdfParser.java b/src/plugin/parse-pdf/src/test/org/apache/nutch/parse/pdf/TestPdfParser.java
index 5a0dea98d..f5e4dbd5a 100644
--- a/src/plugin/parse-pdf/src/test/org/apache/nutch/parse/pdf/TestPdfParser.java
+++ b/src/plugin/parse-pdf/src/test/org/apache/nutch/parse/pdf/TestPdfParser.java
@@ -46,7 +46,10 @@ public class TestPdfParser extends TestCase {
// Make sure sample files are copied to "test.data" as specified in
// ./src/plugin/parse-pdf/build.xml during plugin compilation.
// Check ./src/plugin/parse-pdf/sample/README.txt for what they are.
- private String[] sampleFiles = {"pdftest.pdf"};
+ private String[] sampleFiles = {
+ "pdftest.pdf",
+ "encrypted.pdf"
+ };
private String expectedText = "A VERY SMALL PDF FILE";
@@ -64,7 +67,7 @@ public class TestPdfParser extends TestCase {
Content content;
Parse parse;
- for (int i=0; i<sampleFiles.length; i++) {
+ for (int i = 0; i < sampleFiles.length; i++) {
urlString = "file:" + sampleDir + fileSeparator + sampleFiles[i];
Configuration conf = NutchConfiguration.create();