svn commit: r613990 - in /activemq/camel/trunk/components/camel-http/src: main/java/org/apache/camel/component/http/HttpProducer.java test/java/org/apache/camel/component/http/HttpGetWithHeadersTest.java

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r613990 - in /activemq/camel/trunk/components/camel-http/src: main/java/org/apache/camel/component/http/HttpProducer.java test/java/org/apache/camel/component/http/HttpGetWithHeadersTest.java

romkal-2
Author: romkal
Date: Mon Jan 21 12:00:50 2008
New Revision: 613990

URL: http://svn.apache.org/viewvc?rev=613990&view=rev
Log:
CAMEL-254 : Added a way to set HTTP headers as Camel message headers

Added:
    activemq/camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetWithHeadersTest.java   (with props)
Modified:
    activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java

Modified: activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java?rev=613990&r1=613989&r2=613990&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java (original)
+++ activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java Mon Jan 21 12:00:50 2008
@@ -17,6 +17,9 @@
 package org.apache.camel.component.http;
 
 import java.io.InputStream;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.Message;
@@ -34,7 +37,12 @@
  * @version $Revision: 1.1 $
  */
 public class HttpProducer extends DefaultProducer<HttpExchange> implements Producer<HttpExchange> {
+    private static final String HTTP_RESPONSE_CODE = "http.responseCode";
     public static final String QUERY = "org.apache.camel.component.http.query";
+    
+    // This should be a set of lower-case strings
+    public static final Set<String> HEADERS_TO_SKIP = new HashSet<String>(Arrays.asList(
+            "content-length", "content-type", HTTP_RESPONSE_CODE.toLowerCase()));
     private HttpClient httpClient;
 
     public HttpProducer(HttpEndpoint endpoint) {
@@ -44,6 +52,16 @@
 
     public void process(Exchange exchange) throws Exception {
         HttpMethod method = createMethod(exchange);
+        
+        
+        // propagate headers as HTTP headers
+        for (String headerName : exchange.getIn().getHeaders().keySet()) {
+            String headerValue = exchange.getIn().getHeader(headerName, String.class);
+            if (shouldHeaderBePropagated(headerName, headerValue)) {
+                method.addRequestHeader(headerName, headerValue);
+            }
+        }
+        
         int responseCode = httpClient.executeMethod(method);
 
         // lets store the result in the output message.
@@ -59,7 +77,7 @@
             out.setHeader(name, value);
         }
 
-        out.setHeader("http.responseCode", responseCode);
+        out.setHeader(HTTP_RESPONSE_CODE, responseCode);
     }
 
     public HttpClient getHttpClient() {
@@ -103,5 +121,18 @@
             }
         }
         return entity;
+    }
+    
+    protected boolean shouldHeaderBePropagated(String headerName, String headerValue) {
+        if (headerValue == null) {
+            return false;
+        }
+        if (headerName.startsWith("org.apache.camel")) {
+            return false;
+        }
+        if (HEADERS_TO_SKIP.contains(headerName.toLowerCase())) {
+            return false;
+        }
+        return true;
     }
 }

Added: activemq/camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetWithHeadersTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetWithHeadersTest.java?rev=613990&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetWithHeadersTest.java (added)
+++ activemq/camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetWithHeadersTest.java Mon Jan 21 12:00:50 2008
@@ -0,0 +1,27 @@
+package org.apache.camel.component.http;
+
+import org.apache.camel.builder.RouteBuilder;
+
+public class HttpGetWithHeadersTest extends HttpGetTest {
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() {
+                from("direct:start")
+                    .setHeader("Content-Length", constant(0))
+                    .setHeader("Accept-Language", constant("pl"))
+                    .to("http://www.google.com/search")
+                    .to("mock:results");
+            }
+        };
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        // "Szukaj" is "Search" in polish language
+        expectedText = "Szukaj";
+        super.setUp();
+    }
+
+}

Propchange: activemq/camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetWithHeadersTest.java
------------------------------------------------------------------------------
    svn:eol-style = native