hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r442292 - in /jakarta/httpcomponents/httpcore/trunk: module-main/src/examples/org/apache/http/examples/ module-main/src/main/java/org/apache/http/protocol/ module-nio/src/examples/org/apache/http/nio/examples/
Date Mon, 11 Sep 2006 19:00:51 GMT
Author: olegk
Date: Mon Sep 11 12:00:51 2006
New Revision: 442292

URL: http://svn.apache.org/viewvc?view=rev&rev=442292
Log:
* Added HttpRequestHandler interface
* Added HttpService facility to register HTTP request handlers for HTTP requests whose request
URI matches a given pattern. One is no longer required to extend the HttpService class in
order to add a handler for a specific URI

Added:
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpRequestHandler.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
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/nio/examples/AsyncHttpServerServer.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=442292&r1=442291&r2=442292
==============================================================================
--- 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
Mon Sep 11 12:00:51 2006
@@ -55,6 +55,7 @@
 import org.apache.http.params.HttpProtocolParams;
 import org.apache.http.protocol.HttpContext;
 import org.apache.http.protocol.HttpExecutionContext;
+import org.apache.http.protocol.HttpRequestHandler;
 import org.apache.http.protocol.HttpService;
 import org.apache.http.protocol.ResponseConnControl;
 import org.apache.http.protocol.ResponseContent;
@@ -81,17 +82,9 @@
         t.start();
     }
     
-    static class HttpFileService extends HttpService {
+    static class HttpFileHandler implements HttpRequestHandler  {
         
-        public HttpFileService() {
-            super();
-            addInterceptor(new ResponseDate());
-            addInterceptor(new ResponseServer());                    
-            addInterceptor(new ResponseContent());
-            addInterceptor(new ResponseConnControl());
-        }
-
-        protected void doService(
+        public void handle(
                 final HttpRequest request, 
                 final HttpResponse response,
                 final HttpContext context) throws HttpException, IOException {
@@ -185,11 +178,16 @@
                     conn.bind(socket, this.params);
 
                     // Set up HTTP service
-                    HttpFileService fileServiceHandler = new HttpFileService();
-                    fileServiceHandler.setParams(this.params);
+                    HttpService httpService = new HttpService();
+                    httpService.addInterceptor(new ResponseDate());
+                    httpService.addInterceptor(new ResponseServer());                   

+                    httpService.addInterceptor(new ResponseContent());
+                    httpService.addInterceptor(new ResponseConnControl());
+                    httpService.setParams(this.params);
+                    httpService.registerRequestHandler("*", new HttpFileHandler());
                     
                     // Start worker thread
-                    Thread t = new WorkerThread(fileServiceHandler, conn, this.globalContext);
+                    Thread t = new WorkerThread(httpService, conn, this.globalContext);
                     t.setDaemon(true);
                     t.start();
                 } catch (InterruptedIOException ex) {

Added: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpRequestHandler.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpRequestHandler.java?view=auto&rev=442292
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpRequestHandler.java
(added)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpRequestHandler.java
Mon Sep 11 12:00:51 2006
@@ -0,0 +1,51 @@
+/*
+ * $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.io.IOException;
+
+import org.apache.http.HttpException;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+
+/**
+ *
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ *
+ * @version $Revision$
+ * 
+ * @since 4.0
+ */
+public interface HttpRequestHandler {
+
+    void handle(HttpRequest request, HttpResponse response, HttpContext context) 
+            throws HttpException, IOException;
+    
+}

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

Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpRequestHandler.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=442292&r1=442291&r2=442292
==============================================================================
--- 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
Mon Sep 11 12:00:51 2006
@@ -30,6 +30,9 @@
 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;
@@ -60,9 +63,12 @@
     private HttpParams params = null;
     private ConnectionReuseStrategy connStrategy = null;
     private HttpResponseFactory responseFactory = null;
-
+    
+    private final Map handlerMap;
+    
     public HttpService() {
         super();
+        this.handlerMap = new HashMap();
         this.connStrategy = new DefaultConnectionReuseStrategy();
         this.responseFactory = new DefaultHttpResponseFactory();
     }
@@ -89,6 +95,46 @@
         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);
@@ -157,7 +203,12 @@
             final HttpRequest request, 
             final HttpResponse response,
             final HttpContext context) throws HttpException, IOException {
-        response.setStatusCode(HttpStatus.SC_NOT_IMPLEMENTED);
+        HttpRequestHandler handler = lookupHandler(request.getRequestLine().getUri());
+        if (handler != null) {
+            handler.handle(request, response, context);
+        } else {
+            response.setStatusCode(HttpStatus.SC_NOT_IMPLEMENTED);
+        }
     }
     
 }

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/nio/examples/AsyncHttpServerServer.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/nio/examples/AsyncHttpServerServer.java?view=diff&rev=442292&r1=442291&r2=442292
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/nio/examples/AsyncHttpServerServer.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/nio/examples/AsyncHttpServerServer.java
Mon Sep 11 12:00:51 2006
@@ -33,6 +33,7 @@
 import org.apache.http.params.HttpProtocolParams;
 import org.apache.http.protocol.HttpContext;
 import org.apache.http.protocol.HttpExecutionContext;
+import org.apache.http.protocol.HttpRequestHandler;
 import org.apache.http.protocol.HttpService;
 import org.apache.http.protocol.ResponseConnControl;
 import org.apache.http.protocol.ResponseContent;
@@ -95,10 +96,16 @@
             session.setAttribute(CONSUMER, conn.getIOConsumer());
             session.setAttribute(PRODUCER, conn.getIOProducer());
             
-            HttpFileService service = new HttpFileService();
-            service.setParams(this.params);
+            // Set up HTTP service
+            HttpService httpService = new HttpService();
+            httpService.addInterceptor(new ResponseDate());
+            httpService.addInterceptor(new ResponseServer());                    
+            httpService.addInterceptor(new ResponseContent());
+            httpService.addInterceptor(new ResponseConnControl());
+            httpService.setParams(this.params);
+            httpService.registerRequestHandler("*", new HttpFileHandler());
             
-            Thread worker = new WorkerThread(service, conn, this.context);
+            Thread worker = new WorkerThread(httpService, conn, this.context);
             session.setAttribute(WORKER, worker);
 
             worker.setDaemon(true);
@@ -143,17 +150,9 @@
         
     }
     
-    static class HttpFileService extends HttpService {
+    static class HttpFileHandler implements HttpRequestHandler  {
         
-        public HttpFileService() {
-            super();
-            addInterceptor(new ResponseDate());
-            addInterceptor(new ResponseServer());                    
-            addInterceptor(new ResponseContent());
-            addInterceptor(new ResponseConnControl());
-        }
-
-        protected void doService(
+        public void handle(
                 final HttpRequest request, 
                 final HttpResponse response,
                 final HttpContext context) throws HttpException, IOException {



Mime
View raw message