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 98e27c4f2569 CAMEL-28120: camel-alibaba-oss - close the stream of a
converted putObject body (#31395)
98e27c4f2569 is described below
commit 98e27c4f2569c3613077f74d935837b1f1ec6a40
Author: Saravanakumar Selvaraj <[email protected]>
AuthorDate: Sun Dec 6 02:27:12 2026 +0530
CAMEL-28120: camel-alibaba-oss - close the stream of a converted putObject
body (#31395)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../camel/component/alibaba/oss/OSSProducer.java | 19 ++-
.../component/alibaba/oss/PutObjectStreamTest.java | 143 +++++++++++++++++++++
2 files changed, 157 insertions(+), 5 deletions(-)
diff --git
a/components/camel-alibaba/camel-alibaba-oss/src/main/java/org/apache/camel/component/alibaba/oss/OSSProducer.java
b/components/camel-alibaba/camel-alibaba-oss/src/main/java/org/apache/camel/component/alibaba/oss/OSSProducer.java
index 34ff9a2abbc7..6fe852bc280d 100644
---
a/components/camel-alibaba/camel-alibaba-oss/src/main/java/org/apache/camel/component/alibaba/oss/OSSProducer.java
+++
b/components/camel-alibaba/camel-alibaba-oss/src/main/java/org/apache/camel/component/alibaba/oss/OSSProducer.java
@@ -49,6 +49,7 @@ import
org.apache.camel.component.alibaba.oss.constants.OSSOperations;
import org.apache.camel.component.alibaba.oss.constants.OSSProperties;
import org.apache.camel.component.alibaba.oss.models.ClientConfigurations;
import org.apache.camel.support.DefaultProducer;
+import org.apache.camel.util.IOHelper;
import org.apache.camel.util.ObjectHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -151,11 +152,19 @@ public class OSSProducer extends DefaultProducer {
clientConfigurations.objectName()));
} else {
InputStream is =
exchange.getMessage().getMandatoryBody(InputStream.class);
- requestBuilder.key(clientConfigurations.objectName())
- .body(BinaryData.fromStream(is));
- PutObjectResult result =
ossClient.putObject(requestBuilder.build());
- exchange.getMessage().setBody(toPutObjectMap(result,
clientConfigurations.bucketName(),
- clientConfigurations.objectName()));
+ // the stream that is the body of a file belongs to the consumer
of the file, which closes it
+ boolean closeStream = !(exchange.getMessage().getBody() instanceof
WrappedFile<?> wf && wf.getBody() == is);
+ try {
+ requestBuilder.key(clientConfigurations.objectName())
+ .body(BinaryData.fromStream(is));
+ PutObjectResult result =
ossClient.putObject(requestBuilder.build());
+ exchange.getMessage().setBody(toPutObjectMap(result,
clientConfigurations.bucketName(),
+ clientConfigurations.objectName()));
+ } finally {
+ if (closeStream) {
+ IOHelper.close(is);
+ }
+ }
}
}
diff --git
a/components/camel-alibaba/camel-alibaba-oss/src/test/java/org/apache/camel/component/alibaba/oss/PutObjectStreamTest.java
b/components/camel-alibaba/camel-alibaba-oss/src/test/java/org/apache/camel/component/alibaba/oss/PutObjectStreamTest.java
new file mode 100644
index 000000000000..899c7f0d92d6
--- /dev/null
+++
b/components/camel-alibaba/camel-alibaba-oss/src/test/java/org/apache/camel/component/alibaba/oss/PutObjectStreamTest.java
@@ -0,0 +1,143 @@
+/*
+ * 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.alibaba.oss;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.ConnectException;
+import java.nio.charset.StandardCharsets;
+
+import com.aliyun.sdk.service.oss2.OSSClient;
+import com.aliyun.sdk.service.oss2.exceptions.OperationException;
+import com.aliyun.sdk.service.oss2.models.PutObjectRequest;
+import com.aliyun.sdk.service.oss2.transport.InputStreamBinaryData;
+import com.aliyun.sdk.service.oss2.transport.RequestException;
+import org.apache.camel.BindToRegistry;
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.alibaba.oss.constants.OSSProperties;
+import org.apache.camel.component.file.GenericFile;
+import org.apache.camel.support.TypeConverterSupport;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * The last branch of putObject converts the body to a stream. The HTTP client
closes that stream only when it sends the
+ * body. So the producer closes a stream that the conversion opened, also when
the request fails before the upload. The
+ * stream of a file body, as with streamDownload of camel-ftp, stays open for
the consumer of the file.
+ */
+class PutObjectStreamTest extends CamelTestSupport {
+
+ private final TrackedStream convertedStream = new TrackedStream();
+ private InputStream sentStream;
+ private boolean sentStreamWasOpen;
+
+ @BindToRegistry("ossClient")
+ OSSClient mockClient = Mockito.mock(OSSClient.class);
+
+ @Override
+ protected CamelContext createCamelContext() throws Exception {
+ CamelContext context = super.createCamelContext();
+ // a body that the conversion turns into a new stream, as for a
java.net.URL or a java.nio.file.Path
+ context.getTypeConverterRegistry().addTypeConverter(InputStream.class,
Payload.class, new TypeConverterSupport() {
+ @Override
+ public <T> T convertTo(Class<T> type, Exchange exchange, Object
value) {
+ return type.cast(convertedStream);
+ }
+ });
+ return context;
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ // stream caching would replace a file body with a cache of
its stream, so the route turns it off
+ from("direct:put_object")
+ .streamCache(false)
+ .setHeader(OSSProperties.OBJECT_NAME,
constant("stream_file.txt"))
+
.to("alibaba-oss:test-bucket?operation=putObject&ossClient=#ossClient");
+ }
+ };
+ }
+
+ @Test
+ void convertedStreamIsClosedWhenTheRequestFails() {
+ failTheRequest(convertedStream);
+
+ Exchange exchange = template.send("direct:put_object", e ->
e.getMessage().setBody(new Payload()));
+
+
assertThat(exchange.getException()).isInstanceOf(OperationException.class);
+ assertThat(sentStream).as("the request sends the stream that the
conversion opened").isSameAs(convertedStream);
+ assertThat(sentStreamWasOpen).as("the stream is open when the request
starts").isTrue();
+ assertThat(convertedStream.closed).as("the stream that the conversion
opened is closed").isTrue();
+ }
+
+ @Test
+ void streamOfAFileBodyIsLeftToTheConsumerOfTheFile() {
+ TrackedStream fileStream = new TrackedStream();
+ failTheRequest(fileStream);
+ // a remote file whose body is the stream of the download, as with
streamDownload=true of camel-ftp
+ GenericFile<Object> file = new GenericFile<>();
+ file.setFile(new RemoteFileHandle("stream_file.txt"));
+ file.setBody(fileStream);
+
+ Exchange exchange = template.send("direct:put_object", e ->
e.getMessage().setBody(file));
+
+
assertThat(exchange.getException()).isInstanceOf(OperationException.class);
+ assertThat(sentStream).as("the request sends the stream of the
file").isSameAs(fileStream);
+ assertThat(fileStream.closed).as("the stream of the file body stays
open for the consumer of the file").isFalse();
+ }
+
+ private void failTheRequest(TrackedStream stream) {
+
Mockito.when(mockClient.putObject(Mockito.any(PutObjectRequest.class))).thenAnswer(invocation
-> {
+ PutObjectRequest request = invocation.getArgument(0);
+ sentStream = ((InputStreamBinaryData) request.body()).unwrap();
+ sentStreamWasOpen = !stream.closed;
+ // a failed connection sends no body, so the HTTP client does not
read or close the stream
+ throw new OperationException(
+ "PutObject",
+ new RequestException("Send request raised an exception",
new ConnectException("Connection refused")));
+ });
+ }
+
+ private record Payload() {
+ }
+
+ private record RemoteFileHandle(String name) {
+ }
+
+ private static final class TrackedStream extends ByteArrayInputStream {
+ private volatile boolean closed;
+
+ TrackedStream() {
+ super("a test string".getBytes(StandardCharsets.UTF_8));
+ }
+
+ @Override
+ public void close() throws IOException {
+ closed = true;
+ super.close();
+ }
+ }
+}