Author: tallison
Date: Wed Jul 22 14:30:38 2015
New Revision: 1692283

URL: http://svn.apache.org/r1692283
Log:
TIKA-1692 : allow MimeTypes to look for a registered mime type that may or may 
not have parameters.

Modified:
    tika/trunk/CHANGES.txt
    tika/trunk/tika-core/src/main/java/org/apache/tika/mime/MimeTypes.java
    
tika/trunk/tika-core/src/test/java/org/apache/tika/mime/MimeTypesReaderTest.java

Modified: tika/trunk/CHANGES.txt
URL: 
http://svn.apache.org/viewvc/tika/trunk/CHANGES.txt?rev=1692283&r1=1692282&r2=1692283&view=diff
==============================================================================
--- tika/trunk/CHANGES.txt (original)
+++ tika/trunk/CHANGES.txt Wed Jul 22 14:30:38 2015
@@ -1,4 +1,6 @@
 Release 1.10 - Current Development
+  * MimeTypes now tries to find a registered type with and 
+    without parameters (TIKA-1692).
 
   * Added more robust error handling for encoding detection
     of .MSG files (TIKA-1238).

Modified: tika/trunk/tika-core/src/main/java/org/apache/tika/mime/MimeTypes.java
URL: 
http://svn.apache.org/viewvc/tika/trunk/tika-core/src/main/java/org/apache/tika/mime/MimeTypes.java?rev=1692283&r1=1692282&r2=1692283&view=diff
==============================================================================
--- tika/trunk/tika-core/src/main/java/org/apache/tika/mime/MimeTypes.java 
(original)
+++ tika/trunk/tika-core/src/main/java/org/apache/tika/mime/MimeTypes.java Wed 
Jul 22 14:30:38 2015
@@ -306,17 +306,31 @@ public final class MimeTypes implements
      * Returns the registered media type with the given name (or alias).
      * 
      * Unlike {@link #forName(String)}, this function will *not* create a new
-     * MimeType and register it
+     * MimeType and register it.
+     *
+     * Also, unlike {@link #forName(String)}, this function may return a
+     * mime type that does include the parameters that were included in the 
name.
+     * If the registered mime type has parameters (e.g. 
application/dita+xml;format=map),
+     * then those will be maintained.  However, if the name has paramenters 
(e.g.
+     * "application/xml; charset=UTF-8"), but the _registered_ mime type 
doesn't,
+     * those parameters will not be included -- "application/xml".
      *
      * @param name media type name (case-insensitive)
-     * @return the registered media type with the given name or alias
+     * @return the registered media type with the given name or alias or null 
if not found
      * @throws MimeTypeException if the given media type name is invalid
      */
     public MimeType getRegisteredMimeType(String name) throws 
MimeTypeException {
         MediaType type = MediaType.parse(name);
         if (type != null) {
             MediaType normalisedType = registry.normalize(type);
-            return types.get(normalisedType);
+            MimeType candidate = types.get(normalisedType);
+            if (candidate != null) {
+                return candidate;
+            }
+            if (normalisedType.hasParameters()) {
+                return types.get(normalisedType.getBaseType());
+            }
+            return null;
         } else {
             throw new MimeTypeException("Invalid media type name: " + name);
         }

Modified: 
tika/trunk/tika-core/src/test/java/org/apache/tika/mime/MimeTypesReaderTest.java
URL: 
http://svn.apache.org/viewvc/tika/trunk/tika-core/src/test/java/org/apache/tika/mime/MimeTypesReaderTest.java?rev=1692283&r1=1692282&r2=1692283&view=diff
==============================================================================
--- 
tika/trunk/tika-core/src/test/java/org/apache/tika/mime/MimeTypesReaderTest.java
 (original)
+++ 
tika/trunk/tika-core/src/test/java/org/apache/tika/mime/MimeTypesReaderTest.java
 Wed Jul 22 14:30:38 2015
@@ -16,6 +16,11 @@
  */
 package org.apache.tika.mime;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
 import java.io.ByteArrayInputStream;
 import java.lang.reflect.Field;
 import java.util.ArrayList;
@@ -27,11 +32,6 @@ import org.apache.tika.metadata.Metadata
 import org.junit.Before;
 import org.junit.Test;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
-
 /**
  * These tests try to ensure that the MimeTypesReader
  *  has correctly processed the mime-types.xml file.
@@ -239,4 +239,27 @@ public class MimeTypesReaderTest {
         assertEquals(".ppt",ext);
         assertEquals(".ppt",mt.getExtensions().get(0));
     }
+
+    @Test
+    public void testGetRegisteredMimesWithParameters() throws Exception {
+        //TIKA-1692
+
+        // Media Type always keeps details / parameters
+        String name = "application/xml; charset=UTF-8";
+        MediaType mt = MediaType.parse(name);
+        assertEquals(name, mt.toString());
+
+        // Mime type loses details not in the file
+        MimeType mimeType = this.mimeTypes.getRegisteredMimeType(name);
+        assertEquals("application/xml", mimeType.toString());
+        assertEquals(".xml", mimeType.getExtension());
+
+        // But on well-known parameters stays
+        name = "application/dita+xml; format=map";
+        mt = MediaType.parse(name);
+        assertEquals(name, mt.toString());
+        mimeType = this.mimeTypes.getRegisteredMimeType(name);
+        assertEquals(name, mimeType.toString());
+        assertEquals(".ditamap", mimeType.getExtension());
+    }
 }


Reply via email to