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


I've been thinking about the API for MIMI detection,
and I think it's reasonable to support getNPath for all
gnu.text.Path object s- but sometimes an exception is thrown
if there is no matching FileSystemProvider.  Instead, the
caller (in this case KawaAutoHandler) can catch the exception.

So I wrote the attached patch.

Sorry for procrastinating while deciding on the API.
--
	--Per Bothner
per@bothner.com   http://per.bothner.com/
Index: gnu/kawa/servlet/KawaAutoHandler.java
===================================================================
--- gnu/kawa/servlet/KawaAutoHandler.java	(revision 7534)
+++ gnu/kawa/servlet/KawaAutoHandler.java	(working copy)
@@ -159,8 +159,11 @@
                   }
                 return null;
               }
-            //String contentType = context.getMimeType(path); FIXME
-            //hctx.setContentType(contentType);
+
+            String contentType = absPath.probeContentType();
+            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 7534)
+++ gnu/text/FilePath.java	(working copy)
@@ -313,4 +313,18 @@
       }
     return this;
   }
+
+    /* #ifdef JAVA7 */
+    // /** Convert to a {@code java.nio.file.Path} instance.
+    //  * Unlike the overriden base method, this cannot
+    //  * throw {@code FileSystemNotFoundException}.
+    //  * Use caution if this is a relative path and the {@code currentPath()}
+    //  * is not the default path, since {@code java.nio} assumes a relative path
+    //  * is relative to the default directory.
+    //  */
+    // @Override
+    // public java.nio.file.Path toNPath() {
+    //     return file.toPath();
+    // }
+    /* #endif */
 }
Index: gnu/text/Path.java
===================================================================
--- gnu/text/Path.java	(revision 7534)
+++ gnu/text/Path.java	(working copy)
@@ -5,7 +5,10 @@
 import java.io.*;
 import java.net.*;
 import gnu.mapping.*;
-
+/* #ifdef JAVA7 */
+// import java.nio.file.Files;
+/* #endif */
+ 
 /** A generalized path/location, including File and URIs. */
 
 public abstract class Path
@@ -392,4 +395,39 @@
   {
     return getAbsolute ();
   }
+
+    /* #ifdef JAVA7 */
+    // /** Convert
+    // /** Convert if possible to a {@code java.nio.file.Path} instance.
+    //  * Use caution if this is a relative path and the {@code currentPath()}
+    //  * is not the default path, since {@code java.nio} assumes a relative path
+    //  * is relative to the default directory.
+    //  * Uses {@code java.nio.file.Paths#get(URI)}, and thus
+    //  * throws whatever that method throws.
+    //  * @throws FileSystemNotFoundException - no nio file system provider
+    //  *   was found for the URI scheme
+    //  */
+    // public java.nio.file.Path toNPath()
+    //     throws java.nio.file.FileSystemNotFoundException {
+    //     return java.nio.file.Paths.get(toUri());
+    // }
+    /* #endif */
+
+    public String probeContentType() {
+        String contentType;
+        /* #ifdef JAVA7 */
+        // try {
+        //     contentType = Files.probeContentType(getAbsolute().toNPath());
+        // } catch (Throwable ex) {
+        //     contentType = null;
+        // }
+        /* #else */
+        contentType = null;
+        /* #endif */
+        if (contentType != null) {
+            contentType = URLConnection.guessContentTypeFromName(getPath());
+        }
+        return contentType;
+    }
+
 }

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