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 2009/09/11 04:22:58 UTC

svn commit: r813653 - in /camel/trunk/components: camel-http/src/main/java/org/apache/camel/component/http/ camel-jetty/src/main/java/org/apache/camel/component/jetty/ camel-jetty/src/test/java/org/apache/camel/component/jetty/ camel-servlet/src/main/j...

Author: ningjiang
Date: Fri Sep 11 02:22:57 2009
New Revision: 813653

URL: http://svn.apache.org/viewvc?rev=813653&view=rev
Log:
CAMEL-1925 CAMEL-2002 fixed some http related issues

Added:
    camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpBridgeRouteTest.java   (with props)
Modified:
    camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CamelServlet.java
    camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java
    camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java
    camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
    camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java
    camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java
    camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/CamelHttpTransportServlet.java
    camel/trunk/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/HttpClientRouteTest.java
    camel/trunk/components/camel-servlet/src/test/resources/log4j.properties
    camel/trunk/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/multiServletWeb.xml
    camel/trunk/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/web.xml

Modified: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CamelServlet.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CamelServlet.java?rev=813653&r1=813652&r2=813653&view=diff
==============================================================================
--- camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CamelServlet.java (original)
+++ camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CamelServlet.java Fri Sep 11 02:22:57 2009
@@ -35,12 +35,7 @@
     private static final long serialVersionUID = -7061982839117697829L;
 
     private ConcurrentHashMap<String, HttpConsumer> consumers = new ConcurrentHashMap<String, HttpConsumer>();
-    private boolean matchOnUriPrefix;
-
-    public CamelServlet(boolean matchOnUriPrefix) {
-        this.matchOnUriPrefix = matchOnUriPrefix;
-    }
-    
+   
     @Override
     protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         try {
@@ -78,9 +73,9 @@
         }
         HttpConsumer answer = consumers.get(path);
                
-        if (answer == null && matchOnUriPrefix) {
+        if (answer == null) {
             for (String key : consumers.keySet()) {
-                if (path.startsWith(key)) {
+                if (consumers.get(key).getEndpoint().isMatchOnUriPrefix() && path.startsWith(key)) {
                     answer = consumers.get(key);
                     break;
                 }
@@ -96,12 +91,5 @@
     public void disconnect(HttpConsumer consumer) {
         consumers.remove(consumer.getPath());
     }
-
-    public boolean isMatchOnUriPrefix() {
-        return matchOnUriPrefix;
-    }
     
-    public void setMatchOnUriPrefix(boolean matchOnUriPrefix) {
-        this.matchOnUriPrefix = matchOnUriPrefix;
-    }
 }

Modified: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java?rev=813653&r1=813652&r2=813653&view=diff
==============================================================================
--- camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java (original)
+++ camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java Fri Sep 11 02:22:57 2009
@@ -39,7 +39,7 @@
     protected HttpClientConfigurer httpClientConfigurer;
     protected HttpConnectionManager httpConnectionManager = new MultiThreadedHttpConnectionManager();
     protected HttpBinding httpBinding;
-    private boolean matchOnUriPrefix;
+   
     
     /**
      * Connects the URL specified on the endpoint to the specified processor.
@@ -103,7 +103,7 @@
                     httpClientConfigurer, new ProxyHttpClientConfigurer(host, port));
             }
         }
-        matchOnUriPrefix = Boolean.parseBoolean(getAndRemoveParameter(parameters, "matchOnUriPrefix", String.class));
+        
     }
     
     @Override
@@ -121,6 +121,9 @@
         // should we use an exception for failed error codes?
         Boolean throwExceptionOnFailure = getAndRemoveParameter(parameters, "throwExceptionOnFailure", Boolean.class);
 
+        Boolean bridgeEndpoint = getAndRemoveParameter(parameters, "bridgeEndpoint", Boolean.class);
+        
+        Boolean matchOnUriPrefix = Boolean.parseBoolean(getAndRemoveParameter(parameters, "matchOnUriPrefix", String.class));
         // restructure uri to be based on the parameters left as we dont want to include the Camel internal options
         URI httpUri = URISupport.createRemainingURI(new URI(uri), parameters);
         uri = httpUri.toString();
@@ -143,6 +146,12 @@
         if (throwExceptionOnFailure != null) {
             endpoint.setThrowExceptionOnFailure(throwExceptionOnFailure);
         }
+        if (bridgeEndpoint != null) {
+            endpoint.setBridgeEndpoint(bridgeEndpoint);
+        }
+        if (matchOnUriPrefix != null) {
+            endpoint.setMatchOnUriPrefix(matchOnUriPrefix);
+        }
         setProperties(endpoint, parameters);
         return endpoint;
     }    
@@ -175,9 +184,5 @@
     public void setHttpBinding(HttpBinding httpBinding) {
         this.httpBinding = httpBinding;
     }
-
-    public boolean isMatchOnUriPrefix() {
-        return matchOnUriPrefix;
-    }  
     
 }

Modified: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java?rev=813653&r1=813652&r2=813653&view=diff
==============================================================================
--- camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java (original)
+++ camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java Fri Sep 11 02:22:57 2009
@@ -52,6 +52,8 @@
     private HttpClientConfigurer httpClientConfigurer;
     private HttpConnectionManager httpConnectionManager;
     private boolean throwExceptionOnFailure = true;
+    private boolean bridgeEndpoint;
+    private boolean matchOnUriPrefix;
 
     public HttpEndpoint() {
     }
@@ -229,4 +231,20 @@
     public void setThrowExceptionOnFailure(boolean throwExceptionOnFailure) {
         this.throwExceptionOnFailure = throwExceptionOnFailure;
     }
+    
+    public boolean isBridgeEndpoint() {
+        return bridgeEndpoint;
+    }
+    
+    public void setBridgeEndpoint(boolean bridge) {
+        this.bridgeEndpoint = bridge;
+    }
+    
+    public boolean isMatchOnUriPrefix() {
+        return matchOnUriPrefix;
+    }
+    
+    public void setMatchOnUriPrefix(boolean match) {
+        this.matchOnUriPrefix = match;
+    }
 }

Modified: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java?rev=813653&r1=813652&r2=813653&view=diff
==============================================================================
--- camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java (original)
+++ camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java Fri Sep 11 02:22:57 2009
@@ -89,7 +89,6 @@
                     throw populateHttpOperationFailedException(exchange, method, responseCode);
                 }
             }
-
         } finally {
             method.releaseConnection();
         }
@@ -192,7 +191,8 @@
      * @return the created method as either GET or POST
      */
     protected HttpMethod createMethod(Exchange exchange) {
-        // is a query string provided in the endpoint URI or in a header (header overrules endpoint)
+        // is a query string provided in the endpoint URI or in a header (header
+        // overrules endpoint)
         String queryString = exchange.getIn().getHeader(Exchange.HTTP_QUERY, String.class);
         if (queryString == null) {
             queryString = ((HttpEndpoint)getEndpoint()).getHttpUri().getQuery();
@@ -213,15 +213,20 @@
             methodToUse = requestEntity != null ? HttpMethods.POST : HttpMethods.GET;
         }
 
-        String uri = exchange.getIn().getHeader(Exchange.HTTP_URI, String.class);
+        String uri = null;
+        if (!((HttpEndpoint)getEndpoint()).isBridgeEndpoint()) {
+            uri = exchange.getIn().getHeader(Exchange.HTTP_URI, String.class);
+        }
         if (uri == null) {
             uri = ((HttpEndpoint)getEndpoint()).getHttpUri().toString();
         }
 
         // 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
+            // make sure that there is exactly one "/" between HTTP_URI and
+            // HTTP_PATH
             if (!uri.endsWith("/")) {
                 uri = uri + "/";
             }

Modified: camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java?rev=813653&r1=813652&r2=813653&view=diff
==============================================================================
--- camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java (original)
+++ camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java Fri Sep 11 02:22:57 2009
@@ -37,10 +37,6 @@
     // TODO: should use the new Async API and allow end users to define if they want Jetty continuation support or not
 
     private static final long serialVersionUID = 1L;
-
-    public CamelContinuationServlet(boolean matchOnUriPrefix) {
-        super(matchOnUriPrefix);
-    }
         
     @Override
     protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

Modified: camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java?rev=813653&r1=813652&r2=813653&view=diff
==============================================================================
--- camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java (original)
+++ camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java Fri Sep 11 02:22:57 2009
@@ -279,7 +279,7 @@
     }
 
     protected CamelServlet createServletForConnector(Server server, Connector connector, List<Handler> handlers) throws Exception {
-        CamelServlet camelServlet = new CamelServlet(isMatchOnUriPrefix());
+        CamelServlet camelServlet = new CamelServlet();
 
         Context context = new Context(server, "/", Context.NO_SECURITY | Context.NO_SESSIONS);
         context.setConnectorNames(new String[] {connector.getName()});

Added: 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=813653&view=auto
==============================================================================
--- camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpBridgeRouteTest.java (added)
+++ camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpBridgeRouteTest.java Fri Sep 11 02:22:57 2009
@@ -0,0 +1,76 @@
+/**
+ * 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.component.jetty;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.Processor;
+import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+public class HttpBridgeRouteTest extends CamelTestSupport {
+    @Test
+    public void testHttpClient() throws Exception {
+       
+
+        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);
+        
+        response = template.requestBody("http://localhost:9080/hello/world", "hello", String.class);
+        
+        assertEquals("Get a wrong response", "/hello/world", response);
+        
+        try {
+            template.requestBody("http://localhost:9090/hello/world", "hello", String.class);
+            fail("Expect exception here!");
+        } catch (Exception ex) {
+            assertTrue("We should get a RuntimeCamelException", ex instanceof RuntimeCamelException);
+        }
+    }
+
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() {
+                errorHandler(noErrorHandler());
+
+                Processor serviceProc = new Processor() {
+                    public void process(Exchange exchange) throws Exception {
+                        // get the request URL and copy it to the request body
+                        String uri = exchange.getIn().getHeader(Exchange.HTTP_URI, String.class);
+                        exchange.getOut().setBody(uri);
+                    }
+                };
+                from("jetty:http://localhost:9090/test/hello")
+                    .to("http://localhost:9080?throwExceptionOnFailure=false&bridgeEndpoint=true");
+                
+                from("jetty://http://localhost:9080?matchOnUriPrefix=true").process(serviceProc);
+                              
+                
+            }
+        };
+    }    
+
+}

Propchange: camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpBridgeRouteTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpBridgeRouteTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/CamelHttpTransportServlet.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/CamelHttpTransportServlet.java?rev=813653&r1=813652&r2=813653&view=diff
==============================================================================
--- camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/CamelHttpTransportServlet.java (original)
+++ camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/CamelHttpTransportServlet.java Fri Sep 11 02:22:57 2009
@@ -35,15 +35,9 @@
     private String servletName;
     private AbstractApplicationContext applicationContext;
     
-    public CamelHttpTransportServlet() {
-        super(false);        
-    }
-    
     public void init(ServletConfig config) throws ServletException {
         super.init(config);
         servletName = config.getServletName();
-        String matchOnUriPrefix = config.getInitParameter("matchOnUriPrefix");
-        this.setMatchOnUriPrefix(Boolean.valueOf(matchOnUriPrefix));
         // parser the servlet init parameters
         CAMEL_SERVLET_MAP.put(servletName, this);
         String contextConfigLocation = config.getInitParameter("contextConfigLocation");

Modified: camel/trunk/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/HttpClientRouteTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/HttpClientRouteTest.java?rev=813653&r1=813652&r2=813653&view=diff
==============================================================================
--- camel/trunk/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/HttpClientRouteTest.java (original)
+++ camel/trunk/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/HttpClientRouteTest.java Fri Sep 11 02:22:57 2009
@@ -61,7 +61,7 @@
         public void configure() throws Exception {
             errorHandler(noErrorHandler());
             // START SNIPPET: route
-            from("servlet:///hello").process(new Processor() {
+            from("servlet:///hello?matchOnUriPrefix=true").process(new Processor() {
                 public void process(Exchange exchange) throws Exception {
                     String contentType = exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class);
                     String path = exchange.getIn().getHeader(Exchange.HTTP_PATH, String.class);

Modified: camel/trunk/components/camel-servlet/src/test/resources/log4j.properties
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-servlet/src/test/resources/log4j.properties?rev=813653&r1=813652&r2=813653&view=diff
==============================================================================
--- camel/trunk/components/camel-servlet/src/test/resources/log4j.properties (original)
+++ camel/trunk/components/camel-servlet/src/test/resources/log4j.properties Fri Sep 11 02:22:57 2009
@@ -2,7 +2,7 @@
 #
 # The logging properties used for eclipse testing, We want to see debug output on the console.
 #
-log4j.rootLogger=INFO, out
+log4j.rootLogger=WARN, out
 
 # uncomment the following line to turn on Camel debugging
 #log4j.logger.org.apache.camel=DEBUG

Modified: camel/trunk/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/multiServletWeb.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/multiServletWeb.xml?rev=813653&r1=813652&r2=813653&view=diff
==============================================================================
--- camel/trunk/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/multiServletWeb.xml (original)
+++ camel/trunk/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/multiServletWeb.xml Fri Sep 11 02:22:57 2009
@@ -31,10 +31,6 @@
     <servlet-class>
         org.apache.camel.component.servlet.CamelHttpTransportServlet
     </servlet-class>
-    <init-param> 
-      <param-name>matchOnUriPrefix</param-name> 
-      <param-value>true</param-value> 
-    </init-param>
   </servlet>
   
   <servlet>
@@ -43,10 +39,6 @@
     <servlet-class>
         org.apache.camel.component.servlet.CamelHttpTransportServlet
     </servlet-class>
-    <init-param> 
-      <param-name>matchOnUriPrefix</param-name> 
-      <param-value>false</param-value> 
-    </init-param>
   </servlet>
 
   <servlet-mapping>

Modified: camel/trunk/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/web.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/web.xml?rev=813653&r1=813652&r2=813653&view=diff
==============================================================================
--- camel/trunk/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/web.xml (original)
+++ camel/trunk/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/web.xml Fri Sep 11 02:22:57 2009
@@ -31,10 +31,7 @@
     <servlet-class>
         org.apache.camel.component.servlet.CamelHttpTransportServlet
     </servlet-class>
-    <init-param> 
-      <param-name>matchOnUriPrefix</param-name> 
-      <param-value>true</param-value> 
-    </init-param>
+    
   </servlet>
 
   <servlet-mapping>