michael@0: /*
michael@0: * ====================================================================
michael@0: * Licensed to the Apache Software Foundation (ASF) under one
michael@0: * or more contributor license agreements. See the NOTICE file
michael@0: * distributed with this work for additional information
michael@0: * regarding copyright ownership. The ASF licenses this file
michael@0: * to you under the Apache License, Version 2.0 (the
michael@0: * "License"); you may not use this file except in compliance
michael@0: * with the License. You may obtain a copy of the License at
michael@0: *
michael@0: * http://www.apache.org/licenses/LICENSE-2.0
michael@0: *
michael@0: * Unless required by applicable law or agreed to in writing,
michael@0: * software distributed under the License is distributed on an
michael@0: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
michael@0: * KIND, either express or implied. See the License for the
michael@0: * specific language governing permissions and limitations
michael@0: * under the License.
michael@0: * ====================================================================
michael@0: *
michael@0: * This software consists of voluntary contributions made by many
michael@0: * individuals on behalf of the Apache Software Foundation. For more
michael@0: * information on the Apache Software Foundation, please see
michael@0: * .
michael@0: *
michael@0: */
michael@0:
michael@0: package ch.boye.httpclientandroidlib.protocol;
michael@0:
michael@0: import java.util.Map;
michael@0:
michael@0: /**
michael@0: * Maintains a map of HTTP request handlers keyed by a request URI pattern.
michael@0: *
michael@0: * Patterns may have three formats:
michael@0: *
michael@0: * *
michael@0: * *<uri>
michael@0: * <uri>*
michael@0: *
michael@0: *
michael@0: * This class can be used to resolve an instance of
michael@0: * {@link HttpRequestHandler} matching a particular request URI. Usually the
michael@0: * resolved request handler will be used to process the request with the
michael@0: * specified request URI.
michael@0: *
michael@0: * @since 4.0
michael@0: */
michael@0: public class HttpRequestHandlerRegistry implements HttpRequestHandlerResolver {
michael@0:
michael@0: private final UriPatternMatcher matcher;
michael@0:
michael@0: public HttpRequestHandlerRegistry() {
michael@0: matcher = new UriPatternMatcher();
michael@0: }
michael@0:
michael@0: /**
michael@0: * Registers the given {@link HttpRequestHandler} as a handler for URIs
michael@0: * matching the given pattern.
michael@0: *
michael@0: * @param pattern the pattern to register the handler for.
michael@0: * @param handler the handler.
michael@0: */
michael@0: public void register(final String pattern, final HttpRequestHandler handler) {
michael@0: if (pattern == null) {
michael@0: throw new IllegalArgumentException("URI request pattern may not be null");
michael@0: }
michael@0: if (handler == null) {
michael@0: throw new IllegalArgumentException("Request handler may not be null");
michael@0: }
michael@0: matcher.register(pattern, handler);
michael@0: }
michael@0:
michael@0: /**
michael@0: * Removes registered handler, if exists, for the given pattern.
michael@0: *
michael@0: * @param pattern the pattern to unregister the handler for.
michael@0: */
michael@0: public void unregister(final String pattern) {
michael@0: matcher.unregister(pattern);
michael@0: }
michael@0:
michael@0: /**
michael@0: * Sets handlers from the given map.
michael@0: * @param map the map containing handlers keyed by their URI patterns.
michael@0: */
michael@0: public void setHandlers(final Map map) {
michael@0: matcher.setObjects(map);
michael@0: }
michael@0:
michael@0: public HttpRequestHandler lookup(final String requestURI) {
michael@0: return (HttpRequestHandler) matcher.lookup(requestURI);
michael@0: }
michael@0:
michael@0: /**
michael@0: * @deprecated
michael@0: */
michael@0: protected boolean matchUriRequestPattern(final String pattern, final String requestUri) {
michael@0: return matcher.matchUriRequestPattern(pattern, requestUri);
michael@0: }
michael@0:
michael@0: }