Apache Nutch is an extensible and scalable web crawler
NUTCH-1807 Avoid methods relying on system-specific default locale / charset
Sebastian Nagel9068999c0e99
diff --git a/build.xml b/build.xmlindex c93188430..8660a6c19 100644--- a/build.xml+++ b/build.xml@@ -476,6 +476,7 @@</copy><copy file="${test.src.dir}/log4j.properties" todir="${test.build.classes}"/><copy file="${test.src.dir}/crawl-tests.xml" todir="${test.build.classes}"/>+ <copy file="${test.src.dir}/host-protocol-mapping.txt" todir="${test.build.classes}"/><copy file="${test.src.dir}/filter-all.txt" todir="${test.build.classes}"/><junitlauncher printSummary="true" haltOnFailure="true" failureProperty="tests.failed"><classpath refid="test.classpath"/>diff --git a/src/java/org/apache/nutch/protocol/ProtocolFactory.java b/src/java/org/apache/nutch/protocol/ProtocolFactory.javaindex d5f8eff54..8af30530c 100644--- a/src/java/org/apache/nutch/protocol/ProtocolFactory.java+++ b/src/java/org/apache/nutch/protocol/ProtocolFactory.java@@ -19,22 +19,19 @@ package org.apache.nutch.protocol;import java.io.BufferedReader;import java.io.IOException;import java.lang.invoke.MethodHandles;-import java.net.URL;import java.net.MalformedURLException;+import java.net.URL;import java.util.HashMap;import java.util.Map;+import org.apache.commons.lang3.StringUtils;+import org.apache.hadoop.conf.Configuration;import org.apache.nutch.plugin.Extension;import org.apache.nutch.plugin.ExtensionPoint;import org.apache.nutch.plugin.PluginRepository;import org.apache.nutch.plugin.PluginRuntimeException;import org.apache.nutch.util.ObjectCache;import org.apache.nutch.util.URLUtil;--import org.apache.commons.lang3.StringUtils;--import org.apache.hadoop.conf.Configuration;-import org.slf4j.Logger;import org.slf4j.LoggerFactory;@@ -96,7 +93,7 @@ public class ProtocolFactory {}/*** Returns the appropriate {@link Protocol} implementation for a url.- *+ ** @param urlString* Url String* @return The appropriate {@link Protocol} implementation for a given@@ -116,7 +113,7 @@ public class ProtocolFactory {/*** Returns the appropriate {@link Protocol} implementation for a url.- *+ ** @param url* URL to be fetched by returned {@link Protocol} implementation* @return The appropriate {@link Protocol} implementation for a given@@ -128,21 +125,28 @@ public class ProtocolFactory {throws ProtocolNotFound {try {Protocol protocol = null;-- // First attempt to resolve a protocol implementation by hostname- String host = url.getHost();- String domain = URLUtil.getDomainName(url).toLowerCase().trim();- String hostOrDomain = null;Extension extension = null;- if (hostProtocolMapping.containsKey(host)) {- hostOrDomain = host;- } else if (hostProtocolMapping.containsKey(domain)) {- hostOrDomain = domain;- }- if (hostOrDomain != null) {- extension = getExtensionById(hostProtocolMapping.get(hostOrDomain));- if (extension != null) {- protocol = getProtocolInstanceByExtension(extension);++ /*+ * First attempt to resolve a protocol implementation by host or domain+ * name, if the "host-protocol-mapping.txt" defines rules.+ */+ if (!hostProtocolMapping.isEmpty()) {+ String host = url.getHost();+ String hostOrDomain = null;+ if (hostProtocolMapping.containsKey(host)) {+ hostOrDomain = host;+ } else {+ String domain = URLUtil.getDomainName(url);+ if (hostProtocolMapping.containsKey(domain)) {+ hostOrDomain = domain;+ }+ }+ if (hostOrDomain != null) {+ extension = getExtensionById(hostProtocolMapping.get(hostOrDomain));+ if (extension != null) {+ protocol = getProtocolInstanceByExtension(extension);+ }}}diff --git a/src/test/host-protocol-mapping.txt b/src/test/host-protocol-mapping.txtnew file mode 100644index 000000000..04dc3e1ea--- /dev/null+++ b/src/test/host-protocol-mapping.txt@@ -0,0 +1,20 @@+# This file defines a hostname to protocol plugin mapping. Each line takes a+# host name followed by a tab, followed by the ID of the protocol plugin. You+# can find the ID in the protocol plugin's plugin.xml file.+#+# <hostname>\t<plugin_id>\n+# nutch.apache.org org.apache.nutch.protocol.httpclient.Http+# tika.apache.org org.apache.nutch.protocol.http.Http+#+# If the requested host is not mapped, Nutch can choose any of the enabled+# plugins so you can force defaults using:+#+# protocol:<protocol>\t<plugin_id>\n+#+# This example forces httpclient for all protocol in case the host is not mapped:+protocol:http org.apache.nutch.protocol.http.Http+protocol:https org.apache.nutch.protocol.okhttp.OkHttp+nutch.apache.org org.apache.nutch.protocol.http.Http+square.github.io org.apache.nutch.protocol.okhttp.OkHttp+tika.apache.org org.apache.nutch.protocol.httpclient.Http+selenium.dev org.apache.nutch.protocol.selenium.Httpdiff --git a/src/test/org/apache/nutch/protocol/TestProtocolFactory.java b/src/test/org/apache/nutch/protocol/TestProtocolFactory.javaindex b6639f304..5892340c3 100644--- a/src/test/org/apache/nutch/protocol/TestProtocolFactory.java+++ b/src/test/org/apache/nutch/protocol/TestProtocolFactory.java@@ -16,13 +16,18 @@*/package org.apache.nutch.protocol;+import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.junit.jupiter.api.Assertions.assertFalse;+import static org.junit.jupiter.api.Assertions.assertNotNull;+import static org.junit.jupiter.api.Assertions.assertSame;+import static org.junit.jupiter.api.Assertions.assertTrue;+import static org.junit.jupiter.api.Assertions.fail;+import org.apache.hadoop.conf.Configuration;import org.apache.nutch.util.NutchConfiguration;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;-import static org.junit.jupiter.api.Assertions.*;-public class TestProtocolFactory {Configuration conf;@@ -76,4 +81,27 @@ public class TestProtocolFactory {assertFalse(factory.contains("smb", "smbb"));}+ @Test+ public void testProtocolMapping() throws ProtocolNotFound {+ // test mappings defined in src/test/host-protocol-mapping.txt+ // (copied into and loaded from test classpath build/test/classes)+ Protocol defaultHttpProtocol = factory.getProtocol("http://example.org/");+ assertEquals("org.apache.nutch.protocol.http.Http",+ defaultHttpProtocol.getClass().getCanonicalName());+ Protocol defaultHttpsProtocol = factory.getProtocol("https://example.org/");+ assertEquals("org.apache.nutch.protocol.okhttp.OkHttp",+ defaultHttpsProtocol.getClass().getCanonicalName());+ Protocol nutchProtocol = factory.getProtocol("https://nutch.apache.org/");+ assertSame(defaultHttpProtocol, nutchProtocol);+ Protocol squareProtocol = factory+ .getProtocol("https://square.github.io/okhttp/");+ assertSame(defaultHttpsProtocol, squareProtocol);+ Protocol tikaProtocol = factory.getProtocol("https://tika.apache.org/");+ assertEquals("org.apache.nutch.protocol.httpclient.Http",+ tikaProtocol.getClass().getCanonicalName());+ Protocol seleniumProtocol = factory+ .getProtocol("https://www.selenium.dev/");+ assertEquals("org.apache.nutch.protocol.selenium.Http",+ seleniumProtocol.getClass().getCanonicalName());+ }}