|
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 ch.boye.httpclientandroidlib.protocol; |
|
29 |
|
30 import java.util.HashMap; |
|
31 import java.util.Iterator; |
|
32 import java.util.Map; |
|
33 |
|
34 /** |
|
35 * Maintains a map of objects keyed by a request URI pattern. |
|
36 * <br> |
|
37 * Patterns may have three formats: |
|
38 * <ul> |
|
39 * <li><code>*</code></li> |
|
40 * <li><code>*<uri></code></li> |
|
41 * <li><code><uri>*</code></li> |
|
42 * </ul> |
|
43 * <br> |
|
44 * This class can be used to resolve an object matching a particular request |
|
45 * URI. |
|
46 * |
|
47 * @since 4.0 |
|
48 */ |
|
49 public class UriPatternMatcher { |
|
50 |
|
51 /** |
|
52 * TODO: Replace with ConcurrentHashMap |
|
53 */ |
|
54 private final Map map; |
|
55 |
|
56 public UriPatternMatcher() { |
|
57 super(); |
|
58 this.map = new HashMap(); |
|
59 } |
|
60 |
|
61 /** |
|
62 * Registers the given object for URIs matching the given pattern. |
|
63 * |
|
64 * @param pattern the pattern to register the handler for. |
|
65 * @param obj the object. |
|
66 */ |
|
67 public synchronized void register(final String pattern, final Object obj) { |
|
68 if (pattern == null) { |
|
69 throw new IllegalArgumentException("URI request pattern may not be null"); |
|
70 } |
|
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 use {@link #setObjects(Map)} |
|
88 */ |
|
89 public synchronized void setHandlers(final Map map) { |
|
90 if (map == null) { |
|
91 throw new IllegalArgumentException("Map of handlers may not be null"); |
|
92 } |
|
93 this.map.clear(); |
|
94 this.map.putAll(map); |
|
95 } |
|
96 |
|
97 /** |
|
98 * Sets objects from the given map. |
|
99 * @param map the map containing objects keyed by their URI patterns. |
|
100 */ |
|
101 public synchronized void setObjects(final Map map) { |
|
102 if (map == null) { |
|
103 throw new IllegalArgumentException("Map of handlers may not be null"); |
|
104 } |
|
105 this.map.clear(); |
|
106 this.map.putAll(map); |
|
107 } |
|
108 |
|
109 /** |
|
110 * Looks up an object matching the given request URI. |
|
111 * |
|
112 * @param requestURI the request URI |
|
113 * @return object or <code>null</code> if no match is found. |
|
114 */ |
|
115 public synchronized Object lookup(String requestURI) { |
|
116 if (requestURI == null) { |
|
117 throw new IllegalArgumentException("Request URI may not be null"); |
|
118 } |
|
119 //Strip away the query part part if found |
|
120 int index = requestURI.indexOf("?"); |
|
121 if (index != -1) { |
|
122 requestURI = requestURI.substring(0, index); |
|
123 } |
|
124 |
|
125 // direct match? |
|
126 Object obj = this.map.get(requestURI); |
|
127 if (obj == null) { |
|
128 // pattern match? |
|
129 String bestMatch = null; |
|
130 for (Iterator it = this.map.keySet().iterator(); it.hasNext();) { |
|
131 String pattern = (String) it.next(); |
|
132 if (matchUriRequestPattern(pattern, requestURI)) { |
|
133 // we have a match. is it any better? |
|
134 if (bestMatch == null |
|
135 || (bestMatch.length() < pattern.length()) |
|
136 || (bestMatch.length() == pattern.length() && pattern.endsWith("*"))) { |
|
137 obj = this.map.get(pattern); |
|
138 bestMatch = pattern; |
|
139 } |
|
140 } |
|
141 } |
|
142 } |
|
143 return obj; |
|
144 } |
|
145 |
|
146 /** |
|
147 * Tests if the given request URI matches the given pattern. |
|
148 * |
|
149 * @param pattern the pattern |
|
150 * @param requestUri the request URI |
|
151 * @return <code>true</code> if the request URI matches the pattern, |
|
152 * <code>false</code> otherwise. |
|
153 */ |
|
154 protected boolean matchUriRequestPattern(final String pattern, final String requestUri) { |
|
155 if (pattern.equals("*")) { |
|
156 return true; |
|
157 } else { |
|
158 return |
|
159 (pattern.endsWith("*") && requestUri.startsWith(pattern.substring(0, pattern.length() - 1))) || |
|
160 (pattern.startsWith("*") && requestUri.endsWith(pattern.substring(1, pattern.length()))); |
|
161 } |
|
162 } |
|
163 |
|
164 } |