use of org.apache.http.client.protocol.HttpClientContext 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.client.protocol.HttpClientContext 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.client.protocol.HttpClientContext 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.client.protocol.HttpClientContext in project iaf by ibissource.
the class OAuthPreferringAuthenticationStrategy method select.
// private boolean refreshTokenOn401; // retrying unchallenged request/responses might cause endless authentication loops
@Override
public Queue<AuthOption> select(Map<String, Header> challenges, HttpHost authhost, HttpResponse response, HttpContext context) throws MalformedChallengeException {
final HttpClientContext clientContext = HttpClientContext.adapt(context);
final Queue<AuthOption> options = new LinkedList<AuthOption>();
final CredentialsProvider credsProvider = clientContext.getCredentialsProvider();
if (credsProvider == null) {
this.log.debug("Credentials provider not set in the context");
return options;
}
final AuthScope authScope = new AuthScope(authhost, "", OAuthAuthenticationScheme.SCHEME_NAME);
final Credentials credentials = credsProvider.getCredentials(authScope);
if (credentials != null) {
// always add OAuth as an authentication option, if any challenges are returned by server
options.add(new AuthOption(new OAuthAuthenticationScheme(), credentials));
}
options.addAll(super.select(challenges, authhost, response, clientContext));
return options;
}
use of org.apache.http.client.protocol.HttpClientContext 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