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

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


The following commit(s) were added to refs/heads/main by this push:
     new 1c8e8061c2b1 CAMEL-25573: camel-netty-http - Keep the InputStream 
contract in the request body stream cache (#27757)
1c8e8061c2b1 is described below

commit 1c8e8061c2b1d7018ac44db199fd0488ac57d9ae
Author: allthingssecurity <[email protected]>
AuthorDate: Tue Oct 13 12:25:14 2026 +0530

    CAMEL-25573: camel-netty-http - Keep the InputStream contract in the 
request body stream cache (#27757)
    
    Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 components/camel-netty-http/pom.xml                |   5 +
 .../netty/http/NettyChannelBufferStreamCache.java  |  17 ++-
 .../http/NettyChannelBufferStreamCacheTest.java    | 154 +++++++++++++++++++++
 .../netty/http/NettyHttpGzipRequestBodyTest.java   | 120 ++++++++++++++++
 4 files changed, 290 insertions(+), 6 deletions(-)

diff --git a/components/camel-netty-http/pom.xml 
b/components/camel-netty-http/pom.xml
index df4a9954bbc6..c124d9e7ba11 100644
--- a/components/camel-netty-http/pom.xml
+++ b/components/camel-netty-http/pom.xml
@@ -135,6 +135,11 @@
             <artifactId>camel-jaxb</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-zip-deflater</artifactId>
+            <scope>test</scope>
+        </dependency>
         <dependency>
             <groupId>org.apache.camel</groupId>
             <artifactId>camel-openapi-java</artifactId>
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
index 1f22f51e0418..9f7305ff5939 100644
--- 
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
@@ -46,14 +46,20 @@ public final class NettyChannelBufferStreamCache extends 
InputStream implements
         this.buffer.markReaderIndex();
     }
 
+    /**
+     * Marking is not supported: {@link #mark(int)} does nothing, and {@link 
#reset()} always goes back to the start of
+     * the body, which is what {@link StreamCache#reset()} is for. A reader 
that needs mark support has to wrap this
+     * stream (for example in a {@link java.io.BufferedInputStream}), and so 
cannot change where a reset goes back to.
+     */
     @Override
     public boolean markSupported() {
-        return true;
+        return false;
     }
 
     @Override
     public int read() {
-        return buffer.readByte();
+        // an unsigned byte, and -1 at the end, as InputStream requires
+        return buffer.isReadable() ? buffer.readByte() & 0xFF : -1;
     }
 
     @Override
@@ -64,17 +70,16 @@ public final class NettyChannelBufferStreamCache extends 
InputStream implements
     @Override
     public int read(byte[] b, int off, int len) {
         // are we at end, then return -1
-        if (buffer.readerIndex() == buffer.capacity()) {
+        int max = buffer.readableBytes();
+        if (max == 0) {
             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;
+        return len;
     }
 
     @Override
diff --git 
a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyChannelBufferStreamCacheTest.java
 
b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyChannelBufferStreamCacheTest.java
new file mode 100644
index 000000000000..b25bbcb719fe
--- /dev/null
+++ 
b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyChannelBufferStreamCacheTest.java
@@ -0,0 +1,154 @@
+/*
+ * 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.ByteArrayOutputStream;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.util.Random;
+import java.util.zip.GZIPInputStream;
+import java.util.zip.GZIPOutputStream;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+
+/**
+ * {@link NettyChannelBufferStreamCache} read as a plain {@link 
java.io.InputStream}.
+ */
+class NettyChannelBufferStreamCacheTest {
+
+    @Test
+    void readReturnsEveryByteAsUnsignedValue() throws IOException {
+        byte[] data = { 0x00, 0x7F, (byte) 0x80, (byte) 0xFF, 0x01 };
+        NettyChannelBufferStreamCache cache = new 
NettyChannelBufferStreamCache(Unpooled.wrappedBuffer(data));
+        try {
+            assertEquals(0x00, cache.read());
+            assertEquals(0x7F, cache.read());
+            assertEquals(0x80, cache.read());
+            assertEquals(0xFF, cache.read());
+            assertEquals(0x01, cache.read());
+            assertEquals(-1, cache.read());
+        } finally {
+            release(cache);
+        }
+    }
+
+    @Test
+    void readLoopCopiesBinaryContent() throws IOException {
+        byte[] data = { 0x10, 0x20, (byte) 0xFF, 0x30, 0x40 };
+        NettyChannelBufferStreamCache cache = new 
NettyChannelBufferStreamCache(Unpooled.wrappedBuffer(data));
+        try {
+            ByteArrayOutputStream out = new ByteArrayOutputStream();
+            int b;
+            while ((b = cache.read()) != -1) {
+                out.write(b);
+            }
+            assertArrayEquals(data, out.toByteArray());
+        } finally {
+            release(cache);
+        }
+    }
+
+    @Test
+    void readLoopEndsAtTheEndOfText() throws IOException {
+        NettyChannelBufferStreamCache cache
+                = new 
NettyChannelBufferStreamCache(Unpooled.copiedBuffer("Hello", 
StandardCharsets.UTF_8));
+        try {
+            StringBuilder sb = new StringBuilder();
+            int b;
+            while ((b = cache.read()) != -1) {
+                sb.append((char) b);
+            }
+            assertEquals("Hello", sb.toString());
+        } finally {
+            release(cache);
+        }
+    }
+
+    @Test
+    void markIsNotSupported() {
+        NettyChannelBufferStreamCache cache
+                = new NettyChannelBufferStreamCache(Unpooled.wrappedBuffer(new 
byte[] { 1, 2, 3 }));
+        try {
+            assertFalse(cache.markSupported());
+        } finally {
+            release(cache);
+        }
+    }
+
+    @Test
+    void resetAndWriteToReturnTheWholeBodyAfterAReaderThatMarks() throws 
IOException {
+        // larger than the 8192 byte buffer that the gzip reader of 
commons-compress marks and reads
+        byte[] data = new byte[20000];
+        new Random(42).nextBytes(data);
+        NettyChannelBufferStreamCache cache = new 
NettyChannelBufferStreamCache(Unpooled.wrappedBuffer(data));
+        try {
+            byte[] buf = new byte[8192];
+            do {
+                cache.mark(buf.length);
+            } while (cache.read(buf) != -1);
+
+            cache.reset();
+            assertArrayEquals(data, cache.readAllBytes());
+
+            ByteArrayOutputStream out = new ByteArrayOutputStream();
+            cache.writeTo(out);
+            assertArrayEquals(data, out.toByteArray());
+        } finally {
+            release(cache);
+        }
+    }
+
+    @Test
+    void readStopsAtTheLastWrittenByte() throws IOException {
+        // the capacity (16) is larger than the content (3 bytes)
+        ByteBuf buffer = Unpooled.buffer(16);
+        buffer.writeBytes(new byte[] { 1, 2, 3 });
+        NettyChannelBufferStreamCache cache = new 
NettyChannelBufferStreamCache(buffer);
+        try {
+            assertArrayEquals(new byte[] { 1, 2, 3 }, cache.readAllBytes());
+            assertEquals(-1, cache.read(new byte[4], 0, 4));
+        } finally {
+            release(cache);
+        }
+    }
+
+    @Test
+    void gzipInputStreamReadsItsHeader() throws IOException {
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        try (GZIPOutputStream gzip = new GZIPOutputStream(bos)) {
+            gzip.write("Hello World".getBytes(StandardCharsets.UTF_8));
+        }
+        NettyChannelBufferStreamCache cache = new 
NettyChannelBufferStreamCache(Unpooled.wrappedBuffer(bos.toByteArray()));
+        try (GZIPInputStream gzip = new GZIPInputStream(cache)) {
+            assertEquals("Hello World", new String(gzip.readAllBytes(), 
StandardCharsets.UTF_8));
+        } finally {
+            release(cache);
+        }
+    }
+
+    private static void release(NettyChannelBufferStreamCache cache) {
+        // once for the buffer that the test created and once for the retain 
of the cache
+        cache.release();
+        cache.release();
+    }
+}
diff --git 
a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpGzipRequestBodyTest.java
 
b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpGzipRequestBodyTest.java
new file mode 100644
index 000000000000..bae37cdd1e71
--- /dev/null
+++ 
b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpGzipRequestBodyTest.java
@@ -0,0 +1,120 @@
+/*
+ * 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.ByteArrayOutputStream;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.Random;
+import java.util.zip.GZIPOutputStream;
+
+import org.apache.camel.StreamCache;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * The request body of a netty-http consumer is a {@link 
NettyChannelBufferStreamCache}; a route reads it as an
+ * {@link InputStream}.
+ */
+class NettyHttpGzipRequestBodyTest extends BaseNettyTestSupport {
+
+    @Test
+    void gzipDataFormatUnmarshalsTheRequestBody() throws Exception {
+        String out = 
template.requestBody("netty-http:http://localhost:{{port}}/unmarshal";,
+                gzip("Hello World".getBytes(StandardCharsets.UTF_8)), 
String.class);
+        assertEquals("Hello World", out);
+    }
+
+    @Test
+    void resetReturnsTheWholeRequestBodyAfterTheGzipUnmarshal() throws 
Exception {
+        byte[] gzip = gzip(largeData());
+        byte[] out = 
template.requestBody("netty-http:http://localhost:{{port}}/reset";, gzip, 
byte[].class);
+        assertArrayEquals(gzip, out);
+    }
+
+    @Test
+    void writeToReturnsTheWholeRequestBodyAfterTheGzipUnmarshal() throws 
Exception {
+        byte[] gzip = gzip(largeData());
+        byte[] out = 
template.requestBody("netty-http:http://localhost:{{port}}/writeTo";, gzip, 
byte[].class);
+        assertArrayEquals(gzip, out);
+    }
+
+    @Test
+    void processorReadsTheRequestBodyByteByByte() {
+        byte[] binary = { 0x10, 0x20, (byte) 0xFF, 0x30, 0x40 };
+        String out = 
template.requestBody("netty-http:http://localhost:{{port}}/count";, binary, 
String.class);
+        assertEquals("5", out);
+    }
+
+    private static byte[] largeData() {
+        // random bytes do not compress: the gzip body (20028 bytes) is larger 
than the 8192 byte buffer of the gzip
+        // reader
+        byte[] data = new byte[20000];
+        new Random(42).nextBytes(data);
+        return data;
+    }
+
+    private static byte[] gzip(byte[] data) throws Exception {
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        try (GZIPOutputStream gzip = new GZIPOutputStream(bos)) {
+            gzip.write(data);
+        }
+        return bos.toByteArray();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                from("netty-http:http://0.0.0.0:{{port}}/unmarshal";)
+                        .unmarshal().gzipDeflater()
+                        .convertBodyTo(String.class, "UTF-8");
+
+                // the compressed request body is kept and read again after 
the unmarshal has read it
+                from("netty-http:http://0.0.0.0:{{port}}/reset";)
+                        .setProperty("compressed", body())
+                        .unmarshal().gzipDeflater()
+                        .process(exchange -> {
+                            StreamCache cache = 
exchange.getProperty("compressed", StreamCache.class);
+                            cache.reset();
+                            exchange.getMessage().setBody(((InputStream) 
cache).readAllBytes());
+                        });
+
+                // the type converter from a stream cache to byte[] calls 
writeTo
+                from("netty-http:http://0.0.0.0:{{port}}/writeTo";)
+                        .setProperty("compressed", body())
+                        .unmarshal().gzipDeflater()
+                        .setBody(exchangeProperty("compressed"))
+                        .convertBodyTo(byte[].class);
+
+                from("netty-http:http://0.0.0.0:{{port}}/count";)
+                        .process(exchange -> {
+                            InputStream is = 
exchange.getIn().getBody(InputStream.class);
+                            int count = 0;
+                            while (is.read() != -1) {
+                                count++;
+                            }
+                            
exchange.getMessage().setBody(String.valueOf(count));
+                        });
+            }
+        };
+    }
+}

Reply via email to