incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r1200281 - in /sling/trunk/bundles/commons/mime: pom.xml src/main/java/org/apache/sling/commons/mime/internal/TikaMimeTypeProvider.java src/test/java/org/apache/sling/commons/mime/internal/TikaMimeTypeProviderTest.java
Date Thu, 10 Nov 2011 11:54:10 GMT
Author: fmeschbe
Date: Thu Nov 10 11:54:10 2011
New Revision: 1200281

URL: http://svn.apache.org/viewvc?rev=1200281&view=rev
Log:
SLING-1059 Apply slightly modified patch by Jukka Zitting (thanks alot)

Added:
    sling/trunk/bundles/commons/mime/src/main/java/org/apache/sling/commons/mime/internal/TikaMimeTypeProvider.java
    sling/trunk/bundles/commons/mime/src/test/java/org/apache/sling/commons/mime/internal/TikaMimeTypeProviderTest.java
Modified:
    sling/trunk/bundles/commons/mime/pom.xml

Modified: sling/trunk/bundles/commons/mime/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/mime/pom.xml?rev=1200281&r1=1200280&r2=1200281&view=diff
==============================================================================
--- sling/trunk/bundles/commons/mime/pom.xml (original)
+++ sling/trunk/bundles/commons/mime/pom.xml Thu Nov 10 11:54:10 2011
@@ -66,6 +66,7 @@
                         <Import-Package>
                             javax.servlet.*;resolution:=optional,
                             org.apache.felix.webconsole;resolution:=optional,
+                            org.apache.tika.*;resolution:=optional,
                             *
                         </Import-Package>
                     </instructions>
@@ -91,6 +92,7 @@
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.commons.osgi</artifactId>
             <version>2.0.2-incubator</version>
+            <scope>provided</scope>
         </dependency>
         <dependency>
             <groupId>org.osgi</groupId>
@@ -101,6 +103,12 @@
             <artifactId>org.osgi.compendium</artifactId>
         </dependency>
         <dependency>
+            <groupId>org.apache.tika</groupId>
+            <artifactId>tika-core</artifactId>
+            <version>1.0</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
             <groupId>javax.servlet</groupId>
             <artifactId>servlet-api</artifactId>
         </dependency>

Added: sling/trunk/bundles/commons/mime/src/main/java/org/apache/sling/commons/mime/internal/TikaMimeTypeProvider.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/mime/src/main/java/org/apache/sling/commons/mime/internal/TikaMimeTypeProvider.java?rev=1200281&view=auto
==============================================================================
--- sling/trunk/bundles/commons/mime/src/main/java/org/apache/sling/commons/mime/internal/TikaMimeTypeProvider.java
(added)
+++ sling/trunk/bundles/commons/mime/src/main/java/org/apache/sling/commons/mime/internal/TikaMimeTypeProvider.java
Thu Nov 10 11:54:10 2011
@@ -0,0 +1,63 @@
+/*
+ * 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.sling.commons.mime.internal;
+
+import org.apache.sling.commons.mime.MimeTypeProvider;
+import org.apache.tika.Tika;
+import org.apache.tika.mime.MimeType;
+import org.apache.tika.mime.MimeTypeException;
+import org.apache.tika.mime.MimeTypes;
+
+/**
+ * MIME type provider based on Apache Tika.
+ *
+ * @scr.component
+ * @scr.service interface="org.apache.sling.commons.mime.MimeTypeProvider"
+ * @scr.property name="service.description"
+ *               value="Apache Tika MIME Type Provider"
+ */
+public class TikaMimeTypeProvider implements MimeTypeProvider {
+
+    private final Tika tika = new Tika();
+
+    private final MimeTypes types = MimeTypes.getDefaultMimeTypes();
+
+    public String getMimeType(String name) {
+        String type = tika.detect(name);
+        if ("application/octet-stream".equals(type)) {
+            return null;
+        }
+
+        return type;
+    }
+
+    public String getExtension(String mimeType) {
+        try {
+            MimeType type = types.forName(mimeType);
+            String extension = type.getExtension();
+            if (extension != null && extension.length() > 1) {
+                return extension.substring(1); // skip leading "."
+            }
+        } catch (MimeTypeException e) {
+            // ignore
+        }
+
+        // fall back
+        return null;
+    }
+
+}

Added: sling/trunk/bundles/commons/mime/src/test/java/org/apache/sling/commons/mime/internal/TikaMimeTypeProviderTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/mime/src/test/java/org/apache/sling/commons/mime/internal/TikaMimeTypeProviderTest.java?rev=1200281&view=auto
==============================================================================
--- sling/trunk/bundles/commons/mime/src/test/java/org/apache/sling/commons/mime/internal/TikaMimeTypeProviderTest.java
(added)
+++ sling/trunk/bundles/commons/mime/src/test/java/org/apache/sling/commons/mime/internal/TikaMimeTypeProviderTest.java
Thu Nov 10 11:54:10 2011
@@ -0,0 +1,42 @@
+/*
+ * 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.sling.commons.mime.internal;
+
+import junit.framework.TestCase;
+
+import org.apache.sling.commons.mime.MimeTypeProvider;
+
+/**
+ * Unit tests for the {@link TikaMimeTypeProvider} class.
+ */
+public class TikaMimeTypeProviderTest extends TestCase {
+
+    public void testGetMimeType() {
+        MimeTypeProvider provider = new TikaMimeTypeProvider();
+        assertEquals("text/plain", provider.getMimeType("test.txt"));
+        assertEquals("application/pdf", provider.getMimeType("test.pdf"));
+        assertEquals("image/jpeg", provider.getMimeType("test.jpg"));
+    }
+
+    public void testGetExtension() {
+        MimeTypeProvider provider = new TikaMimeTypeProvider();
+        assertEquals("txt", provider.getExtension("text/plain"));
+        assertEquals("pdf", provider.getExtension("application/pdf"));
+        assertEquals("jpg", provider.getExtension("image/jpeg"));
+    }
+
+}
\ No newline at end of file



Mime
View raw message