View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.internal.util;
21  
22  import org.apache.myfaces.tobago.config.TobagoConfig;
23  
24  import javax.servlet.ServletContext;
25  import java.util.Map;
26  
27  public final class MimeTypeUtils {
28  
29    private static Map<String, String> additionalMimeTypes = null;
30  
31    private MimeTypeUtils() {
32      // utils class
33    }
34  
35    // todo: maybe support more extensions
36    public static String getMimeTypeForFile(final String file) {
37      int length = file.length();
38      if (file.charAt(length - 4) == '.') {
39        if (file.charAt(length - 1) == 'g') {
40          if (file.regionMatches(length - 3, "png", 0, 2)) {
41            return "image/png";
42          }
43          if (file.regionMatches(length - 3, "jpg", 0, 2)) {
44            return "image/jpeg";
45          }
46          if (file.endsWith("svg")) {
47            return "image/svg+xml";
48          }
49        } else {
50          if (file.endsWith("css")) {
51            return "text/css";
52          }
53          if (file.endsWith("gif")) {
54            return "image/gif";
55          }
56          if (file.endsWith("htm")) {
57            return "text/html";
58          }
59          if (file.endsWith("ico")) {
60            return "image/vnd.microsoft.icon";
61          }
62          if (file.endsWith("map")) {
63            return "application/json";
64          }
65          if (file.endsWith("ttf")) {
66            return "application/x-font-ttf";
67          }
68        }
69      } else if (file.charAt(length - 3) == '.') {
70        if (file.endsWith("js")) {
71          return "text/javascript";
72        }
73      } else if (file.charAt(length - 5) == '.') {
74        if (file.endsWith("woff")) {
75          return "application/font-woff";
76        }
77        if (file.endsWith("html")) {
78          return "text/html";
79        }
80      }
81  
82      final int index = file.lastIndexOf('.');
83      if (index > -1) {
84        String extension = file.substring(index + 1);
85        return additionalMimeTypes.get(extension);
86      }
87  
88      return null;
89    }
90  
91    public static void init(ServletContext servletContext) {
92      if (additionalMimeTypes == null) {
93        final TobagoConfig tobagoConfig = TobagoConfig.getInstance(servletContext);
94        additionalMimeTypes = tobagoConfig.getMimeTypes();
95      } else {
96        throw new IllegalStateException(MimeTypeUtils.class.getSimpleName() + " is already initialized!");
97      }
98    }
99  }