Instead of using choice you can use recipientList and then build the URL on the 
fly. This will take care of at least resolving which REST service to invoke.

Your route  will look something like this.

from("servlet:///?matchOnUriPrefix=true")
           .process(customProcessor)
           .recipientList(header("restUrl"))



Custom processor can be modified to build the restUrl. Though following example 
values are hardcoded, you can make these generic/configurable.
In this example, there is just one pattern but you can have many patterns and 
decide which ones match and then proceed.



                String urlReceived = "/client/1232/?resourceId=3243";

                String patternString = "/(\\w*)/(\\d*)/\\?(\\w*)=(\\w*)";

                Pattern pattern = Pattern.compile(patternString);

                Matcher matcher = pattern.matcher(urlReceived);

                if ( matcher.matches()) {
                        System.out.println(matcher.group(1) + ":" + 
matcher.group(3)+":"+matcher.group(4));
                }

           // build your urlToSend from above matcher values.

                exchange.getIn().setHeader("restUrl", urlToSend);



-Ravi



-----Original Message-----
From: mnusry [mailto:mnu...@gmail.com]
Sent: Tuesday, January 28, 2014 7:37 AM
To: users@camel.apache.org
Subject: Routing on REST URL patterns

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.
This e-mail and any files transmitted with it are for the sole use of the 
intended recipient(s) and may contain confidential and privileged information. 
If you are not the intended recipient(s), please reply to the sender and 
destroy all copies of the original message. Any unauthorized review, use, 
disclosure, dissemination, forwarding, printing or copying of this email, 
and/or any action taken in reliance on the contents of this e-mail is strictly 
prohibited and may be unlawful.

Reply via email to