load custom mimetypes XML from sys prop
MODIFY
CHANGES.txt
CHANGES.txt
Release 1.17 - ???
+ * Load external custom mimetypes XML from system property
+ tika.custom-mimetypes (TIKA-2460).
+
* Extract number of tiffs in a multi-page tiff (TIKA-2451).
* Fix detection of emails extracted from mbox (TIKA-2456).

MODIFY
tika-core/src/main/java/org/apache/tika/mime/MimeTypesFactory.java
tika-core/src/main/java/org/apache/tika/mime/MimeTypesFactory.java
*/
package org.apache.tika.mime;
+import java.io.File;
import java.io.InputStream;
import java.io.IOException;
import java.net.URL;
@@ -30,6 +31,12 @@ import org.w3c.dom.Document;
*/
public class MimeTypesFactory {
+ /**
+ * System property to set a path to an additional external custom mimetypes
+ * XML file to be loaded.
+ */
+ public static final String CUSTOM_MIMES_SYS_PROP = "tika.custom-mimetypes";
+
/**
* Creates an empty instance; same as calling new MimeTypes().
*
@@ -139,6 +146,8 @@ public class MimeTypesFactory {
* override mimetypes found will loaded afterwards.
* The file paths will be interpreted by the specified class
* loader in getResource().
+ * It will also load custom mimetypes from the system property
+ * {@link #CUSTOM_MIMES_SYS_PROP}, if specified.
*
* @param coreFilePath The main MimeTypes file to load
* @param extensionFilePath The name of extension MimeType files to load afterwards
@@ -167,6 +176,16 @@ public class MimeTypesFactory {
urls.add(coreURL);
urls.addAll(extensionURLs);
+ String customMimesPath = System.getProperty(CUSTOM_MIMES_SYS_PROP);
+ if(customMimesPath != null){
+ File externalFile = new File(customMimesPath);
+ if(!externalFile.exists())
+ throw new IOException(
+ "Specified custom mimetypes file not found: " + customMimesPath);
+ URL externalURL = externalFile.toURI().toURL();
+ urls.add(externalURL);
+ }
+
return create( urls.toArray(new URL[urls.size()]) );
}
}

MODIFY
tika-core/src/test/java/org/apache/tika/mime/MimeTypesReaderTest.java
tika-core/src/test/java/org/apache/tika/mime/MimeTypesReaderTest.java
@@ -233,6 +233,22 @@ public class MimeTypesReaderTest {
}
}
+ private class CustomClassLoader extends ClassLoader{
+ }
+
+ /**
+ * TIKA-2460 Test loading of custom-mimetypes.xml from sys prop.
+ */
+ @Test
+ public void testExternalMimeTypes() throws Exception {
+ System.setProperty(MimeTypesFactory.CUSTOM_MIMES_SYS_PROP,
+ "src/test/resources/org/apache/tika/mime/external-mimetypes.xml");
+ MimeTypes mimeTypes = MimeTypes.getDefaultMimeTypes(new CustomClassLoader());
+ Metadata m = new Metadata();
+ m.add(Metadata.RESOURCE_NAME_KEY, "test.external.mime.type");
+ assertEquals("external/mime-type", mimeTypes.detect(null, m).toString());
+ }
+
@Test
public void testGetExtensionForPowerPoint() throws Exception {
MimeType mt = this.mimeTypes.forName("application/vnd.ms-powerpoint");

ADD
tika-core/src/test/resources/org/apache/tika/mime/external-mimetypes.xml
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+-->
+<mime-info>
+ <mime-type type="external/mime-type">
+ <glob pattern="*.external.mime.type" />
+ </mime-type>
+</mime-info>

TIKA-2460: load custom mimetypes XML from sys prop