Search in sources :

Example 21 with BasicAuthCache

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;
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) AuthCache(org.apache.http.client.AuthCache) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) IOException(java.io.IOException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 22 with BasicAuthCache

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;
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicScheme(org.apache.http.impl.auth.BasicScheme) HttpClientManager(org.pentaho.di.core.util.HttpClientManager) HttpHost(org.apache.http.HttpHost) AuthCache(org.apache.http.client.AuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache)

Example 23 with BasicAuthCache

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;
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache)

Example 24 with BasicAuthCache

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);
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) ClientResponse(org.glassfish.jersey.client.ClientResponse) BasicScheme(org.apache.http.impl.auth.BasicScheme) HttpEntity(org.apache.http.HttpEntity) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity) AuthCache(org.apache.http.client.AuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) IOException(java.io.IOException) URI(java.net.URI) ProcessingException(javax.ws.rs.ProcessingException) IOException(java.io.IOException) LivyUserException(com.thinkbiganalytics.kylo.spark.exceptions.LivyUserException) ClientResponse(org.glassfish.jersey.client.ClientResponse) Response(javax.ws.rs.core.Response) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Header(org.apache.http.Header) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) ProcessingException(javax.ws.rs.ProcessingException)

Example 25 with BasicAuthCache

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;
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache)

Aggregations

BasicAuthCache (org.apache.http.impl.client.BasicAuthCache)55 AuthCache (org.apache.http.client.AuthCache)49 BasicScheme (org.apache.http.impl.auth.BasicScheme)48 HttpHost (org.apache.http.HttpHost)39 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)36 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)33 CredentialsProvider (org.apache.http.client.CredentialsProvider)33 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)32 AuthScope (org.apache.http.auth.AuthScope)27 IOException (java.io.IOException)14 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)14 URI (java.net.URI)10 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)10 HttpGet (org.apache.http.client.methods.HttpGet)10 HttpResponse (org.apache.http.HttpResponse)9 HttpPost (org.apache.http.client.methods.HttpPost)7 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)7 BasicHttpContext (org.apache.http.protocol.BasicHttpContext)7 File (java.io.File)6 URISyntaxException (java.net.URISyntaxException)6