You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2010/03/01 14:57:02 UTC

svn commit: r917529 - in /camel/trunk: components/camel-http/src/main/java/org/apache/camel/component/http/helper/ components/camel-jetty/src/test/java/org/apache/camel/component/jetty/ tests/camel-itest/src/test/java/org/apache/camel/itest/issues/

Author: ningjiang
Date: Mon Mar  1 13:57:01 2010
New Revision: 917529

URL: http://svn.apache.org/viewvc?rev=917529&view=rev
Log:
CAMEL-2510 Fixed the issue of Mixing jetty/http in a route screws up the URI used by HttpClient

Added:
    camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/issues/JettyHttpTest.java   (with props)
Modified:
    camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/helper/HttpProducerHelper.java
    camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpBridgeRouteTest.java

Modified: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/helper/HttpProducerHelper.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/helper/HttpProducerHelper.java?rev=917529&r1=917528&r2=917529&view=diff
==============================================================================
--- camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/helper/HttpProducerHelper.java (original)
+++ camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/helper/HttpProducerHelper.java Mon Mar  1 13:57:01 2010
@@ -16,7 +16,11 @@
  */
 package org.apache.camel.component.http.helper;
 
+import java.net.URI;
+import java.net.URISyntaxException;
+
 import org.apache.camel.Exchange;
+import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.component.http.HttpEndpoint;
 import org.apache.camel.component.http.HttpMethods;
 
@@ -47,18 +51,44 @@
         }
 
         // append HTTP_PATH to HTTP_URI if it is provided in the header
-        // when the endpoint is not working as a bridge
         String path = exchange.getIn().getHeader(Exchange.HTTP_PATH, String.class);
         if (path != null) {
-            // make sure that there is exactly one "/" between HTTP_URI and
-            // HTTP_PATH
-            if (!uri.endsWith("/")) {
-                uri = uri + "/";
-            }
             if (path.startsWith("/")) {
-                path = path.substring(1);
+                URI baseURI;
+                String baseURIString = exchange.getIn().getHeader(Exchange.HTTP_BASE_URI, String.class);
+                try {
+                    if (baseURIString == null) {
+                        if (exchange.getFromEndpoint() != null) {
+                            baseURIString = exchange.getFromEndpoint().getEndpointUri();
+                        } else {
+                            // will set a default one for it
+                            baseURIString = "/";
+                        }
+                    }
+                    baseURI = new URI(baseURIString);
+                    String basePath = baseURI.getPath();
+                    if (path.startsWith(basePath)) {
+                        path = path.substring(basePath.length());
+                        if (path.startsWith("/")) {
+                            path = path.substring(1);
+                        }
+                    } else {
+                        throw new RuntimeCamelException("Can't anylze the Exchange.HTTP_PATH header, due to: can't find the right HTTP_BASE_URI");
+                    }
+                } catch (Throwable t) {
+                    throw new RuntimeCamelException("Can't anylze the Exchange.HTTP_PATH header, due to: "
+                                                    + t.getMessage(), t);
+                }
+
+            }
+            if (path.length() > 0) {
+                // make sure that there is exactly one "/" between HTTP_URI and
+                // HTTP_PATH
+                if (!uri.endsWith("/")) {
+                    uri = uri + "/";
+                }
+                uri = uri.concat(path);
             }
-            uri = uri.concat(path);
         }
 
         return uri;

Modified: camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpBridgeRouteTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpBridgeRouteTest.java?rev=917529&r1=917528&r2=917529&view=diff
==============================================================================
--- camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpBridgeRouteTest.java (original)
+++ camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpBridgeRouteTest.java Mon Mar  1 13:57:01 2010
@@ -31,7 +31,7 @@
 
         String response = template.requestBodyAndHeader("http://localhost:9090/test/hello", new ByteArrayInputStream("This is a test".getBytes()), "Content-Type", "application/xml", String.class);
         
-        assertEquals("Get a wrong response", "/test/hello", response);
+        assertEquals("Get a wrong response", "/", response);
         
         response = template.requestBody("http://localhost:9080/hello/world", "hello", String.class);
         

Added: camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/issues/JettyHttpTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/issues/JettyHttpTest.java?rev=917529&view=auto
==============================================================================
--- camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/issues/JettyHttpTest.java (added)
+++ camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/issues/JettyHttpTest.java Mon Mar  1 13:57:01 2010
@@ -0,0 +1,86 @@
+/**
+ * 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.
+ */
+package org.apache.camel.itest.issues;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.builder.xml.Namespaces;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Ignore;
+import org.junit.Test;
+
+/**
+ * @version $Revision$
+ */
+public class JettyHttpTest extends CamelTestSupport {
+
+    private String targetProducerUri = "http://localhost:8542/someservice?bridgeEndpoint=true&throwExceptionOnFailure=false";
+    private String targetConsumerUri = "jetty:http://localhost:8542/someservice?matchOnUriPrefix=true";
+    private String sourceUri = "jetty:http://localhost:6323/myservice?matchOnUriPrefix=true";
+    private String sourceProducerUri = "http://localhost:6323/myservice";
+
+    @Test
+    public void testGetRootPath() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("Hi! /someservice");
+
+        template.sendBody("direct:root", "");
+
+        assertMockEndpointsSatisfied();
+    }
+    
+    @Test
+    public void testGetWithRelativePath() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("Hi! /someservice/relative");
+        
+        template.sendBody("direct:relative", "");
+        assertMockEndpointsSatisfied();
+        
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+
+                from(targetConsumerUri)
+                    .process(new Processor() {
+                        public void process(Exchange exchange) throws Exception {
+                            String path = exchange.getIn().getHeader(Exchange.HTTP_PATH, String.class);
+                            exchange.getOut().setBody("Hi! " + path);
+                        }   
+                    });
+
+                from(sourceUri)
+                    .to(targetProducerUri);
+
+                from("direct:root")
+                    .to(sourceProducerUri)
+                    .to("mock:result");
+                
+                from("direct:relative")
+                    .to(sourceProducerUri + "/relative")
+                    .to("mock:result");
+
+            }
+        };
+    }
+}

Propchange: camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/issues/JettyHttpTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/issues/JettyHttpTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date