In Camel I am using the http4 component to make REST request on a remote server.
The component documentation states that the credentials should be put in the options on the endpoint like this:
https4://myremote.server.com/?authUsername=xxx&authPassword=yyy
This was working well until someone put a password with a '+' character on another environment. We notice that the '+' character is transmitted as a space in the server which generates an error. By searching deeper in the Camel documentation we found a page explaining there is a "RAW" function to use like this:
https4://myremote.server.com/?authUsername=xxx&authPassword=RAW(yyy)
to keep the password unchanged.
Unfortunately this function has only been introduced in the Camel 2.11 version and for the moment we are not planning to upgrade to ServiceMix 5.1.x.
We are currently on serviceMix 4.5.x and the camel version is 2.10.7.
I tried these in the route (one by one):
.setProperty("Authorization", "Basic {base64Hash}")
.setHeader("Authorization", "Basic {base64Hash}")
.setProperty(HttpHeaders.AUTHORIZATION, "Basic {base64Hash}")
.setHeader(HttpHeaders.AUTHORIZATION, "Basic {base64Hash}")
but the remote server sends me a 401 (Unauthorized).
The question is: is there any other alternative to send credentials for http4 component than using the option on the endpoint?
I finally found a way and the problem I had was the server that need an extra parameter called : X-Forwarded-Proto
,
then the following way works very well instead passing the credentials in the endpoint option:
from(in.getEndpointUri())
.setHeader(Exchange.HTTP_METHOD, constant("GET"))
.setHeader(Exchange.HTTP_PATH, simple("/path/to/my/resource/1234"))
.setHeader(Exchange.HTTP_QUERY, constant("type=accessories&view=blue"))
.setHeader("X-Forwarded-Proto", constant("https"))
.setHeader("Authorization", constant("Basic bXl1c2VybmFtZTpwYXNzd29yZDEyMzQ="))
.to("https4://myremote.server.com/myrestservices")
.convertBodyTo(String.class)
;
After, using a bean or processor to generate the Base64 hash would be easy.