View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.vfs2.impl;
18  
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import org.apache.commons.vfs2.FileContent;
23  import org.apache.commons.vfs2.FileContentInfo;
24  import org.apache.commons.vfs2.FileObject;
25  import org.apache.commons.vfs2.FileSystemException;
26  
27  /**
28   * A helper class that determines the provider to use for a file.
29   */
30  final class FileTypeMap {
31  
32      private final Map<String, String> mimeTypeMap = new HashMap<>();
33      private final Map<String, String> extensionMap = new HashMap<>();
34  
35      /**
36       * Adds a file name extension mapping.
37       */
38      public void addExtension(final String extension, final String scheme) {
39          extensionMap.put(extension, scheme);
40      }
41  
42      /**
43       * Adds a MIME type mapping.
44       */
45      public void addMimeType(final String mimeType, final String scheme) {
46          mimeTypeMap.put(mimeType, scheme);
47      }
48  
49      /**
50       * Removes all extensions and scheme mappings.
51       */
52      public void clear() {
53          mimeTypeMap.clear();
54          extensionMap.clear();
55      }
56  
57      /**
58       * Gets the scheme for the provider of a layered file system.
59       * <p>
60       * This will check the {@link FileContentInfo} or file extension.
61       * </p>
62       *
63       * @param fileObject The file object to query.
64       * @return Scheme supporting the file type or null (if unknown).
65       * @throws FileSystemException if an error occurs.
66       */
67      public String getScheme(final FileObject fileObject) throws FileSystemException {
68          // Check the file's mime type for a match
69          final FileContent content = fileObject.getContent();
70          final String mimeType = content.getContentInfo().getContentType();
71          if (mimeType != null) {
72              return mimeTypeMap.get(mimeType);
73          }
74  
75          // no specific mime-type - if it is a file also check the extension
76          if (!fileObject.isFile()) {
77              return null; // VFS-490 folders don't use extensions for mime-type
78          }
79          return extensionMap.get(fileObject.getName().getExtension());
80      }
81  }