camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mnusry <mnu...@gmail.com>
Subject Routing on REST URL patterns
Date Tue, 28 Jan 2014 02:06:32 GMT
I am struggling to get the following going,

I want to route REST url patterns to different services based on the URL
pattern, 
Ideally I would like to, based on different REST url patterns combine
results from different services as well and 
forward the result to the calling service.

The part I am struggling is where I need to find the URL of the incoming
request (CamelHttpUri) and match it against different patterns to identify
which REST service should be invoked based on the URL and forward it to that
REST service and return the returned result. 
(Simple URLS without parameters are fine, but if parameters/query parameter
are included this doesn't work)

eg: i have modules as follows, 
Service			REST Urls
Clint Service      -  /client/1232/?resourceId=1222, /client/1 , 
/client/32342/resource, /patner/add, /client/add
Resource Service    - /resource/1, /resource/add, /resource/2342/client,
/resource/23432/?clientId=2342
Finance Service     - /price/resource/2, /resourceprice/32323?clientId=109

so different services have different REST patterns, Currently I can handle
static REST patterns, but when parameters or query parameters get introduced
it seems I need to find a different strategy.

When the request comes to the router module based on the Request URL it has
to be routed to the relevant service,
I should be able to add more REST urls to the relevant module later as well.

I have done as follows for simple REST URLs but for
"/client/1232/?resourceId=3243" how do I match and forward to relevant
module ?
Is there a better way of doing this ?

I have RouteBuilder and a CustomProcessor which does the following,

CustomProcessor -->
public class CustomProcessor implements Processor {
	
    public void process(Exchange exchange) throws Exception {

	// Note: the last uRL pattern doesnt work
        String a = "/client/,/patner/add,
/client/add,/client/*/?resourceId=*"; 
        String b = System.getProperty("B_REST_URLS");
        String c = System.getProperty("C_REST_URLS");
               
        exchange.getIn().setHeader("ARestURN", a);
        exchange.getIn().setHeader("BRestURN", b);
        exchange.getIn().setHeader("CRestURN", c);
        
        exchange.getOut().setHeaders(exchange.getIn().getHeaders());
        exchange.getOut().setBody(exchange.getIn().getBody(String.class),
String.class);
    }
}

RouteBuilder class -->
    @Override
    public void configure() throws Exception {
        String AUrl = System.getProperty("A_URL");
        String BUrl = System.getProperty("B_URL");
        String CUrl = System.getProperty("C_URL");
        
        from("servlet:///?matchOnUriPrefix=true")
           .process(customProcessor)
            .choice()
      		.when(simple("${in.headers.ARestURN} contains
${in.headers.CamelHttpUri}"))
       			.to("http4://" + AUrl +
"?bridgeEndpoint=true&throwExceptionOnFailure=false")
       		.when(simple("${in.headers.BRestURN} contains
${in.headers.CamelHttpUri}"))
       			.to("http4://" + BUrl +
"?bridgeEndpoint=true&throwExceptionOnFailure=false")    
       		.when(simple("${in.headers.CRestURN} contains
${in.headers.CamelHttpUri}"))
       			.to("http4://" + CUrl +
"?bridgeEndpoint=true&throwExceptionOnFailure=false")       			                 	
                 .otherwise()
       			.to("http4://" + DUrl +
"?bridgeEndpoint=true&throwExceptionOnFailure=false");
    }

Note System properties are defined so that it is easy to add more url
patterns and make things configurable a little.









--
View this message in context: http://camel.465427.n5.nabble.com/Routing-on-REST-URL-patterns-tp5746514.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Mime
View raw message