This is the mail archive of the
kawa@sourceware.org
mailing list for the Kawa project.
Re: getting the JDK built-in web server to set the correct mime type
- From: Per Bothner <per at bothner dot com>
- To: alex mitchell <lexaay at gmail dot com>
- Cc: "kawa at sourceware dot org" <kawa at sourceware dot org>
- Date: Wed, 01 May 2013 12:16:28 -0700
- Subject: Re: getting the JDK built-in web server to set the correct mime type
- References: <CDA6B537 dot 4817F%lexaay at gmail dot com> <5180CDF3 dot 1040405 at bothner dot com>
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);