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.LinkedHashMap;
31  import java.util.Map;
32  import java.util.Map.Entry;
33  import java.util.regex.Pattern;
34  
35  import org.apache.hc.core5.annotation.Contract;
36  import org.apache.hc.core5.annotation.ThreadingBehavior;
37  import org.apache.hc.core5.util.Args;
38  
39  /**
40   * Maintains a map of objects keyed by a request URI regular expression.
41   *
42   * <p>
43   * The insertion order is in maintained in that map such that the lookup tests each regex until there is a match. This
44   * class can be used to resolve an object matching a particular request URI.
45   * </p>
46   *
47   * @param <T> The type of registered objects.
48   * @since 5.0
49   */
50  @Contract(threading = ThreadingBehavior.SAFE)
51  public class UriRegexMatcher<T> implements LookupRegistry<T> {
52  
53      private final Map<String, T> objectMap;
54      private final Map<String, Pattern> patternMap;
55  
56      public UriRegexMatcher() {
57          super();
58          this.objectMap = new LinkedHashMap<>();
59          this.patternMap = new LinkedHashMap<>();
60      }
61  
62      /**
63       * Registers the given object for URIs matching the given regex.
64       *
65       * @param regex
66       *            the regex to register the handler for.
67       * @param obj
68       *            the object.
69       */
70      @Override
71      public synchronized void register(final String regex, final T obj) {
72          Args.notNull(regex, "URI request regex");
73          this.objectMap.put(regex, obj);
74          this.patternMap.put(regex, Pattern.compile(regex));
75      }
76  
77      /**
78       * Removes registered object, if exists, for the given regex.
79       *
80       * @param regex
81       *            the regex to unregister.
82       */
83      @Override
84      public synchronized void unregister(final String regex) {
85          if (regex == null) {
86              return;
87          }
88          this.objectMap.remove(regex);
89          this.patternMap.remove(regex);
90      }
91  
92      /**
93       * Looks up an object matching the given request path.
94       *
95       * @param path
96       *            the request path
97       * @return object or {@code null} if no match is found.
98       */
99      @Override
100     public synchronized T lookup(final String path) {
101         Args.notNull(path, "Request path");
102         // direct match?
103         final T obj = this.objectMap.get(path);
104         if (obj == null) {
105             // regex match?
106             for (final Entry<String, Pattern> entry : this.patternMap.entrySet()) {
107                 if (entry.getValue().matcher(path).matches()) {
108                     return objectMap.get(entry.getKey());
109                 }
110             }
111         }
112         return obj;
113     }
114 
115     @Override
116     public String toString() {
117         return this.objectMap.toString();
118     }
119 
120 }