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   * @deprecated Use {@link org.apache.hc.core5.http.impl.routing.RequestRouter} for
52   * request routing.
53   */
54  @Contract(threading = ThreadingBehavior.SAFE)
55  @Deprecated
56  public class UriRegexMatcher<T> implements LookupRegistry<T> {
57  
58      private final Map<String, T> objectMap;
59      private final Map<String, Pattern> patternMap;
60  
61      private final ReentrantLock lock;
62  
63      public UriRegexMatcher() {
64          super();
65          this.objectMap = new LinkedHashMap<>();
66          this.patternMap = new LinkedHashMap<>();
67          this.lock = new ReentrantLock();
68      }
69  
70      /**
71       * Registers the given object for URIs matching the given regex.
72       *
73       * @param regex
74       *            the regex to register the handler for.
75       * @param obj
76       *            the object.
77       */
78      @Override
79      public void register(final String regex, final T obj) {
80          lock.lock();
81          try {
82              Args.notNull(regex, "URI request regex");
83              this.objectMap.put(regex, obj);
84              this.patternMap.put(regex, Pattern.compile(regex));
85          } finally {
86              lock.unlock();
87          }
88      }
89  
90      /**
91       * Removes registered object, if exists, for the given regex.
92       *
93       * @param regex
94       *            the regex to unregister.
95       */
96      @Override
97      public void unregister(final String regex) {
98          lock.lock();
99          try {
100             if (regex == null) {
101                 return;
102             }
103             this.objectMap.remove(regex);
104             this.patternMap.remove(regex);
105         } finally {
106             lock.unlock();
107         }
108     }
109 
110     /**
111      * Looks up an object matching the given request path.
112      *
113      * @param path
114      *            the request path
115      * @return object or {@code null} if no match is found.
116      */
117     @Override
118     public T lookup(final String path) {
119         lock.lock();
120         try {
121             Args.notNull(path, "Request path");
122             // direct match?
123             final T obj = this.objectMap.get(path);
124             if (obj == null) {
125                 // regex match?
126                 for (final Entry<String, Pattern> entry : this.patternMap.entrySet()) {
127                     if (entry.getValue().matcher(path).matches()) {
128                         return objectMap.get(entry.getKey());
129                     }
130                 }
131             }
132             return obj;
133         } finally {
134             lock.unlock();
135         }
136     }
137 
138     @Override
139     public String toString() {
140         return this.objectMap.toString();
141     }
142 
143 }