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.concurrent.ConcurrentHashMap;
31 import java.util.concurrent.ConcurrentMap;
32
33 import org.apache.hc.core5.annotation.Contract;
34 import org.apache.hc.core5.annotation.ThreadingBehavior;
35 import org.apache.hc.core5.function.Supplier;
36 import org.apache.hc.core5.http.HttpRequest;
37 import org.apache.hc.core5.http.HttpRequestMapper;
38 import org.apache.hc.core5.http.MisdirectedRequestException;
39 import org.apache.hc.core5.net.URIAuthority;
40 import org.apache.hc.core5.util.Args;
41 import org.apache.hc.core5.util.TextUtils;
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 = TextUtils.toLowerCase(Args.notNull(canonicalHostName, "Canonical hostname"));
63 this.registrySupplier = registrySupplier != null ? registrySupplier : UriPatternMatcher::new;
64 this.primary = this.registrySupplier.get();
65 this.virtualMap = new ConcurrentHashMap<>();
66 }
67
68 public RequestHandlerRegistry(final String canonicalHostName, final UriPatternType patternType) {
69 this(canonicalHostName, () -> UriPatternType.newMatcher(patternType));
70 }
71
72 public RequestHandlerRegistry(final UriPatternType patternType) {
73 this(LOCALHOST, patternType);
74 }
75
76 public RequestHandlerRegistry() {
77 this(LOCALHOST, UriPatternType.URI_PATTERN);
78 }
79
80 private LookupRegistry<T> getPatternMatcher(final String hostname) {
81 if (hostname == null ||
82 hostname.equals(canonicalHostName) || hostname.equals(LOCALHOST) || hostname.equals(IP_127_0_0_1)) {
83 return primary;
84 }
85 return virtualMap.get(hostname);
86 }
87
88 @Override
89 public T resolve(final HttpRequest request, final HttpContext context) throws MisdirectedRequestException {
90 final URIAuthority authority = request.getAuthority();
91 final String key = authority != null ? TextUtils.toLowerCase(authority.getHostName()) : null;
92 final LookupRegistry<T> patternMatcher = getPatternMatcher(key);
93 if (patternMatcher == null) {
94 throw new MisdirectedRequestException("Not authoritative");
95 }
96 String path = request.getPath();
97 final int i = path.indexOf('?');
98 if (i != -1) {
99 path = path.substring(0, i);
100 }
101 return patternMatcher.lookup(path);
102 }
103
104 public void register(final String hostname, final String uriPattern, final T object) {
105 Args.notBlank(uriPattern, "URI pattern");
106 if (object == null) {
107 return;
108 }
109 final String key = TextUtils.toLowerCase(hostname);
110 if (hostname == null || hostname.equals(canonicalHostName) || hostname.equals(LOCALHOST)) {
111 primary.register(uriPattern, object);
112 } else {
113 LookupRegistry<T> patternMatcher = virtualMap.get(key);
114 if (patternMatcher == null) {
115 final LookupRegistry<T> newPatternMatcher = registrySupplier.get();
116 patternMatcher = virtualMap.putIfAbsent(key, newPatternMatcher);
117 if (patternMatcher == null) {
118 patternMatcher = newPatternMatcher;
119 }
120 }
121 patternMatcher.register(uriPattern, object);
122 }
123 }
124
125 }