2

I use Jersey 2.11 to access the confluence REST API and get content from there.

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.glassfish.jersey.apache.connector.ApacheClientProperties;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

....

HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic(
    "confluenceUsername", "confluencePassword"
);

ClientConfig cc = new ClientConfig();
cc.property(ClientProperties.PROXY_URI, "xx.xx.xx.xx:xxxx");
Client client = ClientBuilder.newClient(cc);

client.register(feature);

WebTarget target = client.target(credential.getUrl())
    .path("rest").path("api").path("content").path(id)
    .queryParam("expand", "space,body.storage,version,container,ancestors");

Response response = target.request().get();

Is this the current right way to use proxy in Java with Jersey 2.11 ? I get always an exception

java.net.ConnectException: Connection refused: connect
javax.ws.rs.ProcessingException: java.net.Connect Exception: Connection refused: connect
    at org.glassfish.jersey.client.HttpUrlConnector.apply(HttpUrlConnector.java:229)
...

The confluence site which I want to access use https. On a machine which don't need a proxy it work with

HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic(
    "confluenceUsername", "confluencePassword");
Client client = ClientBuilder.newClient();
client.register(feature);

so Confluence isn't the problem. I think that is something wrong with proxy spefication but don't know what.

user1791139
  • 606
  • 1
  • 11
  • 27
  • http://stackoverflow.com/a/2684496/2078908 – ursa Nov 27 '14 at 13:56
  • @ursa do you mean that my proxy configuration is up-to-date with jersey 2.11 and the problem is the missing SSL certificate accepting? – user1791139 Nov 27 '14 at 14:04
  • yep. seems your client does not accept certificate from proxy, so I propose you to dig in this side. – ursa Nov 27 '14 at 14:06
  • I propose to use this http://stackoverflow.com/a/9541524/1791139 but the code is for old jersey 1 version, do you have any example for current 2.11 which accept all SSL certifcates? – user1791139 Nov 27 '14 at 15:46
  • @user1791139 see http://stackoverflow.com/questions/18942648/how-to-add-a-http-proxy-for-jersey2-client solution with apache connector – Dejell Mar 23 '15 at 17:30

2 Answers2

1

I could not manage proxy in jersey client so i use the default client creation

HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic(
    "confluenceUsername", "confluencePassword");
Client client = ClientBuilder.newClient();
client.register(feature);

and start my application with

java -Dhttps.proxyHost=xx.xx.xx.xx -Dhttps.proxyPort:xxxx -jar myApp.jar

If someone find out the correct solution to handle this with jersey 2.11 feel free to post an answer :)

user1791139
  • 606
  • 1
  • 11
  • 27
  • yes - see the answer http://stackoverflow.com/questions/18942648/how-to-add-a-http-proxy-for-jersey2-client – Dejell Mar 23 '15 at 17:29
0

Can you please Try the following code .I use Jersey 2.0.1 and I am able to set the proxy setting into clientConfig object.and it's working

ClientConfig config = new ClientConfig();
config.connectorProvider(new ApacheConnectorProvider());
config.property(ClientProperties.PROXY_URI, "proxy_url");
config.property(ClientProperties.PROXY_USERNAME,"user_name");
config.property(ClientProperties.PROXY_PASSWORD,"password");
Client client = ClientBuilder.newClient(config);
Fazeem
  • 21
  • 5