Sunday, September 20, 2015

Exposing your service as SOAP Enpoint using Apache Camel

Today I will be telling you about how to expose your service as SOAP endpoint using Apache Camel.


The four steps for it.

1) creating of  cxf-server.xml -- which contain detail about your soap endpoint.

<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"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:soap="http://cxf.apache.org/bindings/soap"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                           http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf.xsd
                           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
                           http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
                           http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd">


    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

   
    <!-- Create SOAP endpoint  -->
    <cxf:cxfEndpoint id="mySoapEndPoint"
                     address="http://localhost:{{port}}/test/SOAP"
                     serviceClass="com.test.Broker">
        <cxf:binding>
            <soap:soapBinding version="1.2"/>  <!-- this allow you accept soap 1.2 request, as  by default camel accept only soap 1.1 request-->
        </cxf:binding>
        <cxf:properties>
            <entry key="dataFormat" value="PAYLOAD"/>
        </cxf:properties>
    </cxf:cxfEndpoint>
   
 
   
</beans>


2) Creation of your camel-server.xml -- defining your route.


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:camel="http://camel.apache.org/schema/spring"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                           http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring-2.11.0.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <import resource="classpath:META-INF/cxf/cxf-config.xml" />


<!-- Define your route here -->
    <camel:camelContext id="camel-server">

        <!-- Externally Exposed Routes -->
        <camel:routeBuilder ref="soapHandlerRoute"/>
     
    </camel:camelContext>

<bean id="soapHandlerRoute"  class="com.test.SoapRoute "/>




3)  Define your route -- the actual implementation.

package com.test;

public class SoapRoute extends RouteBuilder {

@Override
    public void configure() throws Exception {

     from("cxf:bean:mySoapEndPoint")
                .routeId("soapRouteId")
                .convertBodyTo(String.class)
                .end(); //  add your logic here to handle the input soap request.

    }


}


4) Define your dummy endpoint broker class


package com.test;

@WebServiceProvider()
@ServiceMode(Mode.PAYLOAD)
public class Broker implements Provider<SOAPMessage>{

    public SOAPMessage invoke(SOAPMessage message) {
        // Requests should not come here as the Camel route will
        // intercept the call before this is invoked.
        throw new UnsupportedOperationException("Placeholder method");
    }
 
}


}


That is it your have exposed your service as SOAP Request and Response.


Refer : cxf-example and cxf.html for more detail