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
51
52 @Contract(threading = ThreadingBehavior.SAFE_CONDITIONAL)
53 @Deprecated
54 public class RequestHandlerRegistry<T> implements HttpRequestMapper<T> {
55
56 private final static String LOCALHOST = "localhost";
57 private final static String IP_127_0_0_1 = "127.0.0.1";
58
59 private final String canonicalHostName;
60 private final Supplier<LookupRegistry<T>> registrySupplier;
61 private final LookupRegistry<T> primary;
62 private final ConcurrentMap<String, LookupRegistry<T>> virtualMap;
63
64 public RequestHandlerRegistry(final String canonicalHostName, final Supplier<LookupRegistry<T>> registrySupplier) {
65 this.canonicalHostName = TextUtils.toLowerCase(Args.notNull(canonicalHostName, "Canonical hostname"));
66 this.registrySupplier = registrySupplier != null ? registrySupplier : UriPatternMatcher::new;
67 this.primary = this.registrySupplier.get();
68 this.virtualMap = new ConcurrentHashMap<>();
69 }
70
71 static <T> LookupRegistry<T> newMatcher(final UriPatternType type) {
72 if (type == null) {
73 return new UriPatternMatcher<>();
74 }
75 switch (type) {
76 case REGEX:
77 return new UriRegexMatcher<>();
78 case URI_PATTERN_IN_ORDER:
79 return new UriPatternOrderedMatcher<>();
80 case URI_PATTERN:
81 default:
82 return new UriPatternMatcher<>();
83 }
84 }
85
86 public RequestHandlerRegistry(final String canonicalHostName, final UriPatternType patternType) {
87 this(canonicalHostName, () -> newMatcher(patternType));
88 }
89
90 public RequestHandlerRegistry(final UriPatternType patternType) {
91 this(LOCALHOST, patternType);
92 }
93
94 public RequestHandlerRegistry() {
95 this(LOCALHOST, UriPatternType.URI_PATTERN);
96 }
97
98 private LookupRegistry<T> getPatternMatcher(final String hostname) {
99 if (hostname == null ||
100 hostname.equals(canonicalHostName) || hostname.equals(LOCALHOST) || hostname.equals(IP_127_0_0_1)) {
101 return primary;
102 }
103 return virtualMap.get(hostname);
104 }
105
106 @Override
107 public T resolve(final HttpRequest request, final HttpContext context) throws MisdirectedRequestException {
108 final URIAuthority authority = request.getAuthority();
109 final String key = authority != null ? TextUtils.toLowerCase(authority.getHostName()) : null;
110 final LookupRegistry<T> patternMatcher = getPatternMatcher(key);
111 if (patternMatcher == null) {
112 throw new MisdirectedRequestException("Not authoritative");
113 }
114 String path = request.getPath();
115 final int i = path.indexOf('?');
116 if (i != -1) {
117 path = path.substring(0, i);
118 }
119 return patternMatcher.lookup(path);
120 }
121
122 public void register(final String hostname, final String uriPattern, final T object) {
123 Args.notBlank(uriPattern, "URI pattern");
124 if (object == null) {
125 return;
126 }
127 final String key = TextUtils.toLowerCase(hostname);
128 if (hostname == null || hostname.equals(canonicalHostName) || hostname.equals(LOCALHOST)) {
129 primary.register(uriPattern, object);
130 } else {
131 LookupRegistry<T> patternMatcher = virtualMap.get(key);
132 if (patternMatcher == null) {
133 final LookupRegistry<T> newPatternMatcher = registrySupplier.get();
134 patternMatcher = virtualMap.putIfAbsent(key, newPatternMatcher);
135 if (patternMatcher == null) {
136 patternMatcher = newPatternMatcher;
137 }
138 }
139 patternMatcher.register(uriPattern, object);
140 }
141 }
142
143 }