1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.hc.core5.http.protocol;
29
30 import java.util.Locale;
31 import java.util.concurrent.ConcurrentHashMap;
32 import java.util.concurrent.ConcurrentMap;
33
34 import org.apache.hc.core5.annotation.Contract;
35 import org.apache.hc.core5.annotation.ThreadingBehavior;
36 import org.apache.hc.core5.function.Supplier;
37 import org.apache.hc.core5.http.HttpRequest;
38 import org.apache.hc.core5.http.HttpRequestMapper;
39 import org.apache.hc.core5.http.MisdirectedRequestException;
40 import org.apache.hc.core5.net.URIAuthority;
41 import org.apache.hc.core5.util.Args;
42
43
44
45
46
47
48
49
50 @Contract(threading = ThreadingBehavior.SAFE_CONDITIONAL)
51 public class RequestHandlerRegistry<T> implements HttpRequestMapper<T> {
52
53 private final static String LOCALHOST = "localhost";
54 private final static String IP_127_0_0_1 = "127.0.0.1";
55
56 private final String canonicalHostName;
57 private final Supplier<LookupRegistry<T>> registrySupplier;
58 private final LookupRegistry<T> primary;
59 private final ConcurrentMap<String, LookupRegistry<T>> virtualMap;
60
61 public RequestHandlerRegistry(final String canonicalHostName, final Supplier<LookupRegistry<T>> registrySupplier) {
62 this.canonicalHostName = Args.notNull(canonicalHostName, "Canonical hostname").toLowerCase(Locale.ROOT);
63 this.registrySupplier = registrySupplier != null ? registrySupplier : new Supplier<LookupRegistry<T>>() {
64
65 @Override
66 public LookupRegistry<T> get() {
67 return new UriPatternMatcher<>();
68 }
69
70 };
71 this.primary = this.registrySupplier.get();
72 this.virtualMap = new ConcurrentHashMap<>();
73 }
74
75 public RequestHandlerRegistry(final String canonicalHostName, final UriPatternType patternType) {
76 this(canonicalHostName, new Supplier<LookupRegistry<T>>() {
77
78 @Override
79 public LookupRegistry<T> get() {
80 return UriPatternType.newMatcher(patternType);
81 }
82
83 });
84 }
85
86 public RequestHandlerRegistry(final UriPatternType patternType) {
87 this(LOCALHOST, patternType);
88 }
89
90 public RequestHandlerRegistry() {
91 this(LOCALHOST, UriPatternType.URI_PATTERN);
92 }
93
94 private LookupRegistry<T> getPatternMatcher(final String hostname) {
95 if (hostname == null ||
96 hostname.equals(canonicalHostName) || hostname.equals(LOCALHOST) || hostname.equals(IP_127_0_0_1)) {
97 return primary;
98 }
99 return virtualMap.get(hostname);
100 }
101
102 @Override
103 public T resolve(final HttpRequest request, final HttpContext context) throws MisdirectedRequestException {
104 final URIAuthority authority = request.getAuthority();
105 final String key = authority != null ? authority.getHostName().toLowerCase(Locale.ROOT) : null;
106 final LookupRegistry<T> patternMatcher = getPatternMatcher(key);
107 if (patternMatcher == null) {
108 throw new MisdirectedRequestException("Not authoritative");
109 }
110 String path = request.getPath();
111 final int i = path.indexOf('?');
112 if (i != -1) {
113 path = path.substring(0, i);
114 }
115 return patternMatcher.lookup(path);
116 }
117
118 public void register(final String hostname, final String uriPattern, final T object) {
119 Args.notBlank(uriPattern, "URI pattern");
120 if (object == null) {
121 return;
122 }
123 final String key = hostname != null ? hostname.toLowerCase(Locale.ROOT) : null;
124 if (hostname == null || hostname.equals(canonicalHostName) || hostname.equals(LOCALHOST)) {
125 primary.register(uriPattern, object);
126 } else {
127 LookupRegistry<T> patternMatcher = virtualMap.get(key);
128 if (patternMatcher == null) {
129 final LookupRegistry<T> newPatternMatcher = registrySupplier.get();
130 patternMatcher = virtualMap.putIfAbsent(key, newPatternMatcher);
131 if (patternMatcher == null) {
132 patternMatcher = newPatternMatcher;
133 }
134 }
135 patternMatcher.register(uriPattern, object);
136 }
137 }
138
139 }