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.client5.http.impl.async;
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.function.Supplier;
35  import org.apache.hc.core5.http.HttpRequest;
36  import org.apache.hc.core5.http.nio.AsyncPushConsumer;
37  import org.apache.hc.core5.http.protocol.UriPatternMatcher;
38  import org.apache.hc.core5.net.URIAuthority;
39  import org.apache.hc.core5.util.Args;
40  
41  class AsyncPushConsumerRegistry {
42  
43      private final UriPatternMatcher<Supplier<AsyncPushConsumer>> primary;
44      private final ConcurrentMap<String, UriPatternMatcher<Supplier<AsyncPushConsumer>>> hostMap;
45  
46      public AsyncPushConsumerRegistry() {
47          this.primary = new UriPatternMatcher<>();
48          this.hostMap = new ConcurrentHashMap<>();
49      }
50  
51      private UriPatternMatcher<Supplier<AsyncPushConsumer>> getPatternMatcher(final String hostname) {
52          if (hostname == null) {
53              return primary;
54          }
55          final UriPatternMatcher<Supplier<AsyncPushConsumer>> hostMatcher = hostMap.get(hostname);
56          if (hostMatcher != null) {
57              return hostMatcher;
58          }
59          return primary;
60      }
61  
62      public AsyncPushConsumer get(final HttpRequest request) {
63          Args.notNull(request, "Request");
64          final URIAuthority authority = request.getAuthority();
65          final String key = authority != null ? authority.getHostName().toLowerCase(Locale.ROOT) : null;
66          final UriPatternMatcher<Supplier<AsyncPushConsumer>> patternMatcher = getPatternMatcher(key);
67          if (patternMatcher == null) {
68              return null;
69          }
70          String path = request.getPath();
71          final int i = path.indexOf('?');
72          if (i != -1) {
73              path = path.substring(0, i);
74          }
75          final Supplier<AsyncPushConsumer> supplier = patternMatcher.lookup(path);
76          return supplier != null ? supplier.get() : null;
77      }
78  
79      public void register(final String hostname, final String uriPattern, final Supplier<AsyncPushConsumer> supplier) {
80          Args.notBlank(uriPattern, "URI pattern");
81          Args.notNull(supplier, "Supplier");
82          if (hostname == null) {
83              primary.register(uriPattern, supplier);
84          } else {
85              final String key = hostname.toLowerCase(Locale.ROOT);
86              UriPatternMatcher<Supplier<AsyncPushConsumer>> matcher = hostMap.get(key);
87              if (matcher == null) {
88                  final UriPatternMatcher<Supplier<AsyncPushConsumer>> newMatcher = new UriPatternMatcher<>();
89                  matcher = hostMap.putIfAbsent(key, newMatcher);
90                  if (matcher == null) {
91                      matcher = newMatcher;
92                  }
93              }
94              matcher.register(uriPattern, supplier);
95          }
96      }
97  
98  }