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.HashSet;
31  import java.util.LinkedHashMap;
32  import java.util.Map;
33  import java.util.Map.Entry;
34  import java.util.Set;
35  import java.util.concurrent.locks.ReentrantLock;
36  
37  import org.apache.hc.core5.annotation.Contract;
38  import org.apache.hc.core5.annotation.ThreadingBehavior;
39  import org.apache.hc.core5.http.impl.routing.PathPatternMatcher;
40  import org.apache.hc.core5.util.Args;
41  
42  /**
43   * Maintains a map of objects keyed by a request URI pattern.
44   * <p>
45   * Patterns may have three formats:
46   * </p>
47   * <ul>
48   * <li>{@code *}</li>
49   * <li>{@code *<uri>}</li>
50   * <li>{@code <uri>*}</li>
51   * </ul>
52   * <p>
53   * This class can be used to resolve an object matching a particular request
54   * URI.
55   * </p>
56   *
57   * @param <T> The type of registered objects.
58   * @since 5.0
59   *
60   * @deprecated Use {@link org.apache.hc.core5.http.impl.routing.RequestRouter} for
61   * request routing.
62   */
63  @Contract(threading = ThreadingBehavior.SAFE)
64  @Deprecated
65  public class UriPatternOrderedMatcher<T> implements LookupRegistry<T> {
66  
67      private final Map<String, T> map;
68  
69      private final ReentrantLock lock = new ReentrantLock();
70  
71      public UriPatternOrderedMatcher() {
72          super();
73          this.map = new LinkedHashMap<>();
74      }
75  
76      /**
77       * Returns a {@link Set} view of the mappings contained in this matcher.
78       *
79       * @return a set view of the mappings contained in this matcher.
80       *
81       * @see Map#entrySet()
82       * @since 4.4.9
83       */
84      public Set<Entry<String, T>> entrySet() {
85          lock.lock();
86          try {
87              return new HashSet<>(map.entrySet());
88          } finally {
89              lock.unlock();
90          }
91      }
92  
93      /**
94       * Registers the given object for URIs matching the given pattern.
95       *
96       * @param pattern the pattern to register the handler for.
97       * @param obj     the object.
98       */
99      @Override
100     public void register(final String pattern, final T obj) {
101         lock.lock();
102         try {
103             Args.notNull(pattern, "URI request pattern");
104             this.map.put(pattern, obj);
105         } finally {
106             lock.unlock();
107         }
108     }
109 
110     /**
111      * Removes registered object, if exists, for the given pattern.
112      *
113      * @param pattern the pattern to unregister.
114      */
115     @Override
116     public void unregister(final String pattern) {
117         lock.lock();
118         try {
119             if (pattern == null) {
120                 return;
121             }
122             this.map.remove(pattern);
123         } finally {
124             lock.unlock();
125         }
126     }
127 
128     /**
129      * Looks up an object matching the given request path.
130      *
131      * @param path the request path
132      * @return object or {@code null} if no match is found.
133      */
134     @Override
135     public T lookup(final String path) {
136         lock.lock();
137         try {
138             Args.notNull(path, "Request path");
139             for (final Entry<String, T> entry : this.map.entrySet()) {
140                 final String pattern = entry.getKey();
141                 if (path.equals(pattern)) {
142                     return entry.getValue();
143                 }
144                 if (matchUriRequestPattern(pattern, path)) {
145                     return this.map.get(pattern);
146                 }
147             }
148             return null;
149         } finally {
150             lock.unlock();
151         }
152     }
153 
154     /**
155      * Tests if the given request path matches the given pattern.
156      *
157      * @param pattern the pattern
158      * @param path    the request path
159      * @return {@code true} if the request URI matches the pattern, {@code false}
160      *         otherwise.
161      */
162     protected boolean matchUriRequestPattern(final String pattern, final String path) {
163         return PathPatternMatcher.INSTANCE.match(pattern, path);
164     }
165 
166     @Override
167     public String toString() {
168         return this.map.toString();
169     }
170 
171 }