Apache Nutch is an extensible and scalable web crawler
NUTCH-247 - Robot parser to restrict, contributed by kubes
Sami Siren3a788815dc1b
diff --git a/CHANGES.txt b/CHANGES.txtindex e03c9ff8d..45a13457c 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -357,6 +357,8 @@ Unreleased changes (1.0-dev)133. NUTCH-626 - Fetcher2 breaks out the domain with db.ignore.external.linksset at cross domain redirects (Remco Verhoef, dogacan via siren)+134. NUTCH-247 - Robot parser to restrict (kubes, siren)+Release 0.9 - 2007-04-021. Changed log4j confiquration to log to stdout on commandlinediff --git a/src/java/org/apache/nutch/fetcher/Fetcher2.java b/src/java/org/apache/nutch/fetcher/Fetcher2.javaindex 6a229b127..7f2c377d0 100644--- a/src/java/org/apache/nutch/fetcher/Fetcher2.java+++ b/src/java/org/apache/nutch/fetcher/Fetcher2.java@@ -933,6 +933,8 @@ public class Fetcher2 extends Configured implementspublic void fetch(Path segment, int threads, boolean parsing)throws IOException {+ checkConfiguration();+if (LOG.isInfoEnabled()) {LOG.info("Fetcher: starting");LOG.info("Fetcher: segment: " + segment);@@ -995,4 +997,40 @@ public class Fetcher2 extends Configured implementsfetcher.fetch(segment, threads, parsing); // run the Fetcher}++ private void checkConfiguration() {++ // ensure that a value has been set for the agent name and that that+ // agent name is the first value in the agents we advertise for robot+ // rules parsing+ String agentName = getConf().get("http.agent.name");+ if (agentName == null || agentName.trim().length() == 0) {+ String message = "Fetcher: No agents listed in 'http.agent.name'"+ + " property.";+ if (LOG.isFatalEnabled()) {+ LOG.fatal(message);+ }+ throw new IllegalArgumentException(message);+ } else {++ // get all of the agents that we advertise+ String agentNames = getConf().get("http.robots.agents");+ StringTokenizer tok = new StringTokenizer(agentNames, ",");+ ArrayList<String> agents = new ArrayList<String>();+ while (tok.hasMoreTokens()) {+ agents.add(tok.nextToken().trim());+ }++ // if the first one is not equal to our agent name, log fatal and throw+ // an exception+ if (!(agents.get(0)).equalsIgnoreCase(agentName)) {+ String message = "Fetcher: Your 'http.agent.name' value should be "+ + "listed first in 'http.robots.agents' property.";+ if (LOG.isWarnEnabled()) {+ LOG.warn(message);+ }+ }+ }+ }+}diff --git a/src/plugin/lib-http/src/java/org/apache/nutch/protocol/http/api/RobotRulesParser.java b/src/plugin/lib-http/src/java/org/apache/nutch/protocol/http/api/RobotRulesParser.javaindex 7e6dd318e..8f652f117 100644--- a/src/plugin/lib-http/src/java/org/apache/nutch/protocol/http/api/RobotRulesParser.java+++ b/src/plugin/lib-http/src/java/org/apache/nutch/protocol/http/api/RobotRulesParser.java@@ -223,9 +223,6 @@ public class RobotRulesParser implements Configurable {// Grab the agent names we advertise to robots files.//String agentName = conf.get("http.agent.name");- if (null == agentName) {- throw new RuntimeException("Agent name not configured!");- }String agentNames = conf.get("http.robots.agents");StringTokenizer tok = new StringTokenizer(agentNames, ",");ArrayList agents = new ArrayList();@@ -233,23 +230,6 @@ public class RobotRulesParser implements Configurable {agents.add(tok.nextToken().trim());}- //- // If there are no agents for robots-parsing, use our- // default agent-string. If both are present, our agent-string- // should be the first one we advertise to robots-parsing.- //- if (agents.size() == 0) {- agents.add(agentName);- if (LOG.isFatalEnabled()) {- LOG.fatal("No agents listed in 'http.robots.agents' property!");- }- } else if (!((String)agents.get(0)).equalsIgnoreCase(agentName)) {- agents.add(0, agentName);- if (LOG.isFatalEnabled()) {- LOG.fatal("Agent we advertise (" + agentName- + ") not listed first in 'http.robots.agents' property!");- }- }setRobotNames((String[]) agents.toArray(new String[agents.size()]));}diff --git a/src/test/crawl-tests.xml b/src/test/crawl-tests.xmlindex 71749df26..87eeb2c42 100644--- a/src/test/crawl-tests.xml+++ b/src/test/crawl-tests.xml@@ -33,5 +33,20 @@<value>test-nutch,*</value></property>+<property>+ <name>http.agent.name.check</name>+ <value>true</value>+</property>++<property>+ <name>http.robots.agents</name>+ <value>test-nutch,*</value>+ <description>The agent strings we'll look for in robots.txt files,+ comma-separated, in decreasing order of precedence. You should+ put the value of http.agent.name as the first agent name, and keep the+ default * at the end of the list. E.g.: BlurflDev,Blurfl,*+ </description>+</property>+</configuration>diff --git a/src/test/org/apache/nutch/fetcher/TestFetcher.java b/src/test/org/apache/nutch/fetcher/TestFetcher.javaindex 977a592aa..804593a87 100644--- a/src/test/org/apache/nutch/fetcher/TestFetcher.java+++ b/src/test/org/apache/nutch/fetcher/TestFetcher.java@@ -167,5 +167,24 @@ public class TestFetcher extends TestCase {private void addUrl(ArrayList<String> urls, String page) {urls.add("http://127.0.0.1:" + server.getListeners()[0].getPort() + "/" + page);}++ public void testAgentNameCheck() {++ boolean failedNoAgentName = false;+ conf.set("http.agent.name", "");++ try {+ conf.setBoolean("fetcher.parse", true);+ Fetcher2 fetcher = new Fetcher2(conf);+ fetcher.fetch(null, 1, false);+ } catch (IllegalArgumentException iae) {+ String message = iae.getMessage();+ failedNoAgentName = message.equals("Fetcher: No agents listed in "+ + "'http.agent.name' property.");+ } catch (Exception e) {+ }++ assertTrue(failedNoAgentName);+ }}