This is an automated email from the ASF dual-hosted git repository. coheigea pushed a commit to branch camel-2.25.x in repository https://gitbox.apache.org/repos/asf/camel.git
commit a53c0f964184f46e7139c4d43f2e9ade834df236 Author: Colm O hEigeartaigh <cohei...@apache.org> AuthorDate: Tue Feb 4 13:51:13 2020 +0100 CAMEL-14477 - Disable object serialization --- .../src/main/docs/netty4-component.adoc | 22 +++-- .../component/netty4/ChannelHandlerFactories.java | 42 +------- .../camel/component/netty4/NettyConfiguration.java | 20 ++-- .../camel/component/netty4/NettyBacklogTest.java | 12 +-- .../component/netty4/NettyConcurrentTest.java | 25 ++++- .../camel/component/netty4/NettyOptionTest.java | 12 +-- .../camel/component/netty4/NettyTCPAsyncTest.java | 27 +++++- .../component/netty4/NettyTCPChainedTest.java | 18 +++- .../netty4/NettyTCPSyncNotLazyChannelTest.java | 13 --- .../camel/component/netty4/NettyTCPSyncTest.java | 21 +--- .../netty4/NettyTransferExchangeOptionTest.java | 28 +++++- .../camel/component/netty4/NettyUDPAsyncTest.java | 4 +- .../component/netty4/NettyUDPObjectSyncTest.java | 50 ---------- .../component/netty4/ObjectSerializationTest.java | 107 +++++++++++++++++++++ .../springboot/NettyComponentConfiguration.java | 3 +- 15 files changed, 236 insertions(+), 168 deletions(-) diff --git a/components/camel-netty4/src/main/docs/netty4-component.adoc b/components/camel-netty4/src/main/docs/netty4-component.adoc index de26c00..b41bc31 100644 --- a/components/camel-netty4/src/main/docs/netty4-component.adoc +++ b/components/camel-netty4/src/main/docs/netty4-component.adoc @@ -158,7 +158,7 @@ with the following path and query parameters: | *encoder* (codec) | *Deprecated* A custom ChannelHandler class that can be used to perform special marshalling of outbound payloads. | | ChannelHandler | *encoders* (codec) | A list of encoders to be used. You can use a String which have values separated by comma, and have the values be looked up in the Registry. Just remember to prefix the value with # so Camel knows it should lookup. | | String | *encoding* (codec) | The encoding (a charset name) to use for the textline codec. If not provided, Camel will use the JVM default Charset. | | String -| *textline* (codec) | Only used for TCP. If no codec is specified, you can use this flag to indicate a text line based codec; if not specified or the value is false, then Object Serialization is assumed over TCP. | false | boolean +| *textline* (codec) | Only used for TCP. If no codec is specified, you can use this flag to indicate a text line based codec; if not specified or the value is false, then Object Serialization is assumed over TCP - however only Strings are allowed to be serialized by default. | false | boolean | *enabledProtocols* (security) | Which protocols to enable when using SSL | TLSv1,TLSv1.1,TLSv1.2 | String | *keyStoreFile* (security) | Client side certificate keystore to be used for encryption | | File | *keyStoreFormat* (security) | Keystore format to be used for payload encryption. Defaults to JKS if not set | | String @@ -254,7 +254,7 @@ The component supports 78 options, which are listed below. | *camel.component.netty4.configuration.ssl-handler* | Reference to a class that could be used to return an SSL Handler | | SslHandler | *camel.component.netty4.configuration.sync* | Setting to set endpoint as one-way or request-response | true | Boolean | *camel.component.netty4.configuration.tcp-no-delay* | Setting to improve TCP protocol performance | true | Boolean -| *camel.component.netty4.configuration.textline* | Only used for TCP. If no codec is specified, you can use this flag to indicate a text line based codec; if not specified or the value is false, then Object Serialization is assumed over TCP. | false | Boolean +| *camel.component.netty4.configuration.textline* | Only used for TCP. If no codec is specified, you can use this flag to indicate a text line based codec; if not specified or the value is false, then Object Serialization is assumed over TCP. However note that only Strings are serialized, anything else will only be serialized with a custom encoder/decoder. | false | Boolean | *camel.component.netty4.configuration.transfer-exchange* | Only used for TCP. You can transfer the exchange over the wire instead of just the body. The following fields are transferred: In body, Out body, fault body, In headers, Out headers, fault headers, exchange properties, exchange exception. This requires that the objects are serializable. Camel will exclude any non-serializable objects and log it at WARN level. | false | Boolean | *camel.component.netty4.configuration.trust-store-resource* | Server side certificate keystore to be used for encryption. Is loaded by default from classpath, but you can prefix with classpath:, file:, or http: to load the resource from different systems. | | String | *camel.component.netty4.configuration.udp-byte-array-codec* | For UDP only. If enabled the using byte array codec instead of Java serialization protocol. | false | Boolean @@ -374,16 +374,26 @@ operations. ==== A UDP Netty endpoint using Request-Reply and serialized object payload +Note that Object serialization is not allowed by default, and so a decoder must be configured. + [source,java] ---- +JndiRegistry jndi... +jndi.bind("decoder", new DefaultChannelHandlerFactory() { + @Override + public ChannelHandler newChannelHandler() { + return new ObjectDecoder(ClassResolvers.weakCachingResolver(null)); + } +}); + RouteBuilder builder = new RouteBuilder() { public void configure() { - from("netty4:udp://0.0.0.0:5155?sync=true") + from("netty4:udp://0.0.0.0:5155?sync=true&decoders=#decoder") .process(new Processor() { public void process(Exchange exchange) throws Exception { Poetry poetry = (Poetry) exchange.getIn().getBody(); - poetry.setPoet("Dr. Sarojini Naidu"); - exchange.getOut().setBody(poetry); + // Process poetry in some way + exchange.getOut().setBody("Message received); } } } @@ -798,4 +808,4 @@ NOTE: We recommend extending the `TimeoutCorrelationManagerSupport` when you bui This provides support for timeout and other complexities you otherwise would need to implement as well. You can find an example with the Apache Camel source code in the examples directory -under the `camel-example-netty-custom-correlation` directory. \ No newline at end of file +under the `camel-example-netty-custom-correlation` directory. diff --git a/components/camel-netty4/src/main/java/org/apache/camel/component/netty4/ChannelHandlerFactories.java b/components/camel-netty4/src/main/java/org/apache/camel/component/netty4/ChannelHandlerFactories.java index ff71a18..b40619b 100644 --- a/components/camel-netty4/src/main/java/org/apache/camel/component/netty4/ChannelHandlerFactories.java +++ b/components/camel-netty4/src/main/java/org/apache/camel/component/netty4/ChannelHandlerFactories.java @@ -23,7 +23,6 @@ import io.netty.channel.ChannelHandler; import io.netty.handler.codec.LengthFieldBasedFrameDecoder; import io.netty.handler.codec.bytes.ByteArrayDecoder; import io.netty.handler.codec.bytes.ByteArrayEncoder; -import io.netty.handler.codec.serialization.ClassResolvers; import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder; import org.apache.camel.component.netty4.codec.DatagramPacketByteArrayDecoder; @@ -31,13 +30,9 @@ import org.apache.camel.component.netty4.codec.DatagramPacketByteArrayEncoder; import org.apache.camel.component.netty4.codec.DatagramPacketDecoder; import org.apache.camel.component.netty4.codec.DatagramPacketDelimiterDecoder; import org.apache.camel.component.netty4.codec.DatagramPacketEncoder; -import org.apache.camel.component.netty4.codec.DatagramPacketObjectDecoder; -import org.apache.camel.component.netty4.codec.DatagramPacketObjectEncoder; import org.apache.camel.component.netty4.codec.DatagramPacketStringDecoder; import org.apache.camel.component.netty4.codec.DatagramPacketStringEncoder; import org.apache.camel.component.netty4.codec.DelimiterBasedFrameDecoder; -import org.apache.camel.component.netty4.codec.ObjectDecoder; -import org.apache.camel.component.netty4.codec.ObjectEncoder; /** @@ -58,43 +53,16 @@ public final class ChannelHandlerFactories { public static ChannelHandlerFactory newStringDecoder(Charset charset, String protocol) { if ("udp".equalsIgnoreCase(protocol)) { - return new ShareableChannelHandlerFactory(new DatagramPacketStringDecoder(charset)); + return new ShareableChannelHandlerFactory(new DatagramPacketStringDecoder(charset)); } else { return new ShareableChannelHandlerFactory(new StringDecoder(charset)); } } - - - public static ChannelHandlerFactory newObjectDecoder(String protocol) { - if ("udp".equalsIgnoreCase(protocol)) { - return new DefaultChannelHandlerFactory() { - @Override - public ChannelHandler newChannelHandler() { - return new DatagramPacketObjectDecoder(ClassResolvers.weakCachingResolver(null)); - } - }; - } else { - return new DefaultChannelHandlerFactory() { - @Override - public ChannelHandler newChannelHandler() { - return new ObjectDecoder(ClassResolvers.weakCachingResolver(null)); - } - }; - } - } - - public static ChannelHandlerFactory newObjectEncoder(String protocol) { - if ("udp".equals(protocol)) { - return new ShareableChannelHandlerFactory(new DatagramPacketObjectEncoder()); - } else { - return new ShareableChannelHandlerFactory(new ObjectEncoder()); - } - } - + public static ChannelHandlerFactory newDelimiterBasedFrameDecoder(final int maxFrameLength, final ByteBuf[] delimiters, String protocol) { return newDelimiterBasedFrameDecoder(maxFrameLength, delimiters, true, protocol); } - + public static ChannelHandlerFactory newDelimiterBasedFrameDecoder(final int maxFrameLength, final ByteBuf[] delimiters, final boolean stripDelimiter, String protocol) { if ("udp".equals(protocol)) { return new DefaultChannelHandlerFactory() { @@ -112,11 +80,11 @@ public final class ChannelHandlerFactories { }; } } - + public static ChannelHandlerFactory newDatagramPacketDecoder() { return new ShareableChannelHandlerFactory(new DatagramPacketDecoder()); } - + public static ChannelHandlerFactory newDatagramPacketEncoder() { return new ShareableChannelHandlerFactory(new DatagramPacketEncoder()); } diff --git a/components/camel-netty4/src/main/java/org/apache/camel/component/netty4/NettyConfiguration.java b/components/camel-netty4/src/main/java/org/apache/camel/component/netty4/NettyConfiguration.java index 924e4d5..18faa14 100644 --- a/components/camel-netty4/src/main/java/org/apache/camel/component/netty4/NettyConfiguration.java +++ b/components/camel-netty4/src/main/java/org/apache/camel/component/netty4/NettyConfiguration.java @@ -219,7 +219,7 @@ public class NettyConfiguration extends NettyServerBootstrapConfiguration implem if ("udp".equalsIgnoreCase(protocol)) { encoders.add(ChannelHandlerFactories.newDatagramPacketEncoder()); } - // are we textline or object? + // are we textline or byte array if (isTextline()) { Charset charset = getEncoding() != null ? Charset.forName(getEncoding()) : CharsetUtil.UTF_8; encoders.add(ChannelHandlerFactories.newStringEncoder(charset, protocol)); @@ -235,11 +235,10 @@ public class NettyConfiguration extends NettyServerBootstrapConfiguration implem encoders.add(ChannelHandlerFactories.newByteArrayEncoder(protocol)); decoders.add(ChannelHandlerFactories.newByteArrayDecoder(protocol)); } else { - // object serializable is then used - encoders.add(ChannelHandlerFactories.newObjectEncoder(protocol)); - decoders.add(ChannelHandlerFactories.newObjectDecoder(protocol)); - - LOG.debug("Using object encoders and decoders"); + // Fall back to allowing Strings to be serialized only + Charset charset = getEncoding() != null ? Charset.forName(getEncoding()) : CharsetUtil.UTF_8; + encoders.add(ChannelHandlerFactories.newStringEncoder(charset, protocol)); + decoders.add(ChannelHandlerFactories.newStringDecoder(charset, protocol)); } if ("udp".equalsIgnoreCase(protocol)) { decoders.add(ChannelHandlerFactories.newDatagramPacketDecoder()); @@ -293,7 +292,8 @@ public class NettyConfiguration extends NettyServerBootstrapConfiguration implem /** * Only used for TCP. If no codec is specified, you can use this flag to indicate a text line based codec; - * if not specified or the value is false, then Object Serialization is assumed over TCP. + * if not specified or the value is false, then Object Serialization is assumed over TCP - however only Strings + * are allowed to be serialized by default. */ public void setTextline(boolean textline) { this.textline = textline; @@ -433,7 +433,7 @@ public class NettyConfiguration extends NettyServerBootstrapConfiguration implem public boolean isAllowSerializedHeaders() { return allowSerializedHeaders; } - + /** * Only used for TCP when transferExchange is true. When set to true, serializable objects in headers and properties * will be added to the exchange. Otherwise Camel will exclude any non-serializable objects and log it at WARN @@ -442,7 +442,7 @@ public class NettyConfiguration extends NettyServerBootstrapConfiguration implem public void setAllowSerializedHeaders(final boolean allowSerializedHeaders) { this.allowSerializedHeaders = allowSerializedHeaders; } - + public boolean isDisconnectOnNoReply() { return disconnectOnNoReply; } @@ -657,7 +657,7 @@ public class NettyConfiguration extends NettyServerBootstrapConfiguration implem * When using this, the channel is not returned to the connection pool until the {@link Exchange} is done; or disconnected * if the disconnect option is set to true. * <p/> - * The reused {@link Channel} is stored on the {@link Exchange} as an exchange property with the key {@link NettyConstants#NETTY_CHANNEL} + * The reused {@link Channel} is stored on the {@link Exchange} as an exchange property with the key {@link NettyConstants#NETTY_CHANNEL} * which allows you to obtain the channel during routing and use it as well. */ public void setReuseChannel(boolean reuseChannel) { diff --git a/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyBacklogTest.java b/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyBacklogTest.java index c0c25c0..3f64674 100644 --- a/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyBacklogTest.java +++ b/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyBacklogTest.java @@ -21,7 +21,7 @@ import org.apache.camel.Processor; import org.apache.camel.builder.RouteBuilder; public class NettyBacklogTest extends NettyTCPSyncTest { - + @Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @@ -30,15 +30,9 @@ public class NettyBacklogTest extends NettyTCPSyncTest { from("netty4:tcp://localhost:{{port}}?sync=true&backlog=500") .process(new Processor() { public void process(Exchange exchange) throws Exception { - if (exchange.getIn().getBody() instanceof Poetry) { - Poetry poetry = (Poetry) exchange.getIn().getBody(); - poetry.setPoet("Dr. Sarojini Naidu"); - exchange.getOut().setBody(poetry); - return; - } - exchange.getOut().setBody("When You Go Home, Tell Them Of Us And Say, For Your Tomorrow, We Gave Our Today."); + exchange.getOut().setBody("When You Go Home, Tell Them Of Us And Say, For Your Tomorrow, We Gave Our Today."); } - }); + }); } }; } diff --git a/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyConcurrentTest.java b/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyConcurrentTest.java index f685c57..cb32e41 100644 --- a/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyConcurrentTest.java +++ b/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyConcurrentTest.java @@ -30,10 +30,16 @@ import org.apache.camel.Exchange; import org.apache.camel.Processor; import org.apache.camel.builder.NotifyBuilder; import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.netty4.codec.ObjectDecoder; +import org.apache.camel.component.netty4.codec.ObjectEncoder; +import org.apache.camel.impl.JndiRegistry; import org.apache.camel.util.StopWatch; import org.junit.Ignore; import org.junit.Test; +import io.netty.channel.ChannelHandler; +import io.netty.handler.codec.serialization.ClassResolvers; + public class NettyConcurrentTest extends BaseNettyTest { @Test @@ -52,6 +58,21 @@ public class NettyConcurrentTest extends BaseNettyTest { doSendMessages(250000, 100); } + @Override + protected JndiRegistry createRegistry() throws Exception { + JndiRegistry jndi = super.createRegistry(); + + jndi.bind("encoder", new ShareableChannelHandlerFactory(new ObjectEncoder())); + jndi.bind("decoder", new DefaultChannelHandlerFactory() { + @Override + public ChannelHandler newChannelHandler() { + return new ObjectDecoder(ClassResolvers.weakCachingResolver(null)); + } + }); + + return jndi; + } + private void doSendMessages(int files, int poolSize) throws Exception { StopWatch watch = new StopWatch(); NotifyBuilder notify = new NotifyBuilder(context).whenDone(files).create(); @@ -64,7 +85,7 @@ public class NettyConcurrentTest extends BaseNettyTest { final int index = i; Future<String> out = executor.submit(new Callable<String>() { public String call() throws Exception { - String reply = template.requestBody("netty4:tcp://localhost:{{port}}", index, String.class); + String reply = template.requestBody("netty4:tcp://localhost:{{port}}?encoders=#encoder&decoders=#decoder", index, String.class); log.debug("Sent {} received {}", index, reply); assertEquals("Bye " + index, reply); return reply; @@ -92,7 +113,7 @@ public class NettyConcurrentTest extends BaseNettyTest { protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { public void configure() throws Exception { - from("netty4:tcp://localhost:{{port}}?sync=true").process(new Processor() { + from("netty4:tcp://localhost:{{port}}?sync=true&encoders=#encoder&decoders=#decoder").process(new Processor() { public void process(Exchange exchange) throws Exception { String body = exchange.getIn().getBody(String.class); exchange.getOut().setBody("Bye " + body); diff --git a/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyOptionTest.java b/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyOptionTest.java index 7308d20..7620acb 100644 --- a/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyOptionTest.java +++ b/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyOptionTest.java @@ -21,7 +21,7 @@ import org.apache.camel.Processor; import org.apache.camel.builder.RouteBuilder; public class NettyOptionTest extends NettyTCPSyncTest { - + @Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @@ -30,15 +30,9 @@ public class NettyOptionTest extends NettyTCPSyncTest { from("netty4:tcp://localhost:{{port}}?sync=true&option.child.keepAlive=false") .process(new Processor() { public void process(Exchange exchange) throws Exception { - if (exchange.getIn().getBody() instanceof Poetry) { - Poetry poetry = (Poetry) exchange.getIn().getBody(); - poetry.setPoet("Dr. Sarojini Naidu"); - exchange.getOut().setBody(poetry); - return; - } - exchange.getOut().setBody("When You Go Home, Tell Them Of Us And Say, For Your Tomorrow, We Gave Our Today."); + exchange.getOut().setBody("When You Go Home, Tell Them Of Us And Say, For Your Tomorrow, We Gave Our Today."); } - }); + }); } }; } diff --git a/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyTCPAsyncTest.java b/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyTCPAsyncTest.java index 1314517..9d02a25 100644 --- a/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyTCPAsyncTest.java +++ b/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyTCPAsyncTest.java @@ -25,10 +25,16 @@ import org.apache.camel.Message; import org.apache.camel.Processor; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.component.netty4.codec.ObjectDecoder; +import org.apache.camel.component.netty4.codec.ObjectEncoder; import org.apache.camel.converter.IOConverter; +import org.apache.camel.impl.JndiRegistry; import org.apache.camel.util.IOHelper; import org.junit.Test; +import io.netty.channel.ChannelHandler; +import io.netty.handler.codec.serialization.ClassResolvers; + public class NettyTCPAsyncTest extends BaseNettyTest { @EndpointInject(uri = "mock:result") protected MockEndpoint resultEndpoint; @@ -54,19 +60,30 @@ public class NettyTCPAsyncTest extends BaseNettyTest { public void testTCPInOnlyWithNettyConsumer() throws Exception { MockEndpoint mock = getMockEndpoint("mock:result"); mock.expectedMessageCount(1); - sendFile("netty4:tcp://localhost:{{port}}?sync=false"); - + sendFile("netty4:tcp://localhost:{{port}}?sync=false&encoders=#encoder"); + mock.assertIsSatisfied(); } - + + + @Override + protected JndiRegistry createRegistry() throws Exception { + JndiRegistry jndi = super.createRegistry(); + + jndi.bind("encoder", ChannelHandlerFactories.newByteArrayEncoder("tcp")); + jndi.bind("decoder", ChannelHandlerFactories.newByteArrayEncoder("tcp")); + + return jndi; + } + @Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @Override public void configure() throws Exception { - from("netty4:tcp://localhost:{{port}}?sync=false") + from("netty4:tcp://localhost:{{port}}?sync=false&decoders=decoder") .to("log:result") - .to("mock:result"); + .to("mock:result"); } }; } diff --git a/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyTCPChainedTest.java b/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyTCPChainedTest.java index 0c68435..0af7b91 100644 --- a/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyTCPChainedTest.java +++ b/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyTCPChainedTest.java @@ -26,6 +26,7 @@ import org.apache.camel.Processor; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.mock.MockEndpoint; import org.apache.camel.converter.IOConverter; +import org.apache.camel.impl.JndiRegistry; import org.apache.camel.util.IOHelper; import org.junit.Assert; import org.junit.Test; @@ -65,20 +66,29 @@ public class NettyTCPChainedTest extends BaseNettyTest { MockEndpoint mock = getMockEndpoint("mock:result"); mock.expectedMessageCount(2); sendFile("direct:chainedCalls"); - + mock.assertIsSatisfied(); } - + + @Override + protected JndiRegistry createRegistry() throws Exception { + JndiRegistry jndi = super.createRegistry(); + + jndi.bind("encoder", ChannelHandlerFactories.newByteArrayEncoder("tcp")); + + return jndi; + } + @Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @Override public void configure() throws Exception { - from("netty4:tcp://localhost:{{port}}?sync=false") + from("netty4:tcp://localhost:{{port}}?sync=false&encoders=#encoder") .to("log:result") .to("mock:result"); from("direct:nettyCall") - .to("netty4:tcp://localhost:{{port}}?sync=false&disconnect=true&workerCount=1"); + .to("netty4:tcp://localhost:{{port}}?sync=false&disconnect=true&workerCount=1&encoders=#encoder"); from("direct:chainedCalls") .to("direct:nettyCall") .to("direct:nettyCall"); diff --git a/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyTCPSyncNotLazyChannelTest.java b/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyTCPSyncNotLazyChannelTest.java index 8d522d7..a3bdd89 100644 --- a/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyTCPSyncNotLazyChannelTest.java +++ b/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyTCPSyncNotLazyChannelTest.java @@ -31,13 +31,6 @@ public class NettyTCPSyncNotLazyChannelTest extends BaseNettyTest { assertEquals("When You Go Home, Tell Them Of Us And Say, For Your Tomorrow, We Gave Our Today.", response); } - @Test - public void testTCPObjectInOutWithNettyConsumer() throws Exception { - Poetry poetry = new Poetry(); - Poetry response = (Poetry) template.requestBody("netty4:tcp://localhost:{{port}}?sync=true&lazyChannelCreation=false", poetry); - assertEquals("Dr. Sarojini Naidu", response.getPoet()); - } - @Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @@ -46,12 +39,6 @@ public class NettyTCPSyncNotLazyChannelTest extends BaseNettyTest { from("netty4:tcp://localhost:{{port}}?sync=true") .process(new Processor() { public void process(Exchange exchange) throws Exception { - if (exchange.getIn().getBody() instanceof Poetry) { - Poetry poetry = (Poetry) exchange.getIn().getBody(); - poetry.setPoet("Dr. Sarojini Naidu"); - exchange.getOut().setBody(poetry); - return; - } exchange.getOut().setBody("When You Go Home, Tell Them Of Us And Say, For Your Tomorrow, We Gave Our Today."); } }); diff --git a/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyTCPSyncTest.java b/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyTCPSyncTest.java index 8671eb6..5f6fb25 100644 --- a/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyTCPSyncTest.java +++ b/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyTCPSyncTest.java @@ -22,12 +22,12 @@ import org.apache.camel.builder.RouteBuilder; import org.junit.Test; public class NettyTCPSyncTest extends BaseNettyTest { - + @Test public void testTCPStringInOutWithNettyConsumer() throws Exception { String response = template.requestBody( "netty4:tcp://localhost:{{port}}?sync=true", - "Epitaph in Kohima, India marking the WWII Battle of Kohima and Imphal, Burma Campaign - Attributed to John Maxwell Edmonds", String.class); + "Epitaph in Kohima, India marking the WWII Battle of Kohima and Imphal, Burma Campaign - Attributed to John Maxwell Edmonds", String.class); assertEquals("When You Go Home, Tell Them Of Us And Say, For Your Tomorrow, We Gave Our Today.", response); } @@ -44,13 +44,6 @@ public class NettyTCPSyncTest extends BaseNettyTest { assertEquals("When You Go Home, Tell Them Of Us And Say, For Your Tomorrow, We Gave Our Today.", response); } - @Test - public void testTCPObjectInOutWithNettyConsumer() throws Exception { - Poetry poetry = new Poetry(); - Poetry response = (Poetry) template.requestBody("netty4:tcp://localhost:{{port}}?sync=true", poetry); - assertEquals("Dr. Sarojini Naidu", response.getPoet()); - } - @Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @@ -59,15 +52,9 @@ public class NettyTCPSyncTest extends BaseNettyTest { from("netty4:tcp://localhost:{{port}}?sync=true") .process(new Processor() { public void process(Exchange exchange) throws Exception { - if (exchange.getIn().getBody() instanceof Poetry) { - Poetry poetry = (Poetry) exchange.getIn().getBody(); - poetry.setPoet("Dr. Sarojini Naidu"); - exchange.getOut().setBody(poetry); - return; - } - exchange.getOut().setBody("When You Go Home, Tell Them Of Us And Say, For Your Tomorrow, We Gave Our Today."); + exchange.getOut().setBody("When You Go Home, Tell Them Of Us And Say, For Your Tomorrow, We Gave Our Today."); } - }); + }); } }; } diff --git a/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyTransferExchangeOptionTest.java b/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyTransferExchangeOptionTest.java index 3127b98..0eef6dc 100644 --- a/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyTransferExchangeOptionTest.java +++ b/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyTransferExchangeOptionTest.java @@ -17,6 +17,7 @@ package org.apache.camel.component.netty4; import java.nio.charset.Charset; +import java.util.Properties; import org.apache.camel.Endpoint; import org.apache.camel.Exchange; @@ -25,10 +26,16 @@ import org.apache.camel.Message; import org.apache.camel.Processor; import org.apache.camel.Producer; import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.netty4.codec.ObjectDecoder; +import org.apache.camel.component.netty4.codec.ObjectEncoder; +import org.apache.camel.impl.JndiRegistry; import org.junit.Test; +import io.netty.channel.ChannelHandler; +import io.netty.handler.codec.serialization.ClassResolvers; + /** - * @version + * @version */ public class NettyTransferExchangeOptionTest extends BaseNettyTest { @@ -44,8 +51,23 @@ public class NettyTransferExchangeOptionTest extends BaseNettyTest { assertExchange(exchange, true); } + @Override + protected JndiRegistry createRegistry() throws Exception { + JndiRegistry jndi = super.createRegistry(); + + jndi.bind("encoder", new ShareableChannelHandlerFactory(new ObjectEncoder())); + jndi.bind("decoder", new DefaultChannelHandlerFactory() { + @Override + public ChannelHandler newChannelHandler() { + return new ObjectDecoder(ClassResolvers.weakCachingResolver(null)); + } + }); + + return jndi; + } + private Exchange sendExchange(boolean setException) throws Exception { - Endpoint endpoint = context.getEndpoint("netty4:tcp://localhost:{{port}}?transferExchange=true"); + Endpoint endpoint = context.getEndpoint("netty4:tcp://localhost:{{port}}?transferExchange=true&encoders=#encoder&decoders=#decoder"); Exchange exchange = endpoint.createExchange(); Message message = exchange.getIn(); @@ -97,7 +119,7 @@ public class NettyTransferExchangeOptionTest extends BaseNettyTest { protected RouteBuilder createRouteBuilder() { return new RouteBuilder() { public void configure() { - from("netty4:tcp://localhost:{{port}}?transferExchange=true").process(new Processor() { + from("netty4:tcp://localhost:{{port}}?transferExchange=true&encoders=#encoder&decoders=#decoder").process(new Processor() { public void process(Exchange e) throws InterruptedException { assertNotNull(e.getIn().getBody()); assertNotNull(e.getIn().getHeaders()); diff --git a/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyUDPAsyncTest.java b/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyUDPAsyncTest.java index 812acac9..b8b609f 100644 --- a/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyUDPAsyncTest.java +++ b/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyUDPAsyncTest.java @@ -42,7 +42,7 @@ public class NettyUDPAsyncTest extends BaseNettyTest { mock.expectedMessageCount(1); mock.message(0).body().startsWith("Song Of A Dream".getBytes()); - sendFile("netty4:udp://localhost:{{port}}?sync=false"); + sendFile("netty4:udp://localhost:{{port}}?sync=false&udpByteArrayCodec=true"); mock.assertIsSatisfied(); } @@ -54,7 +54,7 @@ public class NettyUDPAsyncTest extends BaseNettyTest { public void configure() throws Exception { from("netty4:udp://localhost:{{port}}?sync=false") .to("mock:result") - .to("log:Message"); + .to("log:Message"); } }; } diff --git a/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyUDPObjectSyncTest.java b/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyUDPObjectSyncTest.java deleted file mode 100644 index 5d2111d..0000000 --- a/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/NettyUDPObjectSyncTest.java +++ /dev/null @@ -1,50 +0,0 @@ -/** - * 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; - -import org.apache.camel.Exchange; -import org.apache.camel.Processor; -import org.apache.camel.builder.RouteBuilder; -import org.junit.Test; - -public class NettyUDPObjectSyncTest extends BaseNettyTest { - - @Test - public void testUDPObjectInOutWithNettyConsumer() throws Exception { - Poetry poetry = new Poetry(); - Poetry response = template.requestBody("netty4:udp://localhost:{{port}}?sync=true", poetry, Poetry.class); - assertEquals("Dr. Sarojini Naidu", response.getPoet()); - } - - @Override - protected RouteBuilder createRouteBuilder() throws Exception { - return new RouteBuilder() { - @Override - public void configure() throws Exception { - from("netty4:udp://localhost:{{port}}?sync=true") - .process(new Processor() { - public void process(Exchange exchange) throws Exception { - Poetry poetry = (Poetry) exchange.getIn().getBody(); - poetry.setPoet("Dr. Sarojini Naidu"); - exchange.getOut().setBody(poetry); - } - }); - } - }; - } - -} diff --git a/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/ObjectSerializationTest.java b/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/ObjectSerializationTest.java new file mode 100644 index 0000000..6e07560 --- /dev/null +++ b/components/camel-netty4/src/test/java/org/apache/camel/component/netty4/ObjectSerializationTest.java @@ -0,0 +1,107 @@ +/* + * 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; + +import java.util.Date; +import java.util.Properties; + +import org.apache.camel.CamelExecutionException; +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.netty4.codec.ObjectDecoder; +import org.apache.camel.component.netty4.codec.ObjectEncoder; +import org.apache.camel.impl.JndiRegistry; +import org.apache.camel.test.AvailablePortFinder; +import org.junit.BeforeClass; +import org.junit.Test; + +import io.netty.channel.ChannelHandler; +import io.netty.handler.codec.serialization.ClassResolvers; + +/** + * Object Serialization is not allowed by default. However it can be enabled by adding specific encoders/decoders. + */ +public class ObjectSerializationTest extends BaseNettyTest { + + private static volatile int port2; + + @BeforeClass + public static void initPort2() throws Exception { + port2 = AvailablePortFinder.getNextAvailable(); + } + + @Test + public void testObjectSerializationFailureByDefault() throws Exception { + Date date = new Date(); + try { + template.requestBody("netty4:tcp://localhost:{{port}}?sync=true&encoders=#encoder", date, Date.class); + fail("Should have thrown exception"); + } catch (CamelExecutionException e) { + e.printStackTrace(); + // expected + } + } + + @Test + public void testObjectSerializationAllowedViaDecoder() throws Exception { + Date date = new Date(); + Date receivedDate = template.requestBody("netty4:tcp://localhost:{{port2}}?sync=true&encoders=#encoder&decoders=#decoder", date, Date.class); + assertEquals(date, receivedDate); + } + + @Override + protected JndiRegistry createRegistry() throws Exception { + JndiRegistry jndi = super.createRegistry(); + + jndi.lookup("prop", Properties.class).setProperty("port2", "" + port2); + jndi.bind("encoder", new ShareableChannelHandlerFactory(new ObjectEncoder())); + jndi.bind("decoder", new DefaultChannelHandlerFactory() { + @Override + public ChannelHandler newChannelHandler() { + return new ObjectDecoder(ClassResolvers.weakCachingResolver(null)); + } + }); + + return jndi; + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("netty4:tcp://localhost:{{port}}?sync=true") + .process(new Processor() { + public void process(Exchange exchange) throws Exception { + Object obj = exchange.getIn().getBody(); + exchange.getOut().setBody(obj); + } + }); + + from("netty4:tcp://localhost:{{port2}}?sync=true&decoders=#decoder&encoders=#encoder") + .process(new Processor() { + public void process(Exchange exchange) throws Exception { + Object obj = exchange.getIn().getBody(); + exchange.getOut().setBody(obj); + } + }); + } + }; + } + +} \ No newline at end of file diff --git a/platforms/spring-boot/components-starter/camel-netty4-starter/src/main/java/org/apache/camel/component/netty4/springboot/NettyComponentConfiguration.java b/platforms/spring-boot/components-starter/camel-netty4-starter/src/main/java/org/apache/camel/component/netty4/springboot/NettyComponentConfiguration.java index 6b5137c..1b67fbc 100644 --- a/platforms/spring-boot/components-starter/camel-netty4-starter/src/main/java/org/apache/camel/component/netty4/springboot/NettyComponentConfiguration.java +++ b/platforms/spring-boot/components-starter/camel-netty4-starter/src/main/java/org/apache/camel/component/netty4/springboot/NettyComponentConfiguration.java @@ -149,7 +149,8 @@ public class NettyComponentConfiguration /** * Only used for TCP. If no codec is specified, you can use this flag to * indicate a text line based codec; if not specified or the value is - * false, then Object Serialization is assumed over TCP. + * false, then Object Serialization is assumed over TCP - however only + * Strings are allowed to be serialized by default. */ private Boolean textline = false; /**