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 2019/03/23 09:00:24 UTC

[camel] branch camel-2.22.x updated: CAMEL-13351: camel-netty4-http: error resolving relative path

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch camel-2.22.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-2.22.x by this push:
     new e7b2d62  CAMEL-13351: camel-netty4-http: error resolving relative path
e7b2d62 is described below

commit e7b2d622c1426ef049ac9cc14aac365f950ed96c
Author: lburgazzoli <lb...@gmail.com>
AuthorDate: Wed Mar 20 14:28:51 2019 +0100

    CAMEL-13351: camel-netty4-http: error resolving relative path
---
 .../netty4/http/DefaultNettyHttpBinding.java       |  23 ++---
 .../http/NettyHttpBindingUseRelativePath.java      | 105 +++++++++++++++++++++
 2 files changed, 115 insertions(+), 13 deletions(-)

diff --git a/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/DefaultNettyHttpBinding.java b/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/DefaultNettyHttpBinding.java
index eb96cc8..8135799 100644
--- a/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/DefaultNettyHttpBinding.java
+++ b/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/DefaultNettyHttpBinding.java
@@ -71,7 +71,7 @@ public class DefaultNettyHttpBinding implements NettyHttpBinding, Cloneable {
     public DefaultNettyHttpBinding(HeaderFilterStrategy headerFilterStrategy) {
         this.headerFilterStrategy = headerFilterStrategy;
     }
-    
+
     public DefaultNettyHttpBinding copy() {
         try {
             return (DefaultNettyHttpBinding)this.clone();
@@ -329,7 +329,7 @@ public class DefaultNettyHttpBinding implements NettyHttpBinding, Cloneable {
         int defaultCode = failed ? 500 : 200;
 
         int code = message.getHeader(Exchange.HTTP_RESPONSE_CODE, defaultCode, int.class);
-        
+
         LOG.trace("HTTP Status Code: {}", code);
 
         // if there was an exception then use that as body
@@ -384,16 +384,16 @@ public class DefaultNettyHttpBinding implements NettyHttpBinding, Cloneable {
                 }
             }
         }
-        
+
         HttpResponse response;
-        
+
         if (buffer != null) {
             response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.valueOf(code), buffer);
             // We just need to reset the readerIndex this time
             if (buffer.readerIndex() == buffer.writerIndex()) {
                 buffer.setIndex(0, buffer.writerIndex());
             }
-            // TODO How to enable the chunk transport 
+            // TODO How to enable the chunk transport
             int len = buffer.readableBytes();
             // set content-length
             response.headers().set(HttpHeaderNames.CONTENT_LENGTH.toString(), len);
@@ -401,7 +401,7 @@ public class DefaultNettyHttpBinding implements NettyHttpBinding, Cloneable {
         } else {
             response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.valueOf(code));
         }
-        
+
         TypeConverter tc = message.getExchange().getContext().getTypeConverter();
 
         // append headers
@@ -465,15 +465,12 @@ public class DefaultNettyHttpBinding implements NettyHttpBinding, Cloneable {
 
         String uriForRequest = uri;
         if (configuration.isUseRelativePath()) {
-            int indexOfPath = uri.indexOf((new URI(uri)).getPath());
-            if (indexOfPath > 0) {
-                uriForRequest = uri.substring(indexOfPath);               
-            } 
+            uriForRequest = URISupport.pathAndQueryOf(new URI(uriForRequest));
         }
-        
+
         // just assume GET for now, we will later change that to the actual method to use
         HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uriForRequest);
-        
+
         Object body = message.getBody();
         if (body != null) {
             // support bodies as native Netty
@@ -504,7 +501,7 @@ public class DefaultNettyHttpBinding implements NettyHttpBinding, Cloneable {
         // update HTTP method accordingly as we know if we have a body or not
         HttpMethod method = NettyHttpHelper.createMethod(message, body != null);
         request.setMethod(method);
-        
+
         TypeConverter tc = message.getExchange().getContext().getTypeConverter();
 
         // if we bridge endpoint then we need to skip matching headers with the HTTP_QUERY to avoid sending
diff --git a/components/camel-netty4-http/src/test/java/org/apache/camel/component/netty4/http/NettyHttpBindingUseRelativePath.java b/components/camel-netty4-http/src/test/java/org/apache/camel/component/netty4/http/NettyHttpBindingUseRelativePath.java
new file mode 100644
index 0000000..249d92f
--- /dev/null
+++ b/components/camel-netty4-http/src/test/java/org/apache/camel/component/netty4/http/NettyHttpBindingUseRelativePath.java
@@ -0,0 +1,105 @@
+/*
+ * 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.netty4.http;
+
+import io.netty.handler.codec.http.FullHttpRequest;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.http4.HttpMethods;
+import org.junit.Test;
+
+
+public class NettyHttpBindingUseRelativePath extends BaseNettyTest {
+
+    @Test
+    public void testSendToNettyWithPath() throws Exception {
+        Exchange exchange = template.request("netty4-http:http://localhost:{{port}}/mypath?useRelativePath=true", new Processor() {
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(Exchange.HTTP_METHOD, HttpMethods.POST);
+            }
+        });
+
+        // convert the response to a String
+        String body = exchange.getOut().getBody(String.class);
+        assertEquals("Request message is OK", body);
+    }
+
+    @Test
+    public void testSendToNettyWithoutPath() throws Exception {
+        Exchange exchange = template.request("netty4-http:http://localhost:{{port}}?useRelativePath=true", new Processor() {
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(Exchange.HTTP_METHOD, HttpMethods.POST);
+            }
+        });
+
+        // convert the response to a String
+        String body = exchange.getOut().getBody(String.class);
+        assertEquals("Request message is OK", body);
+    }
+
+    @Test
+    public void testSendToNettyWithoutPath2() throws Exception {
+        Exchange exchange = template.request("netty4-http:http://localhost:{{port}}/?useRelativePath=true", new Processor() {
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(Exchange.HTTP_METHOD, HttpMethods.POST);
+            }
+        });
+
+        // convert the response to a String
+        String body = exchange.getOut().getBody(String.class);
+        assertEquals("Request message is OK", body);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                from("netty4-http:http://localhost:{{port}}/mypath").process(new Processor() {
+                    public void process(Exchange exchange) throws Exception {
+                        assertEquals("Get a wrong form parameter from the message header", "localhost:" + getPort(), exchange.getIn().getHeader("host"));
+
+                        NettyHttpMessage in = (NettyHttpMessage) exchange.getIn();
+                        FullHttpRequest request = in.getHttpRequest();
+                        assertEquals("Relative path not used in POST", "/mypath", request.uri());
+
+                        // send a response
+                        exchange.getOut().getHeaders().clear();
+                        exchange.getOut().setHeader(Exchange.CONTENT_TYPE, "text/plain");
+                        exchange.getOut().setBody("Request message is OK");
+                    }
+                });
+
+                from("netty4-http:http://localhost:{{port}}").process(new Processor() {
+                    public void process(Exchange exchange) throws Exception {
+                        assertEquals("Get a wrong form parameter from the message header", "localhost:" + getPort(), exchange.getIn().getHeader("host"));
+
+                        NettyHttpMessage in = (NettyHttpMessage) exchange.getIn();
+                        FullHttpRequest request = in.getHttpRequest();
+                        assertEquals("Relative path not used in POST", "/", request.uri());
+
+                        // send a response
+                        exchange.getOut().getHeaders().clear();
+                        exchange.getOut().setHeader(Exchange.CONTENT_TYPE, "text/plain");
+                        exchange.getOut().setBody("Request message is OK");
+                    }
+                });
+            }
+        };
+    }
+
+}