Mercurial Hosting > luan
annotate src/goodjava/webserver/handlers/ContentTypeHandler.java @ 1820:f3ec7f053078 default tip
minor
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 30 Jun 2024 20:54:36 -0600 |
parents | e647c92a4a12 |
children |
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" ); |
1772 | 39 map.put( "ico", "image/x-icon" ); |
1781 | 40 map.put( "mov", "video/quicktime" ); |
1607 | 41 // add more as need |
1137 | 42 } |
43 | |
1607 | 44 public static String getExtension(String path) { |
45 int iSlash = path.lastIndexOf('/'); | |
46 int iDot = path.lastIndexOf('.'); | |
47 return iDot > iSlash ? path.substring(iDot+1).toLowerCase() : null; | |
48 } | |
49 | |
50 private final Handler handler; | |
51 | |
52 public ContentTypeHandler(Handler handler) { | |
1137 | 53 this.handler = handler; |
54 } | |
55 | |
56 public Response handle(Request request) { | |
57 Response response = handler.handle(request); | |
1607 | 58 if( response!=null && response.status.code==200 && !response.headers.containsKey("Content-Type") ) { |
59 String extension = getExtension(request.path); | |
60 if( extension != null ) { | |
61 String type = map.get(extension); | |
62 if( type != null ) | |
63 response.headers.put("Content-Type",type); | |
64 else | |
1736
a02a75e3daa8
webserver error handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1734
diff
changeset
|
65 logger.info(request.url()+": no type defined for extension: "+extension); |
1137 | 66 } |
67 } | |
68 return response; | |
69 } | |
70 } |