9

I can't seem to get JAX-RS clients to use a web proxy on Java 8. I'm using RESTEasy 3.0.10.Final, and running from inside Eclipse 4.4.2 on Windows 7 Professional 64-bit.

I set up a FreeProxy server on localhost running at 192.168.1.123:3128. I turn logs on and telnet to 192.168.1.123 3128 and issue a manual GET. The request shows up in the logs.

I then fire up my Java application, setting http.proxyHost=192.168.1.123 and http.proxyPort=3128 in the system properties. (I've even tried it using -D when starting the JVM.) (Note that I wouldn't expect the localhost problem to come into play, as I'm connecting to an actual IP address, not to localhost.)

I create a JAX-RS client using ClientBuilder.newBuilder().build() and perform a GET to a resource. Nothing shows up in the FreeProxy logs.

What do I have to do in order to get JAX-RS clients to use a proxy?

Garret Wilson
  • 18,219
  • 30
  • 144
  • 272

3 Answers3

14

The ResteasyClientBuilder provides a method to define the defaultProxy:

ResteasyClient client = new ResteasyClientBuilder().defaultProxy("localhost", 8080, "http").build();
lefloh
  • 10,653
  • 3
  • 28
  • 50
  • I'm just now reading http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d5e475 and trying to use the `SystemDefaultRoutePlanner` to use the standard JRE proxy selector. But how do I pull in the `SystemDefaultRoutePlanner` and `HttpClients` classes? – Garret Wilson Mar 14 '15 at 16:10
  • lefloh, the code you indicated works! (I think RESTEasy brought in the dependency for me.) I'm now getting connection logs in my local proxy server logs. It's a shame that I have to create a separate configuration when Java already has a way to configure proxies. Can you let me know how to pull in the dependencies to use `SystemDefaultRoutePlanner` as indicated in my earlier comment? – Garret Wilson Mar 14 '15 at 17:50
  • ...and how do I set up a proxy to be used both for HTTP and HTTPS? It looks like the technique you showed only installs a proxy for one or the other. – Garret Wilson Mar 14 '15 at 18:27
  • I updated my answer. First of all: Resteasy provides a method to set the default proxy. I didn't see that before. But as you found out only for one scheme. I think this could work with the `SystemDefaultRoutePlanner` you mentioned. I added an example which is compiling with the dependency written above. I didn't test that. – lefloh Mar 14 '15 at 21:25
  • But lefloh I still don't understand what I need to include in Maven to give me the `SystemDefaultRoutePlanner` et. al. The `httpclient` package you mentioned isn't doing it. In addition, note that your example differs from the one at the Apache documentation I linked above --- the one that is linked from the actual RESTEasy documentation. In any case, I need to know the dependency to get `HttpClients` and `SystemDefaultRoutePlanner`. – Garret Wilson Mar 16 '15 at 16:20
  • From http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/conn/SystemDefaultRoutePlanner.html it appears that `SystemDefaultRoutePlanner` is only available since 4.3. But I'm showing that RESTEasy 3.0.10.Final is using httpclient 4.2.6. So lefloh your answer no longer works. – Garret Wilson Mar 16 '15 at 16:27
  • You're right, I had a newer dependency in my project. I rewrote the example to something similar fitting for the older version but in this version HTTPClient are not using `http.proxyHost`. – lefloh Mar 16 '15 at 18:08
  • Thanks for updating your answer, lefloh. I'll check into it. But does it work correctly if Java has `java.net.useSystemProxies` in effect? See http://stackoverflow.com/q/29083945/421049 . – Garret Wilson Mar 16 '15 at 18:12
  • Furthermore I don't see how your new answer improves on the old answer, if it's still not using `http.proxyHost`. How is this any different functionally from your first answer? I'm starting to get all confused. – Garret Wilson Mar 16 '15 at 18:16
  • OK, I reduced the answer to the part which answers your original question. To your comment about http/https: Seems like the Resteasy Client API doesn't provide a way to do this. It may be possible to configure the Apache HttpClient directly but I didn't figure out how. – lefloh Mar 16 '15 at 18:27
  • Huge thanks for your answer! Resteasy is so unstable with proxy settings, what works for 2* doesn't work for 3* or 4*. Wasted whole day, until found this thread. For wildfly with 3.5 your option works fine! – Oleg Gritsak Jul 18 '19 at 10:19
6

It seems to be possible to make RESTeasy use Java's proxy properties (e.g. -Dhttp.proxyHost) by using a different engine instead of HttpClient. java.net.HttpURLConnection supports proxy properties out of the box:

ResteasyClient client = new ResteasyClientBuilder().httpEngine(new URLConnectionEngine()).build();
theglauber
  • 28,367
  • 7
  • 29
  • 47
Nikolai Prokoschenko
  • 8,465
  • 11
  • 58
  • 97
0

For RESTEasy 4, here is what I've done for that:

ResteasyClient client = ((ResteasyClientBuilder) ClientBuilder.newBuilder())
    .defaultProxy(proxyHost, proxyPort)
    .build();

return client
    .target(ENDPOINT_URL)
    .proxy(EndpointResource.class);
Chavjoh
  • 466
  • 5
  • 13