Mercurial Hosting > luan
annotate src/goodjava/webserver/handlers/ContentTypeHandler.java @ 1510:20375c20289d
minor
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 20 May 2020 21:57:27 -0600 |
parents | fb003c4003dd |
children | fa066aaa068c |
rev | line source |
---|---|
1402
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1370
diff
changeset
|
1 package goodjava.webserver.handlers; |
1137 | 2 |
3 import java.util.Map; | |
4 import java.util.HashMap; | |
1402
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1370
diff
changeset
|
5 import goodjava.webserver.Handler; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1370
diff
changeset
|
6 import goodjava.webserver.Request; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1370
diff
changeset
|
7 import goodjava.webserver.Response; |
1137 | 8 |
9 | |
10 public class ContentTypeHandler implements Handler { | |
11 private final Handler handler; | |
12 | |
13 // maps extension to content-type | |
14 // key must be lower case | |
15 public final Map<String,String> map = new HashMap<String,String>(); | |
16 | |
17 // set to null for none | |
18 public String contentTypeForNoExtension; | |
19 | |
20 public ContentTypeHandler(Handler handler) { | |
1165
668f29bc52ea
clean up content-type
Franklin Schmidt <fschmidt@gmail.com>
parents:
1148
diff
changeset
|
21 this(handler,"utf-8"); |
1137 | 22 } |
23 | |
24 public ContentTypeHandler(Handler handler,String charset) { | |
25 this.handler = handler; | |
1165
668f29bc52ea
clean up content-type
Franklin Schmidt <fschmidt@gmail.com>
parents:
1148
diff
changeset
|
26 String attrs = charset== null ? "" : "; charset="+charset; |
668f29bc52ea
clean up content-type
Franklin Schmidt <fschmidt@gmail.com>
parents:
1148
diff
changeset
|
27 String htmlType = "text/html" + attrs; |
668f29bc52ea
clean up content-type
Franklin Schmidt <fschmidt@gmail.com>
parents:
1148
diff
changeset
|
28 String textType = "text/plain" + attrs; |
1137 | 29 contentTypeForNoExtension = htmlType; |
30 map.put( "html", htmlType ); | |
31 map.put( "txt", textType ); | |
1510 | 32 map.put( "luan", textType ); |
1234 | 33 map.put( "css", "text/css" ); |
1423 | 34 map.put( "js", "application/javascript" ); |
1463
fb003c4003dd
better application/json handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1423
diff
changeset
|
35 map.put( "json", "application/json" + attrs ); |
1370 | 36 map.put( "mp4", "video/mp4" ); |
1137 | 37 // add more as need |
38 } | |
39 | |
40 public Response handle(Request request) { | |
41 Response response = handler.handle(request); | |
1165
668f29bc52ea
clean up content-type
Franklin Schmidt <fschmidt@gmail.com>
parents:
1148
diff
changeset
|
42 if( response!=null && !response.headers.containsKey("content-type") ) { |
1137 | 43 String path = request.path; |
44 int iSlash = path.lastIndexOf('/'); | |
45 int iDot = path.lastIndexOf('.'); | |
46 String type; | |
47 if( iDot < iSlash ) { // no extension | |
48 type = contentTypeForNoExtension; | |
49 } else { // extension | |
50 String extension = path.substring(iDot+1); | |
51 type = map.get( extension.toLowerCase() ); | |
52 } | |
53 if( type != null ) | |
1165
668f29bc52ea
clean up content-type
Franklin Schmidt <fschmidt@gmail.com>
parents:
1148
diff
changeset
|
54 response.headers.put("content-type",type); |
1137 | 55 } |
56 return response; | |
57 } | |
58 } |