Mercurial Hosting > luan
annotate src/goodjava/webserver/handlers/ContentTypeHandler.java @ 1734:02d2a6c3bc6f
content-types for doc and docx
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 18 Sep 2022 15:38:27 -0600 |
parents | 8a6e48371c66 |
children | a02a75e3daa8 |
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" ); | |
1624 | 26 map.put( "xml", "text/xml; charset=utf-8" ); |
1607 | 27 map.put( "js", "application/javascript; charset=utf-8" ); |
28 map.put( "json", "application/json; charset=utf-8" ); | |
29 map.put( "mp4", "video/mp4" ); | |
30 map.put( "jpg", "image/jpeg" ); | |
31 map.put( "jpeg", "image/jpeg" ); | |
32 map.put( "png", "image/png" ); | |
1624 | 33 map.put( "gif", "image/gif" ); |
34 map.put( "pdf", "application/pdf" ); | |
1732 | 35 map.put( "woff", "font/woff" ); |
1734
02d2a6c3bc6f
content-types for doc and docx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1732
diff
changeset
|
36 map.put( "doc", "application/msword" ); |
02d2a6c3bc6f
content-types for doc and docx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1732
diff
changeset
|
37 map.put( "docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document" ); |
1607 | 38 // add more as need |
1137 | 39 } |
40 | |
1607 | 41 public static String getExtension(String path) { |
42 int iSlash = path.lastIndexOf('/'); | |
43 int iDot = path.lastIndexOf('.'); | |
44 return iDot > iSlash ? path.substring(iDot+1).toLowerCase() : null; | |
45 } | |
46 | |
47 private final Handler handler; | |
48 | |
49 public ContentTypeHandler(Handler handler) { | |
1137 | 50 this.handler = handler; |
51 } | |
52 | |
53 public Response handle(Request request) { | |
54 Response response = handler.handle(request); | |
1607 | 55 if( response!=null && response.status.code==200 && !response.headers.containsKey("Content-Type") ) { |
56 String extension = getExtension(request.path); | |
57 if( extension != null ) { | |
58 String type = map.get(extension); | |
59 if( type != null ) | |
60 response.headers.put("Content-Type",type); | |
61 else | |
62 logger.info("no type defined for extension: "+extension); | |
1137 | 63 } |
64 } | |
65 return response; | |
66 } | |
67 } |