Mercurial Hosting > luan
annotate src/goodjava/webserver/handlers/ContentTypeHandler.java @ 1609:268b2a26e8d7
minor - cors
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 09 May 2021 11:42:03 -0600 |
parents | fa066aaa068c |
children | fe611f6e3c28 |
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; | |
1607 | 5 import goodjava.logging.Logger; |
6 import goodjava.logging.LoggerFactory; | |
1402
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1370
diff
changeset
|
7 import goodjava.webserver.Handler; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1370
diff
changeset
|
8 import goodjava.webserver.Request; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1370
diff
changeset
|
9 import goodjava.webserver.Response; |
1137 | 10 |
11 | |
12 public class ContentTypeHandler implements Handler { | |
1607 | 13 private static final Logger logger = LoggerFactory.getLogger(ContentTypeHandler.class); |
1137 | 14 |
15 // maps extension to content-type | |
16 // key must be lower case | |
1607 | 17 public static final Map<String,String> map = new HashMap<String,String>(); |
18 static { | |
19 String textType = "text/plain; charset=utf-8"; | |
20 map.put( "txt", textType ); | |
21 map.put( "luan", textType ); | |
22 map.put( "luano", textType ); | |
23 map.put( "log", textType ); | |
24 map.put( "html", "text/html; charset=utf-8" ); | |
25 map.put( "css", "text/css; charset=utf-8" ); | |
26 map.put( "js", "application/javascript; charset=utf-8" ); | |
27 map.put( "json", "application/json; charset=utf-8" ); | |
28 map.put( "mp4", "video/mp4" ); | |
29 map.put( "jpg", "image/jpeg" ); | |
30 map.put( "jpeg", "image/jpeg" ); | |
31 map.put( "png", "image/png" ); | |
32 // add more as need | |
1137 | 33 } |
34 | |
1607 | 35 public static String getExtension(String path) { |
36 int iSlash = path.lastIndexOf('/'); | |
37 int iDot = path.lastIndexOf('.'); | |
38 return iDot > iSlash ? path.substring(iDot+1).toLowerCase() : null; | |
39 } | |
40 | |
41 private final Handler handler; | |
42 | |
43 public ContentTypeHandler(Handler handler) { | |
1137 | 44 this.handler = handler; |
45 } | |
46 | |
47 public Response handle(Request request) { | |
48 Response response = handler.handle(request); | |
1607 | 49 if( response!=null && response.status.code==200 && !response.headers.containsKey("Content-Type") ) { |
50 String extension = getExtension(request.path); | |
51 if( extension != null ) { | |
52 String type = map.get(extension); | |
53 if( type != null ) | |
54 response.headers.put("Content-Type",type); | |
55 else | |
56 logger.info("no type defined for extension: "+extension); | |
1137 | 57 } |
58 } | |
59 return response; | |
60 } | |
61 } |