Camel Routing Config File with Query Params - apache-camel

I have a '.route' file for a rest service that works, it has the resource in the middle of the URI:
<from uri='restlet:/foo/{id}/bar
This works just fine, I am able to retrieve the 'id' in code using:
String id = e.getIn().getHeader("id", String.class);
Now, I want a '.route' with a URI with a query parameter in it.
I tried a bunch of ways, like:
<from uri='restlet:/foo/baz?color={aColor}
But this does not work, I get a 404 error, the server cannot find the URI.
This seems to be a very easy/general thing, anybody know how to do this?
I looked over the docs, but I cannot figure out how to do it.

All params follow after the question mark receive as the camel options(for example restletMethod, connectionTimeout.... see http://camel.apache.org/restlet.html). Just use <from uri='restlet:/foo/{id}/bar in your route, pass the parameters in query like a http://localhost:8080/mywebapp/rs/foo/1234/bar?color=red and get it String id = e.getIn().getHeader("color", String.class);

Related

PrettyPrint feature does not work in Apache Camel

I have been trying to leverage the PrettyPrint feature to display the result of my API that is using Apache Camel. Here is the context. I have this route in my code
// Route Definition for processing Health check request
from("direct:processHealthCheckRequest")
.routeId("health")
.setHeader(Exchange.HTTP_RESPONSE_CODE, constant(200))
.setBody(constant(healthCheckResponse));
When I'm using Postman to test my API, the display is in pretty mode even though it is not set to true, like so
{
"status": "UP"
}
Now when I'm using the following code to set the PrettyPrint to false, I'm still getting the same result. It looks like the PrettyPrint feature is not working as it is supposed to
// Route Definition for processing Health check request
from("direct:processHealthCheckRequest")
.routeId("health")
.setHeader(Exchange.HTTP_RESPONSE_CODE, constant(200))
.setBody(constant(healthCheckResponse))
.unmarshal()
.json(JsonLibrary.Jackson, HealthCheckResponse.class, false);
I'm expecting the result to be displayed on one line like here without changing the type from JSON to string.
{"status": "UP"}
Could someone please advice on this?
I've bumped into the same issue always when manually setting the HTTP_RESPONSE_CODE header. I don't know why it technically happens - without it the HTTP response always returns proper JSON for me.
Setting CONTENT_TYPE header to application/json has solved it:
.setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
The solution that finally worked was to set the following in my application.properties file.
camel.rest.data-format-property.prettyPrint=false
or not to provide that property at all.
Try this:
<removeHeaders id="removeHeaders_http*" pattern="CamelHttp*"/>
<setHeader headerName="Content-type" id="content_setHeader">
<constant>application/x-www-form-urlencoded</constant>
</setHeader>
Same with Java DSL:
.removeHeaders("CamelHttp*")
.setHeader("Content-type", constant("application/x-www-form-urlencoded"))

camel-exec : No endpoint could be found error

Below is the consumer endpoint. Trying to execute linux/unix command using camel exec process uri.
<recipientList>
<simple>exec:bash?args=-c CFTUTIL SEND IDF=${property[cftFlowIdentifier]}, PART=${property[cftParterName]}, FNAME=${property[cftFullFilePath]}, FNAME=${property[cftDestinationPath]}/${property[cftFileName]}</simple>
</recipientList>
Above endpoint resulting in below error..
org.apache.camel.NoSuchEndpointException: No endpoint could be found for: PART=
Kindly check if there is anything wrong in invoking above command using camel exec uri. Same command is getting executed successfully in normal java program.
Recipient List EIP accepts comma separated list of endpoints. If you need to use comma in URL, then disable or use another delimiter.
<recipientList delimiter="false">
<simple>...</simple>
</recipientList>
You can also switch to To D EIP, which is more suitable for your needs, as you call just one endpoint.
<toD uri="exec:bash?args=-c CFTUTIL SEND IDF=${property[cftFlowIdentifier]}, PART=${property[cftParterName]}, FNAME=${property[cftFullFilePath]}, FNAME=${property[cftDestinationPath]}/${property[cftFileName]}"/>

Exchange body substring in log message

How to log exchange body substring as message attribute of log component?
I have tried these:
<log message="SEND RESPONSE TO WEB SERVICE: Headers:[${headers}]\nBody:[${bodyAs(String).substring(0,1000)}]"/>
<log message="SEND RESPONSE TO WEB SERVICE: Headers:[${headers}]\nBody:[${body.toString().substring(0,1000)}]"/>
but none works. First variant is marked as mistake by IDE Camel plugin and does not allow application to start, and the second one throws the exception about calling toString() at null (although body is not).
ps body is really instance of String.
Read this page, particularly the section "Full Customization of the Logged Output"

Zuul url parameters to Solr Query parameters

I am trying to rewrite request with zuul and forward them to an old solr server.
My application.yml looks like this:
zuul:
prefix: /api/v1
routes:
peoples:
path: path: /peoples/**
url: http://solr/api/select?
So far the /people route is working fine, i get the incoming request, pass the correct parameters to the request via a Zuulfilter and forward to solr. The redirected request looks:
http://sorl/api/select?q=*&wt=json&indent=true&collection=MyCollection&fl=Filter1,Filter2,Filter3,Filter4&rows=10
I don't understand how i can define a new route like : peoples/{id}.
I need to pass the {id} route to another ZuulFilter and append it to com.netflix.zuul.context.RequestContext.getCurrentContext().setRequestQueryParams()
Am i missing something in Zuul or is it just no possible to get a param from the requestcontext, and transform it to a query param ?

How to write the correct URI using camel and facebook4j

I'm using apache camel and facebook component with it. When I use string
<from uri="facebook://me?oAuthAppId={{oAuthAppId}}&oAuthAppSecret={{oAuthAppSecret}}&oAuthAccessToken={{oAuthAccessToken}}"/>
facebook returns me
{
"name": "Some Myname",
"id": "my id"
}
Now I want to get all my "likes". In accordance to facebook api I should write something like this: me?fields=likes. So I've tried this uri
<from uri="facebook://me?fields=likes&oAuthAppId={{oAuthAppId}}&oAuthAppSecret={{oAuthAppSecret}}&oAuthAccessToken={{oAuthAccessToken}}"/>
but it doesn't work. How should I write this uri to get all my likes?
Thanks in advance
You can use the getUserLikes method, linked to the userLikes endpoint:
facebook://userLikes?oAuthAppId={{oAuthAppId}}&oAuthAppSecret={{oAuthAppSecret}}&oAuthAccessToken={{oAuthAccessToken}}
You can use an optional userId parameter.

Resources