annotate src/goodjava/webserver/handlers/DomainHandler.java @ 1426:94a6a209d4e2

fix
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 24 Nov 2019 16:38:06 -0700
parents 546edc2df02c
children 42c07ecb0ddc
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: 1401
diff changeset
1 package goodjava.webserver.handlers;
1315
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 import java.io.Closeable;
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 import java.io.IOException;
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 import java.lang.ref.Reference;
1401
ef1620aa99cb fix gc issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1400
diff changeset
6 //import java.lang.ref.WeakReference;
1315
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 import java.lang.ref.SoftReference;
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 import java.lang.ref.ReferenceQueue;
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 import java.util.Map;
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 import java.util.HashMap;
1402
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1401
diff changeset
11 import goodjava.logging.Logger;
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1401
diff changeset
12 import goodjava.logging.LoggerFactory;
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1401
diff changeset
13 import goodjava.webserver.Handler;
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1401
diff changeset
14 import goodjava.webserver.Request;
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1401
diff changeset
15 import goodjava.webserver.Response;
1315
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 public final class DomainHandler implements Handler {
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 private static final Logger logger = LoggerFactory.getLogger(DomainHandler.class);
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 public interface Factory {
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 public Handler newHandler(String domain);
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 }
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 private static void close(Handler handler) {
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 if( handler instanceof Closeable ) {
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 try {
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 ((Closeable)handler).close();
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 } catch(IOException e) {
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 logger.error(handler.toString(),e);
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 }
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 }
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 }
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34
1401
ef1620aa99cb fix gc issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1400
diff changeset
35 private final Map<String,Reference<Handler>> map = new HashMap<String,Reference<Handler>>();
1315
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 private final Factory factory;
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 public DomainHandler(Factory factory) {
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 this.factory = factory;
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 }
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 public Response handle(Request request) {
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 String host = (String)request.headers.get("host");
1331
02fe660e7748 minor fix
Franklin Schmidt <fschmidt@gmail.com>
parents: 1315
diff changeset
45 if( host == null )
02fe660e7748 minor fix
Franklin Schmidt <fschmidt@gmail.com>
parents: 1315
diff changeset
46 return null;
1315
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 int i = host.indexOf(':');
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 String domain = i == -1 ? host : host.substring(0,i);
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 Handler handler = getHandler(domain);
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 return handler==null ? null : handler.handle(request);
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 }
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 public Handler getHandler(String domain) {
1401
ef1620aa99cb fix gc issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1400
diff changeset
54 domain = domain.toLowerCase();
ef1620aa99cb fix gc issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1400
diff changeset
55 synchronized(map) {
ef1620aa99cb fix gc issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1400
diff changeset
56 Reference<Handler> ref = map.get(domain);
ef1620aa99cb fix gc issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1400
diff changeset
57 Handler handler = ref==null ? null : ref.get();
ef1620aa99cb fix gc issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1400
diff changeset
58 if( handler == null ) {
ef1620aa99cb fix gc issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1400
diff changeset
59 //if(ref!=null) logger.info("gc "+domain);
ef1620aa99cb fix gc issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1400
diff changeset
60 handler = factory.newHandler(domain);
ef1620aa99cb fix gc issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1400
diff changeset
61 if( handler == null )
ef1620aa99cb fix gc issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1400
diff changeset
62 return null;
ef1620aa99cb fix gc issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1400
diff changeset
63 map.put(domain,new SoftReference<Handler>(handler));
ef1620aa99cb fix gc issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1400
diff changeset
64 }
ef1620aa99cb fix gc issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1400
diff changeset
65 return handler;
ef1620aa99cb fix gc issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1400
diff changeset
66 }
1315
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 }
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
69 public void removeHandler(String domain) {
1405
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
70 //logger.info("removeHandler "+domain);
1315
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71 domain = domain.toLowerCase();
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 synchronized(map) {
1401
ef1620aa99cb fix gc issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1400
diff changeset
73 Reference<Handler> ref = map.remove(domain);
ef1620aa99cb fix gc issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1400
diff changeset
74 Handler handler = ref==null ? null : ref.get();
ef1620aa99cb fix gc issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1400
diff changeset
75 if( handler != null ) {
ef1620aa99cb fix gc issues
Franklin Schmidt <fschmidt@gmail.com>
parents: 1400
diff changeset
76 close(handler);
1315
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
77 }
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
78 }
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79 }
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
80
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
81 }