Example usage for org.apache.http.auth UsernamePasswordCredentials UsernamePasswordCredentials

List of usage examples for org.apache.http.auth UsernamePasswordCredentials UsernamePasswordCredentials

Introduction

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

Prototype

public UsernamePasswordCredentials(final String usernamePassword) 

Source Link

Document

The constructor with the username and password combined string argument.

Usage

From source file:org.opendaylight.alto.manager.AltoManager.java

protected HttpClient initiateHttpClient() {
    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("admin:admin"));
    return HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).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:com.rometools.test.HttpClientFeedFetcherTest.java

/**
 * @see com.rometools.rome.fetcher.impl.AbstractJettyTest#getAuthenticatedFeedFetcher()
 *//*from  w  ww. jav a2s  .  c om*/
@Override
public FeedFetcher getAuthenticatedFeedFetcher() {
    HttpClientFeedFetcher fetcher = new HttpClientFeedFetcher();
    fetcher.setContextSupplier(new ContextSupplier() {
        @Override
        public HttpClientContext getContext(HttpHost host) {
            CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(AuthScope.ANY,
                    new UsernamePasswordCredentials("username:password"));
            HttpClientContext context = HttpClientContext.create();
            context.setCredentialsProvider(credentialsProvider);

            return context;
        }
    });

    return fetcher;
}

From source file:org.commonjava.redhat.maven.rv.util.InputUtils.java

public static File getFile(final String location, final File downloadsDir, final boolean deleteExisting)
        throws ValidationException {
    if (client == null) {
        final DefaultHttpClient hc = new DefaultHttpClient();
        hc.setRedirectStrategy(new DefaultRedirectStrategy());

        final String proxyHost = System.getProperty("http.proxyHost");
        final int proxyPort = Integer.parseInt(System.getProperty("http.proxyPort", "-1"));

        if (proxyHost != null && proxyPort > 0) {
            final HttpHost proxy = new HttpHost(proxyHost, proxyPort);
            hc.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);
        }/*  ww  w . ja v a  2s  . co  m*/

        client = hc;
    }

    File result = null;

    if (location.startsWith("http")) {
        LOGGER.info("Downloading: '" + location + "'...");

        try {
            final URL url = new URL(location);
            final String userpass = url.getUserInfo();
            if (!isEmpty(userpass)) {
                final AuthScope scope = new AuthScope(url.getHost(), url.getPort());
                final Credentials creds = new UsernamePasswordCredentials(userpass);

                client.getCredentialsProvider().setCredentials(scope, creds);
            }
        } catch (final MalformedURLException e) {
            LOGGER.error("Malformed URL: '" + location + "'", e);
            throw new ValidationException("Failed to download: %s. Reason: %s", e, location, e.getMessage());
        }

        final File downloaded = new File(downloadsDir, new File(location).getName());
        if (deleteExisting && downloaded.exists()) {
            downloaded.delete();
        }

        if (!downloaded.exists()) {
            HttpGet get = new HttpGet(location);
            OutputStream out = null;
            try {
                HttpResponse response = client.execute(get);
                // Work around for scenario where we are loading from a server
                // that does a refresh e.g. gitweb
                if (response.containsHeader("Cache-control")) {
                    LOGGER.info("Waiting for server to generate cache...");
                    try {
                        Thread.sleep(5000);
                    } catch (final InterruptedException e) {
                    }
                    get.abort();
                    get = new HttpGet(location);
                    response = client.execute(get);
                }
                final int code = response.getStatusLine().getStatusCode();
                if (code == 200) {
                    final InputStream in = response.getEntity().getContent();
                    out = new FileOutputStream(downloaded);

                    copy(in, out);
                } else {
                    LOGGER.info(String.format("Received status: '%s' while downloading: %s",
                            response.getStatusLine(), location));

                    throw new ValidationException("Received status: '%s' while downloading: %s",
                            response.getStatusLine(), location);
                }
            } catch (final ClientProtocolException e) {
                throw new ValidationException("Failed to download: '%s'. Error: %s", e, location,
                        e.getMessage());
            } catch (final IOException e) {
                throw new ValidationException("Failed to download: '%s'. Error: %s", e, location,
                        e.getMessage());
            } finally {
                closeQuietly(out);
                get.abort();
            }
        }

        result = downloaded;
    } else {
        LOGGER.info("Using local file: '" + location + "'...");

        result = new File(location);
    }

    return result;
}

From source file:com.sap.core.odata.testutil.tool.core.AbstractTestClient.java

private DefaultHttpClient createHttpClient(final CallerConfig config) {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    if (config.isProxySet()) {
        initProxy(httpClient, config);//from  w  w  w.j  a v  a2s  . co m
    }

    if (config.isBasicAuthCredentialsSet()) {
        final URI baseUri = config.getBaseUri();
        httpClient.getCredentialsProvider().setCredentials(new AuthScope(baseUri.getHost(), baseUri.getPort()),
                new UsernamePasswordCredentials(config.getBasicAuthCredentials()));
    }
    return httpClient;
}

From source file:edu.syr.bytecast.githubcampfire.CampfirePost.java

public CampfirePostReply post(String message) {
    //see: http://stackoverflow.com/questions/2603691/android-httpclient-and-https
    try {//from  www.j  a  v a  2 s .com
        String xml_message = "<message><type>TextMessage</type><body>" + message + "</body></message>";

        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));

        BasicSchemeFactory factory = new BasicSchemeFactory();

        HttpParams params = new BasicHttpParams();
        params.setParameter("realm", "https://trifort.campfirenow.com/");

        SingleClientConnManager mgr = new SingleClientConnManager(params, schemeRegistry);
        DefaultHttpClient client = new DefaultHttpClient(mgr, params);
        String url = "https://trifort.campfirenow.com/room/" + m_room + "/speak.xml";
        HttpPost post = new HttpPost(url);
        post.setHeader("Content-type", "application/xml");

        AuthScheme scheme = factory.newInstance(params);
        Header header = scheme.authenticate(new UsernamePasswordCredentials(m_apiKey), post);

        HttpEntity entity = new StringEntity(xml_message);
        post.setEntity(entity);
        post.setHeader(header);
        HttpResponse response = client.execute(post);

        return new CampfirePostReply(response);
    } catch (Exception ex) {//post.setEntity;
        ex.printStackTrace();
        return null;
    }
}

From source file:com.sap.core.odata.testutil.tool.core.SupaController.java

private void init() {
    httpClient = new DefaultHttpClient();
    if (config.isProxySet()) {
        initProxy();// ww w  .j av  a  2  s. c  om
    }

    if (config.isBasicAuthCredentialsSet()) {
        final URI baseUri = config.getBaseUri();
        httpClient.getCredentialsProvider().setCredentials(new AuthScope(baseUri.getHost(), baseUri.getPort()),
                new UsernamePasswordCredentials(config.getBasicAuthCredentials()));
    }
}