Home » Java » routing – Translating from WSO2 to camel with Java DSL: How to forward with URI pattern-Exceptionshub

routing – Translating from WSO2 to camel with Java DSL: How to forward with URI pattern-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m removing WSO2 from our stack and I have to write in Camel Java DSL the endpoints that were implemented in WSO2.

In WSO2 we had an endpoint as below:

<resource methods="OPTIONS GET" uri-template="/request/{data}" inSequence="requestreset"/>
<http method="GET" uri-template="http://127.0.0.1/index.php?_q=requestreset&amp;data={uri.var.data}"/>

My code in Java Camel’s Router is:


public class DefaultRouteBuilder extends RouteBuilder {

    private HashMap<String, String> routeCorresponding = new HashMap();

    @Override
    public void configure() throws Exception {

        routeCorresponding.put("reset/request/{data}", "http://127.0.0.1/index.php?_q=requestreset&data={data}");

        for (Map.Entry<String, String> pair : routeCorresponding.entrySet()) {
            String url = pair.getKey();
            String target = pair.getValue();

            String resultTarget = target.contains("?") ? target + "&bridgeEndpoint=true" : target + "?bridgeEndpoint=true";

            fromF("servlet:"+ url +"?matchOnUriPrefix=true")
                    .log("Request: ${in.header."+ Exchange.HTTP_METHOD +"} to ${in.header."+ Exchange.HTTP_URI +"}")
                    .toF(resultTarget);
        }

    }

}

But it doesn’t work as I would want it because when I make a request to tomcat.myserver.com:8080/camel-example-servlet/reset/request/blablablablabla I get a response this:
org.apache.camel.http.common.HttpOperationFailedException: HTTP operation failed invoking http://127.0.0.1/index.php/reset/request/blablablablabla?_q=requestreset&data=%7Bdata%7D with statusCode: 404

Instead of http://127.0.0.1/index.php/reset/request/blablablablabla?_q=requestreset&data=%7Bdata%7D, I would like the following request to be on http://127.0.0.1/index.php?_q=requestreset&data=blablablablabla

Is it possible to achieve in Camel/Java DSL that? Basically what WSO2 was implementing with the URI template and the curly brackets around fields?

How to&Answers: