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

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

Introduction

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

Prototype

String ANY_REALM

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

Click Source Link

Document

The null value represents any realm.

Usage

From source file:be.fedict.trust.Credential.java

/**
 * Any scheme and any realm is allowed./*from w  ww .  j av a2s.com*/
 * 
 * @param host
 * @param port
 * @param username
 * @param password
 */
public Credential(String host, int port, String username, String password) {
    this(host, port, AuthScope.ANY_REALM, username, password);
}

From source file:org.sonatype.nexus.ant.staging.deploy.ZapperImpl.java

@Override
public void deployDirectory(final ZapperRequest zapperRequest) throws IOException {
    try {//  w w  w. j av a  2  s. c  o  m
        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.sonatype.nexus.plugin.deploy.ZapperImpl.java

@Override
public void deployDirectory(final ZapperRequest zapperRequest) throws IOException {
    try {/*from  ww  w . j ava2s.com*/
        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.withRealm(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.apache.maven.wagon.providers.http.BasicAuthScopeTest.java

/**
 * Test AuthScope override for all values overridden with "ANY"
 *//*from   ww w . j a  v  a 2s .c om*/
@Test
public void testGetScopeAllAny() {
    BasicAuthScope scope = new BasicAuthScope();
    scope.setHost("ANY");
    scope.setPort("ANY");
    scope.setRealm("ANY");
    AuthScope authScope = scope.getScope("original.host.com", 3456);
    Assert.assertEquals(AuthScope.ANY_HOST, authScope.getHost());
    Assert.assertEquals(AuthScope.ANY_PORT, authScope.getPort());
    Assert.assertEquals(AuthScope.ANY_REALM, authScope.getRealm());
}

From source file:org.opendaylight.defense4all.odl.controller.Connector.java

public void init() throws ExceptionControlApp {

    try {/*from  w  ww. jav a2 s  .c  o  m*/
        restPrefix = "http://" + odlOFC.ipAddrString + ":" + Integer.toString(odlOFC.port);

        // set authentication for rest template
        AuthScope authScope = new AuthScope(odlOFC.hostname, odlOFC.port, AuthScope.ANY_REALM);
        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(odlOFC.username,
                odlOFC.password);
        //         DefaultHttpClient client = new DefaultHttpClient();
        //         client.getCredentialsProvider().setCredentials(authScope, credentials);

        //         HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(client);
        //         restTemplate = new RestTemplate(factory);

        RestTemplateFactory.INSTANCE.setInsecureSsl(true);
        restTemplate = RestTemplateFactory.INSTANCE.createRestTemplate(authScope, credentials);

        if (restTemplate == null)
            throw new Exception("Failed to create restTemplate");
    } catch (Throwable e) {
        log.error("Failed to init connector to " + odlOFC.hostname, e);
        FMHolder.get().getHealthTracker().reportHealthIssue(HealthTracker.MINOR_HEALTH_ISSUE);
        throw new ExceptionControlApp("Failed to init connector to " + odlOFC.hostname, e);
    }
}

From source file:pydio.sdk.java.http.AjxpHttpClient.java

public void refreshCredentials(UsernamePasswordCredentials credentials) {
    this.getCredentialsProvider().setCredentials(
            new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM), credentials);
}

From source file:org.opennms.opennms.pris.plugins.defaults.source.HttpRequisitionSource.java

@Override
public Object dump() throws Exception {
    Requisition requisition = null;//from w w w .ja va 2  s.c  om

    if (getUrl() != null) {
        HttpClientBuilder builder = HttpClientBuilder.create();

        // If username and password was found, inject the credentials
        if (getUserName() != null && getPassword() != null) {

            CredentialsProvider provider = new BasicCredentialsProvider();

            // Create the authentication scope
            AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM);

            // Create credential pair
            UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(getUserName(),
                    getPassword());

            // Inject the credentials
            provider.setCredentials(scope, credentials);

            // Set the default credentials provider
            builder.setDefaultCredentialsProvider(provider);
        }

        HttpClient client = builder.build();
        HttpGet request = new HttpGet(getUrl());
        HttpResponse response = client.execute(request);
        try {

            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(response.getEntity().getContent()));
            JAXBContext jaxbContext = JAXBContext.newInstance(Requisition.class);

            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            requisition = (Requisition) jaxbUnmarshaller.unmarshal(bufferedReader);

        } catch (JAXBException e) {
            LOGGER.error("The response did not contain a valid requisition as xml.", e);
        }
        LOGGER.debug("Got Requisition {}", requisition);
    } else {
        LOGGER.error("Parameter requisition.url is missing in requisition.properties");
    }
    if (requisition == null) {
        LOGGER.error("Requisition is null for unknown reasons");
        return null;
    }
    LOGGER.info("HttpRequisitionSource delivered for requisition '{}'", requisition.getNodes().size());
    return requisition;
}

From source file:org.cvasilak.jboss.mobile.admin.net.TalkToJBossServerTask.java

public TalkToJBossServerTask(Context context, Server server, Callback callback) {
    this.context = context;
    this.client = CustomHTTPClient.getHttpClient();
    this.server = server;
    this.callback = callback;

    this.gjson = ((JBossAdminApplication) context.getApplicationContext()).getJSONParser();
    this.parser = new JsonParser();

    // enable digest authentication
    if (server.getUsername() != null && !server.getUsername().equals("")) {
        Credentials credentials = new UsernamePasswordCredentials(server.getUsername(), server.getPassword());

        client.getCredentialsProvider().setCredentials(
                new AuthScope(server.getHostname(), server.getPort(), AuthScope.ANY_REALM), credentials);
    }//from  ww w  .  jav  a  2 s. c  om
}

From source file:read.taz.TazDownloader.java

private void downloadFile() throws ClientProtocolException, IOException {
    if (tazFile.file.exists()) {
        Log.w(getClass().getSimpleName(), "File " + tazFile + " exists.");
        return;//from   w  w  w .ja v a2s.  co m
    }
    HttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, 30000);
    HttpConnectionParams.setSoTimeout(httpParams, 15000);

    DefaultHttpClient client = new DefaultHttpClient(httpParams);

    Credentials defaultcreds = new UsernamePasswordCredentials(uname, passwd);

    client.getCredentialsProvider()
            .setCredentials(new AuthScope(/* FIXME DRY */"dl.taz.de", 80, AuthScope.ANY_REALM), defaultcreds);

    URI uri = tazFile.getDownloadURI();
    Log.d(getClass().getSimpleName(), "Downloading taz from " + uri);
    HttpGet get = new HttpGet(uri);
    HttpResponse response = client.execute(get);
    if (response.getStatusLine().getStatusCode() != 200) {
        throw new IOException("Download failed with HTTP Error" + response.getStatusLine().getStatusCode());
    }

    String contentType = response.getEntity().getContentType().getValue();
    if (contentType.startsWith("text/html")) {
        Log.d(getClass().getSimpleName(),
                "Content type: " + contentType + " encountered. Assuming a non exisiting file");

        ByteArrayOutputStream htmlOut = new ByteArrayOutputStream();
        response.getEntity().writeTo(htmlOut);
        String resp = new String(htmlOut.toByteArray());
        Log.d(getClass().getSimpleName(), "Response: " + resp);
        throw new FileNotFoundException("No taz found for date " + tazFile.date.getTime());
    } else {
        FileOutputStream tazOut = new FileOutputStream(tazFile.file);
        try {
            response.getEntity().writeTo(tazOut);
        } finally {
            tazOut.close();
        }
    }

    // InputStream docStream = response.getEntity().getContent();
    // FileOutputStream out = null;
    // try {
    // out = tazOut;
    // byte[] buf = new byte[4096];
    // for (int read = docStream.read(buf); read != -1; read = docStream
    // .read(buf)) {
    // out.write(buf, 0, read);
    // }
    // } finally {
    // docStream.close();
    // if (out != null) {
    // out.close();
    // }
    // }

}

From source file:org.cvasilak.jboss.mobile.app.net.TalkToJBossServerTask.java

public TalkToJBossServerTask(Context context, Server server, Callback callback, boolean shouldProcessRequest) {
    this.context = context;
    this.server = server;
    this.callback = callback;
    this.shouldProcessRequest = shouldProcessRequest;

    this.client = CustomHTTPClient.getHttpClient();
    this.jsonBuilder = ((JBossAdminApplication) context.getApplicationContext()).getJSONBuilder();
    this.jsonParser = ((JBossAdminApplication) context.getApplicationContext()).getJSONParser();

    Credentials credentials = new UsernamePasswordCredentials(server.getUsername(), server.getPassword());
    client.getCredentialsProvider().setCredentials(
            new AuthScope(server.getHostname(), server.getPort(), AuthScope.ANY_REALM), credentials);
}