Apache Nutch is an extensible and scalable web crawler
Some improvements to FakeHandler to make the fake pages more predictable and to follow a few predefined spread modes.
Andrzej Bialeckib2fc362a75f9
diff --git a/build.xml b/build.xmlindex 25a215a79..653d2b081 100644--- a/build.xml+++ b/build.xml@@ -225,6 +225,11 @@<java classname="org.apache.nutch.tools.proxy.TestbedProxy" fork="true"><classpath refid="test.classpath"/><arg value="-fake"/>+ <arg value="-hostMode"/>+ <arg value="u"/>+ <arg value="-pageMode"/>+ <arg value="u"/>+ <arg value="-debug"/><!--<arg value="-delay"/><arg value="-200"/>diff --git a/src/java/org/apache/nutch/tools/proxy/FakeHandler.java b/src/java/org/apache/nutch/tools/proxy/FakeHandler.javaindex 4debe6f52..4499005f3 100644--- a/src/java/org/apache/nutch/tools/proxy/FakeHandler.java+++ b/src/java/org/apache/nutch/tools/proxy/FakeHandler.java@@ -19,6 +19,7 @@ package org.apache.nutch.tools.proxy;import java.io.IOException;import java.io.OutputStream;import java.util.Random;+import java.util.concurrent.atomic.AtomicLong;import javax.servlet.ServletException;import javax.servlet.http.HttpServletResponse;@@ -27,7 +28,20 @@ import org.mortbay.jetty.HttpURI;import org.mortbay.jetty.Request;public class FakeHandler extends AbstractTestbedHandler {+ /** Create links to hosts generated from a pool of numHosts/numPages random names. */+ public static enum Mode {UNIQUE, RANDOM};++ int numInternalLinks;+ int numExternalLinks;+ Mode hostMode;+ Mode pageMode;+ AtomicLong hostSeq = new AtomicLong(0);+ AtomicLong pageSeq = new AtomicLong(0);+ int numHosts;+ int numPages;+Random r = new Random(1234567890L); // predictable+ Random pageR;private static final String testA ="<html><body><h1>Internet Weather Forecast Accuracy</h1>\n" +@@ -39,12 +53,33 @@ public class FakeHandler extends AbstractTestbedHandler {"<p>The hail, rain and lightning eventually subsided, but the most alarming news was waiting on cell phone voicemail. A friend who lived in the area had called frantically, knowing we were at the park, as the local news was reporting multiple people had been by struck by lightning at Schlitterbahn during the storm.</p>" +"<p>'So much for the 0% chance of rain,' I repeated.</p></body></html>";+ /**+ * Create fake pages.+ * @param hostMode if UNIQUE then each external outlink will use a unique host name. If+ * RANDOM then each outlink will use a host name allocated from pool of numHosts.+ * @param pageMode if UNIQUE then each internal outlinks will use a unique page name.+ * if RANDOM then each outlink will use a page name allocated from pool of numPages.+ * @param numInternalLinks+ * @param numExternalLinks+ * @param numHosts+ * @param numPages+ */+ public FakeHandler(Mode hostMode, Mode pageMode,+ int numInternalLinks, int numExternalLinks,+ int numHosts, int numPages) {+ this.numExternalLinks = numExternalLinks;+ this.numInternalLinks = numInternalLinks;+ this.numHosts = numHosts;+ this.numPages = numPages;+ this.hostMode = hostMode;+ this.pageMode = pageMode;+ }+@Overridepublic void handle(Request req, HttpServletResponse res, String target,int dispatch) throws IOException, ServletException {HttpURI u = req.getUri();String uri = u.toString();- //System.err.println("-faking " + uri.toString());addMyHeader(res, "URI", uri);// don't pass it down the chainreq.setHandled(true);@@ -61,32 +96,51 @@ public class FakeHandler extends AbstractTestbedHandler {String p = "<p>URI: " + uri + "</p>\r\n";os.write(p.getBytes());// fake some links- String base;+ String basePath;+ String baseDomain;if (u.getPath().length() > 5) {- base = u.getPath().substring(0, u.getPath().length() - 5);+ basePath = u.getPath().substring(0, u.getPath().length() - 5);} else {- base = u.getPath();+ basePath = u.getPath();+ }+ // internal links+ if (pageMode.equals(Mode.RANDOM)) { // initialize random per host+ pageR = new Random(u.getHost().hashCode());}- String prefix = u.getScheme() + "://" + u.getHost();- if (u.getPort() != 80 && u.getPort() != -1) base += ":" + u.getPort();- if (!base.startsWith("/")) prefix += "/";- prefix = prefix + base;- for (int i = 0; i < 10; i++) {- String link = "<p><a href='" + prefix;- if (!prefix.endsWith("/")) {- link += "/";+ for (int i = 0; i < numInternalLinks; i++) {+ String link = "<p><a href='";+ if (pageMode.equals(Mode.RANDOM)) {+ link += pageR.nextInt (numPages) + ".html'>";+ } else {+ if (!basePath.endsWith("/")) {+ link += "/";+ }+ link += pageSeq.getAndIncrement() + ".html'>";}- link += i + ".html'>outlink " + i + "</a></p>\r\n";+ link += "outlink " + i + "</a></p>\r\n";os.write(link.getBytes());}- // fake a few links to random nonexistent hosts- for (int i = 0; i < 5; i++) {- int h = r.nextInt(1000000); // 1 mln hosts- String link = "<p><a href='http://www.fake-" + h + ".com/'>fake host " + h + "</a></p>\r\n";+ baseDomain = u.getHost();+ // chop off the TLD+ int pos = baseDomain.lastIndexOf('.');+ String tld = baseDomain.substring(pos);+ baseDomain = baseDomain.substring(0, pos);+ String link;+ // external links+ for (int i = 0; i < numExternalLinks; i++) {+ String host;+ if (hostMode.equals(Mode.RANDOM)) {+ host = "www.rnd-" + r.nextInt(numHosts) + ".com";+ link = "http://" + host + "/";+ } else {+ host = baseDomain + "-" + hostSeq.getAndIncrement() + ".com";+ link = "http://" + host + "/";+ }+ link = "<p><a href='" + link + "'>fake host " + host + "</a></p>\r\n";os.write(link.getBytes());}// fake a link to the root URL- String link = "<p><a href='" + u.getScheme() + "://" + u.getHost();+ link = "<p><a href='" + u.getScheme() + "://" + u.getHost();if (u.getPort() != 80 && u.getPort() != -1) link += ":" + u.getPort();link += "/'>site " + u.getHost() + "</a></p>\r\n";os.write(link.getBytes());diff --git a/src/java/org/apache/nutch/tools/proxy/TestbedProxy.java b/src/java/org/apache/nutch/tools/proxy/TestbedProxy.javaindex 4ee8eb951..789933400 100644--- a/src/java/org/apache/nutch/tools/proxy/TestbedProxy.java+++ b/src/java/org/apache/nutch/tools/proxy/TestbedProxy.java@@ -27,6 +27,7 @@ import org.apache.hadoop.fs.FileStatus;import org.apache.hadoop.fs.FileSystem;import org.apache.hadoop.fs.Path;import org.apache.hadoop.util.StringUtils;+import org.apache.nutch.tools.proxy.FakeHandler.Mode;import org.apache.nutch.util.HadoopFSUtil;import org.apache.nutch.util.NutchConfiguration;import org.mortbay.jetty.Handler;@@ -44,12 +45,26 @@ public class TestbedProxy {*/public static void main(String[] args) throws Exception {if (args.length == 0) {- System.err.println("TestbedProxy [-port <nnn>] [-forward] [-fake] [-delay nnn] [-debug]");+ System.err.println("TestbedProxy [-port <nnn>] [-forward] [-fake [...]] [-delay nnn] [-debug]");System.err.println("-port <nnn>\trun the proxy on port <nnn> (special permissions may be needed for ports < 1024)");System.err.println("-forward\tif specified, requests to all unknown urls will be passed to");System.err.println("\t\toriginal servers. If false (default) unknown urls generate 404 Not Found.");System.err.println("-delay\tdelay every response by nnn seconds. If delay is negative use a random value up to nnn");System.err.println("-fake\tif specified, requests to all unknown urls will succeed with fake content");+ System.err.println("\nAdditional options for -fake handler (all optional):");+ System.err.println("\t-hostMode (u | r)\tcreate unique host names, or pick random from a pool");+ System.err.println("\t-pageMode (u | r)\tcreate unique page names, or pick random from a pool");+ System.err.println("\t-numHosts N\ttotal number of hosts when using hostMode r");+ System.err.println("\t-numPages N\ttotal number of pages per host when using pageMode r");+ System.err.println("\t-intLinks N\tnumber of internal (same host) links per page");+ System.err.println("\t-extLinks N\tnumber of external (other host) links per page");+ System.err.println("\nDefaults for -fake handler:");+ System.err.println("\t-hostMode r");+ System.err.println("\t-pageMode r");+ System.err.println("\t-numHosts 1000000");+ System.err.println("\t-numPages 10000");+ System.err.println("\t-intLinks 10");+ System.err.println("\t-extLinks 5");System.exit(-1);}@@ -60,6 +75,12 @@ public class TestbedProxy {boolean delay = false;boolean debug = false;int delayVal = 0;+ Mode pageMode = Mode.RANDOM;+ Mode hostMode = Mode.RANDOM;+ int numHosts = 1000000;+ int numPages = 10000;+ int intLinks = 10;+ int extLinks = 5;for (int i = 0; i < args.length; i++) {if (args[i].equals("-port")) {@@ -71,6 +92,22 @@ public class TestbedProxy {delayVal = Integer.parseInt(args[++i]);} else if (args[i].equals("-fake")) {fake = true;+ } else if (args[i].equals("-hostMode")) {+ if (args[++i].equals("u")) {+ hostMode = Mode.UNIQUE;+ }+ } else if (args[i].equals("-pageMode")) {+ if (args[++i].equals("u")) {+ pageMode = Mode.UNIQUE;+ }+ } else if (args[i].equals("-numHosts")) {+ numHosts = Integer.parseInt(args[++i]);+ } else if (args[i].equals("-numPages")) {+ numPages = Integer.parseInt(args[++i]);+ } else if (args[i].equals("-intLinks")) {+ intLinks = Integer.parseInt(args[++i]);+ } else if (args[i].equals("-extLinks")) {+ extLinks = Integer.parseInt(args[++i]);} else if (args[i].equals("-debug")) {debug = true;} else {@@ -113,7 +150,8 @@ public class TestbedProxy {}if (fake) {LOG.info("* Added fake handler for remaining URLs.");- list.addHandler(new FakeHandler());+ list.addHandler(new FakeHandler(hostMode, pageMode, intLinks, extLinks,+ numHosts, numPages));}list.addHandler(new NotFoundHandler());// Start the http server