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


Hi Per,

No problem, I figured this was probably done some time back so it might be
hard to remember why it was commented out.

I tried re-enabling it, and the first problem was that there is no
variable "context". It might be referring to hctx, but I'm not sure. The
only implementation of getMimeType I could find is in CGIServletWrapper,
which is only a stub:
  public String getMimeType (String file) { return null; }

So I'm guessing its commented out because it was not implemented yet.

Anyway, replacing the commented-out code in KawaAutoHandler.java with the
following seems to work:

javax.activation.MimetypesFileTypeMap filetypemap = new
javax.activation.MimetypesFileTypeMap();
        	String contentType = filetypemap.getContentType(path);
            if(contentType!=null) {
  hctx.log("File "+path+" - a "+contentType+" file (based on
filetypemap)");
  hctx.setContentType(contentType);
            } else {
  hctx.log("File "+path+" - cannot detect mimetype");
            }

This assumes that the user has a .mime.types file in one of several
possible locations as described here
http://docs.oracle.com/javase/6/docs/api/javax/activation/MimetypesFileType
Map.html, otherwise only a minimal number of mime types are recognized.


The other options that I came across when searching involved using
java.net.URLConnection.guessContentTypeFromName(path) and/or
java.net.URLConnection.guessContentTypeFromStream(resourceStream), but
these seemed to be wrong most of the time.

I'm not sure if this is the best way of doing this. Any suggestions?


thanks,
Alex
 

On 13-05-01 11:15 AM, "Per Bothner" <per@bothner.com> wrote:

>On 04/30/2013 07:26 PM, alex mitchell wrote:
>> I'm using the JDK built-in web server to serve up .html and .css files.
>> When I view the files using IE9, the .css seems to be ignored, and I get
>> the following error in the IE web console:
>>    SEC7113: CSS was ignored due to mime type mismatch styling.css
>> So I'm assuming that the web server isn't setting the mime type
>>correctly.
>>
>> The Kawa docs say "If Kawa doesn't recognize the language of a script
>>(and
>> it isn't named +default+) then it assumes the file is a data file. It
>>asks
>> the servlet engine to figure out the content type (using the getMimeType
>> method of ServletContext), and just copies the file into the response."
>> However, in KawaAutoHandler.java, I see the following on line 158:
>>    //String contentType =3D context.getMimeType(path); FIXME
>>    //hctx.setContentType(contentType);
>> I'm guessing this is the source of the problem. Does anyone know why
>>this
>> is commented out, and what I might need to do to fix it?
>
>Sorry, I can't remember why this is commented out.
>Try re-enabling it and see what happens.
>-- 
>	--Per Bothner
>per@bothner.com   http://per.bothner.com/



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