annotate src/goodjava/webserver/handlers/ContentTypeHandler.java @ 1624:fe611f6e3c28

more content types
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 07 Aug 2021 20:01:46 -0600
parents fa066aaa068c
children 8a6e48371c66
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 import java.util.Map;
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 import java.util.HashMap;
1607
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 1510
diff changeset
5 import goodjava.logging.Logger;
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 1510
diff changeset
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
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 public class ContentTypeHandler implements Handler {
1607
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 1510
diff changeset
13 private static final Logger logger = LoggerFactory.getLogger(ContentTypeHandler.class);
1137
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 // maps extension to content-type
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 // key must be lower case
1607
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 1510
diff changeset
17 public static final Map<String,String> map = new HashMap<String,String>();
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 1510
diff changeset
18 static {
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 1510
diff changeset
19 String textType = "text/plain; charset=utf-8";
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 1510
diff changeset
20 map.put( "txt", textType );
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 1510
diff changeset
21 map.put( "luan", textType );
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 1510
diff changeset
22 map.put( "luano", textType );
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 1510
diff changeset
23 map.put( "log", textType );
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 1510
diff changeset
24 map.put( "html", "text/html; charset=utf-8" );
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 1510
diff changeset
25 map.put( "css", "text/css; charset=utf-8" );
1624
fe611f6e3c28 more content types
Franklin Schmidt <fschmidt@gmail.com>
parents: 1607
diff changeset
26 map.put( "xml", "text/xml; charset=utf-8" );
1607
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 1510
diff changeset
27 map.put( "js", "application/javascript; charset=utf-8" );
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 1510
diff changeset
28 map.put( "json", "application/json; charset=utf-8" );
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 1510
diff changeset
29 map.put( "mp4", "video/mp4" );
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 1510
diff changeset
30 map.put( "jpg", "image/jpeg" );
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 1510
diff changeset
31 map.put( "jpeg", "image/jpeg" );
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 1510
diff changeset
32 map.put( "png", "image/png" );
1624
fe611f6e3c28 more content types
Franklin Schmidt <fschmidt@gmail.com>
parents: 1607
diff changeset
33 map.put( "gif", "image/gif" );
fe611f6e3c28 more content types
Franklin Schmidt <fschmidt@gmail.com>
parents: 1607
diff changeset
34 map.put( "pdf", "application/pdf" );
1607
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 1510
diff changeset
35 // add more as need
1137
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 }
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37
1607
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 1510
diff changeset
38 public static String getExtension(String path) {
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 1510
diff changeset
39 int iSlash = path.lastIndexOf('/');
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 1510
diff changeset
40 int iDot = path.lastIndexOf('.');
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 1510
diff changeset
41 return iDot > iSlash ? path.substring(iDot+1).toLowerCase() : null;
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 1510
diff changeset
42 }
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 1510
diff changeset
43
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 1510
diff changeset
44 private final Handler handler;
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 1510
diff changeset
45
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 1510
diff changeset
46 public ContentTypeHandler(Handler handler) {
1137
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 this.handler = handler;
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 }
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 public Response handle(Request request) {
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 Response response = handler.handle(request);
1607
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 1510
diff changeset
52 if( response!=null && response.status.code==200 && !response.headers.containsKey("Content-Type") ) {
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 1510
diff changeset
53 String extension = getExtension(request.path);
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 1510
diff changeset
54 if( extension != null ) {
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 1510
diff changeset
55 String type = map.get(extension);
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 1510
diff changeset
56 if( type != null )
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 1510
diff changeset
57 response.headers.put("Content-Type",type);
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 1510
diff changeset
58 else
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 1510
diff changeset
59 logger.info("no type defined for extension: "+extension);
1137
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 }
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 }
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 return response;
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 }
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 }