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:org.sonatype.nexus.ant.staging.deploy.ZapperImpl.java

@Override
public void deployDirectory(final ZapperRequest zapperRequest) throws IOException {
    try {// w  ww .j a va 2  s  .  c om
        HttpHost proxyServer = null;
        BasicCredentialsProvider credentialsProvider = null;
        if (!StringUtils.isBlank(zapperRequest.getProxyProtocol())) {
            proxyServer = new HttpHost(zapperRequest.getProxyHost(), zapperRequest.getProxyPort(),
                    zapperRequest.getProxyProtocol());

            if (!StringUtils.isBlank(zapperRequest.getProxyUsername())) {
                UsernamePasswordCredentials proxyCredentials = new UsernamePasswordCredentials(
                        zapperRequest.getProxyUsername(), zapperRequest.getProxyPassword());

                credentialsProvider = new BasicCredentialsProvider();
                credentialsProvider.setCredentials(new AuthScope(proxyServer.getHostName(),
                        proxyServer.getPort(), AuthScope.ANY_REALM, proxyServer.getSchemeName()),
                        proxyCredentials);
            }
        }

        if (!StringUtils.isBlank(zapperRequest.getRemoteUsername())) {
            UsernamePasswordCredentials remoteCredentials = new UsernamePasswordCredentials(
                    zapperRequest.getRemoteUsername(), zapperRequest.getRemotePassword());

            if (credentialsProvider == null) {
                credentialsProvider = new BasicCredentialsProvider();
            }

            credentialsProvider.setCredentials(AuthScope.ANY, remoteCredentials);
        }

        final Parameters parameters = ParametersBuilder.defaults().build();
        final Hc4ClientBuilder clientBuilder = new Hc4ClientBuilder(parameters, zapperRequest.getRemoteUrl());
        if (credentialsProvider != null) {
            clientBuilder.withPreemptiveRealm(credentialsProvider);
        }
        if (proxyServer != null) {
            clientBuilder.withProxy(proxyServer);
        }
        final Client client = clientBuilder.build();
        final IOSourceListable deployables = new DirectoryIOSource(zapperRequest.getStageRepository());

        try {
            client.upload(deployables);
        } finally {
            client.close();
        }
    } catch (IOException e) {
        throw e;
    } catch (Exception e) {
        throw new IOException("Unable to deploy!", e);
    }
}

From source file:org.oscarehr.fax.core.FaxImporter.java

public void poll() {

    List<FaxConfig> faxConfigList = faxConfigDao.findAll(null, null);
    DefaultHttpClient client = new DefaultHttpClient();

    for (FaxConfig faxConfig : faxConfigList) {
        if (faxConfig.isActive()) {

            client.getCredentialsProvider().setCredentials(AuthScope.ANY,
                    new UsernamePasswordCredentials(faxConfig.getSiteUser(), faxConfig.getPasswd()));

            HttpGet mGet = new HttpGet(faxConfig.getUrl() + PATH + "/" + faxConfig.getFaxUser());
            mGet.setHeader("accept", "application/json");
            mGet.setHeader("user", faxConfig.getFaxUser());
            mGet.setHeader("passwd", faxConfig.getFaxPasswd());

            try {
                HttpResponse response = client.execute(mGet);
                log.info("RESPONSE: " + response.getStatusLine().getStatusCode());

                if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

                    HttpEntity httpEntity = response.getEntity();
                    String content = EntityUtils.toString(httpEntity);

                    log.info("CONTENT: " + content);
                    ObjectMapper mapper = new ObjectMapper();

                    List<FaxJob> faxList = mapper.readValue(content, new TypeReference<List<FaxJob>>() {
                    });/*from ww  w.  j av  a  2s.c  o  m*/

                    FaxJob faxFile;
                    for (FaxJob receivedFax : faxList) {
                        if ((faxFile = downloadFax(client, faxConfig, receivedFax)) != null) {
                            if (saveAndInsertIntoQueue(faxConfig, receivedFax, faxFile)) {
                                deleteFax(client, faxConfig, receivedFax);
                            }
                        }
                    }

                }

                mGet.releaseConnection();
            } catch (ClientProtocolException e) {
                log.error("HTTP WS CLIENT ERROR", e);

            } catch (IOException e) {
                log.error("IO ERROR", e);

            } catch (Exception e) {
                log.error("UNKNOWN ERROR ", e);
            } finally {
                mGet.releaseConnection();
            }

        }
    }

}

From source file:edu.emory.bmi.medicurator.tcia.TciaQuery.java

private HttpClient getInsecureClient() throws Exception {
    SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
        public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            return true;
        }//  w w  w .j a v a2  s .c  o m
    }).build();
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
    HttpClientBuilder builder = HttpClientBuilder.create();
    builder.setSSLSocketFactory(sslsf);
    if (Constants.PROXY_HOST != null && Constants.PROXY_PORT != null) {
        HttpHost proxy = new HttpHost(Constants.PROXY_HOST, Constants.PROXY_PORT, "http");
        builder.setProxy(proxy);
    }
    if (Constants.PROXY_USERNAME != null && Constants.PROXY_PASSWORD != null) {
        Credentials credentials = new UsernamePasswordCredentials(Constants.PROXY_USERNAME,
                Constants.PROXY_PASSWORD);
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(AuthScope.ANY, credentials);
        builder.setDefaultCredentialsProvider(credsProvider);
    }
    return builder.build();
}

From source file:com.cprassoc.solr.auth.SolrHttpHandler.java

protected SolrHttpHandler() {
    PoolingClientConnectionManager cm = new PoolingClientConnectionManager();
    if (props == null) {
        props = SolrAuthManager.getProperties();
    }//from w w  w .j av a 2 s.c o m

    if (props.getProperty("solr.ssl.enabled").equals("true")) {
        solrBaseUrl = "https://";
    } else {
        solrBaseUrl = "http://";
    }
    solrBaseUrl += props.getProperty("solr.host.port");
    //  solr.crawler.cloud.server=localhost:9983
    //admin:password123@
    CredentialsProvider provider = new BasicCredentialsProvider();

    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(
            props.getProperty("solr.admin.user"), props.getProperty("solr.admin.pwd"));
    provider.setCredentials(AuthScope.ANY, credentials);

    // client = new DefaultHttpClient(cm);
    client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();

    zkCloudClient = new CloudSolrClient(props.getProperty("solr.zookeeper.port"), client);
    zkCloudClient.setDefaultCollection(props.getProperty("solr.default.collection"));

    //     solrCloudClient = new CloudSolrClient(props.getProperty("solr.host.port"), client);
    //    solrCloudClient.setDefaultCollection(props.getProperty("solr.default.collection"));

    System.out.println("Solr Base URL: " + solrBaseUrl);
}

From source file:com.jskj.asset.client.login.LoginTask.java

@Override
protected Object doInBackground() throws Exception {
    try {/*  w  w  w  .j  av  a 2 s.c  o  m*/
        RestTemplate restTemplate = (RestTemplate) BeanFactory.instance().createBean(RestTemplate.class);

        if (logined) {
            ComResponse<String> com = restTemplate.getForObject(
                    java.net.URI.create(Constants.HTTP + Constants.APPID + "logout"), ComResponse.class);
            if (com.getResponseStatus() != ComResponse.STATUS_OK) {
                return new Exception("logout failed. ");
            } else {
                BaseTreePane.disTabCount.clear();
            }
        }

        Object userNameObj = map.get("userName");
        ;
        Object passwdObj = map.get("userPassword");

        HttpComponentsClientHttpRequestFactory httpRequestFactory = (HttpComponentsClientHttpRequestFactory) restTemplate
                .getRequestFactory();
        DefaultHttpClient httpClient = (DefaultHttpClient) httpRequestFactory.getHttpClient();
        String unicodeStr = UnicodeConverter.toEncodedUnicode(userNameObj.toString(), false);
        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(unicodeStr,
                passwdObj.toString());
        httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, credentials);

        UserSessionEntity session = restTemplate.getForObject(java.net.URI.create(URI),
                UserSessionEntity.class);

        return session;
    } catch (Exception e) {
        e.printStackTrace();
        return e;
    }
}

From source file:org.opencastproject.remotetest.server.DigestAuthenticationTest.java

@Test
public void testBadDigestAuthenticatedGet() throws Exception {
    UsernamePasswordCredentials creds = new UsernamePasswordCredentials("matterhorn_system_account",
            "wrong_password");
    DefaultHttpClient httpclient = new DefaultHttpClient();
    httpclient.getCredentialsProvider().setCredentials(AuthScope.ANY, creds);
    HttpGet get = new HttpGet(BASE_URL + "/welcome.html");
    get.addHeader("X-Requested-Auth", "Digest");
    try {/*ww w .  j  av a2s. c  om*/
        HttpResponse response = httpclient.execute(get);
        Assert.assertEquals(HttpStatus.SC_UNAUTHORIZED, response.getStatusLine().getStatusCode());
    } finally {
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:org.springframework.boot.autoconfigure.elasticsearch.rest.RestClientAutoConfiguration.java

@Bean
@ConditionalOnMissingBean//  ww  w .j  a va 2 s  .  c o m
public RestClientBuilder restClientBuilder() {
    HttpHost[] hosts = this.properties.getUris().stream().map(HttpHost::create).toArray(HttpHost[]::new);
    RestClientBuilder builder = RestClient.builder(hosts);
    PropertyMapper map = PropertyMapper.get();
    map.from(this.properties::getUsername).whenHasText().to((username) -> {
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        Credentials credentials = new UsernamePasswordCredentials(this.properties.getUsername(),
                this.properties.getPassword());
        credentialsProvider.setCredentials(AuthScope.ANY, credentials);
        builder.setHttpClientConfigCallback(
                (httpClientBuilder) -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
    });
    this.builderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(builder));
    return builder;
}

From source file:test.integ.be.fedict.hsm.rest.security.SecurityTest.java

@Test
public void testInvocation() throws Exception {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials("username", "password"));
    HttpGet httpGet = new HttpGet(this.baseURL + "rest/security/test");
    HttpResponse httpResponse = httpClient.execute(httpGet);
    String result = EntityUtils.toString(httpResponse.getEntity());
    LOG.debug("result: " + result);
    StatusLine statusLine = httpResponse.getStatusLine();
    int statusCode = statusLine.getStatusCode();
    assertEquals(200, statusCode);//  www .j  a v  a 2 s .c o m
}

From source file:org.superbiz.AuthBeanTest.java

private String get(final String user, final String password) {
    final BasicCredentialsProvider basicCredentialsProvider = new BasicCredentialsProvider();
    basicCredentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, password));
    final CloseableHttpClient client = HttpClients.custom()
            .setDefaultCredentialsProvider(basicCredentialsProvider).build();

    final HttpHost httpHost = new HttpHost(webapp.getHost(), webapp.getPort(), webapp.getProtocol());
    final AuthCache authCache = new BasicAuthCache();
    final BasicScheme basicAuth = new BasicScheme();
    authCache.put(httpHost, basicAuth);/*from  w w  w .  j a v  a 2  s . c  o m*/
    final HttpClientContext context = HttpClientContext.create();
    context.setAuthCache(authCache);

    final HttpGet get = new HttpGet(webapp.toExternalForm() + "servlet");
    CloseableHttpResponse response = null;
    try {
        response = client.execute(httpHost, get, context);
        return response.getStatusLine().getStatusCode() + " " + EntityUtils.toString(response.getEntity());
    } catch (final IOException e) {
        throw new IllegalStateException(e);
    } finally {
        try {
            IO.close(response);
        } catch (final IOException e) {
            // no-op
        }
    }
}