in core/src/main/java/org/apache/stormcrawler/bolt/SiteMapParserBolt.java [96:180]
public void execute(Tuple tuple) {
Metadata metadata = (Metadata) tuple.getValueByField("metadata");
byte[] content = tuple.getBinaryByField("content");
String url = tuple.getStringByField("url");
String ct = metadata.getFirstValue(HttpHeaders.CONTENT_TYPE);
LOG.debug("Processing {}", url);
boolean looksLikeSitemap = sniff(content);
// can force the mimetype as we know it is XML
if (looksLikeSitemap) {
ct = "application/xml";
}
String isSitemap = metadata.getFirstValue(isSitemapKey);
boolean treatAsSM = Boolean.parseBoolean(isSitemap);
// doesn't have the key and want to rely on the clue
if (isSitemap == null && looksLikeSitemap) {
LOG.info("{} detected as sitemap based on content", url);
treatAsSM = true;
}
// decided that it is not a sitemap file
if (!treatAsSM) {
LOG.debug("Not a sitemap {}", url);
// just pass it on
metadata.setValue(isSitemapKey, "false");
this.collector.emit(tuple, tuple.getValues());
this.collector.ack(tuple);
return;
}
// mark the current doc as a sitemap
// as it won't have the k/v if it is a redirected sitemap
metadata.setValue(isSitemapKey, "true");
List<Outlink> outlinks;
try {
outlinks = parseSiteMap(url, content, ct, metadata);
} catch (Exception e) {
// exception while parsing the sitemap
String errorMessage = "Exception while parsing " + url + ": " + e;
LOG.error(errorMessage);
// send to status stream in case another component wants to update
// its status
metadata.setValue(Constants.STATUS_ERROR_SOURCE, "sitemap parsing");
metadata.setValue(Constants.STATUS_ERROR_MESSAGE, errorMessage);
collector.emit(
Constants.StatusStreamName, tuple, new Values(url, metadata, Status.ERROR));
collector.ack(tuple);
return;
}
// apply the parse filters if any to the current document
ParseResult parse = new ParseResult(outlinks);
parse.set(url, metadata);
// apply the parse filters if any
try {
parseFilters.filter(url, content, null, parse);
} catch (RuntimeException e) {
String errorMessage = "Exception while running parse filters on " + url + ": " + e;
LOG.error(errorMessage);
metadata.setValue(Constants.STATUS_ERROR_SOURCE, "content filtering");
metadata.setValue(Constants.STATUS_ERROR_MESSAGE, errorMessage);
collector.emit(StatusStreamName, tuple, new Values(url, metadata, Status.ERROR));
collector.ack(tuple);
return;
}
// send to status stream
for (Outlink ol : parse.getOutlinks()) {
Values v = new Values(ol.getTargetURL(), ol.getMetadata(), Status.DISCOVERED);
collector.emit(Constants.StatusStreamName, tuple, v);
}
// marking the main URL as successfully fetched
// regardless of whether we got a parse exception or not
collector.emit(
Constants.StatusStreamName, tuple, new Values(url, metadata, Status.FETCHED));
collector.ack(tuple);
}