Showing posts with label camel. Show all posts
Showing posts with label camel. Show all posts

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







Wednesday, October 29, 2014

Exposing Rest end point using Camel CXFRS

Exposing a Rest end point using Camel cxfrs is quite simple if you know the basics.

I will tell you how to do that in step by step basis:

1) Create a resouce class which act as dummy endpoint to receive your REST,  calling it dummy class as  request as it is basically handeled by Camel Route. It is still needed as that is how camel rest work.


2)  Create a Camel Route to handle the REST Request.  We are going to use cxfrs  for this.

3) Final Step is to using Spring xml based or Java based configuration. for final configuration.

Note :Assumption is you already have all the dependency in your maven pom file  or all the required jars in  your class path. Refer camel cxfrs for more detail

Lets Begin:

1) Creating the Resource class:
-----------------------------------------------------
package com.sj.resources;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
/**
* Rest Resource.
*/
@Path("/data")
public class MyResources {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response getData() throws Exception{
        // Actual response of this method will provided by the Camel route
        return null;
    }
}
----------------------------------------------------------------

2) Create the Camel Route (Which will actually do the request  handling and give back the Response)
 ------------------------------------------------------------------------------
package com.sj.route;                                                                                                                      
                                                                                                                                                         
import org.apache.camel.ExchangePattern;                                                                                    
import org.apache.camel.LoggingLevel;                                                                                         
import org.apache.camel.builder.RouteBuilder;                                                                              
import org.springframework.beans.factory.annotation.Autowired;                                                 
                                                                                                                                                          
/**                                                                                                                                                     
 * This class represent the Route definition                                                                                     
 */                                                                                                                                                      
public class CustomDataRoute extends RouteBuilder {                                                                  

    @Override                                                                                                                                     
    public void configure() throws Exception {                                                                                  

        from("cxfrs:bean:rsServer?bindingStyle=SimpleConsumer")                      
                .setExchangePattern(ExchangePattern.InOut)
                .log(LoggingLevel.INFO,  "Inside the Route  ")
                .process(new Processor() {
                   
                    @Override
                    public void process(Exchange exchange) throws Exception {
                     // this is what will be returned
                     exchange.getIn().setBody("This is the Response Data: Hello Rest");

                    }
                })
                .log(LoggingLevel.INFO,"Exiting the Route ")
                .end();

    }

}
 ---------------------------------------------------------------------------

3) Last and final Step to configure your cxfrs server using your Spring xml.
Let say your spring configuration file name is : camel-server.xml

--------------------------------
       xmlns:cxf="http://camel.apache.org/schema/cxf"
       xmlns:jaxrs="http://cxf.apache.org/jaxrs"
       xsi:schemaLocation="
    ">
 
  <!-- Defined the server endpoint to create the cxf-rs consumer -->
  <cxf:rsServer id="rsServer" address="http://localhost:8080/sj"
      serviceClass="com.sj.resources.MyResources"
      loggingFeatureEnabled="true" 
loggingSizeLimit="20" 
skipFaultLogging="true"/>
 

  
 <bean id="customDataRoute" class="com.sj.route.CustomDataRoute" />
  

<!-- The camel route context -->
<camelContext id="camelContext" xmlns="http://camel.apache.org/schema/spring">
    <routeBuilder ref="customDataRoute" />
</camelContext>
   
</beans>

----------------------------------------------------------------------------------

If you want to  use Java based configuration then it will be something like this :

 @Bean(name = "rsServer")
    JAXRSServerFactoryBean restServer() {
        SpringJAXRSServerFactoryBean sf = new SpringJAXRSServerFactoryBean();
        sf.setServiceBean(Arrays.asList(new MyResources())); // you can add more resource if you want.
        sf.setAddress("http://localhost:8080/sj");
        JacksonJaxbJsonProvider jacksonJaxbJsonProvider= new JacksonJaxbJsonProvider();
        jacksonJaxbJsonProvider.setMapper(new ObjectMapper());
        sf.setProviders(Arrays.asList(jacksonJaxbJsonProvider)); // you can add more provider

        return sf;
    }
 -------------------------------------------------------------------

That is it.  Now you can  deploy your application and

call   http://localhost:8080/sj/data