Using web proxy with Java 8 JAX-RS RESTEasy clients
Asked Answered
P

3

10

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?

Parcae answered 14/3, 2015 at 0:45 Comment(2)
D
15

The ResteasyClientBuilder provides a method to define the defaultProxy:

ResteasyClient client = new ResteasyClientBuilder().defaultProxy("localhost", 8080, "http").build();
Delores answered 14/3, 2015 at 15:8 Comment(11)
I'm just now reading hc.apache.org/httpcomponents-client-ga/tutorial/html/… and trying to use the SystemDefaultRoutePlanner to use the standard JRE proxy selector. But how do I pull in the SystemDefaultRoutePlanner and HttpClients classes?Parcae
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?Parcae
...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.Parcae
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.Delores
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.Parcae
From hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/… 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.Parcae
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.Delores
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 https://mcmap.net/q/1162185/-how-to-get-resteasy-to-use-system-web-proxy/421049 .Parcae
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.Parcae
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.Delores
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!Damiondamita
E
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();
Ers answered 25/3, 2019 at 9:56 Comment(3)
Woarked for me!Soldier
Is there a system property that I could set to make RESTEasy use that http engine? I'm using a third party library where I can't change the RestEasyClientBuilder initialization.Counterpunch
@TobiasLiefke Not one I'm aware of, but I'm not really an expert on this. Which still might be possible and I'd be really interested in that as well!Ers
Z
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);
Zwolle answered 10/10, 2021 at 18:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.