You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2013/04/30 10:11:02 UTC

svn commit: r1477502 - in /camel/trunk/components/camel-netty-http/src: main/java/org/apache/camel/component/netty/http/ test/java/org/apache/camel/component/netty/http/

Author: davsclaus
Date: Tue Apr 30 08:11:01 2013
New Revision: 1477502

URL: http://svn.apache.org/r1477502
Log:
CAMEL-6327: More work on new camel-netty-http component.

Added:
    camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpHelper.java
    camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpMessage.java
    camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpAccessHttpRequestTest.java
      - copied, changed from r1477487, camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSimpleTest.java
    camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpContentTypeTest.java
      - copied, changed from r1477487, camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSimpleTest.java
Modified:
    camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java
    camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/HttpServerChannelHandler.java
    camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpBinding.java
    camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConfiguration.java
    camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpEndpoint.java
    camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSimpleTest.java

Modified: camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java?rev=1477502&r1=1477501&r2=1477502&view=diff
==============================================================================
--- camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java (original)
+++ camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java Tue Apr 30 08:11:01 2013
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.component.netty.http;
 
+import java.util.List;
+
 import org.apache.camel.Exchange;
 import org.apache.camel.Message;
 import org.jboss.netty.buffer.ChannelBuffers;
@@ -32,17 +34,29 @@ import org.jboss.netty.handler.codec.htt
 public class DefaultNettyHttpBinding implements NettyHttpBinding {
 
     @Override
-    public Message toCamelMessage(Exchange exchange, HttpRequest request) {
-        // map http headers to Camel message
-
-        // TODO: map from request to Camel
-        exchange.getIn().setBody("TODO");
+    public Message toCamelMessage(HttpRequest request, Exchange exchange) {
+        NettyHttpMessage answer = new NettyHttpMessage(request);
+        answer.setHeader(Exchange.HTTP_METHOD, request.getMethod().getName());
+        answer.setHeader(Exchange.HTTP_URI, request.getUri());
+
+        for (String name : request.getHeaderNames()) {
+            List<String> values = request.getHeaders(name);
+            if (values.size() == 1) {
+                // flatten the list and store as single value
+                answer.setHeader(name, values.get(0));
+            } else {
+                // if multiple values store them as list
+                answer.setHeader(name, values);
+            }
+        }
 
-        return exchange.getIn();
+        // keep the body as is, and use type converters
+        answer.setBody(request.getContent());
+        return answer;
     }
 
     @Override
-    public HttpResponse toHttpResponse(Message msg) {
+    public HttpResponse fromCamelMessage(Message msg) {
 
         // the status code is default 200, but a header can override that
         Integer code = msg.getHeader(Exchange.HTTP_RESPONSE_CODE, 200, Integer.class);

Modified: camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/HttpServerChannelHandler.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/HttpServerChannelHandler.java?rev=1477502&r1=1477501&r2=1477502&view=diff
==============================================================================
--- camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/HttpServerChannelHandler.java (original)
+++ camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/HttpServerChannelHandler.java Tue Apr 30 08:11:01 2013
@@ -43,8 +43,8 @@ import static org.jboss.netty.handler.co
  */
 public class HttpServerChannelHandler extends ServerChannelHandler {
 
-    // use NettyConsumer as logger to make it easier to read the logs as this is part of the consumer
-    private static final transient Logger LOG = LoggerFactory.getLogger(NettyConsumer.class);
+    // use NettyHttpConsumer as logger to make it easier to read the logs as this is part of the consumer
+    private static final transient Logger LOG = LoggerFactory.getLogger(NettyHttpConsumer.class);
     private final NettyHttpConsumer consumer;
     private HttpRequest request;
 
@@ -102,9 +102,9 @@ public class HttpServerChannelHandler ex
     protected Object getResponseBody(Exchange exchange) {
         // use the binding
         if (exchange.hasOut()) {
-            return consumer.getEndpoint().getNettyHttpBinding().toHttpResponse(exchange.getOut());
+            return consumer.getEndpoint().getNettyHttpBinding().fromCamelMessage(exchange.getOut());
         } else {
-            return consumer.getEndpoint().getNettyHttpBinding().toHttpResponse(exchange.getIn());
+            return consumer.getEndpoint().getNettyHttpBinding().fromCamelMessage(exchange.getIn());
         }
     }
 }

Modified: camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpBinding.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpBinding.java?rev=1477502&r1=1477501&r2=1477502&view=diff
==============================================================================
--- camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpBinding.java (original)
+++ camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpBinding.java Tue Apr 30 08:11:01 2013
@@ -26,7 +26,20 @@ import org.jboss.netty.handler.codec.htt
  */
 public interface NettyHttpBinding {
 
-    Message toCamelMessage(Exchange exchange, HttpRequest request);
+    /**
+     * Binds from Netty {@link HttpRequest} to Camel {@Message}.
+     *
+     * @param request   the netty http request
+     * @param exchange  the exchange that should contain the returned message.
+     * @return the message to store on the given exchange
+     */
+    Message toCamelMessage(HttpRequest request, Exchange exchange);
 
-    HttpResponse toHttpResponse(Message msg);
+    /**
+     * Binds from Camel {@link Message} to Netty {@link HttpResponse}.
+     *
+     * @param msg  the Camel message
+     * @return the http response
+     */
+    HttpResponse fromCamelMessage(Message msg);
 }

Modified: camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConfiguration.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConfiguration.java?rev=1477502&r1=1477501&r2=1477502&view=diff
==============================================================================
--- camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConfiguration.java (original)
+++ camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConfiguration.java Tue Apr 30 08:11:01 2013
@@ -38,6 +38,7 @@ public class NettyHttpConfiguration exte
     @Override
     public NettyHttpConfiguration copy() {
         try {
+            // clone as NettyHttpConfiguration
             NettyHttpConfiguration answer = (NettyHttpConfiguration) clone();
             // make sure the lists is copied in its own instance
             List<ChannelHandler> encodersCopy = new ArrayList<ChannelHandler>(getEncoders());

Modified: camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpEndpoint.java?rev=1477502&r1=1477501&r2=1477502&view=diff
==============================================================================
--- camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpEndpoint.java (original)
+++ camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpEndpoint.java Tue Apr 30 08:11:01 2013
@@ -34,6 +34,7 @@ public class NettyHttpEndpoint extends N
     public NettyHttpEndpoint(String endpointUri, NettyHttpComponent component, NettyConfiguration configuration) {
         super(endpointUri, component, configuration);
         this.nettyHttpBinding = component.getNettyHttpBinding();
+        // ensure a default binding is created if not configured on component
         if (this.nettyHttpBinding == null) {
             this.nettyHttpBinding = new DefaultNettyHttpBinding();
         }
@@ -49,15 +50,22 @@ public class NettyHttpEndpoint extends N
     @Override
     public Exchange createExchange(ChannelHandlerContext ctx, MessageEvent messageEvent) {
         Exchange exchange = createExchange();
-        exchange.getIn().setHeader(NettyConstants.NETTY_CHANNEL_HANDLER_CONTEXT, ctx);
-        exchange.getIn().setHeader(NettyConstants.NETTY_MESSAGE_EVENT, messageEvent);
-        exchange.getIn().setHeader(NettyConstants.NETTY_REMOTE_ADDRESS, messageEvent.getRemoteAddress());
-        exchange.getIn().setHeader(NettyConstants.NETTY_LOCAL_ADDRESS, messageEvent.getChannel().getLocalAddress());
 
+        // use the http binding
         HttpRequest request = (HttpRequest) messageEvent.getMessage();
-        Message in = getNettyHttpBinding().toCamelMessage(exchange, request);
+        Message in = getNettyHttpBinding().toCamelMessage(request, exchange);
         exchange.setIn(in);
 
+        // set additional headers
+        in.setHeader(NettyConstants.NETTY_CHANNEL_HANDLER_CONTEXT, ctx);
+        in.setHeader(NettyConstants.NETTY_MESSAGE_EVENT, messageEvent);
+        in.setHeader(NettyConstants.NETTY_REMOTE_ADDRESS, messageEvent.getRemoteAddress());
+        in.setHeader(NettyConstants.NETTY_LOCAL_ADDRESS, messageEvent.getChannel().getLocalAddress());
+
+        // Honor the character encoding
+        String contentType = request.getHeader("content-type");
+        NettyHttpHelper.setCharsetFromContentType(contentType, exchange);
+
         return exchange;
     }
 

Added: camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpHelper.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpHelper.java?rev=1477502&view=auto
==============================================================================
--- camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpHelper.java (added)
+++ camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpHelper.java Tue Apr 30 08:11:01 2013
@@ -0,0 +1,42 @@
+/**
+ * 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.netty.http;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.converter.IOConverter;
+
+/**
+ * Helpers.
+ */
+public final class NettyHttpHelper {
+
+    private NettyHttpHelper() {
+    }
+
+    @SuppressWarnings("deprecation")
+    public static void setCharsetFromContentType(String contentType, Exchange exchange) {
+        if (contentType != null) {
+            // find the charset and set it to the Exchange
+            int index = contentType.indexOf("charset=");
+            if (index > 0) {
+                String charset = contentType.substring(index + 8);
+                exchange.setProperty(Exchange.CHARSET_NAME, IOConverter.normalizeCharset(charset));
+            }
+        }
+    }
+
+}

Added: camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpMessage.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpMessage.java?rev=1477502&view=auto
==============================================================================
--- camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpMessage.java (added)
+++ camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpMessage.java Tue Apr 30 08:11:01 2013
@@ -0,0 +1,38 @@
+/**
+ * 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.netty.http;
+
+import org.apache.camel.impl.DefaultMessage;
+import org.jboss.netty.handler.codec.http.HttpRequest;
+
+public class NettyHttpMessage extends DefaultMessage {
+
+    private final transient HttpRequest httpRequest;
+
+    public NettyHttpMessage(HttpRequest httpRequest) {
+        this.httpRequest = httpRequest;
+    }
+
+    public HttpRequest getHttpRequest() {
+        return httpRequest;
+    }
+
+    @Override
+    public DefaultMessage newInstance() {
+        return new NettyHttpMessage(httpRequest);
+    }
+}

Copied: camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpAccessHttpRequestTest.java (from r1477487, camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSimpleTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpAccessHttpRequestTest.java?p2=camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpAccessHttpRequestTest.java&p1=camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSimpleTest.java&r1=1477487&r2=1477502&rev=1477502&view=diff
==============================================================================
--- camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSimpleTest.java (original)
+++ camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpAccessHttpRequestTest.java Tue Apr 30 08:11:01 2013
@@ -16,14 +16,17 @@
  */
 package org.apache.camel.component.netty.http;
 
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
 import org.apache.camel.builder.RouteBuilder;
+import org.jboss.netty.handler.codec.http.HttpRequest;
 import org.junit.Test;
 
-public class NettyHttpSimpleTest extends BaseNettyTest {
+public class NettyHttpAccessHttpRequestTest extends BaseNettyTest {
 
     @Test
-    public void testHttpSimple() throws Exception {
-        getMockEndpoint("mock:input").expectedMessageCount(1);
+    public void testAccessHttpRequest() throws Exception {
+        getMockEndpoint("mock:input").expectedBodiesReceived("Hello World");
 
         String out = template.requestBody("http://localhost:{{port}}/foo", "Hello World", String.class);
         assertEquals("Bye World", out);
@@ -38,6 +41,14 @@ public class NettyHttpSimpleTest extends
             public void configure() throws Exception {
                 from("netty-http:http://0.0.0.0:{{port}}/foo")
                     .to("mock:input")
+                    .process(new Processor() {
+                        @Override
+                        public void process(Exchange exchange) throws Exception {
+                            // we can get the original http request
+                            HttpRequest request = exchange.getIn(NettyHttpMessage.class).getHttpRequest();
+                            assertNotNull(request);
+                        }
+                    })
                     .transform().constant("Bye World");
             }
         };

Copied: camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpContentTypeTest.java (from r1477487, camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSimpleTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpContentTypeTest.java?p2=camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpContentTypeTest.java&p1=camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSimpleTest.java&r1=1477487&r2=1477502&rev=1477502&view=diff
==============================================================================
--- camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSimpleTest.java (original)
+++ camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpContentTypeTest.java Tue Apr 30 08:11:01 2013
@@ -16,16 +16,22 @@
  */
 package org.apache.camel.component.netty.http;
 
+import java.nio.charset.Charset;
+
+import org.apache.camel.Exchange;
 import org.apache.camel.builder.RouteBuilder;
 import org.junit.Test;
 
-public class NettyHttpSimpleTest extends BaseNettyTest {
+public class NettyHttpContentTypeTest extends BaseNettyTest {
 
     @Test
-    public void testHttpSimple() throws Exception {
-        getMockEndpoint("mock:input").expectedMessageCount(1);
+    public void testContentType() throws Exception {
+        getMockEndpoint("mock:input").expectedBodiesReceived("Hello World");
+        getMockEndpoint("mock:input").expectedHeaderReceived(Exchange.CONTENT_TYPE, "text/plain; charset=\"iso-8859-1\"");
+        getMockEndpoint("mock:input").expectedPropertyReceived(Exchange.CHARSET_NAME, "iso-8859-1");
 
-        String out = template.requestBody("http://localhost:{{port}}/foo", "Hello World", String.class);
+        byte[] data = "Hello World".getBytes(Charset.forName("iso-8859-1"));
+        String out = template.requestBodyAndHeader("http://localhost:{{port}}/foo", data, "content-type", "text/plain; charset=\"iso-8859-1\"", String.class);
         assertEquals("Bye World", out);
 
         assertMockEndpointsSatisfied();

Modified: camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSimpleTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSimpleTest.java?rev=1477502&r1=1477501&r2=1477502&view=diff
==============================================================================
--- camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSimpleTest.java (original)
+++ camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSimpleTest.java Tue Apr 30 08:11:01 2013
@@ -23,7 +23,7 @@ public class NettyHttpSimpleTest extends
 
     @Test
     public void testHttpSimple() throws Exception {
-        getMockEndpoint("mock:input").expectedMessageCount(1);
+        getMockEndpoint("mock:input").expectedBodiesReceived("Hello World");
 
         String out = template.requestBody("http://localhost:{{port}}/foo", "Hello World", String.class);
         assertEquals("Bye World", out);