Search in sources :

Example 61 with CredentialsProvider

use of org.apache.http.client.CredentialsProvider in project opennms by OpenNMS.

the class OnmsRestEventsClient method getNewClient.

private CloseableHttpClient getNewClient() {
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(onmsUserName, onmsPassWord));
    CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    return httpclient;
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 62 with CredentialsProvider

use of org.apache.http.client.CredentialsProvider 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 63 with CredentialsProvider

use of org.apache.http.client.CredentialsProvider 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)

Example 64 with CredentialsProvider

use of org.apache.http.client.CredentialsProvider in project Activiti by Activiti.

the class SerializableVariablesDiabledTest method assertResponseStatus.

public void assertResponseStatus(HttpUriRequest request, int expectedStatusCode) {
    CloseableHttpResponse response = null;
    try {
        CredentialsProvider provider = new BasicCredentialsProvider();
        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("kermit", "kermit");
        provider.setCredentials(AuthScope.ANY, credentials);
        HttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();
        response = (CloseableHttpResponse) client.execute(request);
        int statusCode = response.getStatusLine().getStatusCode();
        Assert.assertEquals(expectedStatusCode, statusCode);
        if (client instanceof CloseableHttpClient) {
            ((CloseableHttpClient) client).close();
        }
        response.close();
    } catch (ClientProtocolException e) {
        Assert.fail(e.getMessage());
    } catch (IOException e) {
        Assert.fail(e.getMessage());
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpClient(org.apache.http.client.HttpClient) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) IOException(java.io.IOException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 65 with CredentialsProvider

use of org.apache.http.client.CredentialsProvider in project Activiti by Activiti.

the class BaseJPARestTestCase method executeHttpRequest.

public HttpResponse executeHttpRequest(HttpUriRequest request, int expectedStatusCode) {
    CredentialsProvider provider = new BasicCredentialsProvider();
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("kermit", "kermit");
    provider.setCredentials(AuthScope.ANY, credentials);
    HttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();
    try {
        if (request.getFirstHeader(HttpHeaders.CONTENT_TYPE) == null) {
            // Revert to default content-type 
            request.addHeader(new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json"));
        }
        HttpResponse response = client.execute(request);
        Assert.assertNotNull(response.getStatusLine());
        Assert.assertEquals(expectedStatusCode, response.getStatusLine().getStatusCode());
        return response;
    } catch (ClientProtocolException e) {
        Assert.fail(e.getMessage());
    } catch (IOException e) {
        Assert.fail(e.getMessage());
    }
    return null;
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) IOException(java.io.IOException) BasicHeader(org.apache.http.message.BasicHeader) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Aggregations

CredentialsProvider (org.apache.http.client.CredentialsProvider)92 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)65 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)57 AuthScope (org.apache.http.auth.AuthScope)51 HttpHost (org.apache.http.HttpHost)28 HttpGet (org.apache.http.client.methods.HttpGet)19 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)17 Test (org.junit.Test)17 HttpResponse (org.apache.http.HttpResponse)16 Credentials (org.apache.http.auth.Credentials)16 IOException (java.io.IOException)13 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)12 BasicScheme (org.apache.http.impl.auth.BasicScheme)12 Header (org.apache.http.Header)11 HttpRequest (org.apache.http.HttpRequest)10 AuthCache (org.apache.http.client.AuthCache)10 BasicAuthCache (org.apache.http.impl.client.BasicAuthCache)10 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)10 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)9 HttpEntity (org.apache.http.HttpEntity)8