You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2006/11/11 13:42:18 UTC

svn commit: r473708 - in /jakarta/httpcomponents/httpcore/trunk/module-main/src: examples/org/apache/http/examples/ main/java/org/apache/http/protocol/

Author: olegk
Date: Sat Nov 11 04:42:16 2006
New Revision: 473708

URL: http://svn.apache.org/viewvc?view=rev&rev=473708
Log:
* Factored out HttpRequestHandler management code into a separate class HttpRequestHandlerRegistry
* Added HttpRequestHandlerResolver interface that can be used to look up HttpRequestHandler instances by request URI
* Made HttpService a little more DI framework friendly

Added:
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpRequestHandlerRegistry.java   (with props)
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpRequestHandlerResolver.java   (with props)
Modified:
    jakarta/httpcomponents/httpcore/trunk/module-main/src/examples/org/apache/http/examples/ElementalHttpServer.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpService.java

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/examples/org/apache/http/examples/ElementalHttpServer.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/examples/org/apache/http/examples/ElementalHttpServer.java?view=diff&rev=473708&r1=473707&r2=473708
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/examples/org/apache/http/examples/ElementalHttpServer.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/examples/org/apache/http/examples/ElementalHttpServer.java Sat Nov 11 04:42:16 2006
@@ -59,6 +59,7 @@
 import org.apache.http.protocol.HttpContext;
 import org.apache.http.protocol.HttpExecutionContext;
 import org.apache.http.protocol.HttpRequestHandler;
+import org.apache.http.protocol.HttpRequestHandlerRegistry;
 import org.apache.http.protocol.HttpService;
 import org.apache.http.protocol.ResponseConnControl;
 import org.apache.http.protocol.ResponseContent;
@@ -180,19 +181,24 @@
                     System.out.println("Incoming connection from " + socket.getInetAddress());
                     conn.bind(socket, this.params);
 
-                    // Set up HTTP processor and service
+                    // Set up the HTTP protocol processor
                     BasicHttpProcessor httpproc = new BasicHttpProcessor();
                     httpproc.addInterceptor(new ResponseDate());
                     httpproc.addInterceptor(new ResponseServer());
                     httpproc.addInterceptor(new ResponseContent());
                     httpproc.addInterceptor(new ResponseConnControl());
                     
+                    // Set up request handlers
+                    HttpRequestHandlerRegistry reqistry = new HttpRequestHandlerRegistry();
+                    reqistry.register("*", new HttpFileHandler());
+                    
+                    // Set up the HTTP service
                     HttpService httpService = new HttpService(
                             httpproc, 
                             new DefaultConnectionReuseStrategy(), 
                             new DefaultHttpResponseFactory());
                     httpService.setParams(this.params);
-                    httpService.registerRequestHandler("*", new HttpFileHandler());
+                    httpService.setHandlerResolver(reqistry);
                     
                     // Start worker thread
                     Thread t = new WorkerThread(httpService, conn, this.globalContext);

Added: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpRequestHandlerRegistry.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpRequestHandlerRegistry.java?view=auto&rev=473708
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpRequestHandlerRegistry.java (added)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpRequestHandlerRegistry.java Sat Nov 11 04:42:16 2006
@@ -0,0 +1,110 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ *
+ *  Copyright 1999-2006 The Apache Software Foundation
+ *
+ *  Licensed 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
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.protocol;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Properties;
+
+/**
+ * Maintains a map of HTTP request handlers keyed by a request URI pattern. 
+ * {@link HttpRequestHandler} instances can be looked by request URI
+ * using {@link HttpRequestHandlerResolver} interface.
+ *
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ *
+ * @version $Revision$
+ */
+public class HttpRequestHandlerRegistry implements HttpRequestHandlerResolver {
+
+    private final Map handlerMap;
+    
+    public HttpRequestHandlerRegistry() {
+        super();
+        this.handlerMap = new HashMap();
+    }
+    
+    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("HTTP request handelr may not be null");
+        }
+        this.handlerMap.put(pattern, handler);
+    }
+    
+    public void unregister(final String pattern) {
+        if (pattern == null) {
+            return;
+        }
+        this.handlerMap.remove(pattern);
+    }
+    
+    public void setHandlers(final Properties props) {
+        if (props == null) {
+            throw new IllegalArgumentException("Properties may not be null");
+        }
+        this.handlerMap.clear();
+        this.handlerMap.putAll(props);
+    }
+    
+    public HttpRequestHandler lookup(final String requestURI) {
+        // direct match?
+        Object handler = this.handlerMap.get(requestURI);
+        if (handler == null) {
+            // pattern match?
+            String bestMatch = null;
+            for (Iterator it = this.handlerMap.keySet().iterator(); it.hasNext();) {
+                String pattern = (String) it.next();
+                if (matchUriRequestPattern(pattern, requestURI)) {
+                    // we have a match. is it any better?
+                    if (bestMatch == null || bestMatch.length() <= pattern.length()) {
+                        handler = this.handlerMap.get(pattern);
+                        bestMatch = pattern;
+                    }
+                }
+            }
+        }
+        return (HttpRequestHandler) handler;
+    }
+
+    protected boolean matchUriRequestPattern(final String pattern, final String requestUri) {
+        if (pattern.equals("*")) {
+            return true;
+        } else {
+            return 
+            (pattern.endsWith("*") && requestUri.startsWith(pattern.substring(0, pattern.length() - 1))) ||
+            (pattern.startsWith("*") && requestUri.endsWith(pattern.substring(1, pattern.length())));
+        }
+    }
+    
+}

Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpRequestHandlerRegistry.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpRequestHandlerRegistry.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpRequestHandlerResolver.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpRequestHandlerResolver.java?view=auto&rev=473708
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpRequestHandlerResolver.java (added)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpRequestHandlerResolver.java Sat Nov 11 04:42:16 2006
@@ -0,0 +1,44 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ *
+ *  Copyright 1999-2006 The Apache Software Foundation
+ *
+ *  Licensed 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
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.protocol;
+
+/**
+ * Interface to be implemented by objects that can resolve 
+ * {@link HttpRequestHandler} instances by request URI.
+ *
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ *
+ * @version $Revision$
+ */
+public interface HttpRequestHandlerResolver {
+
+    HttpRequestHandler lookup(String requestURI);
+    
+}

Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpRequestHandlerResolver.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpRequestHandlerResolver.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpService.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpService.java?view=diff&rev=473708&r1=473707&r2=473708
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpService.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpService.java Sat Nov 11 04:42:16 2006
@@ -30,9 +30,6 @@
 package org.apache.http.protocol;
 
 import java.io.IOException;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
 
 import org.apache.http.ConnectionReuseStrategy;
 import org.apache.http.HttpEntity;
@@ -58,10 +55,9 @@
  */
 public class HttpService {
 
-    private final Map handlerMap;
-    
     private HttpParams params = null;
     private HttpProcessor processor = null;
+    private HttpRequestHandlerResolver handlerResolver;
     private ConnectionReuseStrategy connStrategy = null;
     private HttpResponseFactory responseFactory = null;
     
@@ -77,33 +73,36 @@
             final ConnectionReuseStrategy connStrategy,
             final HttpResponseFactory responseFactory) {
         super();
-        this.handlerMap = new HashMap();
         setHttpProcessor(proc);
         setConnReuseStrategy(connStrategy);
         setResponseFactory(responseFactory);
     }
     
-    protected void setHttpProcessor(final HttpProcessor processor) {
+    public void setHttpProcessor(final HttpProcessor processor) {
         if (processor == null) {
             throw new IllegalArgumentException("HTTP processor may not be null.");
         }
         this.processor = processor;
     }
 
-    protected void setConnReuseStrategy(final ConnectionReuseStrategy connStrategy) {
+    public void setConnReuseStrategy(final ConnectionReuseStrategy connStrategy) {
         if (connStrategy == null) {
             throw new IllegalArgumentException("Connection reuse strategy may not be null");
         }
         this.connStrategy = connStrategy;
     }
 
-    protected void setResponseFactory(final HttpResponseFactory responseFactory) {
+    public void setResponseFactory(final HttpResponseFactory responseFactory) {
         if (responseFactory == null) {
             throw new IllegalArgumentException("Response factory may not be null");
         }
         this.responseFactory = responseFactory;
     }
     
+    public void setHandlerResolver(final HttpRequestHandlerResolver handlerResolver) {
+        this.handlerResolver = handlerResolver;
+    }
+
     public HttpParams getParams() {
         return this.params;
     }
@@ -112,46 +111,6 @@
         this.params = params;
     }
     
-    public void registerRequestHandler(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("HTTP request handelr may not be null");
-        }
-        this.handlerMap.put(pattern, handler);
-    }
-    
-    protected HttpRequestHandler lookupHandler(final String requestURI) {
-        // direct match?
-        Object handler = this.handlerMap.get(requestURI);
-        if (handler == null) {
-            // pattern match?
-            String bestMatch = null;
-            for (Iterator it = this.handlerMap.keySet().iterator(); it.hasNext();) {
-                String pattern = (String) it.next();
-                if (matchUriRequestPattern(pattern, requestURI)) {
-                    // we have a match. is it any better?
-                    if (bestMatch == null || bestMatch.length() <= pattern.length()) {
-                        handler = this.handlerMap.get(pattern);
-                        bestMatch = pattern;
-                    }
-                }
-            }
-        }
-        return (HttpRequestHandler) handler;
-    }
-
-    protected boolean matchUriRequestPattern(final String pattern, final String requestUri) {
-        if (pattern.equals("*")) {
-            return true;
-        } else {
-            return 
-            (pattern.endsWith("*") && requestUri.startsWith(pattern.substring(0, pattern.length() - 1))) ||
-            (pattern.startsWith("*") && requestUri.endsWith(pattern.substring(1, pattern.length())));
-        }
-    }
-    
     public void handleRequest(final HttpServerConnection conn, final HttpContext context) 
             throws IOException, HttpException { 
         context.setAttribute(HttpExecutionContext.HTTP_CONNECTION, conn);
@@ -220,7 +179,11 @@
             final HttpRequest request, 
             final HttpResponse response,
             final HttpContext context) throws HttpException, IOException {
-        HttpRequestHandler handler = lookupHandler(request.getRequestLine().getUri());
+        HttpRequestHandler handler = null;
+        if (this.handlerResolver != null) {
+            String requestURI = request.getRequestLine().getUri();
+            handler = this.handlerResolver.lookup(requestURI);
+        }
         if (handler != null) {
             handler.handle(request, response, context);
         } else {