camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1477602 - in /camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http: DefaultNettyHttpBinding.java NettyHttpBinding.java NettyHttpComponent.java NettyHttpMessage.java
Date Tue, 30 Apr 2013 12:52:26 GMT
Author: davsclaus
Date: Tue Apr 30 12:52:26 2013
New Revision: 1477602

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

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/NettyHttpBinding.java
    camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpComponent.java
    camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpMessage.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=1477602&r1=1477601&r2=1477602&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 12:52:26 2013
@@ -34,12 +34,15 @@ import org.jboss.netty.handler.codec.htt
 import org.jboss.netty.handler.codec.http.HttpResponse;
 import org.jboss.netty.handler.codec.http.HttpResponseStatus;
 import org.jboss.netty.handler.codec.http.HttpVersion;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Default {@link NettyHttpBinding}.
  */
 public class DefaultNettyHttpBinding implements NettyHttpBinding {
 
+    private static final Logger LOG = LoggerFactory.getLogger(DefaultNettyHttpBinding.class);
     private HeaderFilterStrategy headerFilterStrategy;
 
     public DefaultNettyHttpBinding() {
@@ -51,12 +54,28 @@ public class DefaultNettyHttpBinding imp
 
     @Override
     public Message toCamelMessage(HttpRequest request, Exchange exchange) throws Exception
{
-        NettyHttpMessage answer = new NettyHttpMessage(request);
-        answer.setHeader(Exchange.HTTP_METHOD, request.getMethod().getName());
-        answer.setHeader(Exchange.HTTP_URI, request.getUri());
+        LOG.trace("toCamelMessage: {}", request);
 
-        // populate the headers from the request
-        Map<String, Object> headers = answer.getHeaders();
+        NettyHttpMessage answer = new NettyHttpMessage(request, this);
+        // force getting headers which will populate them
+        answer.getHeaders();
+
+        // keep the body as is, and use type converters
+        answer.setBody(request.getContent());
+        return answer;
+    }
+
+    @Override
+    public void populateCamelHeaders(HttpRequest request, Map<String, Object> headers,
Exchange exchange) throws Exception {
+        LOG.trace("populateCamelHeaders: {}", request);
+
+        headers.put(Exchange.HTTP_METHOD, request.getMethod().getName());
+        headers.put(Exchange.HTTP_URI, request.getUri());
+
+        if (LOG.isTraceEnabled()) {
+            LOG.trace("HTTP-Method {}", request.getMethod().getName());
+            LOG.trace("HTTP-Uri {}", request.getUri());
+        }
 
         for (String name : request.getHeaderNames()) {
             // mapping the content-type
@@ -68,24 +87,23 @@ public class DefaultNettyHttpBinding imp
             Iterator<?> it = ObjectHelper.createIterator(values);
             while (it.hasNext()) {
                 Object extracted = it.next();
+                LOG.trace("HTTP-header: {}", extracted);
                 if (headerFilterStrategy != null
                         && !headerFilterStrategy.applyFilterToExternalHeaders(name,
extracted, exchange)) {
                     NettyHttpHelper.appendHeader(headers, name, extracted);
                 }
             }
         }
-
-        // keep the body as is, and use type converters
-        answer.setBody(request.getContent());
-        return answer;
     }
 
     @Override
     public HttpResponse fromCamelMessage(Message message) throws Exception {
+        LOG.trace("fromCamelMessage: {}", message);
 
         // the status code is default 200, but a header can override that
         Integer code = message.getHeader(Exchange.HTTP_RESPONSE_CODE, 200, Integer.class);
         HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.valueOf(code));
+        LOG.trace("HTTP Status Code: {}", code);
 
         TypeConverter tc = message.getExchange().getContext().getTypeConverter();
 
@@ -100,6 +118,7 @@ public class DefaultNettyHttpBinding imp
                 String headerValue = tc.convertTo(String.class, it.next());
                 if (headerValue != null && headerFilterStrategy != null
                         && !headerFilterStrategy.applyFilterToCamelHeaders(key, headerValue,
message.getExchange())) {
+                    LOG.trace("HTTP-Header: {}={}", key, headerValue);
                     response.addHeader(key, headerValue);
                 }
             }
@@ -122,18 +141,25 @@ public class DefaultNettyHttpBinding imp
             }
             if (buffer != null) {
                 response.setContent(buffer);
-                response.setHeader(HttpHeaders.Names.CONTENT_LENGTH, buffer.readableBytes());
+                int len = buffer.readableBytes();
+                // set content-length
+                response.setHeader(HttpHeaders.Names.CONTENT_LENGTH, len);
+                LOG.trace("Content-Length: {}", len);
             }
         }
 
         // set the content type in the response.
         String contentType = MessageHelper.getContentType(message);
         if (contentType != null) {
+            // set content-type
             response.setHeader(HttpHeaders.Names.CONTENT_TYPE, contentType);
+            LOG.trace("Content-Type: {}", contentType);
         }
 
         // TODO: keep alive should be something we can control
-        response.setHeader(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
+        String keepAlive = HttpHeaders.Values.CLOSE;
+        response.setHeader(HttpHeaders.Names.CONNECTION, keepAlive);
+        LOG.trace("Connection: {}", keepAlive);
 
         return response;
     }

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=1477602&r1=1477601&r2=1477602&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 12:52:26 2013
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.component.netty.http;
 
+import java.util.Map;
+
 import org.apache.camel.Exchange;
 import org.apache.camel.Message;
 import org.apache.camel.spi.HeaderFilterStrategy;
@@ -38,6 +40,16 @@ public interface NettyHttpBinding {
     Message toCamelMessage(HttpRequest request, Exchange exchange) throws Exception;
 
     /**
+     * Binds from Netty {@link HttpRequest} to Camel headers as a {@link Map}.
+     *
+     * @param request   the netty http request
+     * @param headers   the Camel headers that should be populated
+     * @param exchange  the exchange that should contain the returned message.
+     * @throws Exception is thrown if error during binding
+     */
+    void populateCamelHeaders(HttpRequest request, Map<String, Object> headers, Exchange
exchange) throws Exception;
+
+    /**
      * Binds from Camel {@link Message} to Netty {@link HttpResponse}.
      *
      * @param message  the Camel message

Modified: camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpComponent.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpComponent.java?rev=1477602&r1=1477601&r2=1477602&view=diff
==============================================================================
--- camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpComponent.java
(original)
+++ camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpComponent.java
Tue Apr 30 12:52:26 2013
@@ -40,10 +40,8 @@ public class NettyHttpComponent extends 
         setNettyHttpBinding(new DefaultNettyHttpBinding(getHeaderFilterStrategy()));
     }
 
-    // TODO: allow to turn mapMessage=true|false and rely on on-demand mapping
     // TODO: netty http producer
     // TODO: make it easy to turn keep-alive on|off on producer
-    // TODO: add logging
 
     @Override
     protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object>
parameters) throws Exception {

Modified: 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=1477602&r1=1477601&r2=1477602&view=diff
==============================================================================
--- camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpMessage.java
(original)
+++ camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpMessage.java
Tue Apr 30 12:52:26 2013
@@ -16,6 +16,9 @@
  */
 package org.apache.camel.component.netty.http;
 
+import java.util.Map;
+
+import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.impl.DefaultMessage;
 import org.jboss.netty.handler.codec.http.HttpRequest;
 
@@ -28,9 +31,11 @@ import org.jboss.netty.handler.codec.htt
 public class NettyHttpMessage extends DefaultMessage {
 
     private final transient HttpRequest httpRequest;
+    private final transient NettyHttpBinding httpBinding;
 
-    public NettyHttpMessage(HttpRequest httpRequest) {
+    public NettyHttpMessage(HttpRequest httpRequest, NettyHttpBinding httpBinding) {
         this.httpRequest = httpRequest;
+        this.httpBinding = httpBinding;
     }
 
     public HttpRequest getHttpRequest() {
@@ -38,7 +43,16 @@ public class NettyHttpMessage extends De
     }
 
     @Override
+    protected void populateInitialHeaders(Map<String, Object> map) {
+        try {
+            httpBinding.populateCamelHeaders(httpRequest, map, getExchange());
+        } catch (Exception e) {
+            throw new RuntimeCamelException("Error populating initial headers", e);
+        }
+    }
+
+    @Override
     public DefaultMessage newInstance() {
-        return new NettyHttpMessage(httpRequest);
+        return new NettyHttpMessage(httpRequest, httpBinding);
     }
 }



Mime
View raw message