Return-Path: X-Original-To: apmail-hc-commits-archive@www.apache.org Delivered-To: apmail-hc-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id CD1BAD9FE for ; Thu, 30 Aug 2012 18:54:53 +0000 (UTC) Received: (qmail 63488 invoked by uid 500); 30 Aug 2012 18:54:53 -0000 Delivered-To: apmail-hc-commits-archive@hc.apache.org Received: (qmail 63441 invoked by uid 500); 30 Aug 2012 18:54:53 -0000 Mailing-List: contact commits-help@hc.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "HttpComponents Project" Delivered-To: mailing list commits@hc.apache.org Received: (qmail 63433 invoked by uid 99); 30 Aug 2012 18:54:53 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 30 Aug 2012 18:54:53 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 30 Aug 2012 18:54:47 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id C1CEA2388A2C for ; Thu, 30 Aug 2012 18:54:02 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1379062 - in /httpcomponents/httpcore/trunk: httpcore-nio/src/main/java/org/apache/http/nio/protocol/ httpcore-nio/src/test/java/org/apache/http/nio/protocol/ httpcore/src/main/java/org/apache/http/protocol/ Date: Thu, 30 Aug 2012 18:54:02 -0000 To: commits@hc.apache.org From: wspeirs@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20120830185402.C1CEA2388A2C@eris.apache.org> Author: wspeirs Date: Thu Aug 30 18:54:01 2012 New Revision: 1379062 URL: http://svn.apache.org/viewvc?rev=1379062&view=rev Log: - Deprecated Http(Async)RequestHandlerResolver in favor of Http(Async)RequestHandlerMapper - Create UriHttp(Async)RequestHandlerMapper to mimic Http(Async)RequestHandlerRegistry - Updated Http(Async)Service with new constructors and converters between Resolver & Mapper HTTPCORE-308 Added: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestHandlerMapper.java httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/UriHttpAsyncRequestHandlerMapper.java httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpRequestHandlerMapper.java httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/UriHttpRequestHandlerMapper.java Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestHandlerRegistry.java httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestHandlerResolver.java httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncService.java httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncService.java httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpRequestHandlerRegistry.java httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpRequestHandlerResolver.java httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpService.java Added: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestHandlerMapper.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestHandlerMapper.java?rev=1379062&view=auto ============================================================================== --- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestHandlerMapper.java (added) +++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestHandlerMapper.java Thu Aug 30 18:54:01 2012 @@ -0,0 +1,50 @@ +/* + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ + +package org.apache.http.nio.protocol; + +import org.apache.http.HttpRequest; + +/** + * HttpAsyncRequestHandlerMapper can be used to resolve an instance + * of {@link HttpAsyncRequestHandler} matching a particular {@link HttpRequest}. + * Usually the resolved request handler will be used to process the request. + * + * @since 4.3 + */ +public interface HttpAsyncRequestHandlerMapper { + + /** + * Looks up a handler matching the given request. + * + * @param request the request + * @return HTTP request handler or null if no match + * is found. + */ + HttpAsyncRequestHandler lookup(HttpRequest request); + +} Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestHandlerRegistry.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestHandlerRegistry.java?rev=1379062&r1=1379061&r2=1379062&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestHandlerRegistry.java (original) +++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestHandlerRegistry.java Thu Aug 30 18:54:01 2012 @@ -47,8 +47,10 @@ import org.apache.http.protocol.UriPatte * will be used to process the request with the specified request URI. * * @since 4.2 + * @deprecated (4.3) use {@link UriHttpAsyncRequestHandlerMapper} */ @ThreadSafe +@Deprecated public class HttpAsyncRequestHandlerRegistry implements HttpAsyncRequestHandlerResolver { private final UriPatternMatcher> matcher; Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestHandlerResolver.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestHandlerResolver.java?rev=1379062&r1=1379061&r2=1379062&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestHandlerResolver.java (original) +++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestHandlerResolver.java Thu Aug 30 18:54:01 2012 @@ -28,13 +28,15 @@ package org.apache.http.nio.protocol; /** - * HttpAsyncRequestHandlerResolver can be used to resolve an instance + * HttpAsyncRequestHandlerResolver can be used to map an instance * of {@link HttpAsyncRequestHandler} matching a particular request URI. - * Usually the resolved request handler will be used to process the request + * Usually the mapped request handler will be used to process the request * with the specified request URI. * * @since 4.2 + * @deprecated see {@link HttpAsyncRequestHandlerMapper} */ +@Deprecated public interface HttpAsyncRequestHandlerResolver { /** Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncService.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncService.java?rev=1379062&r1=1379061&r2=1379062&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncService.java (original) +++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncService.java Thu Aug 30 18:54:01 2012 @@ -73,7 +73,7 @@ import org.apache.http.util.Args; * Upon receiving an incoming request HttpAsyncService verifies * the message for compliance with the server expectations using * {@link HttpAsyncExpectationVerifier}, if provided, and then - * {@link HttpAsyncRequestHandlerResolver} is used to resolve the request URI + * {@link HttpAsyncRequestHandlerMapper} is used to map the request * to a particular {@link HttpAsyncRequestHandler} intended to handle * the request with the given URI. The protocol handler uses the selected * {@link HttpAsyncRequestHandler} instance to process the incoming request @@ -108,7 +108,7 @@ public class HttpAsyncService implements private final HttpProcessor httpProcessor; private final ConnectionReuseStrategy connStrategy; private final HttpResponseFactory responseFactory; - private final HttpAsyncRequestHandlerResolver handlerResolver; + private final HttpAsyncRequestHandlerMapper handlerMapper; private final HttpAsyncExpectationVerifier expectationVerifier; private final HttpParams params; @@ -118,15 +118,17 @@ public class HttpAsyncService implements * @param httpProcessor HTTP protocol processor (required). * @param connStrategy Connection re-use strategy (required). * @param responseFactory HTTP response factory (required). - * @param handlerResolver Request handler resolver. + * @param handlerMapper Request handler mapper. * @param expectationVerifier Request expectation verifier (optional). * @param params HTTP parameters (required). + * + * @since 4.3 */ public HttpAsyncService( final HttpProcessor httpProcessor, final ConnectionReuseStrategy connStrategy, final HttpResponseFactory responseFactory, - final HttpAsyncRequestHandlerResolver handlerResolver, + final HttpAsyncRequestHandlerMapper handlerMapper, final HttpAsyncExpectationVerifier expectationVerifier, final HttpParams params) { super(); @@ -137,7 +139,7 @@ public class HttpAsyncService implements this.httpProcessor = httpProcessor; this.connStrategy = connStrategy; this.responseFactory = responseFactory; - this.handlerResolver = handlerResolver; + this.handlerMapper = handlerMapper; this.expectationVerifier = expectationVerifier; this.params = params; } @@ -147,16 +149,75 @@ public class HttpAsyncService implements * * @param httpProcessor HTTP protocol processor (required). * @param connStrategy Connection re-use strategy (required). + * @param responseFactory HTTP response factory (required). * @param handlerResolver Request handler resolver. + * @param expectationVerifier Request expectation verifier (optional). * @param params HTTP parameters (required). + * + * @deprecated (4.3) use {@link HttpAsyncService#HttpAsyncService(HttpProcessor, + * ConnectionReuseStrategy, HttpResponseFactory, HttpAsyncRequestHandlerMapper, HttpAsyncExpectationVerifier, HttpParams)} */ public HttpAsyncService( final HttpProcessor httpProcessor, final ConnectionReuseStrategy connStrategy, + final HttpResponseFactory responseFactory, final HttpAsyncRequestHandlerResolver handlerResolver, + final HttpAsyncExpectationVerifier expectationVerifier, final HttpParams params) { - this(httpProcessor, connStrategy, DefaultHttpResponseFactory.INSTANCE, - handlerResolver, null, params); + this(httpProcessor, + connStrategy, + responseFactory, + new HttpAsyncRequestHandlerResolverAdapter(handlerResolver), + expectationVerifier, + params); + } + + /** + * Creates an instance of HttpAsyncServerProtocolHandler. + * + * @param httpProcessor HTTP protocol processor (required). + * @param connStrategy Connection re-use strategy (required). + * @param handlerMapper Request handler mapper. + * @param params HTTP parameters (required). + * + * @since 4.3 + */ + public HttpAsyncService( + final HttpProcessor httpProcessor, + final ConnectionReuseStrategy connStrategy, + final HttpAsyncRequestHandlerMapper handlerMapper, + final HttpParams params) { + this(httpProcessor, + connStrategy, + DefaultHttpResponseFactory.INSTANCE, + handlerMapper, + null, + params); + } + + /** + * Creates an instance of HttpAsyncServerProtocolHandler. + * + * @param httpProcessor HTTP protocol processor (required). + * @param connStrategy Connection re-use strategy (required). + * @param handlerResolver Request handler resolver. + * @param params HTTP parameters (required). + * + * @deprecated (4.3) use {@link HttpAsyncService#HttpAsyncService(HttpProcessor, + * ConnectionReuseStrategy, HttpAsyncRequestHandlerMapper, HttpParams)} + */ + @Deprecated + public HttpAsyncService( + final HttpProcessor httpProcessor, + final ConnectionReuseStrategy connStrategy, + final HttpAsyncRequestHandlerResolver handlerResolver, + final HttpParams params) { + this(httpProcessor, + connStrategy, + DefaultHttpResponseFactory.INSTANCE, + new HttpAsyncRequestHandlerResolverAdapter(handlerResolver), + null, + params); } public void connected(final NHttpServerConnection conn) { @@ -557,9 +618,8 @@ public class HttpAsyncService implements @SuppressWarnings("unchecked") private HttpAsyncRequestHandler getRequestHandler(final HttpRequest request) { HttpAsyncRequestHandler handler = null; - if (this.handlerResolver != null) { - String requestURI = request.getRequestLine().getUri(); - handler = (HttpAsyncRequestHandler) this.handlerResolver.lookup(requestURI); + if (this.handlerMapper != null) { + handler = (HttpAsyncRequestHandler) this.handlerMapper.lookup(request); } if (handler == null) { handler = new NullRequestHandler(); @@ -777,4 +837,21 @@ public class HttpAsyncService implements } + /** + * Adaptor class to transition from HttpAsyncRequestHandlerResolver to HttpAsyncRequestHandlerMapper. + */ + private static class HttpAsyncRequestHandlerResolverAdapter implements HttpAsyncRequestHandlerMapper { + + private final HttpAsyncRequestHandlerResolver resolver; + + public HttpAsyncRequestHandlerResolverAdapter(final HttpAsyncRequestHandlerResolver resolver) { + this.resolver = resolver; + } + + public HttpAsyncRequestHandler lookup(HttpRequest request) { + return resolver.lookup(request.getRequestLine().getUri()); + } + + } + } Added: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/UriHttpAsyncRequestHandlerMapper.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/UriHttpAsyncRequestHandlerMapper.java?rev=1379062&view=auto ============================================================================== --- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/UriHttpAsyncRequestHandlerMapper.java (added) +++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/UriHttpAsyncRequestHandlerMapper.java Thu Aug 30 18:54:01 2012 @@ -0,0 +1,101 @@ +/* + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ + +package org.apache.http.nio.protocol; + +import java.util.Map; + +import org.apache.http.HttpRequest; +import org.apache.http.annotation.ThreadSafe; +import org.apache.http.protocol.UriPatternMatcher; + +/** + * Maintains a map of HTTP request handlers keyed by a request URI pattern. + *
+ * Patterns may have three formats: + *
    + *
  • *
  • + *
  • *<uri>
  • + *
  • <uri>*
  • + *
+ *
+ * This class can be used to map an instance of {@link HttpAsyncRequestHandler} + * matching a particular request URI. Usually the mapped request handler + * will be used to process the request with the specified request URI. + * + * @since 4.2 + */ +@ThreadSafe +public class UriHttpAsyncRequestHandlerMapper implements HttpAsyncRequestHandlerMapper { + + private final UriPatternMatcher> matcher; + + public UriHttpAsyncRequestHandlerMapper() { + matcher = new UriPatternMatcher>(); + } + + /** + * Registers the given {@link NHttpRequestHandler} as a handler for URIs + * matching the given pattern. + * + * @param pattern the pattern to register the handler for. + * @param handler the handler. + */ + public void register(final String pattern, final HttpAsyncRequestHandler handler) { + matcher.register(pattern, handler); + } + + /** + * Removes registered handler, if exists, for the given pattern. + * + * @param pattern the pattern to unregister the handler for. + */ + public void unregister(final String pattern) { + matcher.unregister(pattern); + } + + /** + * Sets handlers from the given map. + * @param map the map containing handlers keyed by their URI patterns. + */ + public void setHandlers(final Map> map) { + matcher.setObjects(map); + } + + /** + * Get the handler map. + * @return The map of handlers and their associated URI patterns. + */ + public Map> getHandlers() { + return matcher.getObjects(); + } + + public HttpAsyncRequestHandler lookup(final HttpRequest request) { + return matcher.lookup(request.getRequestLine().getUri()); + } + +} Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncService.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncService.java?rev=1379062&r1=1379061&r2=1379062&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncService.java (original) +++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncService.java Thu Aug 30 18:54:01 2012 @@ -110,22 +110,22 @@ public class TestHttpAsyncService { @Test public void testInvalidConstruction() throws Exception { try { - new HttpAsyncService(null, this.reuseStrategy, this.responseFactory, null, null, this.params); + new HttpAsyncService(null, this.reuseStrategy, this.responseFactory, (HttpAsyncRequestHandlerMapper)null, null, this.params); Assert.fail("IllegalArgumentException expected"); } catch (IllegalArgumentException ex) { } try { - new HttpAsyncService(this.httpProcessor, null, this.responseFactory, null, null, this.params); + new HttpAsyncService(this.httpProcessor, null, this.responseFactory, (HttpAsyncRequestHandlerMapper)null, null, this.params); Assert.fail("IllegalArgumentException expected"); } catch (IllegalArgumentException ex) { } try { - new HttpAsyncService(this.httpProcessor, this.reuseStrategy, null, null, null, this.params); + new HttpAsyncService(this.httpProcessor, this.reuseStrategy, null, (HttpAsyncRequestHandlerMapper)null, null, this.params); Assert.fail("IllegalArgumentException expected"); } catch (IllegalArgumentException ex) { } try { - new HttpAsyncService(this.httpProcessor, this.reuseStrategy, this.responseFactory, null, null, null); + new HttpAsyncService(this.httpProcessor, this.reuseStrategy, this.responseFactory, (HttpAsyncRequestHandlerMapper)null, null, null); Assert.fail("IllegalArgumentException expected"); } catch (IllegalArgumentException ex) { } Added: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpRequestHandlerMapper.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpRequestHandlerMapper.java?rev=1379062&view=auto ============================================================================== --- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpRequestHandlerMapper.java (added) +++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpRequestHandlerMapper.java Thu Aug 30 18:54:01 2012 @@ -0,0 +1,50 @@ +/* + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ + +package org.apache.http.protocol; + +import org.apache.http.HttpRequest; + +/** + * HttpRequestHandlerMapper can be used to resolve an instance of + * {@link HttpRequestHandler} matching a particular {@link HttpRequest}. Usually the + * mapped request handler will be used to process the request. + * + * @since 4.3 + */ +public interface HttpRequestHandlerMapper { + + /** + * Looks up a handler matching the given request. + * + * @param request the request to map to a handler + * @return HTTP request handler or null if no match + * is found. + */ + HttpRequestHandler lookup(HttpRequest request); + +} Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpRequestHandlerRegistry.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpRequestHandlerRegistry.java?rev=1379062&r1=1379061&r2=1379062&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpRequestHandlerRegistry.java (original) +++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpRequestHandlerRegistry.java Thu Aug 30 18:54:01 2012 @@ -48,8 +48,10 @@ import org.apache.http.util.Args; * specified request URI. * * @since 4.0 + * @deprecated (4.3) use {@link UriHttpRequestHandlerMapper} */ @ThreadSafe // provided injected dependencies are thread-safe +@Deprecated public class HttpRequestHandlerRegistry implements HttpRequestHandlerResolver { private final UriPatternMatcher matcher; Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpRequestHandlerResolver.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpRequestHandlerResolver.java?rev=1379062&r1=1379061&r2=1379062&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpRequestHandlerResolver.java (original) +++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpRequestHandlerResolver.java Thu Aug 30 18:54:01 2012 @@ -30,11 +30,13 @@ package org.apache.http.protocol; /** * HttpRequestHandlerResolver can be used to resolve an instance of * {@link HttpRequestHandler} matching a particular request URI. Usually the - * resolved request handler will be used to process the request with the + * mapped request handler will be used to process the request with the * specified request URI. * * @since 4.0 + * @deprecated see {@link HttpRequestHandlerMapper} */ +@Deprecated public interface HttpRequestHandlerResolver { /** Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpService.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpService.java?rev=1379062&r1=1379061&r2=1379062&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpService.java (original) +++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpService.java Thu Aug 30 18:54:01 2012 @@ -60,7 +60,7 @@ import org.apache.http.util.EntityUtils; * individual {@link HttpRequestHandler}s are expected to implement * application specific content generation and processing. *

- * HttpService uses {@link HttpRequestHandlerResolver} to resolve + * HttpService uses {@link HttpRequestHandlerMapper} to map * matching request handler for a particular request URI of an incoming HTTP * request. *

@@ -77,7 +77,7 @@ public class HttpService { */ private volatile HttpParams params = null; private volatile HttpProcessor processor = null; - private volatile HttpRequestHandlerResolver handlerResolver = null; + private volatile HttpRequestHandlerMapper handlerMapper = null; private volatile ConnectionReuseStrategy connStrategy = null; private volatile HttpResponseFactory responseFactory = null; private volatile HttpExpectationVerifier expectationVerifier = null; @@ -88,17 +88,17 @@ public class HttpService { * @param processor the processor to use on requests and responses * @param connStrategy the connection reuse strategy * @param responseFactory the response factory - * @param handlerResolver the handler resolver. May be null. + * @param handlerMapper the handler mapper. May be null. * @param expectationVerifier the expectation verifier. May be null. * @param params the HTTP parameters * - * @since 4.1 + * @since 4.3 */ public HttpService( final HttpProcessor processor, final ConnectionReuseStrategy connStrategy, final HttpResponseFactory responseFactory, - final HttpRequestHandlerResolver handlerResolver, + final HttpRequestHandlerMapper handlerMapper, final HttpExpectationVerifier expectationVerifier, final HttpParams params) { super(); @@ -106,7 +106,7 @@ public class HttpService { this.connStrategy = Args.notNull(connStrategy, "Connection reuse strategy"); this.responseFactory = Args.notNull(responseFactory, "Response factory"); this.params = Args.notNull(params, "HTTP parameters"); - this.handlerResolver = handlerResolver; + this.handlerMapper = handlerMapper; this.expectationVerifier = expectationVerifier; } @@ -117,17 +117,74 @@ public class HttpService { * @param connStrategy the connection reuse strategy * @param responseFactory the response factory * @param handlerResolver the handler resolver. May be null. + * @param expectationVerifier the expectation verifier. May be null. * @param params the HTTP parameters * * @since 4.1 + * @deprecated (4.3) use {@link HttpService#HttpService(HttpProcessor, + * ConnectionReuseStrategy, HttpResponseFactory, HttpRequestHandlerMapper, HttpExpectationVerifier, HttpParams)} */ + @Deprecated public HttpService( final HttpProcessor processor, final ConnectionReuseStrategy connStrategy, final HttpResponseFactory responseFactory, final HttpRequestHandlerResolver handlerResolver, + final HttpExpectationVerifier expectationVerifier, + final HttpParams params) { + this(processor, + connStrategy, + responseFactory, + new HttpRequestHandlerResolverAdapter(handlerResolver), + expectationVerifier, + params); + } + + /** + * Create a new HTTP service. + * + * @param processor the processor to use on requests and responses + * @param connStrategy the connection reuse strategy + * @param responseFactory the response factory + * @param handlerMapper the handler mapper. May be null. + * @param params the HTTP parameters + * + * @since 4.3 + */ + public HttpService( + final HttpProcessor processor, + final ConnectionReuseStrategy connStrategy, + final HttpResponseFactory responseFactory, + final HttpRequestHandlerMapper handlerMapper, final HttpParams params) { - this(processor, connStrategy, responseFactory, handlerResolver, null, params); + this(processor, connStrategy, responseFactory, handlerMapper, null, params); + } + + /** + * Create a new HTTP service. + * + * @param processor the processor to use on requests and responses + * @param connStrategy the connection reuse strategy + * @param responseFactory the response factory + * @param handlerResolver the handler resolver. May be null. + * @param params the HTTP parameters + * + * @since 4.1 + * @deprecated (4.3) use {@link HttpService#HttpService(HttpProcessor, + * ConnectionReuseStrategy, HttpResponseFactory, HttpRequestHandlerMapper, HttpParams)} + */ + public HttpService( + final HttpProcessor processor, + final ConnectionReuseStrategy connStrategy, + final HttpResponseFactory responseFactory, + final HttpRequestHandlerResolver handlerResolver, + final HttpParams params) { + this(processor, + connStrategy, + responseFactory, + new HttpRequestHandlerResolverAdapter(handlerResolver), + null, + params); } /** @@ -191,7 +248,7 @@ public class HttpService { */ @Deprecated public void setHandlerResolver(final HttpRequestHandlerResolver handlerResolver) { - this.handlerResolver = handlerResolver; + this.handlerMapper = new HttpRequestHandlerResolverAdapter(handlerResolver); } /** @@ -350,9 +407,8 @@ public class HttpService { final HttpResponse response, final HttpContext context) throws HttpException, IOException { HttpRequestHandler handler = null; - if (this.handlerResolver != null) { - String requestURI = request.getRequestLine().getUri(); - handler = this.handlerResolver.lookup(requestURI); + if (this.handlerMapper != null) { + handler = this.handlerMapper.lookup(request); } if (handler != null) { handler.handle(request, response, context); @@ -360,5 +416,22 @@ public class HttpService { response.setStatusCode(HttpStatus.SC_NOT_IMPLEMENTED); } } + + /** + * Adaptor class to transition from HttpRequestHandlerResolver to HttpRequestHandlerMapper. + */ + private static class HttpRequestHandlerResolverAdapter implements HttpRequestHandlerMapper { + + private final HttpRequestHandlerResolver resolver; + + public HttpRequestHandlerResolverAdapter(final HttpRequestHandlerResolver resolver) { + this.resolver = resolver; + } + + public HttpRequestHandler lookup(HttpRequest request) { + return resolver.lookup(request.getRequestLine().getUri()); + } + + } } Added: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/UriHttpRequestHandlerMapper.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/UriHttpRequestHandlerMapper.java?rev=1379062&view=auto ============================================================================== --- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/UriHttpRequestHandlerMapper.java (added) +++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/UriHttpRequestHandlerMapper.java Thu Aug 30 18:54:01 2012 @@ -0,0 +1,109 @@ +/* + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ + +package org.apache.http.protocol; + +import java.util.Map; + +import org.apache.http.HttpRequest; +import org.apache.http.annotation.ThreadSafe; + +/** + * Maintains a map of HTTP request handlers keyed by a request URI pattern. + *
+ * Patterns may have three formats: + *

    + *
  • *
  • + *
  • *<uri>
  • + *
  • <uri>*
  • + *
+ *
+ * This class can be used to map an instance of + * {@link HttpRequestHandler} matching a particular request URI. Usually the + * mapped request handler will be used to process the request with the + * specified request URI. + * + * @since 4.3 + */ +@ThreadSafe // provided injected dependencies are thread-safe +public class UriHttpRequestHandlerMapper implements HttpRequestHandlerMapper { + + private final UriPatternMatcher matcher; + + public UriHttpRequestHandlerMapper() { + matcher = new UriPatternMatcher(); + } + + /** + * Registers the given {@link HttpRequestHandler} as a handler for URIs + * matching the given pattern. + * + * @param pattern the pattern to register the handler for. + * @param handler the handler. + */ + public void register(final String pattern, final HttpRequestHandler handler) { + if (pattern == null) { + throw new IllegalArgumentException("URI request pattern may not be null"); + } + if (handler == null) { + throw new IllegalArgumentException("Request handler may not be null"); + } + matcher.register(pattern, handler); + } + + /** + * Removes registered handler, if exists, for the given pattern. + * + * @param pattern the pattern to unregister the handler for. + */ + public void unregister(final String pattern) { + matcher.unregister(pattern); + } + + /** + * Sets handlers from the given map. + * @param map the map containing handlers keyed by their URI patterns. + */ + public void setHandlers(final Map map) { + matcher.setObjects(map); + } + + /** + * Get the handler map. + * @return The map of handlers and their associated URI patterns. + * + * @since 4.3 + */ + public Map getHandlers() { + return matcher.getObjects(); + } + + public HttpRequestHandler lookup(final HttpRequest request) { + return matcher.lookup(request.getRequestLine().getUri()); + } + +}