Using destinations with third-party libraries using their own HttpClient(s)
In the context of HCP applications, sometimes you need or it is more convenient to use a third party library that encapsulates the functionality of sending http requests. The problem with that is that the library most likely uses an implementation of the HttpClient interface, such as CloseableHttpClient.
What to do then? Surely, there are some pointers on the HCP documentation, namely here and here. There is also an API documentation.
What they all say is that HCP provides access to connectivity configuration, destination configuration and authentication headers to allow the developers making their own clients or http calls. But there is no explicit example of how they should be used for creating a CloseableHttpClient.
So here we go, there is my example for anybody who is interested. Say I have a destination and a third-party library which needs to have a CloseableHttpClient to make http calls. The most important part is apparently the proxy :-).
private CloseableHttpClient makeClient() throws Exception {
String proxyType = destConfiguration.getProperty("ProxyType");
HttpHost proxy = getProxy(proxyType);
if (proxy == null)
throw new Exception("Unable to get system proxy");
CloseableHttpClient newClient = HttpClientBuilder.create().setProxy(proxy).build();
return newClient;
}
private HttpHost getProxy(String proxyType) {
String proxyHost = null;
int proxyPort = 0;
// Need to get proxy for internet destinations
if (proxyType.equals("Internet")) {
proxyHost = System.getProperty("http.proxyHost");
proxyPort = Integer.parseInt(System.getProperty("http.proxyPort"));
return new HttpHost(proxyHost, proxyPort);
}
return null;
}
In the example above, I assumed that the destConfiguration is already present, as specified here.