This is the mail archive of the kawa@sourceware.org mailing list for the Kawa project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: getting the JDK built-in web server to set the correct mime type


On 05/01/2013 01:10 AM, Per Bothner wrote:
I found a webpage suggesting using
java.nio.file.Files.html#probeContentType
This does require Java 7.

I'll explore more later.

Attached is a possible patch.
--
	--Per Bothner
per@bothner.com   http://per.bothner.com/
Index: gnu/kawa/servlet/KawaAutoHandler.java
===================================================================
--- gnu/kawa/servlet/KawaAutoHandler.java	(revision 7491)
+++ gnu/kawa/servlet/KawaAutoHandler.java	(working copy)
@@ -8,6 +8,9 @@
 import java.io.*;
 import java.net.*;
 import java.util.*;
+/* #ifdef JAVA7 */
+import java.nio.file.Files;
+/* #endif */
 
 /**
  * The generic framework for auto-configuring web page scripts.
@@ -159,8 +162,20 @@
                   }
                 return null;
               }
-            //String contentType = context.getMimeType(path); FIXME
-            //hctx.setContentType(contentType);
+
+            String contentType = null;
+            /* #ifdef JAVA7 */
+            if (absPath instanceof FilePath) {
+                contentType = Files.probeContentType
+                    (((FilePath) absPath).toNPath());
+            }
+            /* #endif */
+            if (contentType != null) {
+                contentType = URLConnection.guessContentTypeFromName(path);
+            }
+            if (contentType != null)
+                hctx.setContentType(contentType);
+
             // should set content length.
             long len = absPath.getContentLength();
             hctx.sendResponseHeaders(200, null, len);
Index: gnu/text/FilePath.java
===================================================================
--- gnu/text/FilePath.java	(revision 7491)
+++ gnu/text/FilePath.java	(working copy)
@@ -266,6 +266,12 @@
   }
   /* #endif */
 
+  /* #ifdef JAVA7 */
+  public java.nio.file.Path toNPath() {
+      return file.toPath();
+  }
+  /* #endif */
+
   public InputStream openInputStream () throws IOException
   {
     return new FileInputStream(file);

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]