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 2024/09/05 06:59:16 UTC
(camel) 01/01: CAMEL-21162: camel-http - Http producer should capture response input stream using more standard approach in Camel to ensure stream caching works with spool to disk.
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch http-spool
in repository https://gitbox.apache.org/repos/asf/camel.git
commit c9de7b5214dc1104f730be8151d2e0fa74ec2ffc
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Sep 5 08:58:57 2024 +0200
CAMEL-21162: camel-http - Http producer should capture response input stream using more standard approach in Camel to ensure stream caching works with spool to disk.
---
.../apache/camel/component/http/HttpProducer.java | 25 ++----
.../http/HttpStreamCacheNoSpoolToDiskTest.java | 91 ++++++++++++++++++++++
.../http/HttpStreamCacheSpoolToDiskTest.java | 90 +++++++++++++++++++++
3 files changed, 186 insertions(+), 20 deletions(-)
diff --git a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
index 4004e680800..34faa6b1cc6 100644
--- a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
+++ b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
@@ -42,7 +42,6 @@ import org.apache.camel.RuntimeCamelException;
import org.apache.camel.TypeConverter;
import org.apache.camel.component.file.GenericFile;
import org.apache.camel.component.http.helper.HttpMethodHelper;
-import org.apache.camel.converter.stream.CachedOutputStream;
import org.apache.camel.http.base.HttpOperationFailedException;
import org.apache.camel.http.base.cookie.CookieHandler;
import org.apache.camel.http.common.HttpHelper;
@@ -54,6 +53,7 @@ import org.apache.camel.support.GZIPHelper;
import org.apache.camel.support.MessageHelper;
import org.apache.camel.support.ObjectHelper;
import org.apache.camel.support.SynchronizationAdapter;
+import org.apache.camel.support.builder.OutputStreamBuilder;
import org.apache.camel.support.http.HttpUtil;
import org.apache.camel.util.IOHelper;
import org.apache.camel.util.URISupport;
@@ -585,27 +585,12 @@ public class HttpProducer extends DefaultProducer {
}
}
- private InputStream doExtractResponseBodyAsStream(InputStream is, Exchange exchange) throws IOException {
+ private Object doExtractResponseBodyAsStream(InputStream is, Exchange exchange) throws IOException {
// As httpclient is using a AutoCloseInputStream, it will be closed when the connection is closed
// we need to cache the stream for it.
- CachedOutputStream cos = null;
- try {
- // This CachedOutputStream will not be closed when the exchange is onCompletion
- cos = new CachedOutputStream(exchange, false);
- IOHelper.copy(is, cos);
- // When the InputStream is closed, the CachedOutputStream will be closed
- return cos.getWrappedInputStream();
- } catch (IOException ex) {
- // try to close the CachedOutputStream when we get the IOException
- try {
- cos.close();
- } catch (IOException ignore) {
- //do nothing here
- }
- throw ex;
- } finally {
- IOHelper.close(is, "Extracting response body", LOG);
- }
+ OutputStreamBuilder osb = OutputStreamBuilder.withExchange(exchange);
+ IOHelper.copy(is, osb);
+ return osb.build();
}
/**
diff --git a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpStreamCacheNoSpoolToDiskTest.java b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpStreamCacheNoSpoolToDiskTest.java
new file mode 100644
index 00000000000..03ddaf91a4e
--- /dev/null
+++ b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpStreamCacheNoSpoolToDiskTest.java
@@ -0,0 +1,91 @@
+/*
+ * 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.http;
+
+import java.io.File;
+
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.http.handler.BasicValidationHandler;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.spi.StreamCachingStrategy;
+import org.apache.camel.util.FileUtil;
+import org.apache.hc.core5.http.impl.bootstrap.HttpServer;
+import org.apache.hc.core5.http.impl.bootstrap.ServerBootstrap;
+import org.junit.jupiter.api.Test;
+
+import static org.apache.camel.http.common.HttpMethods.GET;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class HttpStreamCacheNoSpoolToDiskTest extends BaseHttpTest {
+
+ private HttpServer localServer;
+
+ @Override
+ public void setupResources() throws Exception {
+ localServer = ServerBootstrap.bootstrap().setHttpProcessor(getBasicHttpProcessor())
+ .setConnectionReuseStrategy(getConnectionReuseStrategy()).setResponseFactory(getHttpResponseFactory())
+ .setSslContext(getSSLContext())
+ .register("/test/", new BasicValidationHandler(GET.name(), null, null, getExpectedContent())).create();
+ localServer.start();
+
+ FileUtil.removeDir(new File("target/camel-cache"));
+ }
+
+ @Override
+ public void cleanupResources() throws Exception {
+ if (localServer != null) {
+ localServer.stop();
+ }
+ }
+
+ @Test
+ public void httpNoSpoolToDisk() throws Exception {
+ MockEndpoint result = getMockEndpoint("mock:result");
+ result.expectedBodiesReceived("camel rocks!");
+ result.expectedFileExists("target/output/rock.txt");
+
+ Object out = template.requestBody("direct:start", (String) null);
+ assertEquals("camel rocks!", context.getTypeConverter().convertTo(String.class, out));
+
+ result.assertIsSatisfied();
+ }
+
+ @Override
+ protected RoutesBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ StreamCachingStrategy streamCachingStrategy = context.getStreamCachingStrategy();
+ streamCachingStrategy.setSpoolDirectory("target/camel-cache");
+ streamCachingStrategy.setSpoolThreshold(5000); // spool to disk to high so content stays in memory
+ streamCachingStrategy.setSpoolEnabled(true);
+ streamCachingStrategy.setBufferSize(4096);
+
+ from("direct:start").streamCache("true")
+ .to("http://localhost:" + localServer.getLocalPort() + "/test/?disableStreamCache=true")
+ .process(e -> {
+ // should not be temp spool file
+ int files = new File("target/camel-cache").list().length;
+ assertEquals(0, files);
+ })
+ .to("file:target/output?fileName=rock.txt")
+ .to("mock:result");
+ }
+ };
+ }
+}
diff --git a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpStreamCacheSpoolToDiskTest.java b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpStreamCacheSpoolToDiskTest.java
new file mode 100644
index 00000000000..f202856d858
--- /dev/null
+++ b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpStreamCacheSpoolToDiskTest.java
@@ -0,0 +1,90 @@
+/*
+ * 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.http;
+
+import java.io.File;
+
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.http.handler.BasicValidationHandler;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.spi.StreamCachingStrategy;
+import org.apache.camel.util.FileUtil;
+import org.apache.hc.core5.http.impl.bootstrap.HttpServer;
+import org.apache.hc.core5.http.impl.bootstrap.ServerBootstrap;
+import org.junit.jupiter.api.Test;
+
+import static org.apache.camel.http.common.HttpMethods.GET;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class HttpStreamCacheSpoolToDiskTest extends BaseHttpTest {
+
+ private HttpServer localServer;
+
+ @Override
+ public void setupResources() throws Exception {
+ localServer = ServerBootstrap.bootstrap().setHttpProcessor(getBasicHttpProcessor())
+ .setConnectionReuseStrategy(getConnectionReuseStrategy()).setResponseFactory(getHttpResponseFactory())
+ .setSslContext(getSSLContext())
+ .register("/test/", new BasicValidationHandler(GET.name(), null, null, getExpectedContent())).create();
+ localServer.start();
+
+ FileUtil.removeDir(new File("target/camel-cache"));
+ }
+
+ @Override
+ public void cleanupResources() throws Exception {
+ if (localServer != null) {
+ localServer.stop();
+ }
+ }
+
+ @Test
+ public void httpSpoolToDisk() throws Exception {
+ MockEndpoint result = getMockEndpoint("mock:result");
+ result.expectedBodiesReceived("camel rocks!");
+ result.expectedFileExists("target/output/rock.txt");
+
+ template.requestBody("direct:start", (String) null);
+
+ result.assertIsSatisfied();
+ }
+
+ @Override
+ protected RoutesBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ StreamCachingStrategy streamCachingStrategy = context.getStreamCachingStrategy();
+ streamCachingStrategy.setSpoolDirectory("target/camel-cache");
+ streamCachingStrategy.setSpoolThreshold(1); // spool to disk
+ streamCachingStrategy.setSpoolEnabled(true);
+ streamCachingStrategy.setBufferSize(4096);
+
+ from("direct:start").streamCache("true")
+ .to("http://localhost:" + localServer.getLocalPort() + "/test/?disableStreamCache=true")
+ .process(e -> {
+ // should be temp spool file
+ int files = new File("target/camel-cache").list().length;
+ assertEquals(1, files);
+ })
+ .to("file:target/output?fileName=rock.txt")
+ .to("mock:result");
+ }
+ };
+ }
+}