001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/main/java/org/apache/http/protocol/HttpRequestHandlerRegistry.java $
003: * $Revision: 613298 $
004: * $Date: 2008-01-18 23:09:22 +0100 (Fri, 18 Jan 2008) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.protocol;
033:
034: import java.util.HashMap;
035: import java.util.Iterator;
036: import java.util.Map;
037:
038: /**
039: * Maintains a map of HTTP request handlers keyed by a request URI pattern.
040: * {@link HttpRequestHandler} instances can be looked up by request URI
041: * using the {@link HttpRequestHandlerResolver} interface.<br/>
042: * Patterns may have three formats:
043: * <ul>
044: * <li><code>*</code></li>
045: * <li><code>*<uri></code></li>
046: * <li><code><uri>*</code></li>
047: * </ul>
048: *
049: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
050: *
051: * @version $Revision: 613298 $
052: */
053: public class HttpRequestHandlerRegistry implements
054: HttpRequestHandlerResolver {
055:
056: private final Map handlerMap;
057:
058: public HttpRequestHandlerRegistry() {
059: super ();
060: this .handlerMap = new HashMap();
061: }
062:
063: public void register(final String pattern,
064: final HttpRequestHandler handler) {
065: if (pattern == null) {
066: throw new IllegalArgumentException(
067: "URI request pattern may not be null");
068: }
069: if (handler == null) {
070: throw new IllegalArgumentException(
071: "HTTP request handelr may not be null");
072: }
073: this .handlerMap.put(pattern, handler);
074: }
075:
076: public void unregister(final String pattern) {
077: if (pattern == null) {
078: return;
079: }
080: this .handlerMap.remove(pattern);
081: }
082:
083: public void setHandlers(final Map map) {
084: if (map == null) {
085: throw new IllegalArgumentException(
086: "Map of handlers may not be null");
087: }
088: this .handlerMap.clear();
089: this .handlerMap.putAll(map);
090: }
091:
092: public HttpRequestHandler lookup(String requestURI) {
093: if (requestURI == null) {
094: throw new IllegalArgumentException(
095: "Request URI may not be null");
096: }
097: //Strip away the query part part if found
098: int index = requestURI.indexOf("?");
099: if (index != -1) {
100: requestURI = requestURI.substring(0, index);
101: }
102:
103: // direct match?
104: Object handler = this .handlerMap.get(requestURI);
105: if (handler == null) {
106: // pattern match?
107: String bestMatch = null;
108: for (Iterator it = this .handlerMap.keySet().iterator(); it
109: .hasNext();) {
110: String pattern = (String) it.next();
111: if (matchUriRequestPattern(pattern, requestURI)) {
112: // we have a match. is it any better?
113: if (bestMatch == null
114: || (bestMatch.length() < pattern.length())
115: || (bestMatch.length() == pattern.length() && pattern
116: .endsWith("*"))) {
117: handler = this .handlerMap.get(pattern);
118: bestMatch = pattern;
119: }
120: }
121: }
122: }
123: return (HttpRequestHandler) handler;
124: }
125:
126: protected boolean matchUriRequestPattern(final String pattern,
127: final String requestUri) {
128: if (pattern.equals("*")) {
129: return true;
130: } else {
131: return (pattern.endsWith("*") && requestUri
132: .startsWith(pattern.substring(0,
133: pattern.length() - 1)))
134: || (pattern.startsWith("*") && requestUri
135: .endsWith(pattern.substring(1, pattern
136: .length())));
137: }
138: }
139:
140: }
|