Repository: camel Updated Branches: refs/heads/master aa89a5b20 -> d787ad2dc
CAMEL-8103: Make CxfEndpoint CamelContextAware Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/d787ad2d Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/d787ad2d Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/d787ad2d Branch: refs/heads/master Commit: d787ad2dc7ee03d5209a1cbfd63bdbef9ddc430d Parents: aa89a5b Author: Jochen Cordes <jcor...@redhat.com> Authored: Tue Dec 2 08:47:14 2014 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Tue Dec 2 11:00:15 2014 +0100 ---------------------------------------------------------------------- .../apache/camel/impl/DefaultCamelContext.java | 5 ++ .../apache/camel/component/cxf/CxfEndpoint.java | 3 +- .../cxf/CxfJavaOnlyCamelContextAwareTest.java | 88 ++++++++++++++++++++ 3 files changed, 95 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/d787ad2d/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java b/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java index a12f088..4ee7115 100644 --- a/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java +++ b/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java @@ -2362,6 +2362,11 @@ public class DefaultCamelContext extends ServiceSupport implements ModelCamelCon addStartupListener(listener); } + if (service instanceof CamelContextAware) { + CamelContextAware aware = (CamelContextAware) service; + aware.setCamelContext(this); + } + service.start(); } http://git-wip-us.apache.org/repos/asf/camel/blob/d787ad2d/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 dffc1d7..e6abc2a 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 @@ -43,6 +43,7 @@ import org.w3c.dom.Element; import org.w3c.dom.Node; import org.apache.camel.CamelContext; +import org.apache.camel.CamelContextAware; import org.apache.camel.CamelException; import org.apache.camel.Consumer; import org.apache.camel.Processor; @@ -112,7 +113,7 @@ import org.slf4j.LoggerFactory; * mode is {@link DataFormat#POJO}. */ @UriEndpoint(scheme = "cxf", consumerClass = CxfConsumer.class, label = "http,soap,webservice") -public class CxfEndpoint extends DefaultEndpoint implements HeaderFilterStrategyAware, Service, Cloneable { +public class CxfEndpoint extends DefaultEndpoint implements CamelContextAware, HeaderFilterStrategyAware, Service, Cloneable { private static final Logger LOG = LoggerFactory.getLogger(CxfEndpoint.class); http://git-wip-us.apache.org/repos/asf/camel/blob/d787ad2d/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfJavaOnlyCamelContextAwareTest.java ---------------------------------------------------------------------- diff --git a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfJavaOnlyCamelContextAwareTest.java b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfJavaOnlyCamelContextAwareTest.java new file mode 100644 index 0000000..317ee9b --- /dev/null +++ b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfJavaOnlyCamelContextAwareTest.java @@ -0,0 +1,88 @@ +/** + * 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.namespace.QName; +import org.w3c.dom.Document; +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.CamelContextAware; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Test; + + +/** + * A unit test for java only CXF in payload mode + * + * @version + */ +public class CxfJavaOnlyCamelContextAwareTest extends CamelTestSupport { + private static int port1 = CXFTestSupport.getPort1(); + + @Test + public void testCxfEndpointHasCamelContext() throws Exception { + String s = "<GetPerson xmlns=\"http://camel.apache.org/wsdl-first/types\"><personId>123</personId></GetPerson>"; + Document xml = context.getTypeConverter().convertTo(Document.class, s); + + System.out.println("Endpoints: " + context.getEndpoints()); + Object output = template.requestBody("personService", xml); + assertNotNull(output); + + // using CxfPayload in payload mode + CxfPayload<?> payload = (CxfPayload<?>) output; + + // convert the payload body to string + String reply = context.getTypeConverter().convertTo(String.class, payload.getBody().get(0)); + assertNotNull(reply); + + assertTrue(reply.contains("<personId>123</personId")); + assertTrue(reply.contains("<ssn>456</ssn")); + assertTrue(reply.contains("<name>Donald Duck</name")); + + assertTrue(context.getEndpoint("personService") instanceof CamelContextAware); + assertNotNull("CamelContext should be set on CxfEndpoint", context.getEndpoint("personService").getCamelContext()); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + CxfEndpoint endpoint = new CxfEndpoint(); + endpoint.setAddress("http://localhost:" + port1 + "/PersonService"); + endpoint.setServiceName(new QName("http://camel.apache.org/wsdl-first","PersonService")); + endpoint.setPortName(new QName("http://camel.apache.org/wsdl-first","soap")); + endpoint.setWsdlURL("classpath:person.wsdl"); + endpoint.setDataFormat(DataFormat.PAYLOAD); + context.addEndpoint("personService", endpoint); + + from(endpoint).process(new Processor() { + @Override + public void process(Exchange exchange) throws Exception { + String s = "<GetPersonResponse xmlns=\"http://camel.apache.org/wsdl-first/types\">" + + "<personId>123</personId><ssn>456</ssn><name>Donald Duck</name>" + + "</GetPersonResponse>"; + + Document xml = context.getTypeConverter().convertTo(Document.class, s); + exchange.getOut().setBody(xml); + } + }); + } + }; + } +}