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