Hi,


I’m migrating several (around 20-30) Camel REST DSL projects from Camel 2.17 to 
3.4. The projects use a similar solution pattern to route incoming calls to 
backend systems with a similar URL as the Camel REST endpoints. E.g. when 
calling:

http://localhost:8080/services/rest/dummy/lorem/ipsum/123?dummyQueryParam=abc

the calls are routed to:

http://dummyHost:8181/dummySystem/lorem/ipsum/123?dummyQueryParam=abc<http://dummyhost:8181/dummySystem/lorem/ipsum/123?dummyQueryParam=abc>

(the parts that differs are the host, port and beginning of the path)



We use the following REST DSL configuration:

restConfiguration()

    .component("servlet")

    .skipBindingOnErrorCode(true)

    .endpointProperty("servletName", "CamelServletDummyName");



and specifies the context root in WEB-INF/jboss-web.xml. E.g:

<context-root>services/rest/dummy</context-root>



Then we used the following pattern to create the Camel routes which route the 
calls:

rest().tag("Dummy")

    .get("lorem/ipsum/{id}")

        .route()

            .toD("http4://{{baseURLPropertyName}}?bridgeEndpoint=true")

            // or (as a workaround if no path parameter is included in the 
path):

            .toD("http4://{{baseURLPropertyName}}"+ 
"${header.CamelServletContextPath}?bridgeEndpoint=true")

            // normally, we may also perform transformations etc. here

        .endRest()



”baseURLPropertyName” represents a property and different properties are used 
for each Camel project to define the replacement part of the URL (e.g. 
”dummyHost:8181/dummySystem”).



When I started to migrate, I used a similar approach as mentioned above (I have 
replaced the usage of properties with hard coded values to simplify the 
following examples):

rest().tag("Dummy")

    .get("test/oldSolution/{somePathParam}")

        .route()

            .to(log("Old solution (with path param)").showHeaders(true))

            
.toD("http://dummyHost:8181/dummySystem?bridgeEndpoint=true<http://dummyhost:8181/dummySystem?bridgeEndpoint=true>")

            // called: 
http://dummyHost:8181/dummySystem<http://dummyhost:8181/dummySystem>

        .endRest()

    .get("test/oldSolution")

        .route()

            .to(log("Old solution (without path param)").showHeaders(true))

            // adding CamelServletContextPath

            
.toD("http://dummyHost:8181/dummySystem${header.CamelServletContextPath}?bridgeEndpoint=true<http://dummyhost:8181/dummySystem$%7Bheader.CamelServletContextPath%7D?bridgeEndpoint=true>")

            // called: 
http://dummyHost:8181/dummySystem/test/oldSolution<http://dummyhost:8181/dummySystem/test/oldSolution>

        .endRest()



As you can see from the comments above, the path with a path parameter loses 
”/test/oldSolution/xyz?dummyQueryParam=abc” when calling the backend.



I have found 3 alternative ways to solve this:



// --- Alternative A

   .get("test/altA/{somePathParam}")

        .route()

            .to(log("Alternative A (with path param)").showHeaders(true))

            // this is needed or query parameters will disappear

            .setHeader(Exchange.HTTP_RAW_QUERY, header(Exchange.HTTP_QUERY))

            
.toD(http("dummyHost:8181/dummySystem${header.CamelHttpPath}").bridgeEndpoint(true))

            // called: 
http://dummyHost:8181/dummySystem/test/altA/xyz?dummyQueryParam=abc<http://dummyhost:8181/dummySystem/test/altA/xyz?dummyQueryParam=abc>

        .endRest()

    .get("test/altA")

        .route()

            .to(log("Alternative A (without path param)").showHeaders(true))

            // this is needed or query parameters will disappear

            .setHeader(Exchange.HTTP_RAW_QUERY, header(Exchange.HTTP_QUERY))

            // CamelHttpPath is empty, using CamelServletContextPath instead

            
.toD(http("dummyHost:8181/dummySystem${header.CamelServletContextPath}").bridgeEndpoint(true))

            // called: 
http://dummyHost:8181/dummySystem/test/altA?dummyQueryParam=abc<http://dummyhost:8181/dummySystem/test/altA?dummyQueryParam=abc>

        .endRest()



// --- Alternative B

    .get("test/altB/{somePathParam}")

        .route()

            .to(log("Alternative B (with path param)").showHeaders(true))

            .setHeader(Exchange.HTTP_PATH, 
ExpressionBuilder.append(constant("dummySystem"), header(Exchange.HTTP_PATH)))

            .toD(http("dummyHost:8181").bridgeEndpoint(true))

            // called: 
http://dummyHost:8181/dummySystem/test/altB/xyz?dummyQueryParam=abc<http://dummyhost:8181/dummySystem/test/altB/xyz?dummyQueryParam=abc>

        .endRest()

    .get("test/altB")

        .route()

            .to(log("Alternative B (without path param)").showHeaders(true))

            // CamelHttpPath is empty, using CamelServletContextPath instead

            .setHeader(Exchange.HTTP_PATH, 
ExpressionBuilder.append(constant("dummySystem"), 
header("CamelServletContextPath")))

            .toD(http("dummyHost:8181").bridgeEndpoint(true))

            // called: 
http://dummyHost:8181/dummySystem/test/altB?dummyQueryParam=abc<http://dummyhost:8181/dummySystem/test/altB?dummyQueryParam=abc>

        .endRest()



// --- Alternative C

    .get("test/altC/{somePathParam}")

        .route()

            .to(log("Alternative C (with path param)").showHeaders(true))

            // this works (including query params as well)

            .setHeader(Exchange.HTTP_PATH, 
ExpressionBuilder.append(constant("dummySystem"), header(Exchange.HTTP_PATH)))

            .to(http("dummyHost:8181").bridgeEndpoint(true))

            // called: 
http://dummyHost:8181/dummySystem/test/altC/xyz?dummyQueryParam=abc<http://dummyhost:8181/dummySystem/test/altC/xyz?dummyQueryParam=abc>

        .endRest()

    .get("test/altC")

        .route()

            .to(log("Alternative C (without path param)").showHeaders(true))

            // CamelHttpPath is empty, using CamelServletContextPath instead

            .setHeader(Exchange.HTTP_PATH, 
ExpressionBuilder.append(constant("dummySystem"), 
header("CamelServletContextPath")))

            .to(http("dummyHost:8181").bridgeEndpoint(true))

            // called: 
http://dummyHost:8181/dummySystem/test/altC?dummyQueryParam=abc<http://dummyhost:8181/dummySystem/test/altC?dummyQueryParam=abc>

        .endRest()



In alternative A, I can use the existing properties but in B and C I have to 
split each property into two.



How come the CamelHttpPath header is empty when the path contains no path 
parameter? Is it by design or a bug?



Given the alternatives (A, B and C) above, is there any drawbacks or benefits 
of using one over another?



Is there any risk of creating endless dynamic endpoints (as mentioned in the 
toD documentation: 
https://camel.apache.org/components/3.4.x/eips/toD-eip.html#_avoid_creating_endless_dynamic_endpoints_which_takes_up_resources)
 when using alternative A (or any of the other alternatives)? (I find it hard 
to get a grip of this.)



In the toD documentation, there is a note:

”When the optimised component is in use, then you cannot use the headers 
Exchange.HTTP_PATH and Exchange.HTTP_QUERY to provide dynamic values to 
override the uri in toD. If you want to use these headers, then use the plain 
to DSL instead. In other words these headers are used internally by toD to 
carry the dynamic details of the endpoint.”

Based on my understandings of the note, I don’t understand why alternative B is 
working (since i’m pretty sure the optimised component is in use). Is it 
working because I only specifies host and port in toD(…)?



The difference between alternative B and C is only that B uses toD(…) and C 
uses to(…). Is there any reason for me to keep using toD(…) in these cases when 
it seems to(…) is enough?



I have not found an alternative which solves paths with and without path 
parameters in the same way. Is there a way of doing this?



Regards,

Calle

Reply via email to