Bug 2441 - Numerous warnings when tracking resources in web inspector
authorEugene Ostroukhov <eugeneo@symbian.org>
Wed, 07 Apr 2010 13:34:34 -0700
changeset 299 a240ab689b9b
parent 298 d4abe1c863d1
child 300 9505af44d7bf
Bug 2441 - Numerous warnings when tracking resources in web inspector
org.symbian.tools.wrttools.previewer/src/org/symbian/tools/wrttools/previewer/http/WorkspaceResourcesServlet.java
--- a/org.symbian.tools.wrttools.previewer/src/org/symbian/tools/wrttools/previewer/http/WorkspaceResourcesServlet.java	Wed Apr 07 11:51:12 2010 -0700
+++ b/org.symbian.tools.wrttools.previewer/src/org/symbian/tools/wrttools/previewer/http/WorkspaceResourcesServlet.java	Wed Apr 07 13:34:34 2010 -0700
@@ -31,6 +31,7 @@
 import java.text.MessageFormat;
 import java.util.Map;
 import java.util.StringTokenizer;
+import java.util.TreeMap;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -61,6 +62,28 @@
 	private static final Pattern HEAD_TAG_PATTERN = Pattern.compile("<head(\\s*\\w*=\"(^\")*\")*\\s*>", Pattern.CASE_INSENSITIVE);
 	private static final String SCRIPT = "<script language=\"JavaScript\" type=\"text/javascript\" src=\"preview/script/lib/loader.js\"></script>";
 
+    private static final Map<String, String> EXTENSION_CONTENT_TYPE = new TreeMap<String, String>();
+    static {
+        EXTENSION_CONTENT_TYPE.put("htm", "text/html");
+        EXTENSION_CONTENT_TYPE.put("html", "text/html");
+        EXTENSION_CONTENT_TYPE.put("xml", "text/xml");
+        EXTENSION_CONTENT_TYPE.put("plist", "application/octet-stream");
+        EXTENSION_CONTENT_TYPE.put("gif", "image/gif");
+        EXTENSION_CONTENT_TYPE.put("jpg", "image/jpeg");
+        EXTENSION_CONTENT_TYPE.put("jpeg", "image/jpeg");
+        EXTENSION_CONTENT_TYPE.put("png", "application/octet-stream");
+        EXTENSION_CONTENT_TYPE.put("css", "text/css");
+        EXTENSION_CONTENT_TYPE.put("js", "application/x-javascript");
+    }
+
+    public static String getMimeTypeByExtension(String extension) {
+        if (extension != null) {
+            return EXTENSION_CONTENT_TYPE.get(extension.toLowerCase());
+        } else {
+            return null;
+        }
+    }
+
 	@Override
 	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
 			throws ServletException, IOException {
@@ -73,8 +96,15 @@
 		try {
             contents = getSpecialResource(path, req.getParameterMap());
 			if (contents == null) {
-				contents = getWorkspaceResourceContents(path);
+                IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
+                if (file.isAccessible()) {
+                    contents = file.getContents();
+                }
 			}
+            String mimeType = getMimeTypeByExtension(path.getFileExtension());
+            if (mimeType != null) {
+                resp.setContentType(mimeType);
+            }
 			if (contents != null) {
 				copyData(contents, resp.getOutputStream());
 			} else {
@@ -156,16 +186,6 @@
 		}
 	}
 
-	private InputStream getWorkspaceResourceContents(IPath path)
-			throws CoreException {
-		InputStream contents = null;
-		IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
-		if (file.isAccessible()) {
-			contents = file.getContents();
-		}
-		return contents;
-	}
-
 	public static String getHttpUrl(IResource file) {
 		String uri = getServerURIForResource(file != null ? file.getFullPath()
 				.toString() : "/");