I'm trying to setup a proxy in my java code using Jersey client but the proxy is not getting set. I went through the Jersey documentation and have implemented the code in the described way. I'm new to Jersey so not sure where I'm going wrong.
Below is the code.
@Override
@CircuitBreaker(name = "documentServiceCreateDocument", ignore = { NullPointerException.class,
ArrayIndexOutOfBoundsException.class })
public String createDocument(String name, DocumentType docType, List<SourceData> sourceDatas) {
ClientConfig clientConfig = new ClientConfig().register(MultiPartFeature.class)
.register(ClientTransactionIdFilter.class)
.property(ClientProperties.READ_TIMEOUT, "30000")
.property(ClientProperties.CONNECT_TIMEOUT, "30000")
.property(ClientProperties.PROXY_URI, properties.getProxyUrl);
Client client = ClientBuilder.newClient(clientConfig);
Builder builder = resourceTarget.request().header("Authorization", ***);
List<Cookie> iamCookies = ***
Response response = null;
try {
response = builder.post(body);
} catch (Exception e){
if(response != null) {
logger.info("Response code : " + response.getStatus());
logger.info("Response : " + response.toString());
}
e.printStackTrace();
throw new RuntimeException(e);
}
String docLocation = response.getLocation().toString();
logger.debug("Created Document Service document with location=" + docLocation);
return docLocation;
}
After a long duration, I finally figured out the fix. We need to use the ApacheConnectorProvider in order for the proxy to work.
Add the ApacheConnectorProvider to the ClientConfig as shown below:
Don't forget to add the jersey-apache-connector dependency to your pom file(if you are using maven). Refer to the below link for jersey-apache-connector dependency details: https://mvnrepository.com/artifact/org.glassfish.jersey.connectors/jersey-apache-connector/2.6