Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring proxy for JAX-RS 2.0 client API

I have an application that is running on a Java EE 7 application server (WildFly), that queries another service using REST resources.

In previous applications I have used the Jersey 1.x client API. Access to the REST service is granted through a web proxy.

In Jersey I create the Client instance like this:

public Client create() {

    Client client;
    if ( proxyConfiguration != null && proxyConfiguration.getHost() != null && !proxyConfiguration.getHost().trim().isEmpty() ) {
        HttpURLConnectionFactory urlConnectionFactory = new ProxyUrlConnectionFactory( proxyConfiguration );
        client = new Client( new URLConnectionClientHandler( urlConnectionFactory ), clientConfig );
    } else {
        client = Client.create( clientConfig );
    }

    return client;
}

Running on a Java EE 7 application server I wanted to use the JAX-RS 2.0 client API which is provided by the application server.

Now I am having a really hard time to find information on how to configure the JAX-RS 2.0 client in a platform independent way. Setting the http.proxyHost and http.proxyPort system properties had no effect in WildFly (I would prefer to not configure it globally anyway).

Does anyone know how to solve this?

like image 243
Max Fichtelmann Avatar asked Nov 23 '15 13:11

Max Fichtelmann


People also ask

What is JAX-RS Client API?

The JAX-RS client API is a Java based API used to access Web resources. It is not restricted to resources implemented using JAX-RS.

Which of these are implementations of JAX-RS API?

Both Restlet and Jersey are two of the most popular implementation of JAX-RS used for developing RESTful web services in Java ecosystem but there is a couple of other implementation also exist like Apache Wink, Apache CXF, and JBoss RESTEasy, along with omnipresent and the most popular option of creating REST web ...

Is JAX-RS client thread safe?

Official JAX-WS answer: No. According to the JAX-WS spec, the client proxies are NOT thread safe. To write portable code, you should treat them as non-thread safe and synchronize access or use a pool of instances or similar.

What method call makes a JAX-RS client reactive?

The rx() method call is the point from which the reactive handling kicks in. We use the exceptionally function to fluently define our exception handling scenario.


1 Answers

I think there's no vendor independent solution (at least, I didn't find anything related to proxies in the JAX-RS API).

For Jersey 2.x, you can try:

ClientConfig config = new ClientConfig();
config.property(ClientProperties.PROXY_URI, "192.168.1.254:8080");  
Client client = ClientBuilder.withConfig(config).build();

ClientProperties is a class from Jersey API.


For RESTEasy, the configuration is:

Client client = new ResteasyClientBuilder()
                   .defaultProxy("192.168.1.254", 8080, "http")
                   .build();

ResteasyClientBuilder is a class from RESTEasy API.

like image 174
cassiomolin Avatar answered Sep 27 '22 19:09

cassiomolin