use of org.apache.http.impl.client.BasicAuthCache in project pentaho-kettle by pentaho.
the class HttpClientUtil method createPreemptiveBasicAuthentication.
/**
* Returns context with AuthCache or null in case of any exception was thrown.
*
* @param host
* @param port
* @param user
* @param password
* @param schema
* @return {@link org.apache.http.client.protocol.HttpClientContext HttpClientContext}
*/
public static HttpClientContext createPreemptiveBasicAuthentication(String host, int port, String user, String password, String schema) {
HttpClientContext localContext = null;
try {
HttpHost target = new HttpHost(host, port, schema);
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(target.getHostName(), target.getPort()), new UsernamePasswordCredentials(user, password));
// 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
localContext = HttpClientContext.create();
localContext.setAuthCache(authCache);
} catch (Exception e) {
return null;
}
return localContext;
}
use of org.apache.http.impl.client.BasicAuthCache in project pentaho-kettle by pentaho.
the class WebService method getHttpClient.
private HttpClient getHttpClient(HttpClientContext context) {
HttpClientManager.HttpClientBuilderFacade clientBuilder = HttpClientManager.getInstance().createBuilder();
String login = environmentSubstitute(meta.getHttpLogin());
if (StringUtils.isNotBlank(login)) {
clientBuilder.setCredentials(login, Encr.decryptPasswordOptionallyEncrypted(environmentSubstitute(meta.getHttpPassword())));
}
int proxyPort = 0;
if (StringUtils.isNotBlank(meta.getProxyHost())) {
proxyPort = Const.toInt(environmentSubstitute(meta.getProxyPort()), 8080);
clientBuilder.setProxy(meta.getProxyHost(), proxyPort);
}
CloseableHttpClient httpClient = clientBuilder.build();
if (proxyPort != 0) {
// Preemptive authentication
HttpHost target = new HttpHost(meta.getProxyHost(), proxyPort, "http");
// 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
context.setAuthCache(authCache);
}
return httpClient;
}
use of org.apache.http.impl.client.BasicAuthCache in project qpid-broker-j by apache.
the class JavaBrokerAdmin method getHttpClientContext.
private HttpClientContext getHttpClientContext(final HttpHost management) {
final BasicAuthCache authCache = new BasicAuthCache();
authCache.put(management, new BasicScheme());
HttpClientContext localContext = HttpClientContext.create();
localContext.setAuthCache(authCache);
return localContext;
}
use of org.apache.http.impl.client.BasicAuthCache in project kylo by Teradata.
the class CustomApacheConnector method apply.
@Override
public ClientResponse apply(final ClientRequest clientRequest) throws ProcessingException {
final HttpUriRequest request = getUriHttpRequest(clientRequest);
final Map<String, String> clientHeadersSnapshot = writeOutBoundHeaders(clientRequest.getHeaders(), request);
try {
final CloseableHttpResponse response;
final HttpClientContext context = HttpClientContext.create();
if (preemptiveBasicAuth) {
final AuthCache authCache = new BasicAuthCache();
final BasicScheme basicScheme = new BasicScheme();
authCache.put(getHost(request), basicScheme);
context.setAuthCache(authCache);
}
// https://stackoverflow.com/questions/42139436/jersey-client-throws-cannot-retry-request-with-a-non-repeatable-request-entity
response = client.execute(getHost(request), request, context);
HeaderUtils.checkHeaderChanges(clientHeadersSnapshot, clientRequest.getHeaders(), this.getClass().getName());
final Response.StatusType status = response.getStatusLine().getReasonPhrase() == null ? Statuses.from(response.getStatusLine().getStatusCode()) : Statuses.from(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase());
final ClientResponse responseContext = new ClientResponse(status, clientRequest);
final List<URI> redirectLocations = context.getRedirectLocations();
if (redirectLocations != null && !redirectLocations.isEmpty()) {
responseContext.setResolvedRequestUri(redirectLocations.get(redirectLocations.size() - 1));
}
final Header[] respHeaders = response.getAllHeaders();
final MultivaluedMap<String, String> headers = responseContext.getHeaders();
for (final Header header : respHeaders) {
final String headerName = header.getName();
List<String> list = headers.get(headerName);
if (list == null) {
list = new ArrayList<>();
}
list.add(header.getValue());
headers.put(headerName, list);
}
final HttpEntity entity = response.getEntity();
if (entity != null) {
if (headers.get(HttpHeaders.CONTENT_LENGTH) == null) {
headers.add(HttpHeaders.CONTENT_LENGTH, String.valueOf(entity.getContentLength()));
}
final Header contentEncoding = entity.getContentEncoding();
if (headers.get(HttpHeaders.CONTENT_ENCODING) == null && contentEncoding != null) {
headers.add(HttpHeaders.CONTENT_ENCODING, contentEncoding.getValue());
}
}
try {
responseContext.setEntityStream(new CustomApacheConnector.HttpClientResponseInputStream(getInputStream(response)));
} catch (final IOException e) {
LOGGER.error("IOException encountered trying to setEntityStream", e);
}
return responseContext;
} catch (final Exception e) {
throw new ProcessingException(e);
}
}
use of org.apache.http.impl.client.BasicAuthCache in project qpid-broker-j by apache.
the class ManageQpidJMSResources method getHttpClientContext.
private HttpClientContext getHttpClientContext(final HttpHost management) {
final BasicAuthCache authCache = new BasicAuthCache();
authCache.put(management, new BasicScheme());
HttpClientContext localContext = HttpClientContext.create();
localContext.setAuthCache(authCache);
return localContext;
}
Aggregations