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   * @since 5.0
48   */
49  @Contract(threading = ThreadingBehavior.SAFE)
50  public class UriRegexMatcher<T> implements LookupRegistry<T> {
51  
52      private final Map<String, T> objectMap;
53      private final Map<String, Pattern> patternMap;
54  
55      public UriRegexMatcher() {
56          super();
57          this.objectMap = new LinkedHashMap<>();
58          this.patternMap = new LinkedHashMap<>();
59      }
60  
61      /**
62       * Registers the given object for URIs matching the given regex.
63       *
64       * @param regex
65       *            the regex to register the handler for.
66       * @param obj
67       *            the object.
68       */
69      @Override
70      public synchronized void register(final String regex, final T obj) {
71          Args.notNull(regex, "URI request regex");
72          this.objectMap.put(regex, obj);
73          this.patternMap.put(regex, Pattern.compile(regex));
74      }
75  
76      /**
77       * Removes registered object, if exists, for the given regex.
78       *
79       * @param regex
80       *            the regex to unregister.
81       */
82      @Override
83      public synchronized void unregister(final String regex) {
84          if (regex == null) {
85              return;
86          }
87          this.objectMap.remove(regex);
88          this.patternMap.remove(regex);
89      }
90  
91      /**
92       * Looks up an object matching the given request path.
93       *
94       * @param path
95       *            the request path
96       * @return object or {@code null} if no match is found.
97       */
98      @Override
99      public synchronized T lookup(final String path) {
100         Args.notNull(path, "Request path");
101         // direct match?
102         final T obj = this.objectMap.get(path);
103         if (obj == null) {
104             // regex match?
105             for (final Entry<String, Pattern> entry : this.patternMap.entrySet()) {
106                 if (entry.getValue().matcher(path).matches()) {
107                     return objectMap.get(entry.getKey());
108                 }
109             }
110         }
111         return obj;
112     }
113 
114     @Override
115     public String toString() {
116         return this.objectMap.toString();
117     }
118 
119 }