Repository: camel Updated Branches: refs/heads/master 85a5bfbf3 -> 3d7f93f24
CAMEL-8978: Setting SOAP header in payload case via camel header Change-Id: Ib8f7aeda34d52e986ab66c21aabf75acb2d53114 Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/3d7f93f2 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/3d7f93f2 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/3d7f93f2 Branch: refs/heads/master Commit: 3d7f93f24c7c86c9ef804b0654c6c46b2be34b10 Parents: 85a5bfb Author: Franz Forsthofer <franz.forstho...@sap.com> Authored: Fri Jul 17 08:04:06 2015 +0200 Committer: Franz Forsthofer <franz.forstho...@sap.com> Committed: Wed Jul 22 15:39:02 2015 +0200 ---------------------------------------------------------------------- .../apache/camel/component/cxf/CxfEndpoint.java | 16 +++- .../component/cxf/CxfPayLoadSoapHeaderTest.java | 43 +++------- .../cxf/CxfPayLoadSoapHeaderTestAbstract.java | 56 +++++++++++++ .../CxfPayLoadSoapHeaderViaCamelHeaderTest.java | 85 ++++++++++++++++++++ 4 files changed, 167 insertions(+), 33 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/3d7f93f2/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfEndpoint.java b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfEndpoint.java index 090e1ca..8ed858e 100644 --- a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfEndpoint.java +++ b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfEndpoint.java @@ -24,6 +24,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; + import javax.xml.namespace.QName; import javax.xml.stream.XMLStreamConstants; import javax.xml.stream.XMLStreamException; @@ -60,6 +61,7 @@ import org.apache.camel.spi.HeaderFilterStrategyAware; import org.apache.camel.spi.UriEndpoint; import org.apache.camel.spi.UriParam; import org.apache.camel.spi.UriPath; +import org.apache.camel.util.CastUtils; import org.apache.camel.util.EndpointHelper; import org.apache.camel.util.ObjectHelper; import org.apache.camel.util.UnsafeUriCharactersEncoder; @@ -1145,7 +1147,19 @@ public class CxfEndpoint extends DefaultEndpoint implements HeaderFilterStrategy } message.setContent(List.class, content); - message.put(Header.HEADER_LIST, payload.getHeaders()); + // merge header list from request context with header list from CXF payload + List<Object> headerListOfRequestContxt = (List<Object>)message.get(Header.HEADER_LIST); + List<Object> headerListOfPayload = CastUtils.cast(payload.getHeaders()); + if (headerListOfRequestContxt == headerListOfPayload) { + // == is correct, we want to compare the object instances + // nothing to do, this can happen when the CXF payload is already created in the from-cxf-endpoint and then forwarded to a to-cxf-endpoint + } else { + if (headerListOfRequestContxt == null) { + message.put(Header.HEADER_LIST, payload.getHeaders()); + } else { + headerListOfRequestContxt.addAll(headerListOfPayload); + } + } } else { super.setParameters(params, message); } http://git-wip-us.apache.org/repos/asf/camel/blob/3d7f93f2/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfPayLoadSoapHeaderTest.java ---------------------------------------------------------------------- diff --git a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfPayLoadSoapHeaderTest.java b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfPayLoadSoapHeaderTest.java index 70b5927..c1a30c2 100644 --- a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfPayLoadSoapHeaderTest.java +++ b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfPayLoadSoapHeaderTest.java @@ -23,7 +23,6 @@ import javax.xml.namespace.QName; import javax.xml.transform.Source; import javax.xml.transform.dom.DOMSource; import javax.xml.ws.BindingProvider; -import javax.xml.ws.Endpoint; import org.w3c.dom.Element; @@ -37,30 +36,13 @@ import org.apache.camel.pizza.types.CallerIDHeaderType; import org.apache.camel.pizza.types.OrderPizzaResponseType; import org.apache.camel.pizza.types.OrderPizzaType; import org.apache.camel.pizza.types.ToppingsListType; -import org.apache.camel.test.junit4.CamelTestSupport; import org.apache.cxf.binding.soap.SoapHeader; - -import org.junit.Before; +import org.apache.cxf.headers.Header; import org.junit.Test; +public class CxfPayLoadSoapHeaderTest extends CxfPayLoadSoapHeaderTestAbstract { -public class CxfPayLoadSoapHeaderTest extends CamelTestSupport { - static int port1 = CXFTestSupport.getPort1(); - static int port2 = CXFTestSupport.getPort2(); private final QName serviceName = new QName("http://camel.apache.org/pizza", "PizzaService"); - - protected String getRouterEndpointURI() { - return "cxf:http://localhost:" + port1 + "/" + getClass().getSimpleName() - + "/pizza_service/services/PizzaService?wsdlURL=classpath:pizza_service.wsdl&dataFormat=PAYLOAD"; - } - protected String getServiceEndpointURI() { - return "cxf:http://localhost:" + port2 + "/" + getClass().getSimpleName() - + "/new_pizza_service/services/PizzaService?wsdlURL=classpath:pizza_service.wsdl&dataFormat=PAYLOAD"; - } - @Override - public boolean isCreateCamelContextPerClass() { - return true; - } @Override protected RouteBuilder createRouteBuilder() { @@ -85,7 +67,15 @@ public class CxfPayLoadSoapHeaderTest extends CamelTestSupport { assertEquals("Get the wrong headers size", headers.size(), 1); assertEquals("Get the wrong namespace URI", ((Element)(headers.get(0).getObject())).getNamespaceURI(), - "http://camel.apache.org/pizza/types"); + "http://camel.apache.org/pizza/types"); + // alternatively you can also get the SOAP header via the camel header: + headers = exchange.getIn().getHeader(Header.HEADER_LIST, List.class); + assertNotNull("We should get the headers here", headers); + assertEquals("Get the wrong headers size", headers.size(), 1); + assertEquals("Get the wrong namespace URI", + ((Element)(headers.get(0).getObject())).getNamespaceURI(), + "http://camel.apache.org/pizza/types"); + } }) @@ -95,17 +85,6 @@ public class CxfPayLoadSoapHeaderTest extends CamelTestSupport { }; } - protected void start(String n) { - Object implementor = new PizzaImpl(); - String address = "http://localhost:" + port2 + "/" + n - + "/new_pizza_service/services/PizzaService"; - Endpoint.publish(address, implementor); - } - - @Before - public void startService() { - start(getClass().getSimpleName()); - } @Test public void testPizzaService() { http://git-wip-us.apache.org/repos/asf/camel/blob/3d7f93f2/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfPayLoadSoapHeaderTestAbstract.java ---------------------------------------------------------------------- diff --git a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfPayLoadSoapHeaderTestAbstract.java b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfPayLoadSoapHeaderTestAbstract.java new file mode 100644 index 0000000..3ffdf38 --- /dev/null +++ b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfPayLoadSoapHeaderTestAbstract.java @@ -0,0 +1,56 @@ +/** + * 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; + +import javax.xml.ws.Endpoint; + +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Before; + + +public class CxfPayLoadSoapHeaderTestAbstract extends CamelTestSupport { + static int port1 = CXFTestSupport.getPort1(); + static int port2 = CXFTestSupport.getPort2(); + + protected String getRouterEndpointURI() { + return "cxf:http://localhost:" + port1 + "/" + getClass().getSimpleName() + + "/pizza_service/services/PizzaService?wsdlURL=classpath:pizza_service.wsdl&dataFormat=PAYLOAD"; + } + protected String getServiceEndpointURI() { + return "cxf:http://localhost:" + port2 + "/" + getClass().getSimpleName() + + "/new_pizza_service/services/PizzaService?wsdlURL=classpath:pizza_service.wsdl&dataFormat=PAYLOAD"; + } + @Override + public boolean isCreateCamelContextPerClass() { + return true; + } + + + protected void start(String n) { + Object implementor = new PizzaImpl(); + String address = "http://localhost:" + port2 + "/" + n + + "/new_pizza_service/services/PizzaService"; + Endpoint.publish(address, implementor); + } + + @Before + public void startService() { + start(getClass().getSimpleName()); + } + + +} http://git-wip-us.apache.org/repos/asf/camel/blob/3d7f93f2/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfPayLoadSoapHeaderViaCamelHeaderTest.java ---------------------------------------------------------------------- diff --git a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfPayLoadSoapHeaderViaCamelHeaderTest.java b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfPayLoadSoapHeaderViaCamelHeaderTest.java new file mode 100644 index 0000000..b368f18 --- /dev/null +++ b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfPayLoadSoapHeaderViaCamelHeaderTest.java @@ -0,0 +1,85 @@ +/** + * 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; + +import java.util.ArrayList; +import java.util.List; + +import javax.xml.namespace.QName; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.pizza.types.CallerIDHeaderType; +import org.apache.cxf.binding.soap.SoapHeader; +import org.apache.cxf.headers.Header; +import org.apache.cxf.jaxb.JAXBDataBinding; +import org.junit.Test; + +public class CxfPayLoadSoapHeaderViaCamelHeaderTest extends CxfPayLoadSoapHeaderTestAbstract { + + protected RouteBuilder createRouteBuilder() throws Exception { + + return new RouteBuilder() { + public void configure() { + // START SNIPPET: payload_soap_header_set + from("direct:start").process(new Processor() { + public void process(Exchange exchange) throws Exception { + CallerIDHeaderType callerId = new CallerIDHeaderType(); + callerId.setName("Willem"); + callerId.setPhoneNumber("108"); + SoapHeader soapHeader = new SoapHeader(new QName("http://camel.apache.org/pizza/types", "CallerIDHeader"), + callerId, new JAXBDataBinding(CallerIDHeaderType.class)); + List<SoapHeader> soapHeaders = new ArrayList<SoapHeader>(1); + soapHeaders.add(soapHeader); + // sets the SOAP header via a camel header + exchange.getIn().setHeader(Header.HEADER_LIST, soapHeaders); + } + + }).to(getServiceEndpointURI()) // + .to("mock:end"); + // END SNIPPET: payload_soap_header_set + } + }; + } + + @Test + public void testCreateSoapHeaderViaCamelHeaderForSoapRequest() throws Exception { + String body = "<OrderRequest xmlns=\"http://camel.apache.org/pizza/types\"><Toppings><Topping>topping_value</Topping></Toppings></OrderRequest>"; + MockEndpoint mock = getMockEndpoint("mock:end"); + mock.expectedMessageCount(1); + sendBody("direct:start", body); + assertMockEndpointsSatisfied(); + Document message = mock.getExchanges().get(0).getIn().getMandatoryBody(Document.class); + Element root = message.getDocumentElement(); + NodeList nodeList = root.getElementsByTagName("MinutesUntilReady"); + assertEquals(1, nodeList.getLength()); + Element elMinutesUntilReady = (Element) nodeList.item(0); + /** + * the phone number 108 which is given in the SOAP header is added to + * 100 which results in 208, see class + * org.apache.camel.component.cxf.PizzaImpl. + */ + assertEquals("208", elMinutesUntilReady.getTextContent()); + } + +}