[CAMEL-11564]avoid ClassCastException when the gzip is enabled for the cxf endpoint with camel destination
(cherry picked from commit d154650b293b8158de8cb7a19c4fcbc25ba67130)
(cherry picked from commit 3a738454069a1bdbeb02df2a55aa6855a48296e2)
(cherry picked from commit d4d985294a610750e60d804dcedd3a81083ba055)
diff --git a/components/camel-cxf-transport/src/main/java/org/apache/camel/component/cxf/transport/CamelDestination.java b/components/camel-cxf-transport/src/main/java/org/apache/camel/component/cxf/transport/CamelDestination.java
index ad02956..2d38c18 100644
--- a/components/camel-cxf-transport/src/main/java/org/apache/camel/component/cxf/transport/CamelDestination.java
+++ b/components/camel-cxf-transport/src/main/java/org/apache/camel/component/cxf/transport/CamelDestination.java
@@ -277,9 +277,12 @@
if (checkException && exception != null) {
camelExchange.setException(exception);
}
-
- CachedOutputStream outputStream = (CachedOutputStream)outMessage.getContent(OutputStream.class);
- camelExchange.getOut().setBody(outputStream.getInputStream());
+ OutputStream outputStream = outMessage.getContent(OutputStream.class);
+ if (outputStream instanceof CachedOutputStream) {
+ camelExchange.getOut().setBody(((CachedOutputStream)outputStream).getInputStream());
+ } else {
+ camelExchange.getOut().setBody(outputStream);
+ }
LOG.debug("send the response message: {}", outputStream);
}
diff --git a/components/camel-cxf-transport/src/test/java/org/apache/camel/component/cxf/transport/JaxWSCamelDestinationTest.java b/components/camel-cxf-transport/src/test/java/org/apache/camel/component/cxf/transport/JaxWSCamelDestinationTest.java
index f745868..6ca7f6d 100644
--- a/components/camel-cxf-transport/src/test/java/org/apache/camel/component/cxf/transport/JaxWSCamelDestinationTest.java
+++ b/components/camel-cxf-transport/src/test/java/org/apache/camel/component/cxf/transport/JaxWSCamelDestinationTest.java
@@ -21,6 +21,8 @@
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
+import org.apache.cxf.transport.common.gzip.GZIPOutInterceptor;
+
import org.junit.After;
import org.junit.Test;
@@ -63,4 +65,19 @@
assertTrue(exchange.getOut().getBody(String.class).indexOf("something!") > 0);
}
+ @Test
+ public void testDestinationWithGzip() {
+ // publish the endpoint
+ endpoint = publishSampleWSWithGzipEnabled("direct:endpoint");
+ Exchange exchange = template.request("direct:start", new Processor() {
+
+ @Override
+ public void process(Exchange exchange) throws Exception {
+ exchange.getIn().setBody(REQUEST);
+ exchange.getIn().setHeader("Accept-Encoding", "gzip");
+ }
+
+ });
+ assertThat(exchange.getOut().getHeader(Exchange.CONTENT_ENCODING, String.class), is("gzip"));
+ }
}
diff --git a/components/camel-cxf-transport/src/test/java/org/apache/camel/component/cxf/transport/JaxWSCamelTestSupport.java b/components/camel-cxf-transport/src/test/java/org/apache/camel/component/cxf/transport/JaxWSCamelTestSupport.java
index 090ee43..40acf59 100644
--- a/components/camel-cxf-transport/src/test/java/org/apache/camel/component/cxf/transport/JaxWSCamelTestSupport.java
+++ b/components/camel-cxf-transport/src/test/java/org/apache/camel/component/cxf/transport/JaxWSCamelTestSupport.java
@@ -31,6 +31,7 @@
import org.apache.camel.test.junit4.CamelTestSupport;
import org.apache.cxf.Bus;
import org.apache.cxf.BusFactory;
+import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.junit.Before;
@@ -136,5 +137,18 @@
return Endpoint.publish("camel://" + camelEndpoint, new SampleWSImpl());
}
+
+ /**
+ * Create a SampleWS Server with Gzip enabled to a specified route
+ * @param camelEndpoint
+ */
+
+ public Endpoint publishSampleWSWithGzipEnabled(String camelEndpoint) {
+ EndpointImpl endpoint = (EndpointImpl)Endpoint.publish("camel://" + camelEndpoint, new SampleWSImpl());
+ endpoint.getInInterceptors().add(new org.apache.cxf.transport.common.gzip.GZIPInInterceptor());
+ endpoint.getOutInterceptors().add(new org.apache.cxf.transport.common.gzip.GZIPOutInterceptor(0));
+ return endpoint;
+
+ }
}