You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@click.apache.org by sa...@apache.org on 2010/07/04 12:31:48 UTC

svn commit: r960316 - in /click/trunk/click/framework: src/org/apache/click/util/ClickUtils.java test/org/apache/click/util/ClickUtilsTest.java

Author: sabob
Date: Sun Jul  4 10:31:48 2010
New Revision: 960316

URL: http://svn.apache.org/viewvc?rev=960316&view=rev
Log:
improved ClickUtils.getMimeType to work with extensions as well as filename

Modified:
    click/trunk/click/framework/src/org/apache/click/util/ClickUtils.java
    click/trunk/click/framework/test/org/apache/click/util/ClickUtilsTest.java

Modified: click/trunk/click/framework/src/org/apache/click/util/ClickUtils.java
URL: http://svn.apache.org/viewvc/click/trunk/click/framework/src/org/apache/click/util/ClickUtils.java?rev=960316&r1=960315&r2=960316&view=diff
==============================================================================
--- click/trunk/click/framework/src/org/apache/click/util/ClickUtils.java (original)
+++ click/trunk/click/framework/src/org/apache/click/util/ClickUtils.java Sun Jul  4 10:31:48 2010
@@ -1870,31 +1870,39 @@ public class ClickUtils {
     }
 
     /**
-     * Return the mime-type or content-type for the given filename.
+     * Return the mime-type or content-type for the given filename/extension.
+     * <p/>
+     * Example:
+     * <pre class="prettyprint">
+     * // Lookup mimetype for file
+     * String mimeType = ClickUtils.getMimeType("hello-world.pdf");
      *
-     * @param filename the filename to obtain the mime-type for
-     * @return the mime-type for the given filename, or null if not found
+     * // Lookup mimetype for extension
+     * mimeType = ClickUtils.getMimeType("pdf");
+     * </pre>
+     *
+     * @param value the filename or extension to obtain the mime-type for
+     * @return the mime-type for the given filename/extension, or null if not
+     * found
      */
-    public static String getMimeType(String filename) {
-        if (filename == null) {
-            throw new IllegalArgumentException("null filename parameter");
+    public static String getMimeType(String value) {
+        if (value == null) {
+            throw new IllegalArgumentException("null filename/extension parameter");
         }
 
-        int index = filename.lastIndexOf(".");
+        String ext = value;
 
+        int index = value.lastIndexOf(".");
         if (index != -1) {
-            String ext = filename.substring(index + 1);
+            ext = value.substring(index + 1);
+        }
 
-            try {
-                ResourceBundle bundle = getBundle("org/apache/click/util/mime-type");
+        try {
+            ResourceBundle bundle = getBundle("org/apache/click/util/mime-type");
 
-                return bundle.getString(ext.toLowerCase());
+            return bundle.getString(ext.toLowerCase());
 
-            } catch (MissingResourceException mre) {
-                return null;
-            }
-
-        } else {
+        } catch (MissingResourceException mre) {
             return null;
         }
     }

Modified: click/trunk/click/framework/test/org/apache/click/util/ClickUtilsTest.java
URL: http://svn.apache.org/viewvc/click/trunk/click/framework/test/org/apache/click/util/ClickUtilsTest.java?rev=960316&r1=960315&r2=960316&view=diff
==============================================================================
--- click/trunk/click/framework/test/org/apache/click/util/ClickUtilsTest.java (original)
+++ click/trunk/click/framework/test/org/apache/click/util/ClickUtilsTest.java Sun Jul  4 10:31:48 2010
@@ -449,10 +449,16 @@ public class ClickUtilsTest extends Test
      * Sanity checks for ClickUtils.getMimeType.
      */
     public void testGetMimeType() {
+        // Test mime type based on filename
         assertEquals("application/vnd.ms-excel", ClickUtils.getMimeType("worksheet.xls"));
 
         assertEquals("application/vnd.ms-excel", ClickUtils.getMimeType("WORKSHEET.XLS"));
-        
+
+        // Test mime type based on extension
+        assertEquals("application/json", ClickUtils.getMimeType("json"));
+
+        assertEquals("application/json", ClickUtils.getMimeType(".JSON"));
+
         try {
         	assertNull(ClickUtils.getMimeType("broken.xxx"));
         } catch (Exception e) {