Apache Nutch is an extensible and scalable web crawler
NUTCH-2004 ParseChecker does not handle redirects
Lewis John McGibbney2d84fc4a93b9
diff --git a/CHANGES.txt b/CHANGES.txtindex 5d5fe8560..0873d6e28 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -2,7 +2,7 @@ Nutch Change LogNutch Current Development 1.11-SNAPSHOT-* NUTCH-XX+* NUTCH-2004 ParseChecker does not handle redirects (mjoyce via lewismc)Nutch 1.10 Release - 29/04/2015 (dd/mm/yyyy)Release Report: http://s.apache.org/nutch10diff --git a/src/java/org/apache/nutch/parse/ParserChecker.java b/src/java/org/apache/nutch/parse/ParserChecker.javaindex 8cd725d7e..1b44270e4 100644--- a/src/java/org/apache/nutch/parse/ParserChecker.java+++ b/src/java/org/apache/nutch/parse/ParserChecker.java@@ -34,6 +34,7 @@ import org.apache.nutch.protocol.Content;import org.apache.nutch.protocol.Protocol;import org.apache.nutch.protocol.ProtocolFactory;import org.apache.nutch.protocol.ProtocolOutput;+import org.apache.nutch.protocol.ProtocolStatus;import org.apache.nutch.scoring.ScoringFilters;import org.apache.nutch.util.NutchConfiguration;import org.apache.nutch.util.URLUtil;@@ -135,9 +136,30 @@ public class ParserChecker implements Tool {Text turl = new Text(url);ProtocolOutput output = protocol.getProtocolOutput(turl, cd);+ // If the configuration permits, handle redirects until we either run+ // out of allowed redirects or we stop getting redirect statuses.+ int maxRedirects = conf.getInt("http.redirect.max", 0);+ int numRedirects = 0;+ while (output.getStatus().isRedirect() && numRedirects < maxRedirects) {+ String newURL = URLUtil.toASCII(output.getStatus().getArgs()[0]);+ LOG.info("Handling redirect to " + newURL);++ protocol = factory.getProtocol(newURL);+ turl = new Text(newURL);+ output = protocol.getProtocolOutput(turl, cd);++ numRedirects++;+ }+if (!output.getStatus().isSuccess()) {System.err.println("Fetch failed with protocol status: "+ output.getStatus());++ if (output.getStatus().isRedirect()) {+ System.err.println("Redirect(s) not handled due to configuration.");+ System.err.println("Max Redirects to handle per config: " + maxRedirects);+ System.err.println("Number of Redirects handled: " + numRedirects);+ }return (-1);}diff --git a/src/java/org/apache/nutch/protocol/ProtocolStatus.java b/src/java/org/apache/nutch/protocol/ProtocolStatus.javaindex 476039325..9e75531b8 100644--- a/src/java/org/apache/nutch/protocol/ProtocolStatus.java+++ b/src/java/org/apache/nutch/protocol/ProtocolStatus.java@@ -227,6 +227,10 @@ public class ProtocolStatus implements Writable {|| code == ROBOTS_DENIED;}+ public boolean isRedirect() {+ return code == MOVED || code == TEMP_MOVED;+ }+public String getMessage() {if (args != null && args.length > 0)return args[0];