Example usage for org.apache.http.auth AuthScope ANY

List of usage examples for org.apache.http.auth AuthScope ANY

Introduction

In this page you can find the example usage for org.apache.http.auth AuthScope ANY.

Prototype

AuthScope ANY

To view the source code for org.apache.http.auth AuthScope ANY.

Click Source Link

Document

Default scope matching any host, port, realm and authentication scheme.

Usage

From source file:com.github.caldav4j.BaseTestCase.java

public static HttpClient createHttpClient(CaldavCredential caldavCredential) {
    // HttpClient 4 requires a Cred providers, to be added during creation of client
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials(caldavCredential.user, caldavCredential.password));

    return HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
}

From source file:httputils.RavelloHttpClient.java

public RavelloHttpClient(String username, String password) {
    this.client = HttpClients.createDefault();

    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials(username + ":" + password));
    this.context = HttpClientContext.create();
    this.context.setCredentialsProvider(credentialsProvider);
}

From source file:org.apache.camel.component.http4.PreemptiveAuthInterceptor.java

public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE);
    // If no auth scheme avaialble yet, try to initialize it preemptively
    if (authState.getAuthScheme() == null) {
        AuthScheme authScheme = (AuthScheme) context.getAttribute("preemptive-auth");
        CredentialsProvider credsProvider = (CredentialsProvider) context
                .getAttribute(HttpClientContext.CREDS_PROVIDER);
        if (authScheme != null) {
            Credentials creds = credsProvider.getCredentials(AuthScope.ANY);
            if (creds == null) {
                throw new HttpException("No credentials for preemptive authentication");
            }//  w  w w .ja v  a2  s  .c o  m
            authState.update(authScheme, creds);
        }
    }

}

From source file:com.muhardin.endy.training.ws.aplikasi.absen.rest.client.AbsenRestClient.java

public AbsenRestClient() {
    try {/*from w  w w .  jav  a 2 s.c o  m*/
        SSLContextBuilder builder = new SSLContextBuilder();
        builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());

        CredentialsProvider provider = new BasicCredentialsProvider();
        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("endy", "123");
        provider.setCredentials(AuthScope.ANY, credentials);
        HttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider)
                .setSSLSocketFactory(sslsf).build();

        restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory(client));
        restTemplate.setErrorHandler(new AbsenRestClientErrorHandler());
    } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException ex) {
        Logger.getLogger(AbsenRestClient.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:org.jmonkey.external.bintray.BintrayApiClient.java

private CloseableHttpClient createAuthenticatedClient() {

    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials(config.getUser(), config.getApiKey()));

    return HttpClients.custom().setDefaultCookieStore(new BasicCookieStore()).setUserAgent(userAgent)
            .setRedirectStrategy(new LaxRedirectStrategy()).setDefaultCredentialsProvider(credentialsProvider)
            .build();// w  w  w.  j  a  v a2  s .c  o m
}

From source file:com.eviware.soapui.support.httpclient.JCIFSTest.java

@Test
public void test() throws ParseException, IOException {
    try {/* w  w w . ja va2 s.com*/
        DefaultHttpClient httpClient = new DefaultHttpClient();

        httpClient.getAuthSchemes().register(AuthPolicy.NTLM, new NTLMSchemeFactory());
        httpClient.getAuthSchemes().register(AuthPolicy.SPNEGO, new NTLMSchemeFactory());

        NTCredentials creds = new NTCredentials("testuser", "kebabsalladT357", "", "");
        httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, creds);

        HttpHost target = new HttpHost("dev-appsrv01.eviware.local", 81, "http");
        HttpContext localContext = new BasicHttpContext();
        HttpGet httpget = new HttpGet("/");

        HttpResponse response1 = httpClient.execute(target, httpget, localContext);
        HttpEntity entity1 = response1.getEntity();

        //      System.out.println( "----------------------------------------" );
        //System.out.println( response1.getStatusLine() );
        //      System.out.println( "----------------------------------------" );
        if (entity1 != null) {
            //System.out.println( EntityUtils.toString( entity1 ) );
        }
        //      System.out.println( "----------------------------------------" );

        // This ensures the connection gets released back to the manager
        EntityUtils.consume(entity1);

        Assert.assertEquals(response1.getStatusLine().getStatusCode(), 200);
    } catch (UnknownHostException e) {
        /* ignore */
    } catch (HttpHostConnectException e) {
        /* ignore */
    } catch (SocketException e) {
        /* ignore */
    }

    Assert.assertTrue(true);
}

From source file:org.wildfly.swarm.jaxrs.SimpleHttp.java

protected Response getUrlContents(String theUrl, boolean useAuth, boolean followRedirects) {

    StringBuilder content = new StringBuilder();
    int code;/*from www  .j  a  v a2  s . c  o  m*/

    try {

        CredentialsProvider provider = new BasicCredentialsProvider();

        HttpClientBuilder builder = HttpClientBuilder.create();
        if (!followRedirects) {
            builder.disableRedirectHandling();
        }
        if (useAuth) {
            UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("admin", "password");
            provider.setCredentials(AuthScope.ANY, credentials);
            builder.setDefaultCredentialsProvider(provider);
        }
        HttpClient client = builder.build();

        HttpResponse response = client.execute(new HttpGet(theUrl));
        code = response.getStatusLine().getStatusCode();

        if (null == response.getEntity()) {
            throw new RuntimeException("No response content present");
        }

        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(response.getEntity().getContent()));

        String line;

        while ((line = bufferedReader.readLine()) != null) {
            content.append(line + "\n");
        }
        bufferedReader.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    return new Response(code, content.toString());
}

From source file:goofyhts.torrentkinesis.test.HttpTest.java

@Test
public void testGet() {
    try (CloseableHttpClient client = HttpClients.createDefault()) {
        HttpClientContext context = new HttpClientContext();
        CredentialsProvider credProv = new BasicCredentialsProvider();
        credProv.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("root", "Whcinhry21#"));
        context.setCredentialsProvider(credProv);
        HttpGet httpGet = new HttpGet("http://localhost:8150/gui/token.html");

        CloseableHttpResponse response = client.execute(httpGet, context);
        String responseBody = IOUtils.toString(response.getEntity().getContent());
        System.out.println(responseBody);
        Document doc = Jsoup.parse(responseBody);
        System.out.println(doc.getElementById("token").text());
    } catch (ClientProtocolException e) {
        e.printStackTrace();//from   w  w w .ja  v  a 2 s  .  c  o  m
    } catch (IOException e) {
        e.printStackTrace();
    }
    //Assert.assertTrue(true);
}

From source file:org.springframework.cloud.dataflow.shell.command.support.HttpClientUtils.java

/**
 * Ensures that the passed-in {@link RestTemplate} is using the Apache HTTP Client. If the optional {@code username} AND
 * {@code password} are not empty, then a {@link BasicCredentialsProvider} will be added to the {@link CloseableHttpClient}.
 *
 * Furthermore, you can set the underlying {@link SSLContext} of the {@link HttpClient} allowing you to accept self-signed
 * certificates./*ww w  .  ja v  a 2s . c o m*/
 *
 * @param restTemplate Must not be null
 * @param username Can be null
 * @param password Can be null
 * @param skipSslValidation Use with caution! If true certificate warnings will be ignored.
 */
public static void prepareRestTemplate(RestTemplate restTemplate, URI host, String username, String password,
        boolean skipSslValidation) {

    Assert.notNull(restTemplate, "The provided RestTemplate must not be null.");

    final HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();

    if (StringUtils.hasText(username) && StringUtils.hasText(password)) {
        final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
        httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
    }

    if (skipSslValidation) {
        httpClientBuilder.setSSLContext(HttpClientUtils.buildCertificateIgnoringSslContext());
        httpClientBuilder.setSSLHostnameVerifier(new NoopHostnameVerifier());
    }

    final CloseableHttpClient httpClient = httpClientBuilder.build();
    final HttpHost targetHost = new HttpHost(host.getHost(), host.getPort(), host.getScheme());

    final HttpComponentsClientHttpRequestFactory requestFactory = new PreemptiveBasicAuthHttpComponentsClientHttpRequestFactory(
            httpClient, targetHost);
    restTemplate.setRequestFactory(requestFactory);
}

From source file:org.apache.edgent.connectors.http.HttpClients.java

/**
 * Method to create a basic authentication HTTP client.
 * The functions {@code user} and {@code password} are called
 * when this method is invoked to obtain the user and password
 * and runtime.//w  ww .j  ava  2 s  . c o m
 * 
 * @param user Function that provides user for authentication
 * @param password  Function that provides password for authentication
 * @return HTTP client with basic authentication.
 * 
 * @see HttpStreams
 */
public static CloseableHttpClient basic(Supplier<String> user, Supplier<String> password) {

    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user.get(), password.get()));

    return HttpClientBuilder.create().setDefaultCredentialsProvider(credsProvider).build();
}