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