}
@Override
public void handle(Request req, HttpServletResponse res, String target,
int dispatch) throws IOException, ServletException {
HttpURI u = req.getUri();
String uri = u.toString();
addMyHeader(res, "URI", uri);
// don't pass it down the chain
req.setHandled(true);
res.addHeader("X-Handled-By", getClass().getSimpleName());
if (uri.endsWith("/robots.txt")) {
return;
}
res.setContentType("text/html");
try {
OutputStream os = res.getOutputStream();
byte[] bytes = testA.getBytes("UTF-8");
os.write(bytes);
// record URI
String p = "<p>URI: " + uri + "</p>\r\n";
os.write(p.getBytes());
// fake some links
String basePath;
String baseDomain;
if (u.getPath().length() > 5) {
basePath = u.getPath().substring(0, u.getPath().length() - 5);
} else {
basePath = u.getPath();
}
// internal links
if (pageMode.equals(Mode.RANDOM)) { // initialize random per host
pageR = new Random(u.getHost().hashCode());
}
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 += "outlink " + i + "</a></p>\r\n";
os.write(link.getBytes());
}
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
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());
os.write(testB.getBytes());
res.flushBuffer();
} catch (IOException ioe) {
}