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.http.protocol;
29  
30  import java.util.HashMap;
31  import java.util.Map;
32  
33  import org.apache.http.annotation.ThreadingBehavior;
34  import org.apache.http.annotation.Contract;
35  import org.apache.http.util.Args;
36  
37  /**
38   * Maintains a map of objects keyed by a request URI pattern.
39   * <br>
40   * Patterns may have three formats:
41   * <ul>
42   *   <li>{@code *}</li>
43   *   <li>{@code *&lt;uri&gt;}</li>
44   *   <li>{@code &lt;uri&gt;*}</li>
45   * </ul>
46   * <br>
47   * This class can be used to resolve an object matching a particular request
48   * URI.
49   *
50   * @since 4.0
51   */
52  @Contract(threading = ThreadingBehavior.SAFE)
53  public class UriPatternMatcher<T> {
54  
55      private final Map<String, T> map;
56  
57      public UriPatternMatcher() {
58          super();
59          this.map = new HashMap<String, T>();
60      }
61  
62      /**
63       * Registers the given object for URIs matching the given pattern.
64       *
65       * @param pattern the pattern to register the handler for.
66       * @param obj the object.
67       */
68      public synchronized void register(final String pattern, final T obj) {
69          Args.notNull(pattern, "URI request pattern");
70          this.map.put(pattern, obj);
71      }
72  
73      /**
74       * Removes registered object, if exists, for the given pattern.
75       *
76       * @param pattern the pattern to unregister.
77       */
78      public synchronized void unregister(final String pattern) {
79          if (pattern == null) {
80              return;
81          }
82          this.map.remove(pattern);
83      }
84  
85      /**
86       * @deprecated (4.1) do not use
87       */
88      @Deprecated
89      public synchronized void setHandlers(final Map<String, T> map) {
90          Args.notNull(map, "Map of handlers");
91          this.map.clear();
92          this.map.putAll(map);
93      }
94  
95      /**
96       * @deprecated (4.1) do not use
97       */
98      @Deprecated
99      public synchronized void setObjects(final Map<String, T> map) {
100         Args.notNull(map, "Map of handlers");
101         this.map.clear();
102         this.map.putAll(map);
103     }
104 
105     /**
106      * @deprecated (4.1) do not use
107      */
108     @Deprecated
109     public synchronized Map<String, T> getObjects() {
110         return this.map;
111     }
112 
113     /**
114      * Looks up an object matching the given request path.
115      *
116      * @param path the request path
117      * @return object or {@code null} if no match is found.
118      */
119     public synchronized T lookup(final String path) {
120         Args.notNull(path, "Request path");
121         // direct match?
122         T obj = this.map.get(path);
123         if (obj == null) {
124             // pattern match?
125             String bestMatch = null;
126             for (final String pattern : this.map.keySet()) {
127                 if (matchUriRequestPattern(pattern, path)) {
128                     // we have a match. is it any better?
129                     if (bestMatch == null
130                             || (bestMatch.length() < pattern.length())
131                             || (bestMatch.length() == pattern.length() && pattern.endsWith("*"))) {
132                         obj = this.map.get(pattern);
133                         bestMatch = pattern;
134                     }
135                 }
136             }
137         }
138         return obj;
139     }
140 
141     /**
142      * Tests if the given request path matches the given pattern.
143      *
144      * @param pattern the pattern
145      * @param path the request path
146      * @return {@code true} if the request URI matches the pattern,
147      *   {@code false} otherwise.
148      */
149     protected boolean matchUriRequestPattern(final String pattern, final String path) {
150         if (pattern.equals("*")) {
151             return true;
152         } else {
153             return
154             (pattern.endsWith("*") && path.startsWith(pattern.substring(0, pattern.length() - 1))) ||
155             (pattern.startsWith("*") && path.endsWith(pattern.substring(1, pattern.length())));
156         }
157     }
158 
159     @Override
160     public String toString() {
161         return this.map.toString();
162     }
163 
164 }