Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

NUTCH-2579 Fetcher to use parsed URL to call ProtocolFactory.getProtocol(url) - add method ProtocolFactory.getProtocol(URL url) - and use in Fetcher and RobotRulesParser

Sebastian Nagel7d000b18f775
diff --git a/src/java/org/apache/nutch/fetcher/FetcherThread.java b/src/java/org/apache/nutch/fetcher/FetcherThread.java
index 4f67391f0..3b000f995 100644
--- a/src/java/org/apache/nutch/fetcher/FetcherThread.java
+++ b/src/java/org/apache/nutch/fetcher/FetcherThread.java
@@ -296,14 +296,14 @@ public class FetcherThread extends Thread {
LOG.debug("redirectCount={}", redirectCount);
}
redirecting = false;
- Protocol protocol = this.protocolFactory.getProtocol(fit.url
- .toString());
- BaseRobotRules rules = protocol.getRobotRules(fit.url, fit.datum, robotsTxtContent);
+ Protocol protocol = this.protocolFactory.getProtocol(fit.u);
+ BaseRobotRules rules = protocol.getRobotRules(fit.url, fit.datum,
+ robotsTxtContent);
if (robotsTxtContent != null) {
outputRobotsTxt(robotsTxtContent);
robotsTxtContent.clear();
}
- if (!rules.isAllowed(fit.u.toString())) {
+ if (!rules.isAllowed(fit.url.toString())) {
// unblock
((FetchItemQueues) fetchQueues).finishFetchItem(fit, true);
if (LOG.isDebugEnabled()) {
diff --git a/src/java/org/apache/nutch/protocol/ProtocolFactory.java b/src/java/org/apache/nutch/protocol/ProtocolFactory.java
index 6a4205931..b39155b79 100644
--- a/src/java/org/apache/nutch/protocol/ProtocolFactory.java
+++ b/src/java/org/apache/nutch/protocol/ProtocolFactory.java
@@ -65,16 +65,36 @@ public class ProtocolFactory {
* @return The appropriate {@link Protocol} implementation for a given
* {@link URL}.
* @throws ProtocolNotFound
- * when Protocol can not be found for urlString
+ * when Protocol can not be found for urlString or urlString is not
+ * a valid URL
*/
- public synchronized Protocol getProtocol(String urlString)
+ public Protocol getProtocol(String urlString) throws ProtocolNotFound {
+ try {
+ URL url = new URL(urlString);
+ return getProtocol(url);
+ } catch (MalformedURLException e) {
+ throw new ProtocolNotFound(urlString, e.toString());
+ }
+ }
+
+ /**
+ * 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
+ * {@link URL}.
+ * @throws ProtocolNotFound
+ * when Protocol can not be found for url
+ */
+ public synchronized Protocol getProtocol(URL url)
throws ProtocolNotFound {
ObjectCache objectCache = ObjectCache.get(conf);
try {
- URL url = new URL(urlString);
String protocolName = url.getProtocol();
- if (protocolName == null)
- throw new ProtocolNotFound(urlString);
+ if (protocolName == null) {
+ throw new ProtocolNotFound(url.toString());
+ }
String cacheId = Protocol.X_POINT_ID + protocolName;
Protocol protocol = (Protocol) objectCache.getObject(cacheId);
@@ -90,10 +110,8 @@ public class ProtocolFactory {
protocol = (Protocol) extension.getExtensionInstance();
objectCache.setObject(cacheId, protocol);
return protocol;
- } catch (MalformedURLException e) {
- throw new ProtocolNotFound(urlString, e.toString());
} catch (PluginRuntimeException e) {
- throw new ProtocolNotFound(urlString, e.toString());
+ throw new ProtocolNotFound(url.toString(), e.toString());
}
}
diff --git a/src/java/org/apache/nutch/protocol/RobotRulesParser.java b/src/java/org/apache/nutch/protocol/RobotRulesParser.java
index a5a2d975e..1cddeea44 100644
--- a/src/java/org/apache/nutch/protocol/RobotRulesParser.java
+++ b/src/java/org/apache/nutch/protocol/RobotRulesParser.java
@@ -285,7 +285,7 @@ public abstract class RobotRulesParser implements Tool {
}
ProtocolFactory factory = new ProtocolFactory(conf);
try {
- protocol = factory.getProtocol(robotsTxtUrl.toString());
+ protocol = factory.getProtocol(robotsTxtUrl);
} catch (ProtocolNotFound e) {
LOG.error("No protocol found for {}: {}", args[0],
StringUtils.stringifyException(e));