Skip to content

Commit

Permalink
SLING-7872 Add MIME and extension mapping to MimeTypeService API
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverlietz committed Aug 30, 2018
1 parent 9a57f83 commit 13b5592
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 39 deletions.
11 changes: 11 additions & 0 deletions bnd.bnd
@@ -0,0 +1,11 @@
Bundle-DocURL: https://sling.apache.org/site/mime-type-support.html

Import-Package:\
javax.servlet.*;resolution:=optional,\
org.apache.felix.webconsole;resolution:=optional,\
org.apache.tika.*;resolution:=optional,\
*

-removeheaders:\
Include-Resource,\
Private-Package
47 changes: 24 additions & 23 deletions pom.xml
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<?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
Expand All @@ -17,19 +17,18 @@
specific language governing permissions and limitations
under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.sling</groupId>
<artifactId>sling</artifactId>
<version>30</version>
<version>34</version>
<relativePath />
</parent>

<artifactId>org.apache.sling.commons.mime</artifactId>
<packaging>bundle</packaging>
<version>2.1.11-SNAPSHOT</version>
<version>2.2.0-SNAPSHOT</version>

<name>Apache Sling Commons MIME type mapping support</name>
<description>
Expand All @@ -45,22 +44,12 @@
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-DocURL>
http://sling.apache.org/site/mime-type-support.html
</Bundle-DocURL>
<Import-Package>
javax.servlet.*;resolution:=optional,
org.apache.felix.webconsole;resolution:=optional,
org.apache.tika.*;resolution:=optional,
*
</Import-Package>
</instructions>
</configuration>
<groupId>biz.aQute.bnd</groupId>
<artifactId>bnd-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>biz.aQute.bnd</groupId>
<artifactId>bnd-baseline-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.rat</groupId>
Expand Down Expand Up @@ -88,16 +77,28 @@
</plugins>
</reporting>
<dependencies>
<!-- OSGi -->
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.annotation.versioning</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>osgi.core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.service.component.annotations</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.service.log</artifactId>
<version>1.3.0</version>
<artifactId>org.osgi.service.metatype.annotations</artifactId>
<scope>provided</scope>
</dependency>
<!-- ... -->
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-core</artifactId>
Expand Down
Expand Up @@ -18,6 +18,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import org.osgi.annotation.versioning.ProviderType;

Expand Down Expand Up @@ -98,4 +99,9 @@ public interface MimeTypeService {
* @throws IOException If an error occurs reading from the stream
*/
void registerMimeType(InputStream mimeTabStream) throws IOException;

Map<String, String> getMimeMap();

Map<String, String> getExtensionMap();

}
Expand Up @@ -52,7 +52,7 @@
@Component(service = MimeTypeService.class,
property = {
Constants.SERVICE_VENDOR + "=The Apache Software Foundation",
Constants.SERVICE_DESCRIPTION + "=Apache Sling MIME Type Service"
Constants.SERVICE_DESCRIPTION + "=Apache Sling Commons MIME Type Service"
})
@Designate(ocd = MimeTypeServiceImpl.Config.class)
public class MimeTypeServiceImpl implements MimeTypeService, BundleListener {
Expand All @@ -61,8 +61,8 @@ public class MimeTypeServiceImpl implements MimeTypeService, BundleListener {

public static final String MIME_TYPES = "/META-INF/mime.types";

@ObjectClassDefinition(name = "Apache Sling MIME Type Service",
description = "The Sling MIME Type Service provides support for " +
@ObjectClassDefinition(name = "Apache Sling Commons MIME Type Service",
description = "The Sling Commons MIME Type Service provides support for " +
"maintaining and configuring MIME Type mappings.")
public @interface Config {

Expand All @@ -79,7 +79,7 @@ public class MimeTypeServiceImpl implements MimeTypeService, BundleListener {

private Logger log = LoggerFactory.getLogger(this.getClass());

private Map<String, String> mimeTab = new HashMap<>();
private Map<String, String> mimeMap = new HashMap<>();

private Map<String, String> extensionMap = new HashMap<>();

Expand All @@ -98,7 +98,7 @@ public String getMimeType(String name) {
String ext = name.substring(name.lastIndexOf('.') + 1);
ext = ext.toLowerCase();

String type = this.mimeTab.get(ext);
String type = this.mimeMap.get(ext);
if (type == null) {
MimeTypeProvider[] mtp = this.getMimeTypeProviders();
for (int i = 0; type == null && i < mtp.length; i++) {
Expand Down Expand Up @@ -143,13 +143,13 @@ public void registerMimeType(String mimeType, String... extensions) {
if (extension != null && extension.length() > 0) {
extension = extension.toLowerCase();

String oldMimeType = mimeTab.get(extension);
String oldMimeType = mimeMap.get(extension);
if (oldMimeType == null) {

log.debug("registerMimeType: Add mapping "
+ extension + "=" + mimeType);

this.mimeTab.put(extension, mimeType);
this.mimeMap.put(extension, mimeType);

if (defaultExtension == null) {
defaultExtension = extension;
Expand Down Expand Up @@ -253,11 +253,11 @@ public void bundleChanged(BundleEvent event) {

// ---------- plugin support -----------------------------------------------

Map<String, String> getMimeMap() {
return mimeTab;
public Map<String, String> getMimeMap() {
return mimeMap;
}

Map<String, String> getExtensionMap() {
public Map<String, String> getExtensionMap() {
return extensionMap;
}

Expand Down
Expand Up @@ -23,15 +23,16 @@
import java.net.URL;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import java.util.Map.Entry;

import javax.servlet.Servlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.sling.commons.mime.MimeTypeService;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

Expand All @@ -58,7 +59,7 @@ class MimeTypeWebConsolePlugin extends HttpServlet {
static final String CSS_REFS = RES_LOC + "/jquery.treeTable.css";

@Reference
private MimeTypeServiceImpl mimeTypeService;
private MimeTypeService mimeTypeService;

@Override
protected void doGet(HttpServletRequest request,
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/org/apache/sling/commons/mime/package-info.java
Expand Up @@ -24,10 +24,8 @@
* interface for providers to extend the MIME type setup provided by the
* {@link org.apache.sling.commons.mime.MimeTypeService}.
*
* @version 2.1.5
*/
@org.osgi.annotation.versioning.Version("2.1.5")
@Version("2.2.0")
package org.apache.sling.commons.mime;



import org.osgi.annotation.versioning.Version;

0 comments on commit 13b5592

Please sign in to comment.