This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.commons.mime-2.0.4-incubator in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-mime.git
commit 04b5e141316f06dc1982a195471df43eb201b7e3 Author: Felix Meschberger <[email protected]> AuthorDate: Thu Jun 18 09:21:02 2009 +0000 Move Sling to new TLP location git-svn-id: https://svn.eu.apache.org/repos/asf/sling/tags/org.apache.sling.commons.mime-2.0.4-incubator@785979 13f79535-47bb-0310-9956-ffa450edef68 --- LICENSE | 202 +++++++++++++++++ NOTICE | 8 + README.txt | 36 +++ pom.xml | 95 ++++++++ .../sling/commons/mime/MimeTypeProvider.java | 62 ++++++ .../apache/sling/commons/mime/MimeTypeService.java | 68 ++++++ .../commons/mime/internal/MimeTypeServiceImpl.java | 247 +++++++++++++++++++++ src/main/resources/META-INF/DISCLAIMER | 7 + src/main/resources/META-INF/LICENSE | 202 +++++++++++++++++ src/main/resources/META-INF/NOTICE | 8 + src/main/resources/META-INF/mime.types | 133 +++++++++++ .../mime/internal/MimeTypeServiceImplTest.java | 189 ++++++++++++++++ 12 files changed, 1257 insertions(+) diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed 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. diff --git a/NOTICE b/NOTICE new file mode 100644 index 0000000..6c93777 --- /dev/null +++ b/NOTICE @@ -0,0 +1,8 @@ +Apache Sling Mime Library +Copyright 2008-2009 The Apache Software Foundation + +Apache Sling is based on source code originally developed +by Day Software (http://www.day.com/). + +This product includes software developed at +The Apache Software Foundation (http://www.apache.org/). diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..70d25e3 --- /dev/null +++ b/README.txt @@ -0,0 +1,36 @@ +Apache Sling Mime Library + +Support for configurable MIME type mapping and querying. + +Disclaimer +========== +Apache Sling is an effort undergoing incubation at The Apache Software Foundation (ASF), +sponsored by the Apache Jackrabbit PMC. Incubation is required of all newly accepted +projects until a further review indicates that the infrastructure, communications, +and decision making process have stabilized in a manner consistent with other +successful ASF projects. While incubation status is not necessarily a reflection of +the completeness or stability of the code, it does indicate that the project has yet +to be fully endorsed by the ASF. + +Getting Started +=============== + +This component uses a Maven 2 (http://maven.apache.org/) build +environment. It requires a Java 5 JDK (or higher) and Maven (http://maven.apache.org/) +2.0.7 or later. We recommend to use the latest Maven version. + +If you have Maven 2 installed, you can compile and +package the jar using the following command: + + mvn package + +See the Maven 2 documentation for other build features. + +The latest source code for this component is available in the +Subversion (http://subversion.tigris.org/) source repository of +the Apache Software Foundation. If you have Subversion installed, +you can checkout the latest source using the following command: + + svn checkout http://svn.apache.org/repos/asf/incubator/sling/trunk/commons/mime + +See the Subversion documentation for other source control features. diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..4874d68 --- /dev/null +++ b/pom.xml @@ -0,0 +1,95 @@ +<?xml version="1.0" encoding="ISO-8859-1"?> +<!-- + 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. +--> +<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"> + + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.apache.sling</groupId> + <artifactId>sling</artifactId> + <version>5-incubator</version> + <relativePath>../../../parent/pom.xml</relativePath> + </parent> + + <artifactId>org.apache.sling.commons.mime</artifactId> + <packaging>bundle</packaging> + <version>2.0.4-incubator</version> + + <name>Apache Sling MIME type mapping support</name> + <description> + Support for configurable MIME type mapping and querying + </description> + + <scm> + <connection>scm:svn:http://svn.apache.org/repos/asf/incubator/sling/tags/org.apache.sling.commons.mime-2.0.4-incubator</connection> + <developerConnection>scm:svn:https://svn.apache.org/repos/asf/incubator/sling/tags/org.apache.sling.commons.mime-2.0.4-incubator</developerConnection> + <url>http://svn.apache.org/viewvc/incubator/sling/tags/org.apache.sling.commons.mime-2.0.4-incubator</url> + </scm> + + <build> + <plugins> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-scr-plugin</artifactId> + </plugin> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-bundle-plugin</artifactId> + <extensions>true</extensions> + <configuration> + <instructions> + <Export-Package> + org.apache.sling.commons.mime;version=${pom.version} + </Export-Package> + <Private-Package> + org.apache.sling.commons.mime.internal + </Private-Package> + </instructions> + </configuration> + </plugin> + </plugins> + </build> + <reporting> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-javadoc-plugin</artifactId> + <configuration> + <excludePackageNames> + org.apache.sling.commons.mime.internal + </excludePackageNames> + </configuration> + </plugin> + </plugins> + </reporting> + <dependencies> + <dependency> + <groupId>org.apache.felix</groupId> + <artifactId>org.osgi.core</artifactId> + </dependency> + <dependency> + <groupId>org.apache.felix</groupId> + <artifactId>org.osgi.compendium</artifactId> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + </dependency> + </dependencies> +</project> diff --git a/src/main/java/org/apache/sling/commons/mime/MimeTypeProvider.java b/src/main/java/org/apache/sling/commons/mime/MimeTypeProvider.java new file mode 100644 index 0000000..d5c728c --- /dev/null +++ b/src/main/java/org/apache/sling/commons/mime/MimeTypeProvider.java @@ -0,0 +1,62 @@ +/* + * 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; + +/** + * The <code>MimeTypeProvider</code> TODO + * <p> + * This interface may be implemented by bundles wishing to provide control over + * how extensions are mapped to MIME types and vice-versa. + */ +public interface MimeTypeProvider { + + /** + * Returns the MIME type of the extension of the given <code>name</code>. + * The extension is the part of the name after the last dot. If the name + * does not contain a dot, the name as a whole is assumed to be the + * extension. + * + * @param name The name for which the MIME type is to be returned. + * @return The MIME type for the extension of the name. If the extension + * cannot be mapped to a MIME type or <code>name</code> is + * <code>null</code>, <code>null</code> is returned. + * @see #getExtension(String) + */ + String getMimeType(String name); + + /** + * Returns the primary name extension to which the given + * <code>mimeType</code> maps. The returned extension must map to the + * given <code>mimeType</code> when fed to the + * {@link #getMimeType(String)} method. In other words, the expression + * <code>mimeType.equals(getMimeType(getExtension(mimeType)))</code> must + * always be <code>true</code> for any non-<code>null</code> MIME type. + * <p> + * A MIME type may be mapped to multiple extensions (e.g. + * <code>text/plain</code> to <code>txt</code>, <code>log</code>, + * ...). This method is expected to returned one of those extensions. It is + * up to the implementation to select an appropriate extension if multiple + * mappings exist for a single MIME type. + * + * @param mimeType The MIME type whose primary extension is requested. + * @return A extension which maps to the given MIME type or + * <code>null</code> if no such mapping exists. + * @see #getMimeType(String) + */ + String getExtension(String mimeType); + +} diff --git a/src/main/java/org/apache/sling/commons/mime/MimeTypeService.java b/src/main/java/org/apache/sling/commons/mime/MimeTypeService.java new file mode 100644 index 0000000..1d17c53 --- /dev/null +++ b/src/main/java/org/apache/sling/commons/mime/MimeTypeService.java @@ -0,0 +1,68 @@ +/* + * 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; + +import java.io.IOException; +import java.io.InputStream; + +/** + * The <code>MimeTypeService</code> TODO + * <p> + * This interface is not intended to be implemented by bundles. It is + * implemented by this bundle and may be used by client bundles. + */ +public interface MimeTypeService { + + /** + * Returns the MIME type of the extension of the given <code>name</code>. + * The extension is the part of the name after the last dot. If the name + * does not contain a dot, the name as a whole is assumed to be the + * extension. + * + * @param name The name for which the MIME type is to be returned. + * @return The MIME type for the extension of the name. If the extension + * cannot be mapped to a MIME type or <code>name</code> is + * <code>null</code>, <code>null</code> is returned. + * @see #getExtension(String) + */ + String getMimeType(String name); + + /** + * Returns the primary name extension to which the given + * <code>mimeType</code> maps. The returned extension must map to the + * given <code>mimeType</code> when fed to the + * {@link #getMimeType(String)} method. In other words, the expression + * <code>mimeType.equals(getMimeType(getExtension(mimeType)))</code> must + * always be <code>true</code> for any non-<code>null</code> MIME type. + * <p> + * A MIME type may be mapped to multiple extensions (e.g. + * <code>text/plain</code> to <code>txt</code>, <code>log</code>, + * ...). This method is expected to returned one of those extensions. It is + * up to the implementation to select an appropriate extension if multiple + * mappings exist for a single MIME type. + * + * @param mimeType The MIME type whose primary extension is requested. + * @return A extension which maps to the given MIME type or + * <code>null</code> if no such mapping exists. + * @see #getMimeType(String) + */ + String getExtension(String mimeType); + + void registerMimeType(String mimeType, String... extensions); + + void registerMimeType(InputStream mimeTabStream) throws IOException; +} diff --git a/src/main/java/org/apache/sling/commons/mime/internal/MimeTypeServiceImpl.java b/src/main/java/org/apache/sling/commons/mime/internal/MimeTypeServiceImpl.java new file mode 100644 index 0000000..4dc5563 --- /dev/null +++ b/src/main/java/org/apache/sling/commons/mime/internal/MimeTypeServiceImpl.java @@ -0,0 +1,247 @@ +/* + * 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 java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.PrintStream; +import java.net.URL; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.sling.commons.mime.MimeTypeProvider; +import org.apache.sling.commons.mime.MimeTypeService; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleEvent; +import org.osgi.framework.BundleListener; +import org.osgi.service.component.ComponentContext; +import org.osgi.service.log.LogService; + +/** + * The <code>MimeTypeServiceImpl</code> TODO + * + * @scr.component immediate="false" metatype="no" + * @scr.property name="service.vendor" value="The Apache Software Foundation" + * @scr.property name="service.description" value="Sling Servlet" + * @scr.reference name="MimeTypeProvider" + * interface="org.apache.sling.commons.mime.MimeTypeProvider" + * cardinality="0..n" policy="dynamic" + * @scr.service interface="org.apache.sling.commons.mime.MimeTypeService" + */ +public class MimeTypeServiceImpl implements MimeTypeService, BundleListener { + + public static final String MIME_TYPES = "/META-INF/mime.types"; + + /** @scr.reference cardinality="0..1" policy="dynamic" */ + private LogService logService; + + private Map<String, String> mimeTab = new HashMap<String, String>(); + + private Map<String, String> extensionMap = new HashMap<String, String>(); + + private MimeTypeProvider[] typeProviders; + + private List<MimeTypeProvider> typeProviderList = new ArrayList<MimeTypeProvider>(); + + /** + * @see org.apache.sling.commons.mime.MimeTypeService#getMimeType(java.lang.String) + */ + public String getMimeType(String name) { + if (name == null) { + return null; + } + + String ext = name.substring(name.lastIndexOf('.') + 1); + ext = ext.toLowerCase(); + + String type = this.mimeTab.get(ext); + if (type == null) { + MimeTypeProvider[] mtp = this.getMimeTypeProviders(); + for (int i = 0; type == null && i < mtp.length; i++) { + type = mtp[i].getMimeType(ext); + } + } + + return type; + } + + /** + * @see org.apache.sling.commons.mime.MimeTypeService#getExtension(java.lang.String) + */ + public String getExtension(String mimeType) { + if (mimeType == null) { + return null; + } + + // compare using lowercase only + mimeType = mimeType.toLowerCase(); + + String ext = this.extensionMap.get(mimeType); + if (ext == null) { + MimeTypeProvider[] mtp = this.getMimeTypeProviders(); + for (int i = 0; ext == null && i < mtp.length; i++) { + ext = mtp[i].getExtension(mimeType); + } + } + return ext; + } + + public void registerMimeType(String mimeType, String... extensions) { + if (mimeType == null || mimeType.length() == 0 || extensions == null + || extensions.length == 0) { + return; + } + + mimeType = mimeType.toLowerCase(); + + String defaultExtension = null; + for (int i = 0; i < extensions.length; i++) { + if (extensions[i] != null && extensions[i].length() > 0) { + extensions[i] = extensions[i].toLowerCase(); + + this.mimeTab.put(extensions[i], mimeType); + + if (defaultExtension == null) { + defaultExtension = extensions[i]; + } + } + } + + if (defaultExtension != null) { + this.extensionMap.put(mimeType, defaultExtension); + } + } + + public void registerMimeType(InputStream mimeTabStream) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader( + mimeTabStream, "ISO-8859-1")); + + String line; + while ((line = br.readLine()) != null) { + + // ignore comment lines + if (line.startsWith("#")) { + continue; + } + + String[] parts = line.split("\\s+"); + if (parts.length > 1) { + String[] extensions = new String[parts.length - 1]; + System.arraycopy(parts, 1, extensions, 0, extensions.length); + this.registerMimeType(parts[0], extensions); + } + } + } + + // ---------- internal ----------------------------------------------------- + + private MimeTypeProvider[] getMimeTypeProviders() { + MimeTypeProvider[] list = this.typeProviders; + + if (list == null) { + synchronized (this.typeProviderList) { + this.typeProviders = this.typeProviderList.toArray(new MimeTypeProvider[this.typeProviderList.size()]); + list = this.typeProviders; + } + } + + return list; + } + + private void handleBundle(Bundle bundle) { + URL mimes = bundle.getEntry(MIME_TYPES); + if (mimes != null) { + InputStream ins = null; + try { + ins = mimes.openStream(); + this.registerMimeType(ins); + } catch (IOException ioe) { + // log but don't actually care + this.log(LogService.LOG_WARNING, "An error occurred reading " + + mimes, ioe); + } finally { + if (ins != null) { + try { + ins.close(); + } catch (IOException ioe) { + // ignore + } + } + } + } + } + + private void log(int level, String message, Throwable t) { + LogService log = this.logService; + if (log != null) { + log.log(level, message, t); + } else { + PrintStream out = (level == LogService.LOG_ERROR) + ? System.err + : System.out; + out.println(message); + if (t != null) { + t.printStackTrace(out); + } + } + } + + // ---------- SCR implementation ------------------------------------------- + + protected void activate(ComponentContext context) { + context.getBundleContext().addBundleListener(this); + + // register maps of existing bundles + Bundle[] bundles = context.getBundleContext().getBundles(); + for (int i = 0; i < bundles.length; i++) { + if ((bundles[i].getState() & (Bundle.RESOLVED | Bundle.STARTING + | Bundle.ACTIVE | Bundle.STOPPING)) != 0) { + this.handleBundle(bundles[i]); + } + } + } + + protected void deactivate(ComponentContext context) { + context.getBundleContext().removeBundleListener(this); + } + + protected void bindMimeTypeProvider(MimeTypeProvider mimeTypeProvider) { + synchronized (this.typeProviderList) { + this.typeProviderList.add(mimeTypeProvider); + this.typeProviders = null; + } + } + + protected void unbindMimeTypeProvider(MimeTypeProvider mimeTypeProvider) { + synchronized (this.typeProviderList) { + this.typeProviderList.remove(mimeTypeProvider); + this.typeProviders = null; + } + } + + // ---------- BundleListener ----------------------------------------------- + + public void bundleChanged(BundleEvent event) { + if (event.getType() == BundleEvent.RESOLVED) { + this.handleBundle(event.getBundle()); + } + } +} diff --git a/src/main/resources/META-INF/DISCLAIMER b/src/main/resources/META-INF/DISCLAIMER new file mode 100644 index 0000000..90850c2 --- /dev/null +++ b/src/main/resources/META-INF/DISCLAIMER @@ -0,0 +1,7 @@ +Apache Sling is an effort undergoing incubation at The Apache Software Foundation (ASF), +sponsored by the Apache Jackrabbit PMC. Incubation is required of all newly accepted +projects until a further review indicates that the infrastructure, communications, +and decision making process have stabilized in a manner consistent with other +successful ASF projects. While incubation status is not necessarily a reflection of +the completeness or stability of the code, it does indicate that the project has yet +to be fully endorsed by the ASF. \ No newline at end of file diff --git a/src/main/resources/META-INF/LICENSE b/src/main/resources/META-INF/LICENSE new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/src/main/resources/META-INF/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed 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. diff --git a/src/main/resources/META-INF/NOTICE b/src/main/resources/META-INF/NOTICE new file mode 100644 index 0000000..6c93777 --- /dev/null +++ b/src/main/resources/META-INF/NOTICE @@ -0,0 +1,8 @@ +Apache Sling Mime Library +Copyright 2008-2009 The Apache Software Foundation + +Apache Sling is based on source code originally developed +by Day Software (http://www.day.com/). + +This product includes software developed at +The Apache Software Foundation (http://www.apache.org/). diff --git a/src/main/resources/META-INF/mime.types b/src/main/resources/META-INF/mime.types new file mode 100644 index 0000000..06730b9 --- /dev/null +++ b/src/main/resources/META-INF/mime.types @@ -0,0 +1,133 @@ +# +# 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. +# + +# +# A simple, old format, mime.types file +# +application/andrew-inset ez +application/compress z +application/java-archive jar +application/java-serialized-object ser +application/java-vm class +application/json json +application/mac-binhex40 hqx +application/mac-compactpro cpt +application/msexcel xls xlsx +application/msword doc docx +application/mspowerpoint ppt pptx +application/octet-stream bin class dms exe lha lzh msi +application/oda oda +application/pdf pdf +application/photoshop psd +application/postscript ps ai eps +application/rtf rtf +application/smil smi smil +application/vnd.mif mif +application/vnd.wap.wmlc wmlc +application/vnd.wap.wmlscriptc wmlsc +application/vnd.wap.wtls-ca-certificate wtls-ca-certificate +application/x-bcpio bcpio +application/x-bzip2 bz2 +application/x-cdlink vcd +application/x-chess-pgn pgn +application/x-cpio cpio +application/x-csh csh +application/x-director dcr dir dxr +application/x-dvi dvi +application/x-futuresplash spl +application/x-gtar gtar +application/x-gzip gz tgz +application/x-hdf hdf +application/x-java-jnlp-file jnlp +application/x-javascript js +application/x-koan skd skm skp skt +application/x-latex latex +application/x-netcdf cdf nc +application/x-quark-express qxd +application/x-rpm rpm +application/x-shar shar +application/x-shockwave-flash swf +application/x-sh sh +application/x-stuffit sit +application/x-sv4cpio sv4cpio +application/x-sv4crc sv4crc +application/x-tar tar +application/x-tcl tcl +application/x-texinfo texinfo texi +application/x-tex tex +application/x-troff-man man +application/x-troff-me me +application/x-troff-ms ms +application/x-troff tr t roff +application/x-ustar ustar +application/x-wais-source src +application/x-x509-ca-cert crt +application/zip zip +audio/basic au snd +audio/midi midi kar mid +audio/mpeg mp3 mp2 mpga +audio/x-aifc aifc +audio/x-aiff aif aiff +audio/x-mpeg mpeg mpg +audio/x-pn-realaudio ram rm +audio/x-realaudio ra +audio/x-vorbis ogg +audio/x-wav wav +chemical/x-mdl-molfile mol +chemical/x-pdb pdb xyz +image/bmp bmp +image/gif gif +image/ief ief +image/jpeg jpeg jpg jpe +image/pict pict +image/png png +image/tiff tiff tif +image/vnd.wap.wbmp wbmp +image/x-cmu-raster ras +image/x-portable-anymap pnm +image/x-portable-bitmap pbm +image/x-portable-graymap pgm +image/x-portable-pixmap ppm +image/x-rgb rgb +image/x-xbitmap xbm +image/x-xpixmap xpm +image/x-xwindowdump xwd +model/iges iges igs +model/mesh mesh msh silo +model/vrml vrml wrl +text/any any +text/css css +text/html html htm +text/plain txt asc log text apt esp ecma jsp +text/richtext rtx +text/rtf rtf +text/sgml sgml sgm +text/tab-separated-values tsv +text/vnd.wap.wml wml +text/vnd.wap.wmlscript wmls +text/xml xml +text/x-java-source java +text/x-setext etx +video/mpeg mpeg mpg mpe +video/quicktime qt mov +video/x.ms.asf asf +video/x.ms.asx asx +video/x-msvideo avi +video/x-sgi-movie movie +x-conference/x-cooltalk ice diff --git a/src/test/java/org/apache/sling/commons/mime/internal/MimeTypeServiceImplTest.java b/src/test/java/org/apache/sling/commons/mime/internal/MimeTypeServiceImplTest.java new file mode 100644 index 0000000..a4b6db3 --- /dev/null +++ b/src/test/java/org/apache/sling/commons/mime/internal/MimeTypeServiceImplTest.java @@ -0,0 +1,189 @@ +/* + * 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 java.io.IOException; +import java.io.InputStream; + +import org.apache.sling.commons.mime.MimeTypeProvider; +import org.apache.sling.commons.mime.internal.MimeTypeServiceImpl; + +import junit.framework.TestCase; + +/** + * The <code>MimeTypeServiceImplTest</code> TODO + */ +public class MimeTypeServiceImplTest extends TestCase { + + private static final String IMAGE_GIF = "image/gif"; + + private static final String GIF = "gif"; + + private static final String UNMAPPED_GIF = "unmapped_gif"; + + private static final String LOG = "log"; + + private static final String TXT = "txt"; + + private static final String APT = "apt"; + + private static final String TEXT_PLAIN = "text/plain"; + + private MimeTypeServiceImpl service; + + @Override + protected void setUp() throws Exception { + super.setUp(); + + this.service = new MimeTypeServiceImpl(); + } + + @Override + protected void tearDown() throws Exception { + this.service = null; + + super.tearDown(); + } + + public void testNoMapping() throws Exception { + assertNull(this.service.getMimeType("file." + TXT)); + assertNull(this.service.getMimeType(TXT)); + assertNull(this.service.getExtension(TEXT_PLAIN)); + } + + public void testTxtMapping() throws Exception { + + this.service.registerMimeType(TEXT_PLAIN, TXT, LOG, APT); + + final String [] exts = { TXT, LOG, APT }; + for(String ext : exts) { + assertEquals("Extension " + ext + " (1)", TEXT_PLAIN, this.service.getMimeType("file." + ext)); + assertEquals("Extension " + ext + " (2)", TEXT_PLAIN, this.service.getMimeType(ext)); + } + + assertEquals(TEXT_PLAIN, + this.service.getMimeType(("file." + TXT).toUpperCase())); + assertEquals(TEXT_PLAIN, this.service.getMimeType((TXT).toUpperCase())); + assertEquals(TEXT_PLAIN, + this.service.getMimeType(("file." + LOG).toUpperCase())); + assertEquals(TEXT_PLAIN, this.service.getMimeType((LOG).toUpperCase())); + + assertNull(this.service.getMimeType(GIF)); + + assertEquals(TXT, this.service.getExtension(TEXT_PLAIN)); + } + + public void testFileLoading() throws Exception { + InputStream ins = this.getClass().getResourceAsStream("/META-INF/mime.types"); + assertNotNull(ins); + + try { + this.service.registerMimeType(ins); + + final String [] exts = { TXT, LOG, APT }; + for(String ext : exts) { + assertEquals("Extension " + ext + " (1)", TEXT_PLAIN, this.service.getMimeType("file." + ext)); + assertEquals("Extension " + ext + " (2)", TEXT_PLAIN, this.service.getMimeType(ext)); + } + + assertEquals(TEXT_PLAIN, + this.service.getMimeType(("file." + TXT).toUpperCase())); + assertEquals(TEXT_PLAIN, this.service.getMimeType((TXT).toUpperCase())); + assertEquals(TEXT_PLAIN, + this.service.getMimeType(("file." + LOG).toUpperCase())); + assertEquals(TEXT_PLAIN, this.service.getMimeType((LOG).toUpperCase())); + + assertEquals(IMAGE_GIF, this.service.getMimeType(GIF)); + assertNull(this.service.getMimeType(UNMAPPED_GIF)); + + assertEquals(TXT, this.service.getExtension(TEXT_PLAIN)); + + } finally { + try { + ins.close(); + } catch (IOException ignore) { + } + } + } + + public void testProvider() throws Exception { + MimeTypeProvider mtp = this.createMimeTypeProvider(IMAGE_GIF, GIF); + + assertNull(this.service.getMimeType(GIF)); + assertNull(this.service.getExtension(IMAGE_GIF)); + + this.service.bindMimeTypeProvider(mtp); + + assertEquals(IMAGE_GIF, this.service.getMimeType(GIF)); + assertEquals(GIF, this.service.getExtension(IMAGE_GIF)); + + this.service.unbindMimeTypeProvider(mtp); + + assertNull(this.service.getMimeType(GIF)); + assertNull(this.service.getExtension(IMAGE_GIF)); + } + + public void testProvider2() throws Exception { + MimeTypeProvider mtp = this.createMimeTypeProvider(IMAGE_GIF, GIF); + + this.service.registerMimeType(IMAGE_GIF, UNMAPPED_GIF); + + assertEquals(IMAGE_GIF, this.service.getMimeType(UNMAPPED_GIF)); + assertEquals(UNMAPPED_GIF, this.service.getExtension(IMAGE_GIF)); + + assertNull(this.service.getMimeType(GIF)); + + this.service.bindMimeTypeProvider(mtp); + + assertEquals(IMAGE_GIF, this.service.getMimeType(UNMAPPED_GIF)); + assertEquals(UNMAPPED_GIF, this.service.getExtension(IMAGE_GIF)); + + assertEquals(IMAGE_GIF, this.service.getMimeType(GIF)); + + this.service.unbindMimeTypeProvider(mtp); + + assertEquals(IMAGE_GIF, this.service.getMimeType(UNMAPPED_GIF)); + assertEquals(UNMAPPED_GIF, this.service.getExtension(IMAGE_GIF)); + + assertNull(this.service.getMimeType(GIF)); + } + + private MimeTypeProvider createMimeTypeProvider(final String type, final String ext) { + return new MimeTypeProvider() { + public String getMimeType(String name) { + if (name == null) { + return null; + } else if (name.toLowerCase().endsWith(ext)) { + return type; + } else { + return null; + } + } + + public String getExtension(String mimeType) { + if (mimeType == null) { + return null; + } else if (mimeType.toLowerCase().equals(type)) { + return ext; + } else { + return null; + } + } + }; + + } +} -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
