Updated Branches: refs/heads/master 682d068b1 -> b0a6db144
CAMEL-6327: camel-netty-http - Working on CBR with xpath issue. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/b0a6db14 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/b0a6db14 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/b0a6db14 Branch: refs/heads/master Commit: b0a6db144e7162787e635ef596990f40c25cba53 Parents: 682d068 Author: Claus Ibsen <[email protected]> Authored: Thu Jun 27 17:32:59 2013 +0200 Committer: Claus Ibsen <[email protected]> Committed: Thu Jun 27 17:36:13 2013 +0200 ---------------------------------------------------------------------- .../netty/http/DefaultNettyHttpBinding.java | 10 ++- .../http/NettyChannelBufferStreamCache.java | 88 ++++++++++++++++++++ .../netty/http/NettyHttpConfiguration.java | 9 ++ .../netty/http/NettyHttpConverter.java | 5 +- .../http/NettyHttpXMLXPathResponseTest.java | 53 ++++++++++++ .../netty/http/NettyHttpXMLXPathTest.java | 8 -- 6 files changed, 162 insertions(+), 11 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/b0a6db14/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java ---------------------------------------------------------------------- diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java index 0a57638..8e7579a 100644 --- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java +++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java @@ -75,8 +75,14 @@ public class DefaultNettyHttpBinding implements NettyHttpBinding { populateCamelHeaders(request, answer.getHeaders(), exchange, configuration); } - // keep the body as is, and use type converters - answer.setBody(request.getContent()); + if (configuration.isDisableStreamCache()) { + // keep the body as is, and use type converters + answer.setBody(request.getContent()); + } else { + // turn the body into stream cached + NettyChannelBufferStreamCache cache = new NettyChannelBufferStreamCache(request.getContent()); + answer.setBody(cache); + } return answer; } http://git-wip-us.apache.org/repos/asf/camel/blob/b0a6db14/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyChannelBufferStreamCache.java ---------------------------------------------------------------------- diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyChannelBufferStreamCache.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyChannelBufferStreamCache.java new file mode 100644 index 0000000..c5ee8ba --- /dev/null +++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyChannelBufferStreamCache.java @@ -0,0 +1,88 @@ +/** + * 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.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import org.apache.camel.StreamCache; +import org.apache.camel.util.IOHelper; +import org.jboss.netty.buffer.ChannelBuffer; + +/** + * A {@link ChannelBuffer} which is exposed as an {@link InputStream} which makes it very + * easy to use by Camel and other Camel components. Also supported is {@link StreamCache} + * which allows the data to be re-read for example when doing content based routing with XPath. + */ +public final class NettyChannelBufferStreamCache extends InputStream implements StreamCache { + + private final ChannelBuffer buffer; + + public NettyChannelBufferStreamCache(ChannelBuffer buffer) { + this.buffer = buffer; + buffer.markReaderIndex(); + } + + @Override + public boolean markSupported() { + return true; + } + + @Override + public int read() throws IOException { + return buffer.readByte(); + } + + @Override + public int read(byte[] b) throws IOException { + return read(b, 0, b.length); + } + + @Override + public int read(byte[] b, int off, int len) throws IOException { + // are we at end, then return -1 + if (buffer.readerIndex() == buffer.capacity()) { + return -1; + } + + // ensure we don't read more than what we have in the buffer + int before = buffer.readerIndex(); + int max = buffer.capacity() - before; + len = Math.min(max, len); + + buffer.readBytes(b, off, len); + return buffer.readerIndex() - before; + } + + @Override + public void reset() { + buffer.resetReaderIndex(); + } + + @Override + public void writeTo(OutputStream os) throws IOException { + // must remember current index so we can reset back to it after the copy + int idx = buffer.readerIndex(); + try { + buffer.resetReaderIndex(); + IOHelper.copy(this, os); + } finally { + buffer.readerIndex(idx); + } + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/b0a6db14/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConfiguration.java ---------------------------------------------------------------------- diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConfiguration.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConfiguration.java index cc7f154..e2360aa 100644 --- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConfiguration.java +++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConfiguration.java @@ -37,6 +37,7 @@ public class NettyHttpConfiguration extends NettyConfiguration { private boolean matchOnUriPrefix; private boolean bridgeEndpoint; private String path; + private boolean disableStreamCache; public NettyHttpConfiguration() { // we need sync=true as http is request/reply by nature @@ -133,4 +134,12 @@ public class NettyHttpConfiguration extends NettyConfiguration { public void setPath(String path) { this.path = path; } + + public boolean isDisableStreamCache() { + return disableStreamCache; + } + + public void setDisableStreamCache(boolean disableStreamCache) { + this.disableStreamCache = disableStreamCache; + } } http://git-wip-us.apache.org/repos/asf/camel/blob/b0a6db14/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConverter.java ---------------------------------------------------------------------- diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConverter.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConverter.java index affef68..0722651 100644 --- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConverter.java +++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConverter.java @@ -51,7 +51,10 @@ public final class NettyHttpConverter { msg = exchange.getIn(NettyHttpMessage.class); } if (msg != null && msg.getBody() == value) { - return msg.getHttpRequest(); + // ensure the http request content is reset so we can read all the content out-of-the-box + HttpRequest request = msg.getHttpRequest(); + request.getContent().resetReaderIndex(); + return request; } } http://git-wip-us.apache.org/repos/asf/camel/blob/b0a6db14/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpXMLXPathResponseTest.java ---------------------------------------------------------------------- diff --git a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpXMLXPathResponseTest.java b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpXMLXPathResponseTest.java new file mode 100644 index 0000000..80dc732 --- /dev/null +++ b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpXMLXPathResponseTest.java @@ -0,0 +1,53 @@ +/** + * 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.builder.RouteBuilder; +import org.junit.Test; + +public class NettyHttpXMLXPathResponseTest extends BaseNettyTest { + + @Test + public void testHttpXML() throws Exception { + String out = template.requestBody("netty-http:http://localhost:{{port}}/foo", "<person><name>Claus</name></person>", String.class); + assertEquals("<name>Claus</name>", out); + + out = template.requestBody("netty-http:http://localhost:{{port}}/foo", "<person><name>James</name></person>", String.class); + assertEquals("James", out); + + out = template.requestBody("netty-http:http://localhost:{{port}}/foo", "<person><name>Jonathan</name></person>", String.class); + assertEquals("Dont understand <person><name>Jonathan</name></person>", out); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("netty-http:http://0.0.0.0:{{port}}/foo") + .choice() + .when().xpath("/person/name = 'Claus'") + .transform(xpath("/person/name")) + .when().xpath("/person/name = 'James'") + .transform(xpath("/person/name/text()")) + .otherwise() + .transform(simple("Dont understand ${body}")); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/b0a6db14/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpXMLXPathTest.java ---------------------------------------------------------------------- diff --git a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpXMLXPathTest.java b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpXMLXPathTest.java index 4d433e5..f0d660e 100644 --- a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpXMLXPathTest.java +++ b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpXMLXPathTest.java @@ -16,17 +16,11 @@ */ package org.apache.camel.component.netty.http; -import org.apache.camel.CamelContext; import org.apache.camel.builder.RouteBuilder; import org.junit.Test; public class NettyHttpXMLXPathTest extends BaseNettyTest { - @Override - protected CamelContext createCamelContext() throws Exception { - return super.createCamelContext(); - } - @Test public void testHttpXML() throws Exception { String out = template.requestBody("netty-http:http://localhost:{{port}}/foo", "<person><name>Claus</name></person>", String.class); @@ -45,8 +39,6 @@ public class NettyHttpXMLXPathTest extends BaseNettyTest { @Override public void configure() throws Exception { from("netty-http:http://0.0.0.0:{{port}}/foo") - // need to convert to byte[] to have the CBR with xpath working - .convertBodyTo(byte[].class) .choice() .when().xpath("/person/name = 'Claus'") .transform(constant("<quote>Camel rocks</quote>"))
