You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2014/12/25 09:54:23 UTC
[1/2] camel git commit: CAMEL-8169 Camel Jetty/Http4 producers should
respect Content-Length/Transfer-Encoding:Chunked headers
Repository: camel
Updated Branches:
refs/heads/camel-2.13.x 44730ce0c -> 945c7272c
refs/heads/camel-2.14.x 2c454a3ad -> c1d61bc62
CAMEL-8169 Camel Jetty/Http4 producers should respect Content-Length/Transfer-Encoding:Chunked headers
Conflicts:
components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/c1d61bc6
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/c1d61bc6
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/c1d61bc6
Branch: refs/heads/camel-2.14.x
Commit: c1d61bc623812cd9ac00671b067764c867e6bfb4
Parents: 2c454a3
Author: Willem Jiang <wi...@gmail.com>
Authored: Mon Dec 22 08:56:13 2014 +0800
Committer: Willem Jiang <wi...@gmail.com>
Committed: Thu Dec 25 16:41:35 2014 +0800
----------------------------------------------------------------------
.../component/http4/HttpEntityConverter.java | 11 ++-
.../camel/component/http4/HttpProducer.java | 8 +-
.../component/jetty/JettyHttpProducer.java | 9 +-
.../itest/http/HttpRouteContentLengthTest.java | 91 ++++++++++++++++++++
.../http/JettyHttpRouteContentLengthTest.java | 25 ++++++
5 files changed, 140 insertions(+), 4 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/c1d61bc6/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEntityConverter.java
----------------------------------------------------------------------
diff --git a/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEntityConverter.java b/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEntityConverter.java
index 8236da8..bcbe502 100644
--- a/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEntityConverter.java
+++ b/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEntityConverter.java
@@ -22,8 +22,10 @@ import java.io.InputStream;
import org.apache.camel.Converter;
import org.apache.camel.Exchange;
+import org.apache.camel.Message;
import org.apache.camel.util.ExchangeHelper;
import org.apache.camel.util.GZIPHelper;
+import org.apache.camel.util.ObjectHelper;
import org.apache.http.HttpEntity;
import org.apache.http.entity.AbstractHttpEntity;
import org.apache.http.entity.ByteArrayEntity;
@@ -67,7 +69,14 @@ public final class HttpEntityConverter {
entity = new InputStreamEntity(stream, stream instanceof ByteArrayInputStream
? stream.available() != 0 ? stream.available() : -1 : -1);
} else {
- entity = new InputStreamEntity(in, -1);
+ Message inMessage = exchange.getIn();
+ String length = inMessage.getHeader(Exchange.CONTENT_LENGTH, String.class);
+
+ if (ObjectHelper.isEmpty(length)) {
+ entity = new InputStreamEntity(in, -1);
+ } else {
+ entity = new InputStreamEntity(in, Long.parseLong(length));
+ }
}
if (exchange != null) {
String contentEncoding = exchange.getIn().getHeader(Exchange.CONTENT_ENCODING, String.class);
http://git-wip-us.apache.org/repos/asf/camel/blob/c1d61bc6/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpProducer.java b/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpProducer.java
index d41dbe1..01709eb 100644
--- a/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpProducer.java
+++ b/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpProducer.java
@@ -464,7 +464,13 @@ public class HttpProducer extends DefaultProducer {
if (answer == null) {
// force the body as an input stream since this is the fallback
InputStream is = in.getMandatoryBody(InputStream.class);
- InputStreamEntity entity = new InputStreamEntity(is, -1);
+ String length = in.getHeader(Exchange.CONTENT_LENGTH, String.class);
+ InputStreamEntity entity = null;
+ if (ObjectHelper.isEmpty(length)) {
+ entity = new InputStreamEntity(is, -1);
+ } else {
+ entity = new InputStreamEntity(is, Long.parseLong(length));
+ }
if (contentType != null) {
entity.setContentType(contentType.toString());
}
http://git-wip-us.apache.org/repos/asf/camel/blob/c1d61bc6/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java b/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java
index 45fcc53..03092cb 100644
--- a/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java
+++ b/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java
@@ -48,7 +48,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
- * @version
+ * @version
*/
public class JettyHttpProducer extends DefaultProducer implements AsyncProcessor {
private static final Logger LOG = LoggerFactory.getLogger(JettyHttpProducer.class);
@@ -164,6 +164,11 @@ public class JettyHttpProducer extends DefaultProducer implements AsyncProcessor
// then fallback to input stream
InputStream is = exchange.getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, exchange, exchange.getIn().getBody());
httpExchange.setRequestContentSource(is);
+ // setup the content length if it is possible
+ String length = exchange.getIn().getHeader(Exchange.CONTENT_LENGTH, String.class);
+ if (ObjectHelper.isNotEmpty(length)) {
+ httpExchange.addRequestHeader(Exchange.CONTENT_LENGTH, length);
+ }
}
}
}
@@ -177,7 +182,7 @@ public class JettyHttpProducer extends DefaultProducer implements AsyncProcessor
if (queryString != null) {
skipRequestHeaders = URISupport.parseQuery(queryString);
}
- // Need to remove the Host key as it should be not used
+ // Need to remove the Host key as it should be not used
exchange.getIn().getHeaders().remove("host");
}
http://git-wip-us.apache.org/repos/asf/camel/blob/c1d61bc6/tests/camel-itest/src/test/java/org/apache/camel/itest/http/HttpRouteContentLengthTest.java
----------------------------------------------------------------------
diff --git a/tests/camel-itest/src/test/java/org/apache/camel/itest/http/HttpRouteContentLengthTest.java b/tests/camel-itest/src/test/java/org/apache/camel/itest/http/HttpRouteContentLengthTest.java
new file mode 100644
index 0000000..b4c8918
--- /dev/null
+++ b/tests/camel-itest/src/test/java/org/apache/camel/itest/http/HttpRouteContentLengthTest.java
@@ -0,0 +1,91 @@
+/**
+ * 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.itest.http;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.AvailablePortFinder;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+public class HttpRouteContentLengthTest extends CamelTestSupport {
+
+ private int port1;
+ private int port2;
+
+ @Test
+ public void testHttpClientContentLength() throws Exception {
+ invokeService(port1, true);
+ }
+
+ @Test
+ public void testHttpRouteContentLength() throws Exception {
+ invokeService(port2, false);
+ }
+
+ private void invokeService(int port, boolean checkChunkedHeader) {
+ Exchange out = template.request("http://localhost:" + port + "/test", new Processor() {
+
+ @Override
+ public void process(Exchange exchange) throws Exception {
+ exchange.getIn().setBody("Camel request.");
+ }
+ });
+
+ assertNotNull(out);
+ assertEquals("Bye Camel request.: 14", out.getOut().getBody(String.class));
+
+
+
+ }
+
+ protected String getHttpEndpointScheme() {
+ return "http4://localhost:";
+ }
+
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ port1 = AvailablePortFinder.getNextAvailable(8000);
+ port2 = AvailablePortFinder.getNextAvailable(9000);
+
+ // use jetty as server as it supports sending response as chunked encoding
+ from("jetty:http://localhost:" + port1 + "/test")
+ .process(new Processor() {
+
+ @Override
+ public void process(Exchange exchange) throws Exception {
+ String contentLength = exchange.getIn().getHeader(Exchange.CONTENT_LENGTH, String.class);
+ String request = exchange.getIn().getBody(String.class);
+ exchange.getOut().setBody(request + ": " + contentLength);
+ }
+
+ })
+ .transform().simple("Bye ${body}");
+
+ // set up a netty http proxy
+ from("jetty:http://localhost:" + port2 + "/test")
+ .to(getHttpEndpointScheme() + port1 + "/test?bridgeEndpoint=true&throwExceptionOnFailure=false");
+
+ }
+ };
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/c1d61bc6/tests/camel-itest/src/test/java/org/apache/camel/itest/http/JettyHttpRouteContentLengthTest.java
----------------------------------------------------------------------
diff --git a/tests/camel-itest/src/test/java/org/apache/camel/itest/http/JettyHttpRouteContentLengthTest.java b/tests/camel-itest/src/test/java/org/apache/camel/itest/http/JettyHttpRouteContentLengthTest.java
new file mode 100644
index 0000000..db21146
--- /dev/null
+++ b/tests/camel-itest/src/test/java/org/apache/camel/itest/http/JettyHttpRouteContentLengthTest.java
@@ -0,0 +1,25 @@
+/**
+ * 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.itest.http;
+
+public class JettyHttpRouteContentLengthTest extends HttpRouteContentLengthTest {
+
+ protected String getHttpEndpointScheme() {
+ return "jetty://http://localhost:";
+ }
+
+}
[2/2] camel git commit: CAMEL-8169 Camel Jetty/Http4 producers should
respect Content-Length/Transfer-Encoding:Chunked headers
Posted by ni...@apache.org.
CAMEL-8169 Camel Jetty/Http4 producers should respect Content-Length/Transfer-Encoding:Chunked headers
Conflicts:
components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/945c7272
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/945c7272
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/945c7272
Branch: refs/heads/camel-2.13.x
Commit: 945c7272c484b617f73d6201a1da4feda8cb4884
Parents: 44730ce
Author: Willem Jiang <wi...@gmail.com>
Authored: Mon Dec 22 08:56:13 2014 +0800
Committer: Willem Jiang <wi...@gmail.com>
Committed: Thu Dec 25 16:42:35 2014 +0800
----------------------------------------------------------------------
.../component/http4/HttpEntityConverter.java | 11 ++-
.../camel/component/http4/HttpProducer.java | 8 +-
.../component/jetty/JettyHttpProducer.java | 9 +-
.../itest/http/HttpRouteContentLengthTest.java | 91 ++++++++++++++++++++
.../http/JettyHttpRouteContentLengthTest.java | 25 ++++++
5 files changed, 140 insertions(+), 4 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/945c7272/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEntityConverter.java
----------------------------------------------------------------------
diff --git a/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEntityConverter.java b/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEntityConverter.java
index 8236da8..bcbe502 100644
--- a/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEntityConverter.java
+++ b/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEntityConverter.java
@@ -22,8 +22,10 @@ import java.io.InputStream;
import org.apache.camel.Converter;
import org.apache.camel.Exchange;
+import org.apache.camel.Message;
import org.apache.camel.util.ExchangeHelper;
import org.apache.camel.util.GZIPHelper;
+import org.apache.camel.util.ObjectHelper;
import org.apache.http.HttpEntity;
import org.apache.http.entity.AbstractHttpEntity;
import org.apache.http.entity.ByteArrayEntity;
@@ -67,7 +69,14 @@ public final class HttpEntityConverter {
entity = new InputStreamEntity(stream, stream instanceof ByteArrayInputStream
? stream.available() != 0 ? stream.available() : -1 : -1);
} else {
- entity = new InputStreamEntity(in, -1);
+ Message inMessage = exchange.getIn();
+ String length = inMessage.getHeader(Exchange.CONTENT_LENGTH, String.class);
+
+ if (ObjectHelper.isEmpty(length)) {
+ entity = new InputStreamEntity(in, -1);
+ } else {
+ entity = new InputStreamEntity(in, Long.parseLong(length));
+ }
}
if (exchange != null) {
String contentEncoding = exchange.getIn().getHeader(Exchange.CONTENT_ENCODING, String.class);
http://git-wip-us.apache.org/repos/asf/camel/blob/945c7272/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpProducer.java b/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpProducer.java
index d41dbe1..01709eb 100644
--- a/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpProducer.java
+++ b/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpProducer.java
@@ -464,7 +464,13 @@ public class HttpProducer extends DefaultProducer {
if (answer == null) {
// force the body as an input stream since this is the fallback
InputStream is = in.getMandatoryBody(InputStream.class);
- InputStreamEntity entity = new InputStreamEntity(is, -1);
+ String length = in.getHeader(Exchange.CONTENT_LENGTH, String.class);
+ InputStreamEntity entity = null;
+ if (ObjectHelper.isEmpty(length)) {
+ entity = new InputStreamEntity(is, -1);
+ } else {
+ entity = new InputStreamEntity(is, Long.parseLong(length));
+ }
if (contentType != null) {
entity.setContentType(contentType.toString());
}
http://git-wip-us.apache.org/repos/asf/camel/blob/945c7272/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java b/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java
index 45fcc53..03092cb 100644
--- a/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java
+++ b/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java
@@ -48,7 +48,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
- * @version
+ * @version
*/
public class JettyHttpProducer extends DefaultProducer implements AsyncProcessor {
private static final Logger LOG = LoggerFactory.getLogger(JettyHttpProducer.class);
@@ -164,6 +164,11 @@ public class JettyHttpProducer extends DefaultProducer implements AsyncProcessor
// then fallback to input stream
InputStream is = exchange.getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, exchange, exchange.getIn().getBody());
httpExchange.setRequestContentSource(is);
+ // setup the content length if it is possible
+ String length = exchange.getIn().getHeader(Exchange.CONTENT_LENGTH, String.class);
+ if (ObjectHelper.isNotEmpty(length)) {
+ httpExchange.addRequestHeader(Exchange.CONTENT_LENGTH, length);
+ }
}
}
}
@@ -177,7 +182,7 @@ public class JettyHttpProducer extends DefaultProducer implements AsyncProcessor
if (queryString != null) {
skipRequestHeaders = URISupport.parseQuery(queryString);
}
- // Need to remove the Host key as it should be not used
+ // Need to remove the Host key as it should be not used
exchange.getIn().getHeaders().remove("host");
}
http://git-wip-us.apache.org/repos/asf/camel/blob/945c7272/tests/camel-itest/src/test/java/org/apache/camel/itest/http/HttpRouteContentLengthTest.java
----------------------------------------------------------------------
diff --git a/tests/camel-itest/src/test/java/org/apache/camel/itest/http/HttpRouteContentLengthTest.java b/tests/camel-itest/src/test/java/org/apache/camel/itest/http/HttpRouteContentLengthTest.java
new file mode 100644
index 0000000..b4c8918
--- /dev/null
+++ b/tests/camel-itest/src/test/java/org/apache/camel/itest/http/HttpRouteContentLengthTest.java
@@ -0,0 +1,91 @@
+/**
+ * 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.itest.http;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.AvailablePortFinder;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+public class HttpRouteContentLengthTest extends CamelTestSupport {
+
+ private int port1;
+ private int port2;
+
+ @Test
+ public void testHttpClientContentLength() throws Exception {
+ invokeService(port1, true);
+ }
+
+ @Test
+ public void testHttpRouteContentLength() throws Exception {
+ invokeService(port2, false);
+ }
+
+ private void invokeService(int port, boolean checkChunkedHeader) {
+ Exchange out = template.request("http://localhost:" + port + "/test", new Processor() {
+
+ @Override
+ public void process(Exchange exchange) throws Exception {
+ exchange.getIn().setBody("Camel request.");
+ }
+ });
+
+ assertNotNull(out);
+ assertEquals("Bye Camel request.: 14", out.getOut().getBody(String.class));
+
+
+
+ }
+
+ protected String getHttpEndpointScheme() {
+ return "http4://localhost:";
+ }
+
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ port1 = AvailablePortFinder.getNextAvailable(8000);
+ port2 = AvailablePortFinder.getNextAvailable(9000);
+
+ // use jetty as server as it supports sending response as chunked encoding
+ from("jetty:http://localhost:" + port1 + "/test")
+ .process(new Processor() {
+
+ @Override
+ public void process(Exchange exchange) throws Exception {
+ String contentLength = exchange.getIn().getHeader(Exchange.CONTENT_LENGTH, String.class);
+ String request = exchange.getIn().getBody(String.class);
+ exchange.getOut().setBody(request + ": " + contentLength);
+ }
+
+ })
+ .transform().simple("Bye ${body}");
+
+ // set up a netty http proxy
+ from("jetty:http://localhost:" + port2 + "/test")
+ .to(getHttpEndpointScheme() + port1 + "/test?bridgeEndpoint=true&throwExceptionOnFailure=false");
+
+ }
+ };
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/945c7272/tests/camel-itest/src/test/java/org/apache/camel/itest/http/JettyHttpRouteContentLengthTest.java
----------------------------------------------------------------------
diff --git a/tests/camel-itest/src/test/java/org/apache/camel/itest/http/JettyHttpRouteContentLengthTest.java b/tests/camel-itest/src/test/java/org/apache/camel/itest/http/JettyHttpRouteContentLengthTest.java
new file mode 100644
index 0000000..db21146
--- /dev/null
+++ b/tests/camel-itest/src/test/java/org/apache/camel/itest/http/JettyHttpRouteContentLengthTest.java
@@ -0,0 +1,25 @@
+/**
+ * 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.itest.http;
+
+public class JettyHttpRouteContentLengthTest extends HttpRouteContentLengthTest {
+
+ protected String getHttpEndpointScheme() {
+ return "jetty://http://localhost:";
+ }
+
+}