Pass query parameters through a route

Camel learner here. Is there an ability to store incoming query parameters and set various headers/query parameters for various routes?
I want to build proxy-like service with authentication option. Example :
1) I receive an HTTP on http://foo.bar/path?login=admin&password=admin&action=delete
2) Then I'd like to use some <to uri="direct:auth"/> to send request to http://auth.foo.bar/login?login=admin&password=admin and receive authentication token as an answer
3) Then, perform an action with token : going to http://action.foo.bar/perform?authToken=sometoken&action=delete

Finally - is there an option to "cut" "action" param before going auth route and then append it before going action. Thanks.

Upvotes: 0

Views: 6318

Answers (1)

fiw
fiw

Reputation: 756

You'll want to use Camel Jetty component to receive a HTTP request and the Camel HTTP4 component to make requests to other web services.

The Jetty component will take the query parameters from the client's call and put them in the headers of the IN exchange with exactly the same names from the HTTP query.

You can then set the query parameters used for the other HTTP calls by setting the "CamelHttpQuery" header (the Exchange.HTTP_QUERY is a static final variable for that string). Below I've used the simple language camel provides which allows you to put headers and the body into strings which can be used to set the headers. The response from the http calls will be set in the body of the exchange.

For example:

from("jetty:http://0.0.0.0:1234/path")
    .setHeader(Exchange.HTTP_QUERY,  simple("?login=${header.login}&password=${header.password}"))
    .to("http4://auth.foo.bar/login")
    .setHeader("authToken", simple("${body}")) //assuming just the token is in the response body
    .setBody("")
    .setHeader(Exchange.HTTP_QUERY,  simple("?authToken=${header.authToken}&action=${header.action}"))
    .to("http4://action.foo.bar/perform");

This is a simple example but shows where the headers and responses are put. You may want to wrap some of the header setting and response processing into Camel Processors if it's not as simple to keep the route easy to read.

Further Response:

To filter headers from being sent to the http endpoint create an instance of org.apache.camel.spi.HeaderFilterStrategy in your spring beans definition and add it to the .to("http://auth.foo.bar/?headerFilterStrategy=authHeaderFilterImpl") like so. (This is from the http4 page)

The reason I suggested http4 is that it's slightly more customisable but feel free to stick with jetty if you can find the equivalent functionality from http4 that you need.

But from reading about exposing POST request functionality it sounds like the auth endpoint is more of an "enrich" enterprise integration pattern. Have a look at camel-enricher which allows you to hit another endpoint and then aggregate the response with the original exchange (or use a recipient list with aggregation strategy if you're using camel 2.15 or less).

Otherwise you could store the original post request body in a header and then use it later for the real action call.

Upvotes: 1

Related Questions