Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

Fixed bug #57, problems with MimeType.java. Patch by Jerome Charron.

Douglass Cuttinga779336b882c
diff --git a/src/java/org/apache/nutch/util/mime/MimeType.java b/src/java/org/apache/nutch/util/mime/MimeType.java
index 7b6f1aa6a..090ad922b 100644
--- a/src/java/org/apache/nutch/util/mime/MimeType.java
+++ b/src/java/org/apache/nutch/util/mime/MimeType.java
@@ -30,6 +30,9 @@ public final class MimeType {
/** The primary and sub types separator */
private final static String SEPARATOR = "/";
+ /** The parameters separator */
+ private final static String PARAMS_SEP = ";";
+
/** Special characters not allowed in content types. */
private final static String SPECIALS = "()<>@,;:\\\"/[]?=";
@@ -65,18 +68,14 @@ public final class MimeType {
if (name == null || name.length() <= 0) {
throw new MimeTypeException("The type can not be null or empty");
}
-
+
// Split the two parts of the Mime Content Type
String[] parts = name.split(SEPARATOR, 2);
// Checks validity of the parts
- if ((parts.length != 2) || (!isValid(parts[0]))) {
- throw new MimeTypeException("Invalid Primary Type " + parts[0]);
- }
- if (!isValid(parts[1])) {
- throw new MimeTypeException("Invalid Sub Type " + parts[1]);
+ if (parts.length != 2) {
+ throw new MimeTypeException("Invalid Content Type " + name);
}
- // All is ok, init values
init(parts[0], parts[1]);
}
@@ -86,23 +85,29 @@ public final class MimeType {
* @param sub the content type sub type.
*/
public MimeType(String primary, String sub) throws MimeTypeException {
+ init(primary, sub);
+ }
+
+ /** Init method used by constructors. */
+ private void init(String primary, String sub) throws MimeTypeException {
+
// Preliminary checks...
if ((primary == null) || (primary.length() <= 0) || (!isValid(primary))) {
throw new MimeTypeException("Invalid Primary Type " + primary);
}
- if ((sub == null) || (sub.length() <= 0) || (!isValid(sub))) {
- throw new MimeTypeException("Invalid Sub Type " + sub);
+ // Remove optional parameters from the sub type
+ String clearedSub = null;
+ if (sub != null) {
+ clearedSub = sub.split(PARAMS_SEP)[0];
}
- // All is ok, init values
- init(primary, sub);
- }
-
- /** Init method used by constructors. */
- private void init(String primary, String sub) {
+ if ((clearedSub == null) || (clearedSub.length() <= 0) || (!isValid(clearedSub))) {
+ throw new MimeTypeException("Invalid Sub Type " + clearedSub);
+ }
+
// All is ok, assign values
this.name = primary + SEPARATOR + sub;
this.primary = primary;
- this.sub = sub;
+ this.sub = clearedSub;
this.extensions = new ArrayList();
this.magics = new ArrayList();
}
diff --git a/src/test/org/apache/nutch/util/mime/TestMimeType.java b/src/test/org/apache/nutch/util/mime/TestMimeType.java
index c2193b8e1..aed4e1a44 100644
--- a/src/test/org/apache/nutch/util/mime/TestMimeType.java
+++ b/src/test/org/apache/nutch/util/mime/TestMimeType.java
@@ -43,6 +43,7 @@ public class TestMimeType extends TestCase {
/** Test of <code>MimeType(String)</code> constructor. */
public void testConstructorString() {
+ MimeType type = null;
constructorFailure(null);
constructorFailure("");
constructorFailure("mimetype");
@@ -50,19 +51,27 @@ public class TestMimeType extends TestCase {
constructorFailure("/mimetype");
constructorFailure("mime@type");
constructorFailure("mime;type");
- constructorSuccess("mime/type");
+ type = constructorSuccess("mime/type");
+ assertEquals("mime", type.getPrimaryType());
+ assertEquals("type", type.getSubType());
+ type = constructorSuccess("mime/type;parameter=value");
+ assertEquals("mime", type.getPrimaryType());
+ assertEquals("type", type.getSubType());
}
/** Test of <code>MimeType(String, String)</code> constructor. */
public void testConstructorStringString() {
- constructorFailure(null);
- constructorFailure("");
- constructorFailure("mimetype");
- constructorFailure("mime/type/");
- constructorFailure("/mimetype");
- constructorFailure("mime@type");
- constructorFailure("mime;type");
- constructorSuccess("mime/type");
+ MimeType type = null;
+ constructorFailure(null, null);
+ constructorFailure("", "");
+ constructorFailure("mime", "type/");
+ constructorFailure("", "mimetype");
+ type = constructorSuccess("mime", "type");
+ assertEquals("mime", type.getPrimaryType());
+ assertEquals("type", type.getSubType());
+ type = constructorSuccess("mime", "type;parameter=value");
+ assertEquals("mime", type.getPrimaryType());
+ assertEquals("type", type.getSubType());
}
/** Test of <code>getName</code> method. */