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 2023/11/19 07:59:44 UTC

(camel) branch camel-4.0.x updated: CAMEL-20124: camel-netty - Fix ChannelHandlerFactories' usage of unsharable ByteArrayDecoder (#12072)

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

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


The following commit(s) were added to refs/heads/camel-4.0.x by this push:
     new 271cdfa6640 CAMEL-20124: camel-netty - Fix ChannelHandlerFactories' usage of unsharable ByteArrayDecoder (#12072)
271cdfa6640 is described below

commit 271cdfa664065460b9ac8b395428cb89734d45f3
Author: Kengo Seki <se...@apache.org>
AuthorDate: Sun Nov 19 16:56:06 2023 +0900

    CAMEL-20124: camel-netty - Fix ChannelHandlerFactories' usage of unsharable ByteArrayDecoder (#12072)
---
 .../component/netty/ChannelHandlerFactories.java   |  7 +-
 .../component/netty/NettyBinaryProxyTest.java      | 77 ++++++++++++++++++++++
 2 files changed, 83 insertions(+), 1 deletion(-)

diff --git a/components/camel-netty/src/main/java/org/apache/camel/component/netty/ChannelHandlerFactories.java b/components/camel-netty/src/main/java/org/apache/camel/component/netty/ChannelHandlerFactories.java
index 7e91ae19b4f..b773b958b48 100644
--- a/components/camel-netty/src/main/java/org/apache/camel/component/netty/ChannelHandlerFactories.java
+++ b/components/camel-netty/src/main/java/org/apache/camel/component/netty/ChannelHandlerFactories.java
@@ -107,7 +107,12 @@ public final class ChannelHandlerFactories {
         if ("udp".equals(protocol)) {
             return new ShareableChannelHandlerFactory(new DatagramPacketByteArrayDecoder());
         } else {
-            return new ShareableChannelHandlerFactory(new ByteArrayDecoder());
+            return new DefaultChannelHandlerFactory() {
+                @Override
+                public ChannelHandler newChannelHandler() {
+                    return new ByteArrayDecoder();
+                }
+            };
         }
     }
 
diff --git a/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyBinaryProxyTest.java b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyBinaryProxyTest.java
new file mode 100644
index 00000000000..d8863272a31
--- /dev/null
+++ b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyBinaryProxyTest.java
@@ -0,0 +1,77 @@
+/*
+ * 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;
+
+import org.apache.camel.BindToRegistry;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.support.DefaultRegistry;
+import org.apache.camel.test.AvailablePortFinder;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+
+public class NettyBinaryProxyTest extends BaseNettyTest {
+
+    @RegisterExtension
+    protected static AvailablePortFinder.Port port2 = AvailablePortFinder.find();
+
+    @BindToRegistry("bytesDecoder")
+    private final ChannelHandlerFactory bytesDecoder = ChannelHandlerFactories.newByteArrayDecoder("tcp");
+
+    @BindToRegistry("bytesEncoder")
+    private final ChannelHandlerFactory bytesEncoder = ChannelHandlerFactories.newByteArrayEncoder("tcp");
+
+    @Test
+    public void testNettyBinaryProxy() throws Exception {
+        getMockEndpoint("mock:before").expectedBodiesReceived("Camel".getBytes());
+        getMockEndpoint("mock:proxy").expectedBodiesReceived("Camel".getBytes());
+        getMockEndpoint("mock:after").expectedBodiesReceived("Camel".getBytes());
+
+        byte[] body = template.requestBody(
+                "netty:tcp://localhost:%s?sync=true&disconnect=true&decoders=#bytesDecoder&encoders=#bytesEncoder"
+                        .formatted(port.getPort()),
+                "Camel".getBytes(), byte[].class);
+        assertArrayEquals("Camel".getBytes(), body);
+
+        MockEndpoint.assertIsSatisfied(context);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                DefaultRegistry registry = (DefaultRegistry) getContext().getRegistry();
+                registry.bind("bytesDecoder", ChannelHandlerFactories.newByteArrayDecoder("tcp"));
+                registry.bind("bytesEncoder", ChannelHandlerFactories.newByteArrayEncoder("tcp"));
+
+                fromF("netty:tcp://0.0.0.0:%s?sync=true&disconnect=true&decoders=#bytesDecoder&encoders=#bytesEncoder",
+                        port.getPort())
+                        .to("mock:before")
+                        .toF("netty:tcp://localhost:%s?sync=true&disconnect=true&decoders=#bytesDecoder&encoders=#bytesEncoder",
+                                port2.getPort())
+                        .to("mock:after");
+
+                fromF("netty:tcp://0.0.0.0:%s?sync=true&disconnect=true&decoders=#bytesDecoder&encoders=#bytesEncoder",
+                        port2.getPort())
+                        .to("mock:proxy");
+            }
+        };
+    }
+}