use of org.apache.http.client.protocol.HttpClientContext in project metron by apache.
the class TaxiiHandler method createContext.
private static HttpClientContext createContext(URL endpoint, String username, String password, int port) {
HttpClientContext context = null;
HttpHost target = new HttpHost(endpoint.getHost(), port, endpoint.getProtocol());
if (username != null && password != null) {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(target.getHostName(), target.getPort()), new UsernamePasswordCredentials(username, password));
// http://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html
AuthCache authCache = new BasicAuthCache();
authCache.put(target, new BasicScheme());
// Add AuthCache to the execution context
context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
context.setAuthCache(authCache);
} else {
context = null;
}
return context;
}
use of org.apache.http.client.protocol.HttpClientContext in project flow by vaadin.
the class DefaultFileDownloader method execute.
private CloseableHttpResponse execute(URI requestUri) throws IOException {
CloseableHttpResponse response;
ProxyConfig.Proxy proxy = proxyConfig.getProxyForUrl(requestUri.toString());
if (proxy != null) {
getLogger().info("Downloading via proxy {}", proxy.toString());
return executeViaProxy(proxy, requestUri);
} else {
getLogger().info("No proxy was configured, downloading directly");
if (userName != null && !userName.isEmpty() && password != null && !password.isEmpty()) {
getLogger().info("Using credentials ({})", userName);
// Auth target host
URL aURL = requestUri.toURL();
HttpClientContext localContext = makeLocalContext(aURL);
CredentialsProvider credentialsProvider = makeCredentialsProvider(aURL.getHost(), aURL.getPort(), userName, password);
response = buildHttpClient(credentialsProvider).execute(new HttpGet(requestUri), localContext);
} else {
response = buildHttpClient(null).execute(new HttpGet(requestUri));
}
}
return response;
}
use of org.apache.http.client.protocol.HttpClientContext in project flow by vaadin.
the class DefaultFileDownloader method makeLocalContext.
private HttpClientContext makeLocalContext(URL requestUrl) {
// Auth target host
HttpHost target = new HttpHost(requestUrl.getHost(), requestUrl.getPort(), requestUrl.getProtocol());
// 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(target, basicAuth);
// Add AuthCache to the execution context
HttpClientContext localContext = HttpClientContext.create();
localContext.setAuthCache(authCache);
return localContext;
}
use of org.apache.http.client.protocol.HttpClientContext in project stocator by SparkTC.
the class SwiftConnectionManager method getRetryHandler.
/**
* Creates custom retry handler to be used if HTTP exception happens
*
* @return retry handler
*/
private HttpRequestRetryHandler getRetryHandler() {
final HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler() {
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
if (executionCount >= connectionConfiguration.getExecutionCount()) {
// Do not retry if over max retry count
LOG.debug("Execution count {} is bigger than threshold. Stop", executionCount);
return false;
}
if (exception instanceof NoHttpResponseException) {
LOG.debug("NoHttpResponseException exception. Retry count {}", executionCount);
return true;
}
if (exception instanceof UnknownHostException) {
LOG.debug("UnknownHostException. Retry count {}", executionCount);
return true;
}
if (exception instanceof ConnectTimeoutException) {
LOG.debug("ConnectTimeoutException. Retry count {}", executionCount);
return true;
}
if (exception instanceof SocketTimeoutException || exception.getClass() == SocketTimeoutException.class || exception.getClass().isInstance(SocketTimeoutException.class)) {
// Connection refused
LOG.debug("socketTimeoutException Retry count {}", executionCount);
return true;
}
if (exception instanceof InterruptedIOException) {
// Timeout
LOG.debug("InterruptedIOException Retry count {}", executionCount);
return true;
}
if (exception instanceof SSLException) {
LOG.debug("SSLException Retry count {}", executionCount);
return true;
}
final HttpClientContext clientContext = HttpClientContext.adapt(context);
final HttpRequest request = clientContext.getRequest();
boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
if (idempotent) {
LOG.debug("HttpEntityEnclosingRequest. Retry count {}", executionCount);
return true;
}
LOG.debug("Retry stopped. Retry count {}", executionCount);
return false;
}
};
return myRetryHandler;
}
use of org.apache.http.client.protocol.HttpClientContext in project pentaho-kettle by pentaho.
the class SlaveServerTest method testSendExportOk.
@Test
public void testSendExportOk() throws Exception {
slaveServer.setUsername("uname");
slaveServer.setPassword("passw");
slaveServer.setHostname("hname");
slaveServer.setPort("1111");
HttpPost httpPostMock = mock(HttpPost.class);
URI uriMock = new URI("fake");
final String responseContent = "baah";
when(httpPostMock.getURI()).thenReturn(uriMock);
doReturn(uriMock).when(httpPostMock).getURI();
HttpClient client = mock(HttpClient.class);
when(client.execute(any(), any(HttpContext.class))).then(new Answer<HttpResponse>() {
@Override
public HttpResponse answer(InvocationOnMock invocation) throws Throwable {
HttpClientContext context = invocation.getArgumentAt(1, HttpClientContext.class);
Credentials cred = context.getCredentialsProvider().getCredentials(new AuthScope("hname", 1111));
assertEquals("uname", cred.getUserPrincipal().getName());
return mockResponse(200, responseContent);
}
});
// override init
when(slaveServer.getHttpClient()).thenReturn(client);
when(slaveServer.getResponseBodyAsString(any())).thenCallRealMethod();
doReturn(httpPostMock).when(slaveServer).buildSendExportMethod(anyString(), anyString(), any(InputStream.class));
File tempFile;
tempFile = File.createTempFile("PDI-", "tmp");
tempFile.deleteOnExit();
String result = slaveServer.sendExport(tempFile.getAbsolutePath(), null, null);
assertEquals(responseContent, result);
}
Aggregations