myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From we...@apache.org
Subject svn commit: r1649750 - in /myfaces/tobago/trunk/tobago-core/src: main/java/org/apache/myfaces/tobago/internal/util/ main/java/org/apache/myfaces/tobago/servlet/ test/java/org/apache/myfaces/tobago/internal/util/
Date Tue, 06 Jan 2015 08:35:26 GMT
Author: weber
Date: Tue Jan  6 08:35:26 2015
New Revision: 1649750

URL: http://svn.apache.org/r1649750
Log:
TOBAGO-1433 - Supported resource types should be configurable

Added:
    myfaces/tobago/trunk/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/util/MimeTypeUtilsTest.java
Modified:
    myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/MimeTypeUtils.java
    myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/servlet/ResourceServlet.java

Modified: myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/MimeTypeUtils.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/MimeTypeUtils.java?rev=1649750&r1=1649749&r2=1649750&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/MimeTypeUtils.java
(original)
+++ myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/MimeTypeUtils.java
Tue Jan  6 08:35:26 2015
@@ -19,30 +19,41 @@
 
 package org.apache.myfaces.tobago.internal.util;
 
+import java.util.HashMap;
+import java.util.Map;
+
 public final class MimeTypeUtils {
 
+  private static final Map<String, String> extensionTypeMap = new HashMap<String,
String>();
+
+  public static final String DEFAULT_MAPPING = ".gif:image/gif,.png:image/png,.jpg:image/jpeg,.js:text/javascript,"
+      + ".css:text/css,.ico:image/vnd.microsoft.icon,.html:text/html,.htm:text/html,.map:application/json";
+
   private MimeTypeUtils() {
     // utils class
   }
 
-  // todo: maybe support more extensions (configurable?)  
+  // init() is invoked by ResourceServlet.init(), this is at application startup.
+  public static void init(String mimeTypeMapping) {
+    if (mimeTypeMapping == null) {
+      mimeTypeMapping = DEFAULT_MAPPING;
+    }
+    try {
+      extensionTypeMap.clear();
+      for (String typeMapping : mimeTypeMapping.split(",")) {
+        int idx = typeMapping.indexOf(':');
+        extensionTypeMap.put(typeMapping.substring(0, idx).trim(), typeMapping.substring(idx
+ 1).trim());
+      }
+    } catch (Exception e) {
+      throw new IllegalArgumentException("Invalid parameter 'mimeTypeMapping': \"" + mimeTypeMapping
+ "\"", e);
+    }
+  }
+
   public static String getMimeTypeForFile(final String file) {
-    if (file.endsWith(".gif")) {
-      return "image/gif";
-    } else if (file.endsWith(".png")) {
-      return "image/png";
-    } else if (file.endsWith(".jpg")) {
-      return "image/jpeg";
-    } else if (file.endsWith(".js")) {
-      return "text/javascript";
-    } else if (file.endsWith(".css")) {
-      return "text/css";
-    } else if (file.endsWith(".ico")) {
-      return "image/vnd.microsoft.icon";
-    } else if (file.endsWith(".html") || file.endsWith(".htm")) {
-      return "text/html";
-    } else if (file.endsWith(".map")) {
-      return "application/json";
+    for (Map.Entry<String, String> entry : extensionTypeMap.entrySet()) {
+      if (file.endsWith(entry.getKey())) {
+        return entry.getValue();
+      }
     }
     return null;
   }

Modified: myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/servlet/ResourceServlet.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/servlet/ResourceServlet.java?rev=1649750&r1=1649749&r2=1649750&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/servlet/ResourceServlet.java
(original)
+++ myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/servlet/ResourceServlet.java
Tue Jan  6 08:35:26 2015
@@ -57,6 +57,14 @@ import java.util.Set;
  *     &lt;param-name&gt;bufferSize&lt;/param-name&gt;
  *     &lt;param-value&gt;4096&lt;/param-value&gt;
  *   &lt;/init-param&gt;
+ *   &lt;init-param&gt;
+ *     &lt;description&gt; File extension to mimeType mapping.
+ *       Comma separated list of <extension>:<mime-type>
+ *       Default is ".gif:image/gif,.png:image/png,.jpg:image/jpeg,.js:text/javascript,.css:text/css,.ico:image/vnd.microsoft.icon,.html:text/html,.htm:text/html,.map:application/json"
+ *    .&lt;/description&gt;
+ *     &lt;param-name&gt;mimeTypeMapping&lt;/param-name&gt;
+ *     &lt;param-value&gt;.gif:image/gif,.htm:text/html&lt;/param-value&gt;
+ *   &lt;/init-param&gt;
  * &lt;/servlet&gt;
  * &lt;servlet-mapping&gt;
  *   &lt;servlet-name&gt;ResourceServlet&lt;/servlet-name&gt;
@@ -106,6 +114,8 @@ public class ResourceServlet extends Htt
       }
     }
     nosniffHeader = tobagoConfig.isSetNosniffHeader();
+
+    MimeTypeUtils.init(servletConfig.getInitParameter("mimeTypeMapping"));
   }
 
   private void addResourceDir(final List<Theme> themes) {

Added: myfaces/tobago/trunk/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/util/MimeTypeUtilsTest.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/util/MimeTypeUtilsTest.java?rev=1649750&view=auto
==============================================================================
--- myfaces/tobago/trunk/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/util/MimeTypeUtilsTest.java
(added)
+++ myfaces/tobago/trunk/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/util/MimeTypeUtilsTest.java
Tue Jan  6 08:35:26 2015
@@ -0,0 +1,22 @@
+package org.apache.myfaces.tobago.internal.util;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class MimeTypeUtilsTest {
+
+  @Test
+  public void testGetMimeTypeForFile() throws Exception {
+     MimeTypeUtils.init(null);
+    Assert.assertEquals("image/gif", MimeTypeUtils.getMimeTypeForFile("image.gif"));
+    Assert.assertEquals("image/png", MimeTypeUtils.getMimeTypeForFile("images/red.png"));
+    Assert.assertEquals("image/jpeg", MimeTypeUtils.getMimeTypeForFile("images/button.jpg"));
+    Assert.assertEquals("text/javascript", MimeTypeUtils.getMimeTypeForFile("path/tobago.js"));
+    Assert.assertEquals("text/css", MimeTypeUtils.getMimeTypeForFile("tobago.css"));
+    Assert.assertEquals("image/vnd.microsoft.icon", MimeTypeUtils.getMimeTypeForFile("tobago.ico"));
+    Assert.assertEquals("text/html", MimeTypeUtils.getMimeTypeForFile("test.html"));
+    Assert.assertEquals("text/html", MimeTypeUtils.getMimeTypeForFile("test.htm"));
+    Assert.assertEquals("application/json", MimeTypeUtils.getMimeTypeForFile("object.map"));
+    Assert.assertNull(MimeTypeUtils.getMimeTypeForFile("notValid.extension"));
+  }
+}
\ No newline at end of file



Mime
View raw message