Apache Nutch is an extensible and scalable web crawler
- fix for NUTCH-608
Chris Mattmannfb34dadcf126
diff --git a/CHANGES.txt b/CHANGES.txtindex 04e78f82a..774377567 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -202,6 +202,8 @@ Unreleased changes (1.0-dev)71. NUTCH-607 - Update build.xml to include tika jar when building war (kubes)+72. NUTCH-608 - Upgrade nutch to use released apache-tika-0.1-incubating (mattmann)+Release 0.9 - 2007-04-021. Changed log4j confiquration to log to stdout on commandlinediff --git a/lib/tika-0.1-dev.jar b/lib/tika-0.1-dev.jardeleted file mode 100644index 7d60b1e22..000000000Binary files a/lib/tika-0.1-dev.jar and /dev/null differdiff --git a/lib/tika-0.1-incubating.jar b/lib/tika-0.1-incubating.jarnew file mode 100644index 000000000..910563f6cBinary files /dev/null and b/lib/tika-0.1-incubating.jar differdiff --git a/src/java/org/apache/nutch/parse/ParserFactory.java b/src/java/org/apache/nutch/parse/ParserFactory.javaindex 35a1c3eb5..3c820f580 100644--- a/src/java/org/apache/nutch/parse/ParserFactory.java+++ b/src/java/org/apache/nutch/parse/ParserFactory.java@@ -36,12 +36,9 @@ import org.apache.nutch.plugin.ExtensionPoint;import org.apache.nutch.plugin.PluginRuntimeException;import org.apache.nutch.plugin.PluginRepository;import org.apache.nutch.util.LogUtil;+import org.apache.nutch.util.MimeUtil;import org.apache.nutch.util.ObjectCache;-// Tika imports-import org.apache.tika.mime.MimeType;-import org.apache.tika.mime.MimeTypeException;-/** Creates and caches {@link Parser} plugins.*/public final class ParserFactory {@@ -222,15 +219,8 @@ public final class ParserFactory {ObjectCache objectCache = ObjectCache.get(conf);// First of all, tries to clean the content-typeString type = null;- try {- type = MimeType.clean(contentType);- } catch (MimeTypeException mte) {- if (LOG.isDebugEnabled()) {- LOG.debug("Could not clean the content-type [" + contentType +- "], Reason is [" + mte + "]. Using its raw version...");- }- type = contentType;- }+ type = MimeUtil.cleanMimeType(contentType);+List<Extension> extensions = (List<Extension>) objectCache.getObject(type);diff --git a/src/java/org/apache/nutch/protocol/Content.java b/src/java/org/apache/nutch/protocol/Content.javaindex 34d545c99..a9e8e2a56 100755--- a/src/java/org/apache/nutch/protocol/Content.java+++ b/src/java/org/apache/nutch/protocol/Content.java@@ -38,14 +38,9 @@ import org.apache.hadoop.io.Writable;//Nutch importsimport org.apache.nutch.metadata.Metadata;+import org.apache.nutch.util.MimeUtil;import org.apache.nutch.util.NutchConfiguration;-//Tika imports-import org.apache.tika.mime.MimeType;-import org.apache.tika.mime.MimeTypeException;-import org.apache.tika.mime.MimeUtils;--public final class Content implements Writable{public static final String DIR_NAME = "content";@@ -64,9 +59,7 @@ public final class Content implements Writable{private Metadata metadata;- private boolean mimeTypeMagic;-- private static MimeUtils mimeTypes;+ private MimeUtil mimeTypes;public Content() {metadata = new Metadata();@@ -88,10 +81,8 @@ public final class Content implements Writable{this.base = base;this.content = content;this.metadata = metadata;- this.mimeTypeMagic = conf.getBoolean("mime.type.magic", true);- if(this.mimeTypes == null){- this.mimeTypes = new MimeUtils(conf.get("mime.types.file"), this.mimeTypeMagic);- }++ this.mimeTypes = new MimeUtil(conf);this.contentType = getContentType(contentType, url, content);}@@ -289,41 +280,7 @@ public final class Content implements Writable{}private String getContentType(String typeName, String url, byte[] data) {- MimeType type = null;- String cleanedMimeType = null;-- try {- cleanedMimeType = MimeType.clean(typeName);- } catch (MimeTypeException mte) {- // Seems to be a malformed mime type name...- }-- // first try to get the type from the cleaned type name- type = cleanedMimeType != null ? this.mimeTypes.getRepository().forName(- cleanedMimeType) : null;-- // if returned null, then try url resolution- if (type == null) {- // If no mime-type header, or cannot find a corresponding registered- // mime-type, then guess a mime-type from the url pattern- type = this.mimeTypes.getRepository().getMimeType(url) != null ? this.mimeTypes- .getRepository().getMimeType(url)- : type;- }-- // if magic is enabled use mime magic to guess if the mime type returned- // from the magic guess is different than the one that's already set so far- // if it is, go with the mime type returned by the magic- if (this.mimeTypeMagic) {- MimeType magicType = this.mimeTypes.getRepository().getMimeType(data);- if (magicType != null && !type.getName().equals(magicType.getName())) {- // If magic enabled and the current mime type differs from that of the- // one returned from the magic, take the magic mimeType-- type = magicType;- }- }-- return type.getName();+ return this.mimeTypes.autoResolveContentType(typeName, url, data);}+}diff --git a/src/java/org/apache/nutch/util/MimeUtil.java b/src/java/org/apache/nutch/util/MimeUtil.javanew file mode 100644index 000000000..6e7c0d164--- /dev/null+++ b/src/java/org/apache/nutch/util/MimeUtil.java@@ -0,0 +1,212 @@+/**+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++package org.apache.nutch.util;++// JDK imports+import java.io.File;+import java.util.logging.Logger;++// Hadoop imports+import org.apache.hadoop.conf.Configuration;++// Tika imports+import org.apache.tika.mime.MimeType;+import org.apache.tika.mime.MimeTypeException;+import org.apache.tika.mime.MimeTypes;+import org.apache.tika.mime.MimeTypesFactory;++/**+ * @author mattmann+ * @since NUTCH-608+ *+ * <p>+ * This is a facade class to insulate Nutch from its underlying Mime Type+ * substrate library, <a href="http://incubator.apache.org/tika/">Apache Tika</a>.+ * Any mime handling code should be placed in this utility class, and hidden+ * from the Nutch classes that rely on it.+ * </p>+ */+public final class MimeUtil {++ private static final String SEPARATOR = ";";++ /* our Tika mime type registry */+ private MimeTypes mimeTypes;++ /* whether or not magic should be employed or not */+ private boolean mimeMagic;++ /* our log stream */+ private static final Logger LOG = Logger.getLogger(MimeUtil.class.getName());++ public MimeUtil(Configuration conf) {+ this.mimeTypes = MimeTypesFactory.create(conf+ .getConfResourceAsInputStream(conf.get("mime.types.file")));+ this.mimeMagic = conf.getBoolean("mime.type.magic", true);+ }++ /**+ * Cleans a {@link MimeType} name by removing out the actual {@link MimeType},+ * from a string of the form:+ *+ * <pre>+ * <primary type>/<sub type> ; < optional params+ * </pre>+ *+ * @param origType+ * The original mime type string to be cleaned.+ * @return The primary type, and subtype, concatenated, e.g., the actual mime+ * type.+ */+ public static String cleanMimeType(String origType) {+ if (origType == null)+ return null;++ // take the origType and split it on ';'+ String[] tokenizedMimeType = origType.split(SEPARATOR);+ if (tokenizedMimeType.length > 1) {+ // there was a ';' in there, take the first value+ return tokenizedMimeType[0];+ } else {+ // there wasn't a ';', so just return the orig type+ return origType;+ }+ }++ /**+ * A facade interface to trying all the possible mime type resolution+ * strategies available within Tika. First, the mime type provided in+ * <code>typeName</code> is cleaned, with {@link #cleanMimeType(String)}.+ * Then the cleaned mime type is looked up in the underlying Tika+ * {@link MimeTypes} registry, by its cleaned name. If the {@link MimeType} is+ * found, then that mime type is used, otherwise {@link URL} resolution is+ * used to try and determine the mime type. If that means is unsuccessful, and+ * if <code>mime.type.magic</code> is enabled in {@link NutchConfiguration},+ * then mime type magic resolution is used to try and obtain a+ * better-than-the-default approximation of the {@link MimeType}.+ *+ * @param typeName+ * The original mime type, returned from a {@link ProtocolOutput}.+ * @param url+ * The given {@link URL}, that Nutch was trying to crawl.+ * @param data+ * The byte data, returned from the crawl, if any.+ * @return The correctly, automatically guessed {@link MimeType} name.+ */+ public String autoResolveContentType(String typeName, String url, byte[] data) {+ MimeType type = null;+ String cleanedMimeType = null;++ try {+ cleanedMimeType = MimeUtil.cleanMimeType(typeName) != null ? this.mimeTypes+ .forName(MimeUtil.cleanMimeType(typeName)).getName()+ : null;+ } catch (MimeTypeException mte) {+ // Seems to be a malformed mime type name...+ }++ // first try to get the type from the cleaned type name+ try {+ type = cleanedMimeType != null ? this.mimeTypes.forName(cleanedMimeType)+ : null;+ } catch (MimeTypeException e) {+ type = null;+ }++ // if returned null, or if it's the default type then try url resolution+ if (type == null+ || (type != null && type.getName().equals(MimeTypes.DEFAULT))) {+ // If no mime-type header, or cannot find a corresponding registered+ // mime-type, then guess a mime-type from the url pattern+ type = this.mimeTypes.getMimeType(url) != null ? this.mimeTypes+ .getMimeType(url) : type;+ }++ // if magic is enabled use mime magic to guess if the mime type returned+ // from the magic guess is different than the one that's already set so far+ // if it is, and it's not the default mime type, then go with the mime type+ // returned by the magic+ if (this.mimeMagic) {+ MimeType magicType = this.mimeTypes.getMimeType(data);+ if (magicType != null && !magicType.getName().equals(MimeTypes.DEFAULT)+ && type != null && !type.getName().equals(magicType.getName())) {+ // If magic enabled and the current mime type differs from that of the+ // one returned from the magic, take the magic mimeType+ type = magicType;+ }++ // if type is STILL null after all the resolution strategies, go for the+ // default type+ if (type == null) {+ try {+ type = this.mimeTypes.forName(MimeTypes.DEFAULT);+ } catch (Exception ignore) {+ }+ }+ }++ return type.getName();+ }++ /**+ * Facade interface to Tika's underlying {@link MimeTypes#getMimeType(String)}+ * method.+ *+ * @param url+ * A string representation of the document {@link URL} to sense the+ * {@link MimeType} for.+ * @return An appropriate {@link MimeType}, identified from the given+ * Document url in string form.+ */+ public MimeType getMimeType(String url) {+ return this.mimeTypes.getMimeType(url);+ }++ /**+ * A facade interface to Tika's underlying {@link MimeTypes#forName(String)}+ * method.+ *+ * @param name+ * The name of a valid {@link MimeType} in the Tika mime registry.+ * @return The object representation of the {@link MimeType}, if it exists,+ * or null otherwise.+ */+ public MimeType forName(String name) {+ try {+ return this.mimeTypes.forName(name);+ } catch (MimeTypeException e) {+ LOG.warning("Exception getting mime type by name: [" + name+ + "]: Message: " + e.getMessage());+ return null;+ }+ }++ /**+ * Facade interface to Tika's underlying {@link MimeTypes#getMimeType(File)}+ * method.+ *+ * @param f+ * The {@link File} to sense the {@link MimeType} for.+ * @return The {@link MimeType} of the given {@link File}, or null if it+ * cannot be determined.+ */+ public MimeType getMimeType(File f) {+ return this.mimeTypes.getMimeType(f);+ }++}diff --git a/src/plugin/index-more/src/java/org/apache/nutch/indexer/more/MoreIndexingFilter.java b/src/plugin/index-more/src/java/org/apache/nutch/indexer/more/MoreIndexingFilter.javaindex fa7760755..f574499a1 100644--- a/src/plugin/index-more/src/java/org/apache/nutch/indexer/more/MoreIndexingFilter.java+++ b/src/plugin/index-more/src/java/org/apache/nutch/indexer/more/MoreIndexingFilter.java@@ -24,8 +24,6 @@ import org.apache.oro.text.regex.PatternMatcher;import org.apache.oro.text.regex.MatchResult;import org.apache.oro.text.regex.MalformedPatternException;import org.apache.tika.mime.MimeType;-import org.apache.tika.mime.MimeTypeException;-import org.apache.tika.mime.MimeUtils;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;@@ -46,6 +44,7 @@ import org.apache.nutch.indexer.IndexingException;import org.apache.nutch.crawl.CrawlDatum;import org.apache.nutch.crawl.Inlinks;import org.apache.nutch.parse.ParseData;+import org.apache.nutch.util.MimeUtil;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.io.Text;@@ -79,7 +78,7 @@ public class MoreIndexingFilter implements IndexingFilter {private boolean MAGIC;/** Get the MimeTypes resolver instance. */- private static MimeUtils MIME;+ private MimeUtil MIME;public Document filter(Document doc, Parse parse, Text url, CrawlDatum datum, Inlinks inlinks)throws IndexingException {@@ -193,14 +192,9 @@ public class MoreIndexingFilter implements IndexingFilter {// } else {// contentType = MIME.getMimeType(url);// }- mimeType = MIME.getRepository().getMimeType(url);+ mimeType = MIME.getMimeType(url);} else {- try {- mimeType = new MimeType(contentType);- } catch (MimeTypeException e) {- if (LOG.isWarnEnabled()) { LOG.warn(url + e.toString()); }- mimeType = null;- }+ mimeType = MIME.forName(contentType);}// Checks if we solved the content-type.@@ -209,8 +203,8 @@ public class MoreIndexingFilter implements IndexingFilter {}contentType = mimeType.getName();- String primaryType = mimeType.getPrimaryType();- String subType = mimeType.getSubType();+ String primaryType = mimeType.getSuperType().getName();+ String subType = mimeType.getSubTypes().first().getName();// leave this for future improvement//MimeTypeParameterList parameterList = mimeType.getParameters()@@ -279,9 +273,7 @@ public class MoreIndexingFilter implements IndexingFilter {public void setConf(Configuration conf) {this.conf = conf;- MAGIC = conf.getBoolean("mime.type.magic", true);- if(MIME == null)- MIME = new MimeUtils(getConf().get("mime.types.file"), MAGIC);+ MIME = new MimeUtil(conf);}public Configuration getConf() {diff --git a/src/plugin/parse-zip/src/java/org/apache/nutch/parse/zip/ZipTextExtractor.java b/src/plugin/parse-zip/src/java/org/apache/nutch/parse/zip/ZipTextExtractor.javaindex e5a67babb..2b505e0cf 100644--- a/src/plugin/parse-zip/src/java/org/apache/nutch/parse/zip/ZipTextExtractor.java+++ b/src/plugin/parse-zip/src/java/org/apache/nutch/parse/zip/ZipTextExtractor.java@@ -42,9 +42,7 @@ import org.apache.nutch.parse.ParseImpl;import org.apache.nutch.parse.ParseException;import org.apache.nutch.parse.Outlink;import org.apache.nutch.protocol.Content;--// Tika imports-import org.apache.tika.mime.MimeUtils;+import org.apache.nutch.util.MimeUtil;@@ -55,7 +53,7 @@ import org.apache.tika.mime.MimeUtils;public class ZipTextExtractor {/** Get the MimeTypes resolver instance. */- private static MimeUtils MIME;+ private MimeUtil MIME;public static final Log LOG = LogFactory.getLog(ZipTextExtractor.class);@@ -64,9 +62,8 @@ public class ZipTextExtractor {/** Creates a new instance of ZipTextExtractor */public ZipTextExtractor(Configuration conf) {- this.conf = conf;- if(this.MIME == null)- this.MIME = new MimeUtils(conf.get("mime.types.file"),conf.getBoolean("mime.type.magic", true));+ this.conf = conf;+ this.MIME = new MimeUtil(conf);}public String extractText(InputStream input, String url, List outLinksList) throws IOException {@@ -96,7 +93,7 @@ public class ZipTextExtractor {int i = fname.lastIndexOf('.');if (i != -1) {// Trying to resolve the Mime-Type- String contentType = MIME.getRepository().getMimeType(fname).getName();+ String contentType = MIME.getMimeType(fname).getName();try {Metadata metadata = new Metadata();metadata.set(Response.CONTENT_LENGTH, Long.toString(entry.getSize()));diff --git a/src/plugin/protocol-file/src/java/org/apache/nutch/protocol/file/FileResponse.java b/src/plugin/protocol-file/src/java/org/apache/nutch/protocol/file/FileResponse.javaindex 66344f7e2..d07ee8114 100644--- a/src/plugin/protocol-file/src/java/org/apache/nutch/protocol/file/FileResponse.java+++ b/src/plugin/protocol-file/src/java/org/apache/nutch/protocol/file/FileResponse.java@@ -26,13 +26,13 @@ import java.io.IOException;// Nutch importsimport org.apache.nutch.crawl.CrawlDatum;import org.apache.nutch.protocol.Content;+import org.apache.nutch.util.MimeUtil;import org.apache.nutch.metadata.Metadata;import org.apache.nutch.net.protocols.HttpDateFormat;import org.apache.nutch.net.protocols.Response;// Tika importsimport org.apache.tika.mime.MimeType;-import org.apache.tika.mime.MimeUtils;// Hadoop importsimport org.apache.hadoop.conf.Configuration;@@ -76,7 +76,7 @@ public class FileResponse {private final File file;private Configuration conf;- private static MimeUtils MIME;+ private MimeUtil MIME;/** Returns the response code. */public int getCode() { return code; }@@ -102,8 +102,7 @@ public class FileResponse {this.file = file;this.conf = conf;- if(MIME == null)- MIME = new MimeUtils(conf.get("mime.types.file"),conf.getBoolean("mime.type.magic", true));+ MIME = new MimeUtil(conf);if (!"file".equals(url.getProtocol()))throw new FileException("Not a file url:" + url);@@ -210,7 +209,7 @@ public class FileResponse {headers.set(Response.LAST_MODIFIED, HttpDateFormat.toString(f.lastModified()));- MimeType mimeType = MIME.getRepository().getMimeType(f);+ MimeType mimeType = MIME.getMimeType(f);String mimeTypeString = mimeType != null ? mimeType.getName() : "";headers.set(Response.CONTENT_TYPE, mimeTypeString);