Sunday, September 21, 2014

How to write REST client with proxy configurations?


Proxy?

Most of the enterprises have the proxy settings in-order to hide the actual endpoint from the client. Client will interact with the proxy, but the proxy will forward the request to the actual endpoint and return the response back to the client.

So what?

Till now we have seen code, how to communicate directly to the REST end-point. So the approach is straight forward. We don't need to have any special handling for the communication. Where-as if the REST service is hosted behind the proxy how to communicate?

Still thinking how? Don't worry it is very easy with Jersey client API.

client = ClientBuilder.newClient();
client.property(ClientProperties.PROXY_URI, "<proxy_host>:<proxy_port");
After created the client object, we have to add the property with specific with the proxy url configuration into the client.

In some cases, our network proxy has been restricted with user name and password. In such a situation we need to set the user name and password configuration into the client object as below,

client.property(ClientProperties.PROXY_USERNAME, "<username>");
client.property(ClientProperties.PROXY_PASSWORD, "<password>");
This is how we have to communicate with the REST end-point if our services are available behind proxy.

I hope this has clarified your doubts on REST client with proxy configuration. If you have any comments or questions please add your comments below...

2 comments:

  1. Which connector were you using ?

    ReplyDelete
  2. I've the following requirement :-
    1. first system is the entry point for all the REST calls and then it will forward these requests to different system, which will be deployed on different host.
    for ex:- GET /products/{id} will be forwarded to catalog system
    POST /messages/ will be forwarded to notification system

    like above i will have lots of REST calls calling these different system. let me know if i can create a proxy catch-all system where i don't have to mirror each and every REST call.

    Also could you provide a sample code for calling the webservices of another system , how do i pass the type of HTTP method and param of it . and pass the application name proxy system.

    ReplyDelete