Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sagarsane committed Oct 10, 2015
1 parent 5b853f7 commit f0df1f9
Showing 1 changed file with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import org.apache.http.auth.AuthScope
import org.apache.http.auth.UsernamePasswordCredentials
import org.apache.http.client.AuthCache
import org.apache.http.client.CredentialsProvider
import org.apache.http.client.HttpClient
import org.apache.http.client.methods.HttpGet
import org.apache.http.client.protocol.HttpClientContext
import org.apache.http.impl.auth.BasicScheme
Expand Down Expand Up @@ -113,6 +114,24 @@ class ClientBatchJobExecutionListener implements JobExecutionListener {
client
}

/**
* Gets a new HttpClientContext for the given HttpRequest. The ClientContext sets up preemptive
* authentication using BasicAuthCache
* @param get the HttpGet request
* @return
*/
private HttpClientContext getHttpClientContext(HttpGet get) {
// Setup preemptive Authentication
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
HttpHost host = new HttpHost(get.URI.host, get.URI.port, get.URI.scheme)
authCache.put(host, new BasicScheme());
// Add AuthCache to the execution context
HttpClientContext context = HttpClientContext.create();
context.setAuthCache(authCache);
context
}

/**
* Makes a Http Get request to the grab path and returns the response
* @param jobParameters that contain information like the path, host, port, etc.
Expand Down Expand Up @@ -140,18 +159,9 @@ class ClientBatchJobExecutionListener implements JobExecutionListener {

//create the get request
HttpGet get = new HttpGet(uriBuilder.build())
// setup pre-emptive auth
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local auth cache
BasicScheme basicAuth = new BasicScheme();
authCache.put(new HttpHost(host, port as Integer), basicAuth);

// Add AuthCache to the execution context
HttpClientContext context = HttpClientContext.create();
context.setAuthCache(authCache);

HttpResponse response = getHttpClient(username, password).execute(get, context)
HttpClient client = getHttpClient(username, password)
HttpClientContext context = getHttpClientContext(get)
HttpResponse response = client.execute(get, context)
response
}

Expand Down

0 comments on commit f0df1f9

Please sign in to comment.