[camel] branch master updated: CAMEL-12714 - support handlers in cxf payload mode without SEI (#2457)

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[camel] branch master updated: CAMEL-12714 - support handlers in cxf payload mode without SEI (#2457)

davsclaus-2
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new 88d04ae  CAMEL-12714 - support handlers in cxf payload mode without SEI (#2457)
88d04ae is described below

commit 88d04ae1d3568df33c9e1819d5fa53133c7daae5
Author: Peter Van den Bosch <[hidden email]>
AuthorDate: Wed Aug 8 10:03:56 2018 +0200

    CAMEL-12714 - support handlers in cxf payload mode without SEI (#2457)
---
 .../apache/camel/component/cxf/CxfEndpoint.java    |  46 +++++-----
 .../camel/component/cxf/CxfSpringEndpoint.java     | 100 +++++++--------------
 .../component/cxf/WSDLServiceFactoryBean.java      |   4 +-
 .../cxf/CxfPayloadWsdlWithoutSEITest.java          |  70 +++++++++++++++
 .../component/cxf/CxfPayloadWsdlWithoutSEI.xml     |  55 ++++++++++++
 5 files changed, 179 insertions(+), 96 deletions(-)

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 58051c2..8f521e7 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
@@ -439,7 +439,7 @@ public class CxfEndpoint extends DefaultEndpoint implements AsyncEndpoint, Heade
     protected void setupHandlers(ClientFactoryBean factoryBean, Client client)
         throws Exception {
 
-        if (factoryBean instanceof JaxWsClientFactoryBean && handlers != null) {
+        if (handlers != null) {
             AnnotationHandlerChainBuilder
                 builder = new AnnotationHandlerChainBuilder();
             Method m = factoryBean.getClass().getMethod("getServiceFactory");
@@ -629,28 +629,28 @@ public class CxfEndpoint extends DefaultEndpoint implements AsyncEndpoint, Heade
             }
         }
 
-        Class<?> cls = null;
-        if (getServiceClass() != null) {
-            cls = getServiceClass();
+        Class<?> cls = getServiceClass();
+        ClientFactoryBean factoryBean;
+        if (cls != null) {
             // create client factory bean
-            ClientFactoryBean factoryBean = createClientFactoryBean(cls);
-            // setup client factory bean
-            setupClientFactoryBean(factoryBean, cls);
-            Client client = factoryBean.create();
-            // setup the handlers
-            setupHandlers(factoryBean, client);
-            return client;
+            factoryBean = createClientFactoryBean(cls);
         } else {
-            // create the client without service class
-
-            checkName(getPortName(), "endpoint/port name");
-            checkName(getServiceName(), "service name");
-
-            ClientFactoryBean factoryBean = createClientFactoryBean();
-            // setup client factory bean
-            setupClientFactoryBean(factoryBean, null);
-            return factoryBean.create();
+            factoryBean = createClientFactoryBean();
+        }
+        
+        // setup client factory bean
+        setupClientFactoryBean(factoryBean, cls);
+        
+        if (cls == null) {
+            checkName(factoryBean.getEndpointName(), "endpoint/port name");
+            checkName(factoryBean.getServiceName(), "service name");
         }
+        
+        Client client = factoryBean.create();
+
+        // setup the handlers
+        setupHandlers(factoryBean, client);
+        return client;
     }
 
     void checkName(Object value, String name) {
@@ -688,11 +688,7 @@ public class CxfEndpoint extends DefaultEndpoint implements AsyncEndpoint, Heade
         if (cls == null) {
             checkName(portName, " endpoint/port name");
             checkName(serviceName, " service name");
-            answer = new JaxWsServerFactoryBean(new WSDLServiceFactoryBean()) {
-                {
-                    doInit = false;
-                }
-            };
+            answer = new JaxWsServerFactoryBean(new WSDLServiceFactoryBean());
             cls = Provider.class;
         } else if (CxfEndpointUtils.hasWebServiceAnnotation(cls)) {
             answer = new JaxWsServerFactoryBean();
diff --git a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfSpringEndpoint.java b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfSpringEndpoint.java
index cc67660..0671b16 100644
--- a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfSpringEndpoint.java
+++ b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfSpringEndpoint.java
@@ -47,23 +47,6 @@ public class CxfSpringEndpoint extends CxfEndpoint implements ApplicationContext
     public CxfSpringEndpoint() {
     }
 
-    /**
-     *
-     * A help to get the service class.  The serviceClass classname in URI
-     * query takes precedence over the serviceClass in CxfEndpointBean.
-     */
-    private Class<?> getSEIClass() throws ClassNotFoundException {
-        
-        // get service class
-        Class<?> answer = null;
-        if (getServiceClass() != null) {
-            // classname is specified in URI which overrides the bean properties
-            answer = getServiceClass();
-        }
-        return answer;
-    }
-    
-
     // Package private methods
     // -------------------------------------------------------------------------
 
@@ -74,7 +57,7 @@ public class CxfSpringEndpoint extends CxfEndpoint implements ApplicationContext
     Client createClient() throws Exception {
         
         // get service class
-        Class<?> cls = getSEIClass();    
+        Class<?> cls = getServiceClass();    
         
         if (getDataFormat().equals(DataFormat.POJO)) {
             ObjectHelper.notNull(cls, CxfConstants.SERVICE_CLASS);
@@ -88,61 +71,44 @@ public class CxfSpringEndpoint extends CxfEndpoint implements ApplicationContext
             if (getDataFormat().equals(DataFormat.PAYLOAD)) {
                 setSkipPayloadMessagePartCheck(true);
             }
-            cls = getSEIClass();
+            cls = getServiceClass();
         }
         
+        ClientFactoryBean factoryBean;
         if (cls != null) {
             // create client factory bean
-            ClientFactoryBean factoryBean = createClientFactoryBean(cls);
-
-            // setup client factory bean
-            setupClientFactoryBean(factoryBean, cls);
-
-            // fill in values that have not been filled.
-            QName serviceQName = null;
-            try {
-                serviceQName = factoryBean.getServiceName();
-            } catch (IllegalStateException e) {
-                // It throws IllegalStateException if serviceName has not been set.
-            }
+            factoryBean = createClientFactoryBean(cls);
+        } else {
+            factoryBean = createClientFactoryBean();
+        }
 
-            if (serviceQName == null && getServiceLocalName() != null) {
-                factoryBean.setServiceName(new QName(getServiceNamespace(), getServiceLocalName()));
-            }
-            if (factoryBean.getEndpointName() == null && getEndpointLocalName() != null) {
-                factoryBean.setEndpointName(new QName(getEndpointNamespace(), getEndpointLocalName()));
-            }
+        // setup client factory bean
+        setupClientFactoryBean(factoryBean, cls);
 
-            Client client = factoryBean.create();
-            // setup the handlers
-            setupHandlers(factoryBean, client);
-            return client;
-        } else {
-            
-            ClientFactoryBean factoryBean = createClientFactoryBean();
+        // fill in values that have not been filled.
+        QName serviceQName = null;
+        try {
+            serviceQName = factoryBean.getServiceName();
+        } catch (IllegalStateException e) {
+            // It throws IllegalStateException if serviceName has not been set.
+        }
 
-            // setup client factory bean
-            setupClientFactoryBean(factoryBean, null);
-            
-            // fill in values that have not been filled.
-            QName serviceQName = null;
-            try {
-                serviceQName = factoryBean.getServiceName();
-            } catch (IllegalStateException e) {
-                // It throws IllegalStateException if serviceName has not been set.
-            }
-            
-            if (serviceQName == null && getServiceLocalName() != null) {
-                factoryBean.setServiceName(new QName(getServiceNamespace(), getServiceLocalName()));
-            }
-            if (factoryBean.getEndpointName() == null && getEndpointLocalName() != null) {
-                factoryBean.setEndpointName(new QName(getEndpointNamespace(), getEndpointLocalName()));
-            }
-            
+        if (serviceQName == null && getServiceLocalName() != null) {
+            factoryBean.setServiceName(new QName(getServiceNamespace(), getServiceLocalName()));
+        }
+        if (factoryBean.getEndpointName() == null && getEndpointLocalName() != null) {
+            factoryBean.setEndpointName(new QName(getEndpointNamespace(), getEndpointLocalName()));
+        }
+        
+        if (cls == null) {
             checkName(factoryBean.getEndpointName(), "endpoint/port name");
             checkName(factoryBean.getServiceName(), "service name");
-            return factoryBean.create();
         }
+
+        Client client = factoryBean.create();
+        // setup the handlers
+        setupHandlers(factoryBean, client);
+        return client;
     }
 
 
@@ -153,7 +119,7 @@ public class CxfSpringEndpoint extends CxfEndpoint implements ApplicationContext
     ServerFactoryBean createServerFactoryBean() throws Exception  {
         
         // get service class
-        Class<?> cls = getSEIClass();                
+        Class<?> cls = getServiceClass();                
         
         if (getWsdlURL() == null && cls == null) {
             // no WSDL and serviceClass specified, set our default serviceClass
@@ -170,11 +136,7 @@ public class CxfSpringEndpoint extends CxfEndpoint implements ApplicationContext
 
         if (cls == null) {
             if (!getDataFormat().equals(DataFormat.POJO)) {
-                answer = new JaxWsServerFactoryBean(new WSDLServiceFactoryBean()) {
-                    {
-                        doInit = false;
-                    }
-                };
+                answer = new JaxWsServerFactoryBean(new WSDLServiceFactoryBean());
                 cls = Provider.class;
             } else {
                 ObjectHelper.notNull(cls, CxfConstants.SERVICE_CLASS);
diff --git a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/WSDLServiceFactoryBean.java b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/WSDLServiceFactoryBean.java
index 4b5d1b2..254d997 100644
--- a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/WSDLServiceFactoryBean.java
+++ b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/WSDLServiceFactoryBean.java
@@ -26,8 +26,8 @@ import javax.xml.ws.Provider;
 import org.apache.camel.RuntimeCamelException;
 import org.apache.cxf.endpoint.Endpoint;
 import org.apache.cxf.endpoint.EndpointException;
-import org.apache.cxf.endpoint.EndpointImpl;
 import org.apache.cxf.helpers.CastUtils;
+import org.apache.cxf.jaxws.support.JaxWsEndpointImpl;
 import org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean;
 import org.apache.cxf.service.factory.FactoryBeanListener.Event;
 import org.apache.cxf.service.invoker.Invoker;
@@ -102,7 +102,7 @@ public class WSDLServiceFactoryBean extends JaxWsServiceFactoryBean {
         super.buildServiceFromWSDL(url);
     }
     public Endpoint createEndpoint(EndpointInfo ei) throws EndpointException {
-        Endpoint ep = new EndpointImpl(getBus(), getService(), ei);
+        Endpoint ep = new JaxWsEndpointImpl(getBus(), getService(), ei);
         sendEvent(Event.ENDPOINT_CREATED, ei, ep, getServiceClass());
         return ep;
     }
diff --git a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfPayloadWsdlWithoutSEITest.java b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfPayloadWsdlWithoutSEITest.java
new file mode 100644
index 0000000..da19e09
--- /dev/null
+++ b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfPayloadWsdlWithoutSEITest.java
@@ -0,0 +1,70 @@
+/**
+ * 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.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.component.cxf.common.message.CxfConstants;
+import org.apache.camel.wsdl_first.PersonImpl;
+import org.apache.cxf.binding.soap.SoapFault;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+public class CxfPayloadWsdlWithoutSEITest extends AbstractCxfWsdlFirstTest {
+
+    @BeforeClass
+    public static void startService() {
+        Object implementor = new PersonImpl();
+        String address = "http://localhost:" + getPort1() + "/CxfPayloadWsdlWithoutSEITest/PersonService/";
+        Endpoint.publish(address, implementor);
+    }
+
+    @Override
+    protected ClassPathXmlApplicationContext createApplicationContext() {
+        return new ClassPathXmlApplicationContext("org/apache/camel/component/cxf/CxfPayloadWsdlWithoutSEI.xml");
+    }
+
+    @Test
+    @Override
+    public void testInvokingServiceWithCamelProducer() {
+        Exchange exchange = sendJaxWsMessage("hello");
+        assertEquals("The request should be handled sucessfully ", exchange.isFailed(), false);
+        org.apache.camel.Message out = exchange.getOut();
+        String result =  out.getBody(String.class);
+        assertStringContains(result, "Bonjour");
+
+        exchange = sendJaxWsMessage("");
+        assertEquals("We should get a fault here", exchange.isFailed(), true);
+        Throwable ex = exchange.getException();
+        assertTrue("We should get a SoapFault here", ex instanceof SoapFault);
+    }
+
+    private Exchange sendJaxWsMessage(final String personIdString) {
+        Exchange exchange = template.send("direct:producer", new Processor() {
+            public void process(final Exchange exchange) {
+                String body = "<GetPerson xmlns=\"http://camel.apache.org/wsdl-first/types\"><personId>" + personIdString + "</personId></GetPerson>\n";
+                exchange.getIn().setBody(body);
+                exchange.getIn().setHeader(CxfConstants.OPERATION_NAME, "GetPerson");
+            }
+        });
+        return exchange;
+    }
+
+}
diff --git a/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/CxfPayloadWsdlWithoutSEI.xml b/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/CxfPayloadWsdlWithoutSEI.xml
new file mode 100644
index 0000000..fa60edd
--- /dev/null
+++ b/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/CxfPayloadWsdlWithoutSEI.xml
@@ -0,0 +1,55 @@
+<?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:camel="http://camel.apache.org/schema/spring"
+    xmlns:cxf="http://camel.apache.org/schema/cxf"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"/>
+    
+    <cxf:cxfEndpoint
+        address="<a href="http://localhost:$">http://localhost:${CXFTestSupport.port2}/CxfPayloadWsdlWithoutSEITest/PersonService/"
+        endpointName="person:soap" id="routerEndpoint"
+        serviceName="person:PersonService"
+        wsdlURL="classpath:person.wsdl" xmlns:person="http://camel.apache.org/wsdl-first">
+        <cxf:properties>
+            <entry key="schema-validation-enabled" value="true"/>
+        </cxf:properties>
+        <cxf:handlers>
+            <ref bean="fromEndpointJaxwsHandler"/>
+        </cxf:handlers>
+    </cxf:cxfEndpoint>
+    
+    <cxf:cxfEndpoint
+        address="<a href="http://localhost:$">http://localhost:${CXFTestSupport.port1}/CxfPayloadWsdlWithoutSEITest/PersonService/"
+        endpointName="person:soap" id="serviceEndpoint"
+        serviceName="person:PersonService" xmlns:person="http://camel.apache.org/wsdl-first"
+        wsdlURL="classpath:person.wsdl">
+        <cxf:handlers>
+            <ref bean="toEndpointJaxwsHandler"/>
+        </cxf:handlers>
+    </cxf:cxfEndpoint>
+    
+    <bean class="org.apache.camel.builder.NoErrorHandlerBuilder" id="noErrorHandler"/>
+    
+    <bean class="org.apache.camel.wsdl_first.JaxwsTestHandler" id="fromEndpointJaxwsHandler"/>
+    <bean class="org.apache.camel.wsdl_first.JaxwsTestHandler" id="toEndpointJaxwsHandler"/>
+    
+    <camelContext errorHandlerRef="noErrorHandler" id="camel" xmlns="http://camel.apache.org/schema/spring">
+        <route>
+            <from uri="cxf:bean:routerEndpoint?dataFormat=PAYLOAD"/>
+            <to uri="cxf:bean:serviceEndpoint?dataFormat=PAYLOAD"/>
+        </route>
+        <route>
+            <from uri="direct:producer"/>
+            <to uri="cxf:bean:serviceEndpoint?dataFormat=PAYLOAD"/>
+        </route>
+    </camelContext>
+    
+</beans>