Repository: camel
Updated Branches:
  refs/heads/camel-2.12.x 30d9b4361 -> b4cb381c7


CAMEL-7642 Netty consumer should return error on invalid request


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/b4cb381c
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/b4cb381c
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/b4cb381c

Branch: refs/heads/camel-2.12.x
Commit: b4cb381c7ed5343cde23aaa6fcc729f149c4fbd8
Parents: 30d9b43
Author: Willem Jiang <willem.ji...@gmail.com>
Authored: Mon Jul 28 23:11:09 2014 +0800
Committer: Willem Jiang <willem.ji...@gmail.com>
Committed: Mon Jul 28 23:13:50 2014 +0800

----------------------------------------------------------------------
 .../http/handlers/HttpServerChannelHandler.java |   1 +
 .../HttpServerMultiplexChannelHandler.java      |  10 +-
 .../NettyHttpGetWithInvalidMessageTest.java     | 104 +++++++++++++++++++
 .../netty/handlers/ClientChannelHandler.java    |   1 +
 4 files changed, 115 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/b4cb381c/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerChannelHandler.java
----------------------------------------------------------------------
diff --git 
a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerChannelHandler.java
 
b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerChannelHandler.java
index 6cf4b7f..d4ff8dc 100644
--- 
a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerChannelHandler.java
+++ 
b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerChannelHandler.java
@@ -267,6 +267,7 @@ public class HttpServerChannelHandler extends 
ServerChannelHandler {
 
     @Override
     public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent 
exceptionEvent) throws Exception {
+        System.out.println("Get the exception here" + exceptionEvent);
         // only close if we are still allowed to run
         if (consumer.isRunAllowed()) {
 

http://git-wip-us.apache.org/repos/asf/camel/blob/b4cb381c/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerMultiplexChannelHandler.java
----------------------------------------------------------------------
diff --git 
a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerMultiplexChannelHandler.java
 
b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerMultiplexChannelHandler.java
index e00abd4..3c219f6 100644
--- 
a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerMultiplexChannelHandler.java
+++ 
b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerMultiplexChannelHandler.java
@@ -117,7 +117,15 @@ public class HttpServerMultiplexChannelHandler extends 
SimpleChannelUpstreamHand
         if (handler != null) {
             handler.exceptionCaught(ctx, e);
         } else {
-            throw new IllegalStateException("HttpServerChannelHandler not 
found as attachment. Cannot handle caught exception.", e.getCause());
+            // we cannot throw the exception here
+            LOG.warn("HttpServerChannelHandler is not found as attachment for 
exception {}, send 404 back to the client.", e.getCause());
+            // Now we just send 404 back to the client
+            HttpResponse response = new DefaultHttpResponse(HTTP_1_1, 
NOT_FOUND);
+            response.headers().set(Exchange.CONTENT_TYPE, "text/plain");
+            response.headers().set(Exchange.CONTENT_LENGTH, 0);
+            // Here we don't want to expose the exception detail to the client
+            response.setContent(ChannelBuffers.copiedBuffer(new byte[]{}));
+            ctx.getChannel().write(response);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/b4cb381c/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpGetWithInvalidMessageTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpGetWithInvalidMessageTest.java
 
b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpGetWithInvalidMessageTest.java
new file mode 100644
index 0000000..5e6a46b
--- /dev/null
+++ 
b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpGetWithInvalidMessageTest.java
@@ -0,0 +1,104 @@
+/**
+ * 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 java.util.ArrayList;
+import java.util.List;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.impl.JndiRegistry;
+import org.apache.camel.test.AvailablePortFinder;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.jboss.netty.channel.ChannelHandler;
+import org.jboss.netty.handler.codec.string.StringDecoder;
+import org.jboss.netty.handler.codec.string.StringEncoder;
+import org.junit.Test;
+
+public class NettyHttpGetWithInvalidMessageTest extends CamelTestSupport {
+    private static final String REQUEST_STRING = "user: Willem\n" 
+        + "GET http://localhost:8101/test HTTP/1.1\n" + "another: value\n 
Host: localhost\n";
+    private int port1;
+   
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry registry = super.createRegistry();
+        
+        // setup the String encoder and decoder 
+       
+        StringDecoder stringDecoder = new StringDecoder();
+        registry.bind("string-decoder", stringDecoder);
+
+        StringEncoder stringEncoder = new StringEncoder();
+        registry.bind("string-encoder", stringEncoder);
+
+        List<ChannelHandler> decoders = new ArrayList<ChannelHandler>();
+        decoders.add(stringDecoder);
+
+        List<ChannelHandler> encoders = new ArrayList<ChannelHandler>();
+        encoders.add(stringEncoder);
+
+        registry.bind("encoders", encoders);
+        registry.bind("decoders", decoders);
+        
+        return registry;
+    }
+    
+    @Test
+    public void testNettyHttpServer() throws Exception {
+        invokeService(8100);
+    }
+    
+    //@Test
+    public void testJettyHttpServer() throws Exception {
+        invokeService(port1);
+    }
+    
+    private void invokeService(int port) {
+        Exchange out = template.request("netty:tcp://localhost:" + port + 
"?encoders=#encoders&decoders=#decoders&sync=true" , new Processor() {
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setBody(REQUEST_STRING);
+            }
+        });
+
+        assertNotNull(out);
+        String result = out.getOut().getBody(String.class);
+        assertNotNull(result);
+        assertTrue("We should get the 404 response.", result.indexOf("404 Not 
Found") > 0);
+        
+    }
+    
+
+    
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                port1 = AvailablePortFinder.getNextAvailable(8100);
+                
+               // set up a netty http proxy
+                from("netty-http:http://localhost:"; + port1 + "/test")
+                    .transform().simple("Bye ${header.user}.");
+          
+            }
+        };
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/b4cb381c/components/camel-netty/src/main/java/org/apache/camel/component/netty/handlers/ClientChannelHandler.java
----------------------------------------------------------------------
diff --git 
a/components/camel-netty/src/main/java/org/apache/camel/component/netty/handlers/ClientChannelHandler.java
 
b/components/camel-netty/src/main/java/org/apache/camel/component/netty/handlers/ClientChannelHandler.java
index e0ddb9f..f21391e 100644
--- 
a/components/camel-netty/src/main/java/org/apache/camel/component/netty/handlers/ClientChannelHandler.java
+++ 
b/components/camel-netty/src/main/java/org/apache/camel/component/netty/handlers/ClientChannelHandler.java
@@ -209,6 +209,7 @@ public class ClientChannelHandler extends 
SimpleChannelUpstreamHandler {
 
         // if textline enabled then covert to a String which must be used for 
textline
         if (producer.getConfiguration().isTextline()) {
+            System.out.println("body is " + body);
             body = 
producer.getContext().getTypeConverter().mandatoryConvertTo(String.class, 
exchange, body);
         }
 

Reply via email to