Mercurial Hosting > luan
annotate src/goodjava/webserver/handlers/ContentTypeHandler.java @ 1423:2c06a7ff4173
minor
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 19 Nov 2019 17:42:11 -0700 |
parents | 27efb1fcbcb5 |
children | fb003c4003dd |
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 ); | |
1234 | 32 map.put( "css", "text/css" ); |
1423 | 33 map.put( "js", "application/javascript" ); |
1370 | 34 map.put( "mp4", "video/mp4" ); |
1137 | 35 // add more as need |
36 } | |
37 | |
38 public Response handle(Request request) { | |
39 Response response = handler.handle(request); | |
1165
668f29bc52ea
clean up content-type
Franklin Schmidt <fschmidt@gmail.com>
parents:
1148
diff
changeset
|
40 if( response!=null && !response.headers.containsKey("content-type") ) { |
1137 | 41 String path = request.path; |
42 int iSlash = path.lastIndexOf('/'); | |
43 int iDot = path.lastIndexOf('.'); | |
44 String type; | |
45 if( iDot < iSlash ) { // no extension | |
46 type = contentTypeForNoExtension; | |
47 } else { // extension | |
48 String extension = path.substring(iDot+1); | |
49 type = map.get( extension.toLowerCase() ); | |
50 } | |
51 if( type != null ) | |
1165
668f29bc52ea
clean up content-type
Franklin Schmidt <fschmidt@gmail.com>
parents:
1148
diff
changeset
|
52 response.headers.put("content-type",type); |
1137 | 53 } |
54 return response; | |
55 } | |
56 } |