You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@wookie.apache.org by sc...@apache.org on 2009/12/18 00:29:21 UTC

svn commit: r891989 - in /incubator/wookie/trunk: src-tests/org/apache/wookie/tests/ContentTypeUtilsTest.java src/org/apache/wookie/manifestmodel/impl/IconEntity.java src/org/apache/wookie/util/ContentTypeUtils.java

Author: scottbw
Date: Thu Dec 17 23:29:20 2009
New Revision: 891989

URL: http://svn.apache.org/viewvc?rev=891989&view=rev
Log:
Added content-type checks for images to determine if an icon is of a supported image type; this fixes WOOKIE-80.

Added:
    incubator/wookie/trunk/src-tests/org/apache/wookie/tests/ContentTypeUtilsTest.java
    incubator/wookie/trunk/src/org/apache/wookie/util/ContentTypeUtils.java
Modified:
    incubator/wookie/trunk/src/org/apache/wookie/manifestmodel/impl/IconEntity.java

Added: incubator/wookie/trunk/src-tests/org/apache/wookie/tests/ContentTypeUtilsTest.java
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/src-tests/org/apache/wookie/tests/ContentTypeUtilsTest.java?rev=891989&view=auto
==============================================================================
--- incubator/wookie/trunk/src-tests/org/apache/wookie/tests/ContentTypeUtilsTest.java (added)
+++ incubator/wookie/trunk/src-tests/org/apache/wookie/tests/ContentTypeUtilsTest.java Thu Dec 17 23:29:20 2009
@@ -0,0 +1,58 @@
+/*
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wookie.tests;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.apache.wookie.util.ContentTypeUtils;
+import org.junit.Test;
+
+public class ContentTypeUtilsTest {
+	
+	@Test
+	public void Dots(){
+		assertTrue(ContentTypeUtils.isSupportedImageType("test.png"));
+		assertTrue(ContentTypeUtils.isSupportedImageType(".test.png"));
+		assertTrue(ContentTypeUtils.isSupportedImageType("...test.png"));
+		assertTrue(ContentTypeUtils.isSupportedImageType(".test.test.png"));
+		assertFalse(ContentTypeUtils.isSupportedImageType("test.png."));
+	}
+
+	@Test
+	public void Types(){
+		assertTrue(ContentTypeUtils.isSupportedImageType("test.gif"));
+		assertTrue(ContentTypeUtils.isSupportedImageType("test.jpg"));
+		assertTrue(ContentTypeUtils.isSupportedImageType("test.svg"));
+		assertTrue(ContentTypeUtils.isSupportedImageType("test.png"));
+	}
+	
+	@Test
+	public void Types2(){
+		assertFalse(ContentTypeUtils.isSupportedImageType("test.exe"));
+		assertFalse(ContentTypeUtils.isSupportedImageType("test.mpeg"));
+		assertFalse(ContentTypeUtils.isSupportedImageType("test.wav"));
+		assertFalse(ContentTypeUtils.isSupportedImageType("test.html"));
+		assertFalse(ContentTypeUtils.isSupportedImageType("test.png.exe"));
+	}
+	
+	@Test
+	public void InvalidExtensions(){
+		assertFalse(ContentTypeUtils.isSupportedImageType("test.1exe"));
+		assertFalse(ContentTypeUtils.isSupportedImageType("test.p n g"));
+		assertFalse(ContentTypeUtils.isSupportedImageType("test.p3ng"));
+		assertFalse(ContentTypeUtils.isSupportedImageType("test.png0"));
+		assertFalse(ContentTypeUtils.isSupportedImageType("test.p–g"));
+	}
+}

Modified: incubator/wookie/trunk/src/org/apache/wookie/manifestmodel/impl/IconEntity.java
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/manifestmodel/impl/IconEntity.java?rev=891989&r1=891988&r2=891989&view=diff
==============================================================================
--- incubator/wookie/trunk/src/org/apache/wookie/manifestmodel/impl/IconEntity.java (original)
+++ incubator/wookie/trunk/src/org/apache/wookie/manifestmodel/impl/IconEntity.java Thu Dec 17 23:29:20 2009
@@ -18,6 +18,7 @@
 import org.apache.wookie.exceptions.BadManifestException;
 import org.apache.wookie.manifestmodel.IIconEntity;
 import org.apache.wookie.manifestmodel.IW3CXMLConfiguration;
+import org.apache.wookie.util.ContentTypeUtils;
 import org.apache.wookie.util.NumberUtils;
 import org.apache.wookie.util.UnicodeUtils;
 import org.apache.wookie.util.WidgetPackageUtils;
@@ -80,9 +81,14 @@
 			fSrc = WidgetPackageUtils.locateFilePath(fSrc,locales, zip);
 			setLang(WidgetPackageUtils.languageTagForPath(fSrc));
 		} catch (Exception e) {
-			e.printStackTrace();
 			fSrc = null;
 		}
+		try {
+			if (!ContentTypeUtils.isSupportedImageType(fSrc)) fSrc = null;
+		} catch (Exception e1) {
+			// TODO Auto-generated catch block
+			e1.printStackTrace();
+		}
 
 		// height is optional
 		String height  = element.getAttributeValue(IW3CXMLConfiguration.HEIGHT_ATTRIBUTE);

Added: incubator/wookie/trunk/src/org/apache/wookie/util/ContentTypeUtils.java
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/util/ContentTypeUtils.java?rev=891989&view=auto
==============================================================================
--- incubator/wookie/trunk/src/org/apache/wookie/util/ContentTypeUtils.java (added)
+++ incubator/wookie/trunk/src/org/apache/wookie/util/ContentTypeUtils.java Thu Dec 17 23:29:20 2009
@@ -0,0 +1,126 @@
+/*
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wookie.util;
+
+import java.io.File;
+
+import org.apache.commons.lang.StringUtils;
+
+/**
+ * Utils for content type sniffing
+ */
+public class ContentTypeUtils {
+
+	private static final String[] SUPPORTED_IMAGE_TYPES = {"image/png", "image/jpeg", "image/svg+xml", "image/gif", "image/vnd.microsoft.icon"};
+
+	/**
+	 * Checks to see if a filename is a supported image type based on its file extension
+	 * @param filename the filename to check
+	 * @return true if the filename has an extension for a supported image type
+	 */
+	public static boolean isSupportedImageType(String filename){
+		String type = getContentType(filename);
+		return isSupported(type, SUPPORTED_IMAGE_TYPES);
+	}	
+
+	/**
+	 * Checks to see if a file is a supported image type
+	 * @param file the file to check
+	 * @return true if the file is a supported image type
+	 */
+	public static boolean isSupportedImageType(File file){
+		String type = getContentType(file);
+		return isSupported(type, SUPPORTED_IMAGE_TYPES);
+	}	
+
+	/**
+	 * Gets the content type of a file
+	 * TODO actually implement SNIFF algorithm
+	 * @param file
+	 * @return the matched content type, or null if there is no match
+	 */
+	private static String getContentType(File file){
+		String type = getContentType(file.getName());
+		if (type == null){ 
+			//TODO implement the SNIFF spec for binary content-type checking
+		}
+		return type;
+	}
+
+	/**
+	 * Extracts the file extension from the given filename and looks up the
+	 * content type
+	 * @param filename
+	 * @return the matched content type, or null if there is no match
+	 */
+	private static String getContentType(String filename){
+
+		if (filename == null) return null;
+		if (filename.length() == 0) return null;
+		if (filename.endsWith(".")) return null;
+		if (filename.startsWith(",") && filename.lastIndexOf(".")==0) return null;
+		if (filename.contains(".")){
+			String type = null;
+			String[] parts = filename.split("\\.");
+			if (parts.length == 0) return null;
+			String ext = parts[parts.length-1];
+			if (ext.length() != 0){
+				if (StringUtils.isAlpha(ext)){
+					type = getContentTypeForExtension(ext);
+					if (type!=null) return type;
+				}
+			}
+		}
+		return null;
+	}
+
+	/**
+	 * @param ext
+	 * @return the content-type for the given file extension, or null if there is no match
+	 */
+	private static String getContentTypeForExtension(String ext){
+		if(ext.equals("html")) return "text/html";
+		if(ext.equals("htm")) return "text/html";
+		if(ext.equals("css")) return "text/css";
+		if(ext.equals("js")) return "application/javascript";
+		if(ext.equals("xml")) return "application/xml";
+		if(ext.equals("txt")) return "text/plain";		
+		if(ext.equals("wav")) return "audio/x-wav";
+		if(ext.equals("xhtml")) return "application/xhtml+xml";
+		if(ext.equals("xht")) return "application/xhtml+xml";
+		if(ext.equals("gif")) return "image/gif";
+		if(ext.equals("png")) return "image/png";
+		if(ext.equals("ico")) return "image/vnd.microsoft.icon";
+		if(ext.equals("svg")) return "image/svg+xml";
+		if(ext.equals("jpg")) return "image/jpeg";
+		return null;
+
+	}
+
+	/**
+	 * Checks to see if the supplied value is one of the supported values
+	 * @param value
+	 * @param supportedValues
+	 * @return true if the value is one of the supported values
+	 */
+	public static boolean isSupported(String value, String[] supportedValues){
+		if (value == null) return false;
+		boolean supported = false;
+		for (String type: supportedValues){
+			if (StringUtils.equals(value, type)) supported = true;
+		}
+		return supported;
+	}
+
+}