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