CAMEL-6684 fixed the issue of camel-cxf RAW message data format doesn't support MTOM
Conflicts: components/camel-cxf/src/main/java/org/apache/camel/component/cxf/DefaultCxfBinding.java Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/a14bdbf0 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/a14bdbf0 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/a14bdbf0 Branch: refs/heads/camel-2.10.x Commit: a14bdbf044a600ddd9ab216fd0310e7c28d63d6c Parents: 9f34fa6 Author: Willem Jiang <ningji...@apache.org> Authored: Fri Aug 30 10:56:09 2013 +0800 Committer: Willem Jiang <ningji...@apache.org> Committed: Fri Aug 30 10:56:09 2013 +0800 ---------------------------------------------------------------------- .../camel/component/cxf/DefaultCxfBinding.java | 43 +++++++++- .../cxf/mtom/CxfMtomRouterRawModeTest.java | 46 ++++++++++ .../mtom/CxfMtomRouterRawModeTest-context.xml | 88 ++++++++++++++++++++ 3 files changed, 173 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/a14bdbf0/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/DefaultCxfBinding.java ---------------------------------------------------------------------- diff --git a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/DefaultCxfBinding.java b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/DefaultCxfBinding.java index b9f6524..d849fda 100644 --- a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/DefaultCxfBinding.java +++ b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/DefaultCxfBinding.java @@ -475,7 +475,7 @@ public class DefaultCxfBinding implements CxfBinding, HeaderFilterStrategyAware Map<String, List<String>> cxfHeaders = CastUtils.cast((Map<?, ?>)cxfMessage.get(Message.PROTOCOL_HEADERS)); Map<String, Object> camelHeaders = camelMessage.getHeaders(); camelHeaders.put(CxfConstants.CAMEL_CXF_MESSAGE, cxfMessage); - + if (cxfHeaders != null) { for (Map.Entry<String, List<String>> entry : cxfHeaders.entrySet()) { if (!headerFilterStrategy.applyFilterToExternalHeaders(entry.getKey(), @@ -483,12 +483,19 @@ public class DefaultCxfBinding implements CxfBinding, HeaderFilterStrategyAware // We need to filter the content type with multi-part, // as the multi-part stream is already consumed by AttachmentInInterceptor, // it will cause some trouble when route this message to another CXF endpoint. + if ("Content-Type".compareToIgnoreCase(entry.getKey()) == 0 && entry.getValue().get(0) != null && entry.getValue().get(0).startsWith("multipart/related")) { - String contentType = replaceMultiPartContentType(entry.getValue().get(0)); - LOG.trace("Find the multi-part Conent-Type, and replace it with {}", contentType); - camelHeaders.put(entry.getKey(), contentType); + // We need to keep the Content-Type if the data format is RAW message + DataFormat dataFormat = exchange.getProperty(CxfConstants.DATA_FORMAT_PROPERTY, DataFormat.class); + if (dataFormat.equals(DataFormat.RAW)) { + camelHeaders.put(entry.getKey(), getContentTypeString(entry.getValue())); + } else { + String contentType = replaceMultiPartContentType(entry.getValue().get(0)); + LOG.trace("Find the multi-part Conent-Type, and replace it with {}", contentType); + camelHeaders.put(entry.getKey(), contentType); + } } else { LOG.trace("Populate header from CXF header={} value={}", entry.getKey(), entry.getValue()); @@ -545,6 +552,18 @@ public class DefaultCxfBinding implements CxfBinding, HeaderFilterStrategyAware } return result; } + + protected String getContentTypeString(List<String> values) { + String result = ""; + for (String value : values) { + if (result.length() == 0) { + result = value; + } else { + result = result + "; " + value; + } + } + return result; + } @SuppressWarnings("unchecked") protected void propagateHeadersFromCamelToCxf(Exchange camelExchange, @@ -565,6 +584,8 @@ public class DefaultCxfBinding implements CxfBinding, HeaderFilterStrategyAware if (headers != null) { transportHeaders.putAll(headers); } + + DataFormat dataFormat = camelExchange.getProperty(CxfConstants.DATA_FORMAT_PROPERTY, DataFormat.class); for (Map.Entry<String, Object> entry : camelHeaders.entrySet()) { // put response code in request context so it will be copied to CXF message's property @@ -574,6 +595,20 @@ public class DefaultCxfBinding implements CxfBinding, HeaderFilterStrategyAware continue; } + // We need to copy the content-type if the dataformat is RAW + if (Message.CONTENT_TYPE.equalsIgnoreCase(entry.getKey()) && dataFormat.equals(DataFormat.RAW)) { + LOG.debug("Propagate to CXF header: {} value: {}", Message.CONTENT_TYPE, entry.getValue()); + cxfContext.put(Message.CONTENT_TYPE, entry.getValue().toString()); + continue; + } + + // need to filter the User-Agent ignore the case, as CXF just check the header with "User-Agent" + if (entry.getKey().equalsIgnoreCase("User-Agent")) { + List<String> listValue = new ArrayList<String>(); + listValue.add(entry.getValue().toString()); + transportHeaders.put("User-Agent", listValue); + } + // this header should be filtered, continue to the next header if (headerFilterStrategy.applyFilterToCamelHeaders(entry.getKey(), entry.getValue(), camelExchange)) { continue; http://git-wip-us.apache.org/repos/asf/camel/blob/a14bdbf0/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/mtom/CxfMtomRouterRawModeTest.java ---------------------------------------------------------------------- diff --git a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/mtom/CxfMtomRouterRawModeTest.java b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/mtom/CxfMtomRouterRawModeTest.java new file mode 100644 index 0000000..5ac6d97 --- /dev/null +++ b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/mtom/CxfMtomRouterRawModeTest.java @@ -0,0 +1,46 @@ +/** + * 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.cxf.mtom; + +import java.net.URL; + +import javax.xml.ws.BindingProvider; + +import org.apache.camel.cxf.mtom_feature.Hello; +import org.apache.camel.cxf.mtom_feature.HelloService; +import org.springframework.test.context.ContextConfiguration; + +import static org.junit.Assert.assertNotNull; + + +@ContextConfiguration +public class CxfMtomRouterRawModeTest extends CxfMtomRouterPayloadModeTest { + @Override + protected Hello getPort() { + URL wsdl = getClass().getResource("/mtom.wsdl"); + assertNotNull("WSDL is null", wsdl); + + HelloService service = new HelloService(wsdl, HelloService.SERVICE); + assertNotNull("Service is null ", service); + Hello port = service.getHelloPort(); + ((BindingProvider)port).getRequestContext() + .put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, + "http://localhost:" + port1 + "/CxfMtomRouterRawModeTest/jaxws-mtom/hello"); + return port; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/a14bdbf0/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/mtom/CxfMtomRouterRawModeTest-context.xml ---------------------------------------------------------------------- diff --git a/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/mtom/CxfMtomRouterRawModeTest-context.xml b/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/mtom/CxfMtomRouterRawModeTest-context.xml new file mode 100644 index 0000000..ee8644b --- /dev/null +++ b/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/mtom/CxfMtomRouterRawModeTest-context.xml @@ -0,0 +1,88 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns:cxf="http://camel.apache.org/schema/cxf" + + xsi:schemaLocation=" + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf.xsd + http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd + "> + <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/> + + <!-- START SNIPPET: enableMtom --> + + <cxf:cxfEndpoint id="routerEndpoint" address="http://localhost:${CXFTestSupport.port1}/CxfMtomRouterRawModeTest/jaxws-mtom/hello" + wsdlURL="mtom.wsdl" + serviceName="ns:HelloService" + endpointName="ns:HelloPort" + xmlns:ns="http://apache.org/camel/cxf/mtom_feature"> + + <cxf:properties> + <!-- enable mtom by setting this property to true --> + <entry key="mtom-enabled" value="true"/> + + <!-- set the camel-cxf endpoint data fromat to RAW mode --> + <entry key="dataFormat" value="RAW"/> + </cxf:properties> + + <!-- END SNIPPET: enableMtom --> + +<!-- + <cxf:inInterceptors> + <ref bean="logInbound"/> + </cxf:inInterceptors> + <cxf:outInterceptors> + <ref bean="logOutbound"/> + </cxf:outInterceptors> +--> + </cxf:cxfEndpoint> + + <cxf:cxfEndpoint id="serviceEndpoint" address="http://localhost:${CXFTestSupport.port2}/CxfMtomRouterRawModeTest/jaxws-mtom/hello" + wsdlURL="mtom.wsdl" + serviceName="ns:HelloService" + endpointName="ns:HelloPort" + xmlns:ns="http://apache.org/camel/cxf/mtom_feature"> + + <cxf:properties> + <entry key="mtom-enabled" value="true"/> + <entry key="dataFormat" value="RAW"/> + </cxf:properties> + +<!-- + <cxf:inInterceptors> + <ref bean="logInbound"/> + </cxf:inInterceptors> + <cxf:outInterceptors> + <ref bean="logOutbound"/> + </cxf:outInterceptors> +--> + </cxf:cxfEndpoint> + + <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring"> + <route> + <from uri="cxf:bean:routerEndpoint" /> + <to uri="cxf:bean:serviceEndpoint" /> + </route> + </camelContext> + + <bean id="logOutbound" class="org.apache.cxf.interceptor.LoggingOutInterceptor" /> + <bean id="logInbound" class="org.apache.cxf.interceptor.LoggingInInterceptor"/> + +</beans> \ No newline at end of file