View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
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  @Contract(threading = ThreadingBehavior.SAFE_CONDITIONAL)
44  public class RequestHandlerRegistry<T> implements HttpRequestMapper<T> {
45  
46      private final static String LOCALHOST = "localhost";
47  
48      private final String canonicalHostName;
49      private final Supplier<LookupRegistry<T>> registrySupplier;
50      private final LookupRegistry<T> primary;
51      private final ConcurrentMap<String, LookupRegistry<T>> virtualMap;
52  
53      public RequestHandlerRegistry(final String canonicalHostName, final Supplier<LookupRegistry<T>> registrySupplier) {
54          this.canonicalHostName = Args.notNull(canonicalHostName, "Canonical hostname").toLowerCase(Locale.ROOT);
55          this.registrySupplier = registrySupplier != null ? registrySupplier : new Supplier<LookupRegistry<T>>() {
56  
57              @Override
58              public LookupRegistry<T> get() {
59                  return new UriPatternMatcher<>();
60              }
61  
62          };
63          this.primary = this.registrySupplier.get();
64          this.virtualMap = new ConcurrentHashMap<>();
65      }
66  
67  
68  
69      public RequestHandlerRegistry(final String canonicalHostName, final UriPatternType patternType) {
70          this(canonicalHostName, new Supplier<LookupRegistry<T>>() {
71  
72              @Override
73              public LookupRegistry<T> get() {
74                  return UriPatternType.newMatcher(patternType);
75              }
76  
77          });
78      }
79  
80      public RequestHandlerRegistry(final UriPatternType patternType) {
81          this(LOCALHOST, patternType);
82      }
83  
84      public RequestHandlerRegistry() {
85          this(LOCALHOST, UriPatternType.BASIC);
86      }
87  
88      private LookupRegistry<T> getPatternMatcher(final String hostname) {
89          if (hostname == null) {
90              return primary;
91          }
92          if (hostname.equals(canonicalHostName) || hostname.equals(LOCALHOST)) {
93              return primary;
94          }
95          return virtualMap.get(hostname);
96      }
97  
98      @Override
99      public T resolve(final HttpRequest request, final HttpContext context) throws MisdirectedRequestException {
100         final URIAuthority authority = request.getAuthority();
101         final String key = authority != null ? authority.getHostName().toLowerCase(Locale.ROOT) : null;
102         final LookupRegistry<T> patternMatcher = getPatternMatcher(key);
103         if (patternMatcher == null) {
104             throw new MisdirectedRequestException("Not authoritative");
105         }
106         String path = request.getPath();
107         final int i = path.indexOf('?');
108         if (i != -1) {
109             path = path.substring(0, i);
110         }
111         return patternMatcher.lookup(path);
112     }
113 
114     public void register(final String hostname, final String uriPattern, final T object) {
115         Args.notBlank(uriPattern, "URI pattern");
116         if (object == null) {
117             return;
118         }
119         final String key = hostname != null ? hostname.toLowerCase(Locale.ROOT) : null;
120         if (hostname == null || hostname.equals(canonicalHostName) || hostname.equals(LOCALHOST)) {
121             primary.register(uriPattern, object);
122         } else {
123             LookupRegistry<T> patternMatcher = virtualMap.get(key);
124             if (patternMatcher == null) {
125                 final LookupRegistry<T> newPatternMatcher = registrySupplier.get();
126                 patternMatcher = virtualMap.putIfAbsent(key, newPatternMatcher);
127                 if (patternMatcher == null) {
128                     patternMatcher = newPatternMatcher;
129                 }
130             }
131             patternMatcher.register(uriPattern, object);
132         }
133     }
134 
135 }