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      final 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          if (file.endsWith("eot")) {
69            return "application/vnd.ms-fontobject";
70          }
71          if (file.endsWith("otf")) {
72            return "application/x-font-opentype";
73          }
74        }
75      } else if (file.charAt(length - 3) == '.') {
76        if (file.endsWith("js")) {
77          return "text/javascript";
78        }
79      } else if (file.charAt(length - 5) == '.') {
80        if (file.endsWith("woff")) {
81          return "application/font-woff";
82        }
83        if (file.endsWith("html")) {
84          return "text/html";
85        }
86      } else if (file.charAt(length - 6) == '.') {
87        if (file.endsWith("woff2")) {
88          return "application/font-woff2";
89        }
90      }
91  
92      final int index = file.lastIndexOf('.');
93      if (index > -1) {
94        final String extension = file.substring(index + 1);
95        return additionalMimeTypes.get(extension);
96      }
97  
98      return null;
99    }
100 
101   public static void init(final ServletContext servletContext) {
102     if (additionalMimeTypes == null) {
103       final TobagoConfig tobagoConfig = TobagoConfig.getInstance(servletContext);
104       additionalMimeTypes = tobagoConfig.getMimeTypes();
105     } else {
106       throw new IllegalStateException(MimeTypeUtils.class.getSimpleName() + " is already initialized!");
107     }
108   }
109 }