Apache Nutch is an extensible and scalable web crawler
NUTCH-824 FileProtocol does not resolve encoded URLs
Julien Nioche171e58436925
diff --git a/CHANGES.txt b/CHANGES.txtindex e920fd899..46d370fb9 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -2,6 +2,8 @@ Nutch Change LogRelease 2.0 - Current Development+* NUTCH-824 Crawling - File Error 404 when fetching file with an hexadecimal character in the file name (Michela Becchi via jnioche)+* NUTCH-954 Strict application of Content-Length limit for http protocols (Alexis Detreglode via jnioche)* NUTCH-953 Fixed crawl command in Nutch script (Alexis Detreglode via jnioche)diff --git a/src/plugin/protocol-file/sample/testprotocolfile.txt b/src/plugin/protocol-file/sample/testprotocolfile.txtindex fbe8a8acf..ba1bed917 100644--- a/src/plugin/protocol-file/sample/testprotocolfile.txt+++ b/src/plugin/protocol-file/sample/testprotocolfile.txt@@ -1 +1,2 @@Protocol File Test+Protocol File Testdiff --git a/src/plugin/protocol-file/sample/testprotocolfile_(encoded).txt b/src/plugin/protocol-file/sample/testprotocolfile_(encoded).txtnew file mode 100644index 000000000..fbe8a8acf--- /dev/null+++ b/src/plugin/protocol-file/sample/testprotocolfile_(encoded).txt@@ -0,0 +1 @@+Protocol File Testdiff --git a/src/plugin/protocol-file/src/java/org/apache/nutch/protocol/file/FileResponse.java b/src/plugin/protocol-file/src/java/org/apache/nutch/protocol/file/FileResponse.javaindex 1616d2631..2b6a91abd 100644--- a/src/plugin/protocol-file/src/java/org/apache/nutch/protocol/file/FileResponse.java+++ b/src/plugin/protocol-file/src/java/org/apache/nutch/protocol/file/FileResponse.java@@ -19,6 +19,7 @@ package org.apache.nutch.protocol.file;// JDK importsimport java.io.IOException;+import java.io.UnsupportedEncodingException;import java.net.URL;import org.apache.hadoop.conf.Configuration;@@ -112,6 +113,12 @@ public class FileResponse {String path = "".equals(url.getPath()) ? "/" : url.getPath();+ try {+ // specify the encoding via the config later?+ path = java.net.URLDecoder.decode(path, "UTF-8");+ } catch (UnsupportedEncodingException ex) {+ }+try {this.content = null;diff --git a/src/plugin/protocol-file/src/test/org/apache/nutch/protocol/file/TestProtocolFile.java b/src/plugin/protocol-file/src/test/org/apache/nutch/protocol/file/TestProtocolFile.javaindex 34928e1b5..d3eccc28d 100644--- a/src/plugin/protocol-file/src/test/org/apache/nutch/protocol/file/TestProtocolFile.java+++ b/src/plugin/protocol-file/src/test/org/apache/nutch/protocol/file/TestProtocolFile.java@@ -20,7 +20,13 @@ package org.apache.nutch.protocol.file;// Hadoop importsimport junit.framework.TestCase;+import org.apache.hadoop.conf.Configuration;+import org.apache.hadoop.io.Text;import org.apache.nutch.net.protocols.Response;+import org.apache.nutch.protocol.Protocol;+import org.apache.nutch.protocol.ProtocolException;+import org.apache.nutch.protocol.ProtocolFactory;+import org.apache.nutch.protocol.ProtocolNotFound;import org.apache.nutch.protocol.ProtocolOutput;import org.apache.nutch.protocol.ProtocolStatusCodes;import org.apache.nutch.storage.WebPage;@@ -36,34 +42,42 @@ import org.apache.nutch.util.NutchConfiguration;*/public class TestProtocolFile extends TestCase {- private static final org.apache.nutch.protocol.file.File fileProtocol =- new org.apache.nutch.protocol.file.File();+ private String fileSeparator = System.getProperty("file.separator");+ private String sampleDir = System.getProperty("test.data", ".");- private static final String testTextFile = "testprotocolfile.txt";+ private static final String[] testTextFiles = new String[] {+ "testprotocolfile.txt", "testprotocolfile_(encoded).txt", "testprotocolfile_%28encoded%29.txt" };private static final String expectedMimeType = "text/plain";-- static {- fileProtocol.setConf(NutchConfiguration.create());++ private Configuration conf;++ protected void setUp() {+ conf = NutchConfiguration.create();}- private String fileSeparator = System.getProperty("file.separator");- // This system property is defined in ./src/plugin/build-plugin.xml- private String sampleDir = System.getProperty("test.data", ".");+ public void testSetContentType() throws ProtocolException {+ for (String testTextFile : testTextFiles) {+ setContentType(testTextFile);+ }+ }/*** Tests the setting of the <code>Response.CONTENT_TYPE</code> metadata* field.+ * @throws ProtocolNotFound** @since NUTCH-384**/- public void testSetContentType() {- String fileURL = "file:"+sampleDir + fileSeparator + testTextFile;- assertNotNull(fileURL);+ public void setContentType(String testTextFile) throws ProtocolNotFound {+ String urlString = "file:" + sampleDir + fileSeparator + testTextFile;+ assertNotNull(urlString);WebPage datum = new WebPage();- ProtocolOutput output = fileProtocol.getProtocolOutput(fileURL, datum);+ Protocol protocol = new ProtocolFactory(conf).getProtocol(urlString);+ ProtocolOutput output = protocol.getProtocolOutput(urlString,datum);assertNotNull(output);+assertEquals("Status code: [" + output.getStatus().getCode()+ "], not equal to: [" + ProtocolStatusCodes.SUCCESS + "]: args: ["+ output.getStatus().getArgs() + "]", ProtocolStatusCodes.SUCCESS, output