hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r502391 - in /jakarta/httpcomponents/httpcore/trunk: ./ module-main/src/main/java/org/apache/http/protocol/ module-main/src/test/java/org/apache/http/protocol/
Date Thu, 01 Feb 2007 22:53:18 GMT
Author: olegk
Date: Thu Feb  1 14:53:17 2007
New Revision: 502391

URL: http://svn.apache.org/viewvc?view=rev&rev=502391
Log:
HTTPCORE-36: Fixed #setHandlers() method and matching of request URIs with a query part in
HttpRequestHandlerRegistry

Added:
    jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/protocol/TestHttpRequestHandlerRegistry.java
  (with props)
Modified:
    jakarta/httpcomponents/httpcore/trunk/RELEASE_NOTES.txt
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpRequestHandlerRegistry.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/protocol/TestAllProtocol.java

Modified: jakarta/httpcomponents/httpcore/trunk/RELEASE_NOTES.txt
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/RELEASE_NOTES.txt?view=diff&rev=502391&r1=502390&r2=502391
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/RELEASE_NOTES.txt (original)
+++ jakarta/httpcomponents/httpcore/trunk/RELEASE_NOTES.txt Thu Feb  1 14:53:17 2007
@@ -1,5 +1,9 @@
 Changes since release 4.0 Alpha 3
 -------------------
+* [HTTPCORE-36]: Fixed #setHandlers() method and matching of request URIs 
+  with a query part in HttpRequestHandlerRegistry
+  Contributed by Oleg Kalnichevski <olegk at apache.org>
+
 * [HTTPCORE-28]: DefaultConnectingIOReactor now maintains a queue of connect 
   requests and registers new sessions with the selector on the I/O thread.
   Contributed by Oleg Kalnichevski <olegk at apache.org>

Modified: 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=diff&rev=502391&r1=502390&r2=502391
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpRequestHandlerRegistry.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpRequestHandlerRegistry.java
Thu Feb  1 14:53:17 2007
@@ -34,7 +34,6 @@
 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. 
@@ -77,15 +76,24 @@
         this.handlerMap.remove(pattern);
     }
     
-    public void setHandlers(final Properties props) {
-        if (props == null) {
-            throw new IllegalArgumentException("Properties may not be null");
+    public void setHandlers(final Map map) {
+        if (map == null) {
+            throw new IllegalArgumentException("Map of handlers may not be null");
         }
         this.handlerMap.clear();
-        this.handlerMap.putAll(props);
+        this.handlerMap.putAll(map);
     }
     
-    public HttpRequestHandler lookup(final String requestURI) {
+    public HttpRequestHandler lookup(String requestURI) {
+        if (requestURI == null) {
+            throw new IllegalArgumentException("Request URI may not be null");
+        }
+        //Strip away the query part part if found
+        int index = requestURI.indexOf("?");
+        if (index != -1) {
+            requestURI = requestURI.substring(0, index);
+        }
+        
         // direct match?
         Object handler = this.handlerMap.get(requestURI);
         if (handler == null) {

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/protocol/TestAllProtocol.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/protocol/TestAllProtocol.java?view=diff&rev=502391&r1=502390&r2=502391
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/protocol/TestAllProtocol.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/protocol/TestAllProtocol.java
Thu Feb  1 14:53:17 2007
@@ -42,6 +42,7 @@
         TestSuite suite = new TestSuite();
         suite.addTest(TestHttpExecutionContext.suite());
         suite.addTest(TestStandardInterceptors.suite());
+        suite.addTest(TestHttpRequestHandlerRegistry.suite());
         return suite;
     }
 

Added: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/protocol/TestHttpRequestHandlerRegistry.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/protocol/TestHttpRequestHandlerRegistry.java?view=auto&rev=502391
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/protocol/TestHttpRequestHandlerRegistry.java
(added)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/protocol/TestHttpRequestHandlerRegistry.java
Thu Feb  1 14:53:17 2007
@@ -0,0 +1,230 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ * 
+ * ====================================================================
+ * 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
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.protocol;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.http.HttpException;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ */
+public class TestHttpRequestHandlerRegistry extends TestCase {
+
+    public TestHttpRequestHandlerRegistry(String testName) {
+        super(testName);
+    }
+
+    public static void main(String args[]) {
+        String[] testCaseName = { TestHttpRequestHandlerRegistry.class.getName() };
+        junit.textui.TestRunner.main(testCaseName);
+    }
+
+    public static Test suite() {
+        return new TestSuite(TestHttpRequestHandlerRegistry.class);
+    }
+
+    private static class DummyHttpRequestHandler implements HttpRequestHandler {
+
+        public void handle(
+                final HttpRequest request, 
+                final HttpResponse response, 
+                final HttpContext context) throws HttpException, IOException {
+        }
+        
+    }
+    
+    public void testRegisterUnregister() throws Exception {
+        HttpRequestHandler h1 = new DummyHttpRequestHandler();
+        HttpRequestHandler h2 = new DummyHttpRequestHandler();
+        HttpRequestHandler h3 = new DummyHttpRequestHandler();
+        
+        HttpRequestHandlerRegistry registry = new HttpRequestHandlerRegistry();
+        registry.register("/h1", h1);
+        registry.register("/h2", h2);
+        registry.register("/h3", h3);
+        
+        HttpRequestHandler h;
+        
+        h = registry.lookup("/h1");
+        assertNotNull(h);
+        assertTrue(h1 == h);
+        h = registry.lookup("/h2");
+        assertNotNull(h);
+        assertTrue(h2 == h);
+        h = registry.lookup("/h3");
+        assertNotNull(h);
+        assertTrue(h3 == h);
+        
+        registry.unregister("/h1");
+        h = registry.lookup("/h1");
+        assertNull(h);
+        
+        Map map = new HashMap();
+        map.put("/a1", h1);
+        map.put("/a2", h2);
+        map.put("/a3", h3);
+        registry.setHandlers(map);
+
+        h = registry.lookup("/h2");
+        assertNull(h);
+        h = registry.lookup("/h3");
+        assertNull(h);
+
+        h = registry.lookup("/a1");
+        assertNotNull(h);
+        assertTrue(h1 == h);
+    }
+    
+    public void testWildCardMatching1() throws Exception {
+        HttpRequestHandler h1 = new DummyHttpRequestHandler();
+        HttpRequestHandler h2 = new DummyHttpRequestHandler();
+        HttpRequestHandler h3 = new DummyHttpRequestHandler();
+        HttpRequestHandler def = new DummyHttpRequestHandler();
+        
+        HttpRequestHandlerRegistry registry = new HttpRequestHandlerRegistry();
+        registry.register("*", def);
+        registry.register("/one/*", h1);
+        registry.register("/one/two/*", h2);
+        registry.register("/one/two/three/*", h3);
+        
+        HttpRequestHandler h;
+        
+        h = registry.lookup("/one/request");
+        assertNotNull(h);
+        assertTrue(h1 == h);
+        
+        h = registry.lookup("/one/two/request");
+        assertNotNull(h);
+        assertTrue(h2 == h);
+
+        h = registry.lookup("/one/two/three/request");
+        assertNotNull(h);
+        assertTrue(h3 == h);
+
+        h = registry.lookup("default/request");
+        assertNotNull(h);
+        assertTrue(def == h);
+    }
+
+    public void testWildCardMatching2() throws Exception {
+        HttpRequestHandler h1 = new DummyHttpRequestHandler();
+        HttpRequestHandler h2 = new DummyHttpRequestHandler();
+        HttpRequestHandler def = new DummyHttpRequestHandler();
+        
+        HttpRequestHandlerRegistry registry = new HttpRequestHandlerRegistry();
+        registry.register("*", def);
+        registry.register("*.view", h1);
+        registry.register("*.form", h2);
+        
+        HttpRequestHandler h;
+        
+        h = registry.lookup("/that.view");
+        assertNotNull(h);
+        assertTrue(h1 == h);
+        
+        h = registry.lookup("/that.form");
+        assertNotNull(h);
+        assertTrue(h2 == h);
+
+        h = registry.lookup("/whatever");
+        assertNotNull(h);
+        assertTrue(def == h);
+    }
+
+    public void testWildCardMatchingWithQuery() throws Exception {
+        HttpRequestHandler h1 = new DummyHttpRequestHandler();
+        HttpRequestHandler h2 = new DummyHttpRequestHandler();
+        HttpRequestHandler def = new DummyHttpRequestHandler();
+        
+        HttpRequestHandlerRegistry registry = new HttpRequestHandlerRegistry();
+        registry.register("*", def);
+        registry.register("*.view", h1);
+        registry.register("*.form", h2);
+        
+        HttpRequestHandler h;
+        
+        h = registry.lookup("/that.view?param=value");
+        assertNotNull(h);
+        assertTrue(h1 == h);
+        
+        h = registry.lookup("/that.form?whatever");
+        assertNotNull(h);
+        assertTrue(h2 == h);
+
+        h = registry.lookup("/whatever");
+        assertNotNull(h);
+        assertTrue(def == h);
+    }
+
+    public void testInvalidInput() throws Exception {
+        HttpRequestHandlerRegistry registry = new HttpRequestHandlerRegistry();
+        try {
+            registry.register(null, null);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+        
+        try {
+            registry.register("", null);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+
+        registry.unregister(null);
+        
+        try {
+            registry.setHandlers(null);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+        
+        try {
+            registry.lookup(null);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+    }
+    
+}

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

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



Mime
View raw message