001    /*
002     * JBoss DNA (http://www.jboss.org/dna)
003     * See the COPYRIGHT.txt file distributed with this work for information
004     * regarding copyright ownership.  Some portions may be licensed
005     * to Red Hat, Inc. under one or more contributor license agreements.
006     * See the AUTHORS.txt file in the distribution for a full listing of 
007     * individual contributors. 
008     *
009     * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
010     * is licensed to you under the terms of the GNU Lesser General Public License as
011     * published by the Free Software Foundation; either version 2.1 of
012     * the License, or (at your option) any later version.
013     *
014     * JBoss DNA is distributed in the hope that it will be useful,
015     * but WITHOUT ANY WARRANTY; without even the implied warranty of
016     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017     * Lesser General Public License for more details.
018     *
019     * You should have received a copy of the GNU Lesser General Public
020     * License along with this software; if not, write to the Free
021     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
022     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
023     */
024    package org.jboss.dna.graph.mimetype;
025    
026    import java.io.IOException;
027    import java.io.InputStream;
028    import java.util.Collections;
029    import java.util.HashMap;
030    import java.util.HashSet;
031    import java.util.List;
032    import java.util.Map;
033    import java.util.Set;
034    import java.util.regex.Matcher;
035    import java.util.regex.Pattern;
036    import net.jcip.annotations.Immutable;
037    import org.jboss.dna.common.i18n.I18n;
038    import org.jboss.dna.common.util.CheckArg;
039    import org.jboss.dna.common.util.IoUtil;
040    import org.jboss.dna.common.util.Logger;
041    import org.jboss.dna.common.util.StringUtil;
042    import org.jboss.dna.graph.GraphI18n;
043    
044    /**
045     * A {@link MimeTypeDetector} that attempts to match the extension of the supplied name against a set of known file extensions.
046     * 
047     * @author Randall Hauch
048     */
049    @Immutable
050    public class ExtensionBasedMimeTypeDetector implements MimeTypeDetector {
051    
052        /**
053         * The default location of the properties file containing the extension patterns to MIME types. Value is "{@value} ".
054         */
055        public static final String MIME_TYPE_EXTENSIONS_RESOURCE_PATH = "/org/jboss/dna/graph/mime.types";
056        // public static final String MIME_TYPE_EXTENSIONS_RESOURCE_PATH = "/org/jboss/dna/graph/MimeTypes.properties";
057    
058        /**
059         * The mapping of extension (which includes the leading '.') to MIME type.
060         */
061        private final Map<String, String> mimeTypesByExtension;
062    
063        /**
064         * Create a default instance of the extension-based MIME type detector. The set of extension patterns to MIME-types is loaded
065         * from the "org/jboss/dna/graph/mime.types" classpath resource.
066         */
067        public ExtensionBasedMimeTypeDetector() {
068            this(null, true);
069        }
070    
071        /**
072         * Create an instance of the extension-based MIME type detector by using the supplied mappings. The set of extension patterns
073         * to MIME-types is loaded from the "org/jboss/dna/graph/mime.types" classpath resource, but the supplied extension mappings
074         * override any default mappings.
075         * 
076         * @param extensionsToMimeTypes the mapping of extension patterns to MIME types, which will override the default mappings; may
077         *        be null if the default mappings are to be used
078         */
079        public ExtensionBasedMimeTypeDetector( Map<String, String> extensionsToMimeTypes ) {
080            this(extensionsToMimeTypes, true);
081        }
082    
083        /**
084         * Create an instance of the extension-based MIME type detector by using the supplied mappings. If requested, the set of
085         * extension patterns to MIME-types is loaded from the "org/jboss/dna/graph/mime.types" classpath resource and any supplied
086         * extension mappings override any default mappings.
087         * 
088         * @param extensionsToMimeTypes the mapping of extension patterns to MIME types, which will override the default mappings; may
089         *        be null if the default mappings are to be used
090         * @param initWithDefaults true if the default mappings are to be loaded first
091         */
092        public ExtensionBasedMimeTypeDetector( Map<String, String> extensionsToMimeTypes,
093                                               boolean initWithDefaults ) {
094            Map<String, String> mappings = getDefaultMappings();
095            if (extensionsToMimeTypes != null) {
096                for (Map.Entry<String, String> entry : extensionsToMimeTypes.entrySet()) {
097                    String extensionString = entry.getKey();
098                    if (extensionString == null) continue;
099                    // Lowercase, trime, and remove all leading '.' characters ...
100                    extensionString = extensionString.toLowerCase().trim().replaceAll("^.+", "");
101                    if (extensionString.length() == 0) continue;
102                    String mimeType = entry.getValue();
103                    if (mimeType == null) continue;
104                    mimeType = entry.getValue().trim();
105                    if (mimeType.length() == 0) continue;
106                    assert extensionString.length() != 0;
107                    assert mimeType.length() != 0;
108                    mappings.put(extensionString, mimeType);
109                }
110            }
111            // Now put the mappings into the different maps ...
112            Map<String, String> mappingsByAnyCharExtension = new HashMap<String, String>();
113            for (Map.Entry<String, String> entry : mappings.entrySet()) {
114                String extensionString = entry.getKey();
115                String mimeType = entry.getValue();
116                assert extensionString != null;
117                assert extensionString.length() != 0;
118                assert mimeType != null;
119                assert mimeType.length() != 0;
120                mappingsByAnyCharExtension.put("." + extensionString, mimeType);
121            }
122            mimeTypesByExtension = Collections.unmodifiableMap(mappingsByAnyCharExtension);
123        }
124    
125        /**
126         * Load the default extensions from {@link #MIME_TYPE_EXTENSIONS_RESOURCE_PATH}, which can either be a property file or a
127         * tab-delimited *nix-style MIME types file (common in web servers and libraries). If an extension applies to more than one
128         * MIME type, the first one in the file wins.
129         * 
130         * @return the default mappings; never null
131         */
132        protected static Map<String, String> getDefaultMappings() {
133            Map<String, Set<String>> duplicates = new HashMap<String, Set<String>>();
134            return load(ExtensionBasedMimeTypeDetector.class.getResourceAsStream(MIME_TYPE_EXTENSIONS_RESOURCE_PATH), duplicates);
135        }
136    
137        /**
138         * Load the default extensions from the supplied stream, which may provide the contents in the format of property file or a
139         * tab-delimited *nix-style MIME types file (common in web servers and libraries). If an extension applies to more than one
140         * MIME type, the first one in the file wins.
141         * 
142         * @param stream the stream containing the content; may not be null
143         * @param duplicateMimeTypesByExtension a map into which any extension should be placed if there are multiple MIME types that
144         *        apply; may be null if this information is not required
145         * @return the default mappings; never null
146         */
147        protected static Map<String, String> load( InputStream stream,
148                                                   Map<String, Set<String>> duplicateMimeTypesByExtension ) {
149            CheckArg.isNotNull(stream, "stream");
150            // Create a Regex pattern that can be used for each line. This pattern looks for a mime type
151            // (which may contain no whitespace or '=') followed by an optional whitespace, an optional equals sign,
152            // optionally more whitespace, and finally by a string of one or more extensions.
153            Pattern linePattern = Pattern.compile("\\s*([^\\s=]+)\\s*=?\\s*(.*)");
154            List<String> lines = null;
155            try {
156                String content = IoUtil.read(stream);
157                lines = StringUtil.splitLines(content);
158            } catch (IOException e) {
159                I18n msg = GraphI18n.unableToAccessResourceFileFromClassLoader;
160                Logger.getLogger(ExtensionBasedMimeTypeDetector.class).warn(e, msg, MIME_TYPE_EXTENSIONS_RESOURCE_PATH);
161            }
162            Map<String, String> mimeTypesByExtension = new HashMap<String, String>();
163            if (lines != null) {
164                for (String line : lines) {
165                    line = line.trim();
166                    if (line.length() == 0 || line.startsWith("#")) continue;
167                    // Apply the pattern to each line ...
168                    Matcher matcher = linePattern.matcher(line);
169                    if (matcher.matches()) {
170                        String mimeType = matcher.group(1).trim().toLowerCase();
171                        String extensions = matcher.group(2).trim().toLowerCase();
172                        if (extensions.length() != 0) {
173                            // A valid mime type with at least one extension was found, so for each extension ...
174                            for (String extensionString : extensions.split("\\s+")) {
175                                extensionString = extensionString.trim();
176                                if (extensionString.length() != 0) {
177                                    // Register the extension with the MIME type ...
178                                    String existingMimeType = mimeTypesByExtension.put(extensionString, mimeType);
179                                    if (existingMimeType != null) {
180                                        // A MIME type already had this extension, so use the first one ...
181                                        mimeTypesByExtension.put(extensionString, existingMimeType);
182                                        if (duplicateMimeTypesByExtension != null) {
183                                            // And record the duplicate ...
184                                            Set<String> dups = duplicateMimeTypesByExtension.get(extensionString);
185                                            if (dups == null) {
186                                                dups = new HashSet<String>();
187                                                duplicateMimeTypesByExtension.put(extensionString, dups);
188                                            }
189                                            dups.add(existingMimeType);
190                                            dups.add(mimeType);
191                                        }
192                                    }
193                                }
194                            }
195                        }
196                    }
197                }
198            }
199            return mimeTypesByExtension;
200        }
201    
202        /**
203         * {@inheritDoc}
204         * 
205         * @see org.jboss.dna.graph.mimetype.MimeTypeDetector#mimeTypeOf(java.lang.String, java.io.InputStream)
206         */
207        public String mimeTypeOf( String name,
208                                  InputStream content ) {
209            if (name == null || name.length() == 0) return null;
210            String trimmedName = name.trim();
211            if (trimmedName.length() == 0) return null;
212    
213            // Find the extension ...
214            int indexOfDelimiter = trimmedName.lastIndexOf('.');
215            if (indexOfDelimiter < 1) return null;
216            String extension = trimmedName.substring(indexOfDelimiter).toLowerCase();
217    
218            // Look for a match ...
219            return mimeTypesByExtension.get(extension);
220        }
221    }