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