Search in sources :

Example 76 with AuthScope

use of org.apache.http.auth.AuthScope in project indy by Commonjava.

the class HttpProxyTest method proxyContext.

protected HttpClientContext proxyContext(final String user, final String pass) {
    final CredentialsProvider creds = new BasicCredentialsProvider();
    creds.setCredentials(new AuthScope(HOST, proxyPort), new UsernamePasswordCredentials(user, pass));
    final HttpClientContext ctx = HttpClientContext.create();
    ctx.setCredentialsProvider(creds);
    return ctx;
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) AuthScope(org.apache.http.auth.AuthScope) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 77 with AuthScope

use of org.apache.http.auth.AuthScope in project Asqatasun by Asqatasun.

the class HttpRequestHandler method getHttpClient.

private CloseableHttpClient getHttpClient(String url) {
    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout).setConnectTimeout(connectionTimeout).build();
    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    httpClientBuilder.setDefaultRequestConfig(requestConfig);
    httpClientBuilder.setConnectionManager(new PoolingHttpClientConnectionManager());
    httpClientBuilder.setUserAgent(ASQATASUN_USER_AGENT);
    if (isProxySet(url)) {
        LOGGER.debug(("Set proxy with " + proxyHost + " and " + proxyPort));
        httpClientBuilder.setProxy(new HttpHost(proxyHost, Integer.valueOf(proxyPort)));
        if (isProxyCredentialSet()) {
            CredentialsProvider credsProvider = new BasicCredentialsProvider();
            credsProvider.setCredentials(new AuthScope(proxyHost, Integer.valueOf(proxyPort)), new UsernamePasswordCredentials(proxyUser, proxyPassword));
            httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
            LOGGER.debug(("Set proxy credentials " + proxyHost + " and " + proxyPort + " and " + proxyUser + " and " + proxyPassword));
        }
    }
    return httpClientBuilder.build();
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 78 with AuthScope

use of org.apache.http.auth.AuthScope in project ats-framework by Axway.

the class HttpClient method setupAuthentication.

/**
     * Set up authentication for HTTP Basic/HTTP Digest/SPNEGO.
     *
     * @param httpClientBuilder The client builder
     * @return The context
     * @throws HttpException
     */
private HttpClientContext setupAuthentication(HttpClientBuilder httpClientBuilder) throws HttpException {
    HttpClientContext localContext = null;
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials(username, password));
    httpClientBuilder = httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
    if (authType == AuthType.always) {
        AuthCache authCache = new BasicAuthCache();
        // Generate BASIC scheme object and add it to the local
        // auth cache
        BasicScheme basicAuth = new BasicScheme();
        URL uri = null;
        try {
            uri = new URL(url);
        } catch (MalformedURLException e) {
            throw new HttpException("Exception occurred creating URL from '" + url + "'.", e);
        }
        HttpHost target = new HttpHost(uri.getHost(), uri.getPort(), uri.getProtocol());
        authCache.put(target, basicAuth);
        // Add AuthCache to the execution context
        localContext = HttpClientContext.create();
        localContext.setAuthCache(authCache);
    } else {
        if (!StringUtils.isNullOrEmpty(kerberosServicePrincipalName)) {
            GssClient gssClient = new GssClient(username, password, kerberosClientKeytab, krb5ConfFile);
            AuthSchemeProvider nsf = new SPNegoSchemeFactory(gssClient, kerberosServicePrincipalName, kerberosServicePrincipalType);
            final Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create().register(AuthSchemes.SPNEGO, nsf).build();
            httpClientBuilder.setDefaultAuthSchemeRegistry(authSchemeRegistry);
        }
    }
    return localContext;
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) MalformedURLException(java.net.MalformedURLException) AuthCache(org.apache.http.client.AuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) 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) SPNegoSchemeFactory(com.axway.ats.core.gss.spnego.SPNegoSchemeFactory) URL(java.net.URL) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) GssClient(com.axway.ats.core.gss.GssClient) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) AuthSchemeProvider(org.apache.http.auth.AuthSchemeProvider)

Example 79 with AuthScope

use of org.apache.http.auth.AuthScope in project opennms by OpenNMS.

the class RestSessionIT method queryUri.

private Header[] queryUri(final String uri, final String header) throws IOException {
    final HttpGet httpGet = new HttpGet(getBaseUrl() + uri);
    final HttpHost targetHost = new HttpHost(httpGet.getURI().getHost(), httpGet.getURI().getPort(), httpGet.getURI().getScheme());
    final CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials("admin", "admin"));
    final AuthCache authCache = new BasicAuthCache();
    final BasicScheme basicAuth = new BasicScheme();
    authCache.put(targetHost, basicAuth);
    final HttpClientContext context = HttpClientContext.create();
    context.setCredentialsProvider(credsProvider);
    context.setAuthCache(authCache);
    final CloseableHttpResponse closeableHttpResponse = HttpClients.createDefault().execute(targetHost, httpGet, context);
    closeableHttpResponse.close();
    return closeableHttpResponse.getHeaders(header);
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpHost(org.apache.http.HttpHost) HttpGet(org.apache.http.client.methods.HttpGet) AuthScope(org.apache.http.auth.AuthScope) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) AuthCache(org.apache.http.client.AuthCache) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) 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) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 80 with AuthScope

use of org.apache.http.auth.AuthScope in project Activiti by Activiti.

the class MuleSendActivitiBehavior method execute.

public void execute(ActivityExecution execution) throws Exception {
    String endpointUrlValue = this.getStringFromField(this.endpointUrl, execution);
    String languageValue = this.getStringFromField(this.language, execution);
    String payloadExpressionValue = this.getStringFromField(this.payloadExpression, execution);
    String resultVariableValue = this.getStringFromField(this.resultVariable, execution);
    String usernameValue = this.getStringFromField(this.username, execution);
    String passwordValue = this.getStringFromField(this.password, execution);
    ScriptingEngines scriptingEngines = Context.getProcessEngineConfiguration().getScriptingEngines();
    Object payload = scriptingEngines.evaluate(payloadExpressionValue, languageValue, execution);
    if (endpointUrlValue.startsWith("vm:")) {
        LocalMuleClient client = this.getMuleContext().getClient();
        MuleMessage message = new DefaultMuleMessage(payload, this.getMuleContext());
        MuleMessage resultMessage = client.send(endpointUrlValue, message);
        Object result = resultMessage.getPayload();
        if (resultVariableValue != null) {
            execution.setVariable(resultVariableValue, result);
        }
    } else {
        HttpClientBuilder clientBuilder = HttpClientBuilder.create();
        if (usernameValue != null && passwordValue != null) {
            CredentialsProvider provider = new BasicCredentialsProvider();
            UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(usernameValue, passwordValue);
            provider.setCredentials(new AuthScope("localhost", -1, "mule-realm"), credentials);
            clientBuilder.setDefaultCredentialsProvider(provider);
        }
        HttpClient client = clientBuilder.build();
        HttpPost request = new HttpPost(endpointUrlValue);
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(payload);
            oos.flush();
            oos.close();
            request.setEntity(new ByteArrayEntity(baos.toByteArray()));
        } catch (Exception e) {
            throw new ActivitiException("Error setting message payload", e);
        }
        byte[] responseBytes = null;
        try {
            // execute the POST request
            HttpResponse response = client.execute(request);
            responseBytes = IOUtils.toByteArray(response.getEntity().getContent());
        } finally {
            // release any connection resources used by the method
            request.releaseConnection();
        }
        if (responseBytes != null) {
            try {
                ByteArrayInputStream in = new ByteArrayInputStream(responseBytes);
                ObjectInputStream is = new ObjectInputStream(in);
                Object result = is.readObject();
                if (resultVariableValue != null) {
                    execution.setVariable(resultVariableValue, result);
                }
            } catch (Exception e) {
                throw new ActivitiException("Failed to read response value", e);
            }
        }
    }
    this.leave(execution);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) ActivitiException(org.activiti.engine.ActivitiException) ScriptingEngines(org.activiti.engine.impl.scripting.ScriptingEngines) HttpResponse(org.apache.http.HttpResponse) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ActivitiException(org.activiti.engine.ActivitiException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) MuleMessage(org.mule.api.MuleMessage) DefaultMuleMessage(org.mule.DefaultMuleMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpClient(org.apache.http.client.HttpClient) AuthScope(org.apache.http.auth.AuthScope) LocalMuleClient(org.mule.api.client.LocalMuleClient) DefaultMuleMessage(org.mule.DefaultMuleMessage) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

AuthScope (org.apache.http.auth.AuthScope)103 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)64 CredentialsProvider (org.apache.http.client.CredentialsProvider)50 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)49 HttpHost (org.apache.http.HttpHost)30 Credentials (org.apache.http.auth.Credentials)25 Test (org.junit.Test)22 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)19 HttpResponse (org.apache.http.HttpResponse)17 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)15 HttpGet (org.apache.http.client.methods.HttpGet)14 BasicScheme (org.apache.http.impl.auth.BasicScheme)14 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)12 IOException (java.io.IOException)11 HttpEntity (org.apache.http.HttpEntity)10 AuthCache (org.apache.http.client.AuthCache)10 BasicAuthCache (org.apache.http.impl.client.BasicAuthCache)10 AuthScheme (org.apache.http.auth.AuthScheme)8 NTCredentials (org.apache.http.auth.NTCredentials)8 URL (java.net.URL)6