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