Recent Java Client API Changes

LabKey Support Forum (Inactive)
Recent Java Client API Changes Andy Straw  2015-05-14 12:29
Status: Closed
 
I was able to get authentication to work, but this required another 10 lines of code to properly create the HttpClientContext that is needed as a second parameter the execute() method of CloseableHttpClient. Since the code to create the context object requires only the LabKey Connection object, it would be nice if your API somehow could do that for us. Here's the way I factored this into a helper method:

private static HttpClientContext buildHttpContext( Connection labkeyConnection ) throws MalformedURLException {
URL labkeyBaseUrl = new URL( labkeyConnection.getBaseUrl() );
HttpHost labkeyHost = new HttpHost( labkeyBaseUrl.getHost(), labkeyBaseUrl.getPort(), labkeyBaseUrl.getProtocol() );
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials( new AuthScope(labkeyHost),
new UsernamePasswordCredentials(labkeyConnection.getEmail(), labkeyConnection.getPassword()) );
AuthCache authCache = new BasicAuthCache();
authCache.put( labkeyHost, new BasicScheme());
HttpClientContext httpContext = HttpClientContext.create();
httpContext.setCredentialsProvider(credsProvider);
httpContext.setAuthCache(authCache);
return httpContext;
}

Andy