Mercurial Hosting > luan
annotate src/goodjava/webserver/handlers/ContentTypeHandler.java @ 1769:9d9683e76496
bug fix
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 11 Jun 2023 19:17:26 -0600 |
parents | db52c29605e2 |
children | f7b90d9a4639 |
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" ); |
1744 | 38 map.put( "svg", "image/svg+xml" ); |
1607 | 39 // add more as need |
1137 | 40 } |
41 | |
1607 | 42 public static String getExtension(String path) { |
43 int iSlash = path.lastIndexOf('/'); | |
44 int iDot = path.lastIndexOf('.'); | |
45 return iDot > iSlash ? path.substring(iDot+1).toLowerCase() : null; | |
46 } | |
47 | |
48 private final Handler handler; | |
49 | |
50 public ContentTypeHandler(Handler handler) { | |
1137 | 51 this.handler = handler; |
52 } | |
53 | |
54 public Response handle(Request request) { | |
55 Response response = handler.handle(request); | |
1607 | 56 if( response!=null && response.status.code==200 && !response.headers.containsKey("Content-Type") ) { |
57 String extension = getExtension(request.path); | |
58 if( extension != null ) { | |
59 String type = map.get(extension); | |
60 if( type != null ) | |
61 response.headers.put("Content-Type",type); | |
62 else | |
1736
a02a75e3daa8
webserver error handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1734
diff
changeset
|
63 logger.info(request.url()+": no type defined for extension: "+extension); |
1137 | 64 } |
65 } | |
66 return response; | |
67 } | |
68 } |