You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by dvsridhar <da...@cgifederal.com> on 2012/03/05 20:25:31 UTC

RESTful service proxy ..

I have an use case to support where the camel route has to act as a proxy to
a restful service implementation. 

Camel route (interface based resource implementation) will conduct common
auth, authorization, other common task and then call the REST full
implementation.

Using the BookStore example I was able to create a Proxy route that goes to
the route builder. However i was not able to call to the remote
implementation using both http or proxy.

Do we have any example for such scenario?

I went through the following the following link
http://camel.apache.org/cxfrs.html and used "How to invoke the REST service
through camel-cxfrs producer" demonstration to emulate this implementation.
However this example kind of test scenario and uses the template approach to
send information to the end point. Since my implementation is based on war,
I don't have the producer template available. I tried using @EndpointInject,
but had some issues with it. 

*I am wondering what's the right approach for this kind of routing.*


*Find the attached for complete implementation of my project. 
http://camel.465427.n5.nabble.com/file/n5538594/CamelTests.zip
CamelTests.zip *

In the following route builder code, please find the commented code for both
http and proxy implementations. None of them are invoking the remote
service. The only i could invoke the remote service is using the
JAXRSClientFactory which part of commented junk operation. I am not sure if
that is the right approach.

*BookStoreProxyRoute 
----------------------
*
package camelinaction;

import org.apache.camel.Exchange;
import org.apache.camel.ExchangePattern;
import org.apache.camel.Message;
import org.apache.camel.Processor;
import org.apache.camel.builder.NoErrorHandlerBuilder;
import org.apache.camel.builder.RouteBuilder;

import camelinaction.rs.bookstore.Book;
import camelinaction.rs.bookstore.BookStoreImpl;

public class BookStoreProxyRoute extends RouteBuilder {
	private String CXF_RS_ENDPOINT_URI =
"cxfrs://http://localhost:8080/BookStoreRouterTest/rest?resourceClasses=" +
BookStoreImpl.class.getName();	
	
/*	@EndpointInject(uri="")
	ProducerTemplate template;
*/	
	@Override
	public void configure() throws Exception {
		 errorHandler(new NoErrorHandlerBuilder());

		 from(CXF_RS_ENDPOINT_URI)
	            .process(new Processor() {
	                public void process(Exchange exchange) throws Exception {
	                	System.out.println("*************** In BookStoreProxyRoute
process method**********************");
	    		        exchange.setPattern(ExchangePattern.InOut);
	    		        Message inMessage = exchange.getIn();

	    		        /**
	    		         * Http Client Approach
	    		         */

/*	    		        inMessage.setHeader(Exchange.HTTP_URL,
"http://davenkat-d23898:8080/BookStoreRS-1.0.0/bookstorers");
	    		        inMessage.setHeader(Exchange.HTTP_METHOD, "GET");
	    		        inMessage.setHeader(Exchange.HTTP_PATH, "/bookstore");
	    		        inMessage.setBody("101");	    		        
	    		        
	    		        inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API,
Boolean.TRUE);	
	    		        inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_RESPONSE_CLASS,
Book.class);
*/
	    		        /**
	    		         * Proxy Approach
	    		         */
/*	    		        inMessage.setHeader(Exchange.HTTP_URL,
"http://davenkat-d23898:8080/BookStoreRS-1.0.0/bookstorers");	    		        
	    		        inMessage.setHeader(CxfConstants.OPERATION_NAME, "getBook");
	    		        inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API,
Boolean.FALSE);
	    		        inMessage.setBody("101");
	    		        inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_RESPONSE_CLASS,
Book.class);	    		        
*/	    		        Book response = (Book) exchange.getOut().getBody();
	                	System.out.println("*************** In BookStoreProxyRoute
process Ending!!!!!!!!!!!!!!!");	    				
	                }
	            });
	}
	
/*	private void junk()throws Exception{
		ResponseReader reader = new ResponseReader();
		reader.setEntityClass(Book.class);
		BookStore bs =
JAXRSClientFactory.create("http://davenkat-d23898:8080/BookStoreRS-1.0.0/bookstorers",
BookStore.class,Collections.singletonList(reader));		
		Long l=new Long(101);
		Book book = bs.getBook(l);
		if(book!=null){
			System.out.println(book.getName());
		}else{
			System.out.println("No Book Found !!!!!!!!");			
		}
	}
*/	
}

*Following the is the spring configuration sample
---------------------
*
<?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:util="http://www.springframework.org/schema/util"
xmlns:jaxrs="http://cxf.apache.org/jaxrs" 
	xmlns:cxf="http://camel.apache.org/schema/cxf"
	xsi:schemaLocation="
         http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		 http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.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">

	
	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
	<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/>
	<util:list id="jsonTypes">
		<value>application/json</value>
	</util:list>
    <bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/>
	
	<cxf:cxfEndpoint id="inventoryEndpoint"
address="http://localhost:8080/CxfWsTest/inventory" 
		serviceClass="camelinaction.inventory.InventoryEndpoint"
loggingFeatureEnabled="true" />

	<util:map id="jsonNamespaceMap" map-class="java.util.Hashtable">
		
	</util:map>

	<util:list id="jsonKeys">
		<value>books</value>
	</util:list>
	<bean id="jsonProvider" class="org.apache.cxf.jaxrs.provider.JSONProvider">
		<property name="namespaceMap" ref="jsonNamespaceMap" />

		<property name="serializeAsArray" value="true" />
		

		<property name="produceMediaTypes" ref="jsonTypes" />
		<property name="consumeMediaTypes" ref="jsonTypes" />
	</bean>

	
	<bean id="inventoryService" class="camelinaction.InventoryService" />
	
	<bean id="inventoryRoute" class="camelinaction.InventoryRoute" />
	<bean id="customerRoute" class="camelinaction.CustomerRoute" />	
 	<bean id="bookStoreRoute" class="camelinaction.BookStoreProxyRoute" />	
 	<bean id="bookStoreImpl" class="camelinaction.rs.bookstore.BookStoreImpl"
/>

	
	<camel:camelContext>
		<camel:routeBuilder ref="inventoryRoute" />
		<camel:routeBuilder ref="customerRoute" />		
 		<camel:routeBuilder ref="bookStoreRoute" />		
	</camel:camelContext>

	<camel:camelContext id="cxfrs_context">
		<camel:package>camelinaction.rs.test</camel:package>
	</camel:camelContext>

	<jaxrs:server id="restService"
address="http://localhost:7181/CxfRsRouterTest/rest" 
staticSubresourceResolution="true">
		<jaxrs:serviceBeans>
			<ref bean="customerServiceImpl" />
		</jaxrs:serviceBeans>
		<jaxrs:extensionMappings>
			<entry key="xml" value="application/xml" />
			<entry key="json" value="application/json"/>			
		</jaxrs:extensionMappings>
	</jaxrs:server>
	<bean id="customerServiceImpl"
class="camelinaction.rs.test.CustomerService" />
  <cxf:rsServer id="custServer1"
address="http://localhost:8080/CxfRsRouterTest/route"
    serviceClass="camelinaction.rs.test.CustomerService" 
    loggingFeatureEnabled="true" loggingSizeLimit="20"/>

  <cxf:rsClient id="custClient1"
address="http://localhost:7181/CxfRsRouterTest/rest"
serviceClass="camelinaction.rs.test.CustomerService"
    loggingFeatureEnabled="true" />
  
  <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
    <route>
       <from uri="cxfrs://bean://custServer1"/>
       <setHeader headerName="CamelCxfRsUsingHttpAPI">
         <constant>True</constant>        
       </setHeader>
       <to uri="cxfrs://bean://custClient1"/>
    </route>
    <route>
       <from uri="cxfrs://bean://bookStoreServer"/>
       <setHeader headerName="CamelCxfRsUsingHttpAPI">
         <constant>False</constant>        
       </setHeader>
       <to uri="cxfrs://bean://bookStoreClient"/>
    </route>
  </camelContext>	
  
  <jaxrs:server id="bookService"
address="http://localhost:8080/BookStoreRouterTest/rest" 
staticSubresourceResolution="true"> 
		<jaxrs:serviceBeans>
			<ref bean="bookStoreImpl" />
		</jaxrs:serviceBeans>
  </jaxrs:server>

  <cxf:rsServer id="bookStoreServer"
address="http://localhost:8080/BookStoreRouterTest/route"
    serviceClass="camelinaction.rs.bookstore.BookStoreImpl" 
    loggingFeatureEnabled="true" loggingSizeLimit="20"/>

  <cxf:rsClient id="bookStoreClient"
address="http://localhost:8080/BookStoreRouterTest/rest" 
    serviceClass="camelinaction.rs.bookstore.BookStore"
inheritHeaders="true"
    loggingFeatureEnabled="true" />
</beans>

--
View this message in context: http://camel.465427.n5.nabble.com/RESTful-service-proxy-tp5538594p5538594.html
Sent from the Camel - Users mailing list archive at Nabble.com.