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:io.wcm.caravan.commons.httpasyncclient.impl.HttpAsyncClientItem.java

/**
 * @param config Http client configuration
 *///  w w  w .jav a 2s . c  om
HttpAsyncClientItem(HttpClientConfig config) {
    this.config = config;

    // optional SSL client certificate support
    SSLContext sslContext;
    if (CertificateLoader.isSslKeyManagerEnabled(config) || CertificateLoader.isSslTrustStoreEnbaled(config)) {
        try {
            sslContext = CertificateLoader.buildSSLContext(config);
        } catch (IOException | GeneralSecurityException ex) {
            throw new IllegalArgumentException("Invalid SSL client certificate configuration.", ex);
        }
    } else {
        sslContext = CertificateLoader.createDefaultSSlContext();
    }

    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    // optional proxy authentication
    if (StringUtils.isNotEmpty(config.getProxyUser())) {
        credentialsProvider.setCredentials(new AuthScope(config.getProxyHost(), config.getProxyPort()),
                new UsernamePasswordCredentials(config.getProxyUser(), config.getProxyPassword()));
    }
    // optional http basic authentication support
    if (StringUtils.isNotEmpty(config.getHttpUser())) {
        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(config.getHttpUser(), config.getHttpPassword()));
    }

    // build http clients
    asyncConnectionManager = buildAsyncConnectionManager(config, sslContext);
    httpAsyncClient = buildHttpAsyncClient(config, asyncConnectionManager, credentialsProvider);

    // start async client
    httpAsyncClient.start();
}

From source file:org.fusesource.fabric.itests.paxexam.FabricMavenProxyTest.java

@Test
public void testUpload() throws Exception {
    String featureLocation = System.getProperty("feature.location");
    System.out.println("Testing with feature from:" + featureLocation);
    System.err.println(executeCommand("fabric:create -n"));
    Set<Container> containers = ContainerBuilder.create(2).withName("maven").withProfiles("fabric")
            .assertProvisioningResult().build();

    FabricService fabricService = getFabricService();
    CuratorFramework curator = getCurator();
    List<String> children = getChildren(curator, ZkPath.MAVEN_PROXY.getPath("upload"));
    List<String> uploadUrls = new ArrayList<String>();
    for (String child : children) {
        String uploadeUrl = getSubstitutedPath(curator, ZkPath.MAVEN_PROXY.getPath("upload") + "/" + child);
        uploadUrls.add(uploadeUrl);/*ww  w .j  a  v  a 2  s.  c om*/
    }
    //Pick a random maven proxy from the list.
    Random random = new Random();
    int index = random.nextInt(uploadUrls.size());
    String targetUrl = uploadUrls.get(index);

    String uploadUrl = targetUrl + "itest/itest/1.0/itest-1.0-features.xml";
    System.out.println("Using URI: " + uploadUrl);
    DefaultHttpClient client = new DefaultHttpClient();
    HttpPut put = new HttpPut(uploadUrl);
    client.getCredentialsProvider().setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials("admin", "admin"));

    FileNIOEntity entity = new FileNIOEntity(new File(featureLocation), "text/xml");
    put.setEntity(entity);
    HttpResponse response = client.execute(put);
    System.err.println("Response:" + response.getStatusLine());
    Assert.assertTrue(
            response.getStatusLine().getStatusCode() == 200 || response.getStatusLine().getStatusCode() == 202);

    System.err.println(
            executeCommand("fabric:profile-edit --repositories mvn:itest/itest/1.0/xml/features default"));
    System.err.println(executeCommand("fabric:profile-edit --features example-cbr default"));
    Provision.waitForContainerStatus(containers, PROVISION_TIMEOUT);
}

From source file:org.fusesource.fabric.itests.smoke.FabricMavenProxyTest.java

@Test
public void testUpload() throws Exception {
    String featureLocation = System.getProperty("feature.location");
    System.out.println("Testing with feature from:" + featureLocation);
    System.err.println(executeCommand("fabric:create -n"));
    Set<Container> containers = ContainerBuilder.create(2).withName("maven").withProfiles("fabric")
            .assertProvisioningResult().build();

    FabricService fabricService = getFabricService();
    CuratorFramework curator = getCurator();
    List<String> children = getChildren(curator, ZkPath.MAVEN_PROXY.getPath("upload"));
    List<String> uploadUrls = new ArrayList<String>();
    for (String child : children) {
        String uploadeUrl = getSubstitutedPath(curator, ZkPath.MAVEN_PROXY.getPath("upload") + "/" + child);
        uploadUrls.add(uploadeUrl);/*from  w w  w  . jav  a  2 s. c  o m*/
    }
    //Pick a random maven proxy from the list.
    Random random = new Random();
    int index = random.nextInt(uploadUrls.size());
    String targetUrl = uploadUrls.get(index);

    String uploadUrl = targetUrl + "itest/itest/1.0/itest-1.0-features.xml";
    System.out.println("Using URI: " + uploadUrl);
    DefaultHttpClient client = new DefaultHttpClient();
    HttpPut put = new HttpPut(uploadUrl);
    client.getCredentialsProvider().setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials("admin", "admin"));

    FileNIOEntity entity = new FileNIOEntity(new File(featureLocation), "text/xml");
    put.setEntity(entity);
    HttpResponse response = client.execute(put);
    System.err.println("Response:" + response.getStatusLine());
    Assert.assertTrue(
            response.getStatusLine().getStatusCode() == 200 || response.getStatusLine().getStatusCode() == 202);

    System.err.println(
            executeCommand("fabric:profile-edit --repositories mvn:itest/itest/1.0/xml/features default"));
    System.err.println(executeCommand("fabric:profile-edit --features example-cbr default"));
    Provision.containerStatus(containers, PROVISION_TIMEOUT);
}

From source file:org.ops4j.pax.web.itest.DigestAuthenticationTest.java

@Test
public void shouldDenyAccessOnWrongPassword() throws Exception {
    assertThat(servletContext.getContextPath(), is("/digest"));

    String path = String.format("http://localhost:%d/digest/hello", getHttpPort());
    HttpClientContext context = HttpClientContext.create();
    BasicCredentialsProvider cp = new BasicCredentialsProvider();
    cp.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("mustermann", "wrong"));
    CloseableHttpClient client = HttpClients.custom().setDefaultCredentialsProvider(cp).build();

    HttpGet httpGet = new HttpGet(path);
    HttpResponse response = client.execute(httpGet, context);

    int statusCode = response.getStatusLine().getStatusCode();
    assertThat(statusCode, is(401));//from www  .j  a  v  a  2 s.c o  m
}

From source file:org.eclipse.lyo.testsuite.server.util.OSLCUtils.java

private static HttpClient getHttpClient(Credentials creds) {
    //If this is our first request, initialized our httpclient
    if (httpclient == null) {
        httpclient = new DefaultHttpClient();
        setupLazySSLSupport(httpclient);
        if (creds != null)
            httpclient.getCredentialsProvider().setCredentials(AuthScope.ANY, creds);
    }/*  w w  w  .jav  a2  s.  com*/
    return httpclient;
}

From source file:com.activiti.service.activiti.ActivitiClientService.java

public CloseableHttpClient getHttpClient(String userName, String password) {

    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));

    SSLConnectionSocketFactory sslsf = null;
    try {/*  w  ww.j a va 2s .  c  o m*/
        SSLContextBuilder builder = new SSLContextBuilder();
        builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
        sslsf = new SSLConnectionSocketFactory(builder.build(),
                SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    } catch (Exception e) {
        log.warn("Could not configure HTTP client to use SSL", e);
    }

    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);

    if (sslsf != null) {
        httpClientBuilder.setSSLSocketFactory(sslsf);
    }

    return httpClientBuilder.build();
}

From source file:org.fuin.esc.eshttp.ESHttpEventStoreIT.java

@Before
public void setup() throws MalformedURLException {

    final ThreadFactory threadFactory = Executors.defaultThreadFactory();
    final URL url = new URL("http://127.0.0.1:2113/");
    final XmlDeSerializer xmlDeSer = new XmlDeSerializer(false, MyMeta.class, MyEvent.class, EscEvent.class,
            EscEvents.class, EscMeta.class);

    final SimpleSerializerDeserializerRegistry registry = new SimpleSerializerDeserializerRegistry();
    registry.add(new SerializedDataType(MyEvent.TYPE.asBaseType()), "application/xml", xmlDeSer);
    registry.add(new SerializedDataType(MyMeta.TYPE.asBaseType()), "application/xml", xmlDeSer);
    registry.add(new SerializedDataType(EscEvent.TYPE.asBaseType()), "application/xml", xmlDeSer);
    registry.add(new SerializedDataType(EscEvents.TYPE.asBaseType()), "application/xml", xmlDeSer);
    registry.add(new SerializedDataType(EscMeta.TYPE.asBaseType()), "application/xml", xmlDeSer);

    registry.add(new SerializedDataType(CUSTOMER_CREATED.asBaseType()), "application/xml", xmlDeSer);
    registry.add(new SerializedDataType(CUSTOMER_RENAMED.asBaseType()), "application/xml", xmlDeSer);

    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("admin", "changeit");
    credentialsProvider.setCredentials(AuthScope.ANY, credentials);

    testee = new ESHttpEventStore(threadFactory, url, ESEnvelopeType.XML, registry, registry,
            credentialsProvider);/*  w w  w.j  a v  a 2 s. co m*/
    testee.open();

}

From source file:org.sentilo.common.test.rest.RESTClientImplTest.java

@Test
public void afterPropertiesSet() throws Exception {
    ReflectionTestUtils.setField(restClient, "httpClient", null);

    restClient.afterPropertiesSet();//w  ww  .  java2  s.  com

    final HttpClient defaultHttpClient = (HttpClient) ReflectionTestUtils.getField(restClient, "httpClient");
    Assert.assertNotNull(defaultHttpClient);
    Assert.assertTrue(defaultHttpClient instanceof DefaultHttpClient);
    Assert.assertTrue(((DefaultHttpClient) defaultHttpClient).getRequestInterceptorCount() > 0);
    Assert.assertNotNull(
            ((DefaultHttpClient) defaultHttpClient).getCredentialsProvider().getCredentials(AuthScope.ANY));
}

From source file:org.springframework.data.solr.HttpSolrClientFactoryTests.java

@Test
public void testInitFactoryWithAuthentication() {
    HttpSolrClientFactory factory = new HttpSolrClientFactory(solrClient, "core",
            new UsernamePasswordCredentials("username", "password"), "BASIC");

    AbstractHttpClient solrHttpClient = (AbstractHttpClient) ((HttpSolrClient) factory.getSolrClient())
            .getHttpClient();//from   w w w .  jav a 2  s. com
    Assert.assertNotNull(solrHttpClient.getCredentialsProvider().getCredentials(AuthScope.ANY));
    Assert.assertNotNull(solrHttpClient.getParams().getParameter(AuthPNames.TARGET_AUTH_PREF));
    Assert.assertEquals("username", ((UsernamePasswordCredentials) solrHttpClient.getCredentialsProvider()
            .getCredentials(AuthScope.ANY)).getUserName());
    Assert.assertEquals("password", ((UsernamePasswordCredentials) solrHttpClient.getCredentialsProvider()
            .getCredentials(AuthScope.ANY)).getPassword());
}