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.2-incubator in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-mime.git
commit f334b7207945a78d525414914fb7ee89f043e89b Author: Felix Meschberger <[email protected]> AuthorDate: Mon Sep 10 08:50:41 2007 +0000 Import initial Sling source git-svn-id: https://svn.apache.org/repos/asf/incubator/sling/trunk/mime@574179 13f79535-47bb-0310-9956-ffa450edef68 --- pom.xml | 80 +++++++ .../org/apache/sling/mime/MimeTypeProvider.java | 60 +++++ .../org/apache/sling/mime/MimeTypeService.java | 66 ++++++ .../sling/mime/internal/MimeTypeServiceImpl.java | 249 +++++++++++++++++++++ src/main/resources/META-INF/LICENSE | 202 +++++++++++++++++ src/main/resources/META-INF/NOTICE | 8 + src/main/resources/META-INF/mime.types | 92 ++++++++ .../mime/internal/MimeTypeServiceImplTest.java | 182 +++++++++++++++ 8 files changed, 939 insertions(+) diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..8a9a5b3 --- /dev/null +++ b/pom.xml @@ -0,0 +1,80 @@ +<?xml version="1.0" encoding="ISO-8859-1"?> +<!-- + Copyright 2007 The Apache Software Foundation. + + 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. +--> +<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>1-incubator-SNAPSHOT</version> + <relativePath>../parent/pom.xml</relativePath> + </parent> + + <artifactId>sling-mime</artifactId> + <packaging>bundle</packaging> + <version>2.0.0-incubator-SNAPSHOT</version> + + <name>Sling - MIME type mapping support</name> + <description> + Support for configurable MIME type mapping and querying + </description> + + <scm> + <url>http://svn.day.com/repos/sling/trunk/mime</url> + <connection>scm:svn:${scm.url}</connection> + <developerConnection>scm:svn:${scm.url}</developerConnection> + </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.mime + </Export-Package> + <Private-Package> + org.apache.sling.mime.internal + </Private-Package> + </instructions> + </configuration> + </plugin> + </plugins> + </build> + <dependencies> + <dependency> + <groupId>org.osgi</groupId> + <artifactId>osgi_R4_core</artifactId> + </dependency> + <dependency> + <groupId>org.osgi</groupId> + <artifactId>osgi_R4_compendium</artifactId> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + </dependency> + </dependencies> +</project> diff --git a/src/main/java/org/apache/sling/mime/MimeTypeProvider.java b/src/main/java/org/apache/sling/mime/MimeTypeProvider.java new file mode 100644 index 0000000..90b5631 --- /dev/null +++ b/src/main/java/org/apache/sling/mime/MimeTypeProvider.java @@ -0,0 +1,60 @@ +/* + * $Url: $ + * $Id: $ + * + * Copyright 1997-2005 Day Management AG + * Barfuesserplatz 6, 4001 Basel, Switzerland + * All Rights Reserved. + * + * This software is the confidential and proprietary information of + * Day Management AG, ("Confidential Information"). You shall not + * disclose such Confidential Information and shall use it only in + * accordance with the terms of the license agreement you entered into + * with Day. + */ +package org.apache.sling.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/mime/MimeTypeService.java b/src/main/java/org/apache/sling/mime/MimeTypeService.java new file mode 100644 index 0000000..031849e --- /dev/null +++ b/src/main/java/org/apache/sling/mime/MimeTypeService.java @@ -0,0 +1,66 @@ +/* + * $Url: $ + * $Id: $ + * + * Copyright 1997-2005 Day Management AG + * Barfuesserplatz 6, 4001 Basel, Switzerland + * All Rights Reserved. + * + * This software is the confidential and proprietary information of + * Day Management AG, ("Confidential Information"). You shall not + * disclose such Confidential Information and shall use it only in + * accordance with the terms of the license agreement you entered into + * with Day. + */ +package org.apache.sling.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/mime/internal/MimeTypeServiceImpl.java b/src/main/java/org/apache/sling/mime/internal/MimeTypeServiceImpl.java new file mode 100644 index 0000000..add0df6 --- /dev/null +++ b/src/main/java/org/apache/sling/mime/internal/MimeTypeServiceImpl.java @@ -0,0 +1,249 @@ +/* + * $Url: $ + * $Id: $ + * + * Copyright 1997-2005 Day Management AG + * Barfuesserplatz 6, 4001 Basel, Switzerland + * All Rights Reserved. + * + * This software is the confidential and proprietary information of + * Day Management AG, ("Confidential Information"). You shall not + * disclose such Confidential Information and shall use it only in + * accordance with the terms of the license agreement you entered into + * with Day. + */ +package org.apache.sling.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.mime.MimeTypeProvider; +import org.apache.sling.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="false" + * @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.mime.MimeTypeProvider" + * cardinality="0..n" policy="dynamic" + * @scr.service interface="org.apache.sling.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>(); + + /* + * (non-Javadoc) + * + * @see org.apache.sling.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 = mimeTab.get(ext); + if (type == null) { + MimeTypeProvider[] mtp = getMimeTypeProviders(); + for (int i = 0; type == null && i < mtp.length; i++) { + type = mtp[i].getMimeType(ext); + } + } + + return type; + } + + /* + * (non-Javadoc) + * + * @see org.apache.sling.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 = extensionMap.get(mimeType); + if (ext == null) { + MimeTypeProvider[] mtp = 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(); + + mimeTab.put(extensions[i], mimeType); + + if (defaultExtension == null) { + defaultExtension = extensions[i]; + } + } + } + + if (defaultExtension != null) { + 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); + registerMimeType(parts[0], extensions); + } + } + } + + // ---------- internal ----------------------------------------------------- + + private MimeTypeProvider[] getMimeTypeProviders() { + MimeTypeProvider[] list = typeProviders; + + if (list == null) { + synchronized (typeProviderList) { + typeProviders = typeProviderList.toArray(new MimeTypeProvider[typeProviderList.size()]); + list = typeProviders; + } + } + + return list; + } + + private void handleBundle(Bundle bundle) { + URL mimes = bundle.getEntry(MIME_TYPES); + if (mimes != null) { + InputStream ins = null; + try { + ins = mimes.openStream(); + registerMimeType(ins); + } catch (IOException ioe) { + // log but don't actually care + 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 = 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) { + handleBundle(bundles[i]); + } + } + } + + protected void deactivate(ComponentContext context) { + context.getBundleContext().removeBundleListener(this); + } + + protected void bindMimeTypeProvider(MimeTypeProvider mimeTypeProvider) { + synchronized (typeProviderList) { + typeProviderList.add(mimeTypeProvider); + typeProviders = null; + } + } + + protected void unbindMimeTypeProvider(MimeTypeProvider mimeTypeProvider) { + synchronized (typeProviderList) { + typeProviderList.remove(mimeTypeProvider); + typeProviders = null; + } + } + + // ---------- BundleListener ----------------------------------------------- + + public void bundleChanged(BundleEvent event) { + if (event.getType() == BundleEvent.RESOLVED) { + handleBundle(event.getBundle()); + } + } +} 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..4894d83 --- /dev/null +++ b/src/main/resources/META-INF/NOTICE @@ -0,0 +1,8 @@ +Apache Sling +Copyright 2007 The Apache Software Foundation + +This product includes software developed at +The Apache Software Foundation (http://www.apache.org/). + +Based on source code originally developed by +Day Software (http://www.day.com/). diff --git a/src/main/resources/META-INF/mime.types b/src/main/resources/META-INF/mime.types new file mode 100644 index 0000000..d4f7f43 --- /dev/null +++ b/src/main/resources/META-INF/mime.types @@ -0,0 +1,92 @@ +# +# A simple, old format, mime.types file +# +application/andrew-inset ez +application/mac-binhex40 hqx +application/mac-compactpro cpt +application/msexcel xls xlsx +application/msword doc docx +application/octet-stream bin class dms exe lha lzh +application/oda oda +application/pdf pdf +application/postscript ps ai eps +application/rtf rtf +application/smil smi smil +application/vnd.mif mif +application/vnd.ms-powerpoint ppt pptx +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-javascript js +application/x-koan skd skm skp skt +application/x-latex latex +application/x-netcdf cdf nc +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/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-wav wav +chemical/x-pdb pdb xyz +image/gif gif +image/ief ief +image/jpeg jpeg jpg jpe +image/png png +image/tiff tiff tif +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 +text/richtext rtx +text/rtf rtf +text/sgml sgml sgm +text/tab-separated-values tsv +text/xml xml +text/x-setext etx +video/mpeg mpeg mpg mpe +video/quicktime qt mov +video/x-msvideo avi +video/x-sgi-movie movie +x-conference/x-cooltalk ice diff --git a/src/test/java/org/apache/sling/mime/internal/MimeTypeServiceImplTest.java b/src/test/java/org/apache/sling/mime/internal/MimeTypeServiceImplTest.java new file mode 100644 index 0000000..83bff38 --- /dev/null +++ b/src/test/java/org/apache/sling/mime/internal/MimeTypeServiceImplTest.java @@ -0,0 +1,182 @@ +/* + * $Url: $ + * $Id: $ + * + * Copyright 1997-2005 Day Management AG + * Barfuesserplatz 6, 4001 Basel, Switzerland + * All Rights Reserved. + * + * This software is the confidential and proprietary information of + * Day Management AG, ("Confidential Information"). You shall not + * disclose such Confidential Information and shall use it only in + * accordance with the terms of the license agreement you entered into + * with Day. + */ +package org.apache.sling.mime.internal; + +import java.io.IOException; +import java.io.InputStream; + +import org.apache.sling.mime.MimeTypeProvider; + +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 TEXT_PLAIN = "text/plain"; + + private MimeTypeServiceImpl service; + + @Override + protected void setUp() throws Exception { + super.setUp(); + + service = new MimeTypeServiceImpl(); + } + + @Override + protected void tearDown() throws Exception { + service = null; + + super.tearDown(); + } + + public void testNoMapping() throws Exception { + assertNull(service.getMimeType("file." + TXT)); + assertNull(service.getMimeType(TXT)); + assertNull(service.getExtension(TEXT_PLAIN)); + } + + public void testTxtMapping() throws Exception { + + service.registerMimeType(TEXT_PLAIN, TXT, LOG); + + assertEquals(TEXT_PLAIN, service.getMimeType("file." + TXT)); + assertEquals(TEXT_PLAIN, service.getMimeType(TXT)); + assertEquals(TEXT_PLAIN, service.getMimeType("file." + LOG)); + assertEquals(TEXT_PLAIN, service.getMimeType(LOG)); + + assertEquals(TEXT_PLAIN, + service.getMimeType(("file." + TXT).toUpperCase())); + assertEquals(TEXT_PLAIN, service.getMimeType((TXT).toUpperCase())); + assertEquals(TEXT_PLAIN, + service.getMimeType(("file." + LOG).toUpperCase())); + assertEquals(TEXT_PLAIN, service.getMimeType((LOG).toUpperCase())); + + assertNull(service.getMimeType(GIF)); + + assertEquals(TXT, service.getExtension(TEXT_PLAIN)); + } + + public void testFileLoading() throws Exception { + InputStream ins = getClass().getResourceAsStream("/META-INF/mime.types"); + assertNotNull(ins); + + try { + service.registerMimeType(ins); + + assertEquals(TEXT_PLAIN, service.getMimeType("file." + TXT)); + assertEquals(TEXT_PLAIN, service.getMimeType(TXT)); + assertEquals(TEXT_PLAIN, service.getMimeType("file." + LOG)); + assertEquals(TEXT_PLAIN, service.getMimeType(LOG)); + + assertEquals(TEXT_PLAIN, + service.getMimeType(("file." + TXT).toUpperCase())); + assertEquals(TEXT_PLAIN, service.getMimeType((TXT).toUpperCase())); + assertEquals(TEXT_PLAIN, + service.getMimeType(("file." + LOG).toUpperCase())); + assertEquals(TEXT_PLAIN, service.getMimeType((LOG).toUpperCase())); + + assertEquals(IMAGE_GIF, service.getMimeType(GIF)); + assertNull(service.getMimeType(UNMAPPED_GIF)); + + assertEquals(TXT, service.getExtension(TEXT_PLAIN)); + + } finally { + try { + ins.close(); + } catch (IOException ignore) { + } + } + } + + public void testProvider() throws Exception { + MimeTypeProvider mtp = createMimeTypeProvider(IMAGE_GIF, GIF); + + assertNull(service.getMimeType(GIF)); + assertNull(service.getExtension(IMAGE_GIF)); + + service.bindMimeTypeProvider(mtp); + + assertEquals(IMAGE_GIF, service.getMimeType(GIF)); + assertEquals(GIF, service.getExtension(IMAGE_GIF)); + + service.unbindMimeTypeProvider(mtp); + + assertNull(service.getMimeType(GIF)); + assertNull(service.getExtension(IMAGE_GIF)); + } + + public void testProvider2() throws Exception { + MimeTypeProvider mtp = createMimeTypeProvider(IMAGE_GIF, GIF); + + service.registerMimeType(IMAGE_GIF, UNMAPPED_GIF); + + assertEquals(IMAGE_GIF, service.getMimeType(UNMAPPED_GIF)); + assertEquals(UNMAPPED_GIF, service.getExtension(IMAGE_GIF)); + + assertNull(service.getMimeType(GIF)); + + service.bindMimeTypeProvider(mtp); + + assertEquals(IMAGE_GIF, service.getMimeType(UNMAPPED_GIF)); + assertEquals(UNMAPPED_GIF, service.getExtension(IMAGE_GIF)); + + assertEquals(IMAGE_GIF, service.getMimeType(GIF)); + + service.unbindMimeTypeProvider(mtp); + + assertEquals(IMAGE_GIF, service.getMimeType(UNMAPPED_GIF)); + assertEquals(UNMAPPED_GIF, service.getExtension(IMAGE_GIF)); + + assertNull(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]>.
