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 userName, final String password) 

Source Link

Document

The constructor with the username and password arguments.

Usage

From source file:org.apache.jena.fuseki.embedded.TestFusekiTestAuth.java

@Test(expected = HttpException.class)
public void testServer_auth_bad_user() {
    BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
    Credentials credentials = new UsernamePasswordCredentials("USERUSER", PASSWORD);
    credsProvider.setCredentials(AuthScope.ANY, credentials);
    HttpClient client = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    try (TypedInputStream in = HttpOp.execHttpGet(FusekiTestAuth.urlDataset(), "*/*", client, null)) {
    } catch (HttpException ex) {
        throw assertAuthHttpException(ex);
    }//from w ww  . j  a v  a 2s  . co  m
}

From source file:com.ibm.ecod.watson.RetrieveAndRankSolrJExample.java

private static HttpClient createHttpClient(String uri, String username, String password) {
    final URI scopeUri = URI.create(uri);

    final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(new AuthScope(scopeUri.getHost(), scopeUri.getPort()),
            new UsernamePasswordCredentials(username, password));

    final HttpClientBuilder builder = HttpClientBuilder.create().setMaxConnTotal(128).setMaxConnPerRoute(32)
            .setDefaultRequestConfig(//  www. j  a v  a  2s  .c om
                    RequestConfig.copy(RequestConfig.DEFAULT).setRedirectsEnabled(true).build());
    builder.setDefaultCredentialsProvider(credentialsProvider);

    return builder.build();
}

From source file:org.miloss.fgsms.presentation.StatusHelper.java

/**
 * determines if an fgsms service is currently accessible. not for use
 * with other services./*from w  w  w  .j  ava 2  s  .c o m*/
 *
 * @param endpoint
 * @return
 */
public String sendGetRequest(String endpoint) {
    //   String result = null;
    if (endpoint.startsWith("http")) {
        // Send a GET request to the servlet
        try {

            URL url = new URL(endpoint);
            int port = url.getPort();
            if (port <= 0) {
                if (endpoint.startsWith("https:")) {
                    port = 443;
                } else {
                    port = 80;
                }
            }

            HttpClientBuilder create = HttpClients.custom();

            if (mode == org.miloss.fgsms.common.Constants.AuthMode.UsernamePassword) {
                CredentialsProvider credsProvider = new BasicCredentialsProvider();
                credsProvider.setCredentials(new AuthScope(url.getHost(), port),
                        new UsernamePasswordCredentials(username, Utility.DE(password)));
                create.setDefaultCredentialsProvider(credsProvider);
                ;

            }
            CloseableHttpClient c = create.build();

            CloseableHttpResponse response = c.execute(new HttpHost(url.getHost(), port),
                    new HttpGet(endpoint));

            c.close();
            int status = response.getStatusLine().getStatusCode();
            if (status == 200) {
                return "OK";
            }
            return String.valueOf(status);

        } catch (Exception ex) {
            Logger.getLogger(Helper.class).log(Level.WARN,
                    "error fetching http doc from " + endpoint + ex.getLocalizedMessage());
            return "offline";
        }
    } else {
        return "undeterminable";
    }
}

From source file:org.jboss.jdf.stacks.StacksFactory.java

private static void configureProxy(final DefaultHttpClient client, final StacksConfiguration stacksConfig) {
    if (stacksConfig.getProxyHost() != null) {
        final String proxyHost = stacksConfig.getProxyHost();
        final int proxyPort = stacksConfig.getProxyPort();
        final HttpHost proxy = new HttpHost(proxyHost, proxyPort);
        client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        final String proxyUsername = stacksConfig.getProxyUser();
        if (proxyUsername != null && !proxyUsername.isEmpty()) {
            final String proxyPassword = stacksConfig.getProxyPassword();
            final AuthScope authScope = new AuthScope(proxyHost, proxyPort);
            final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(proxyUsername,
                    proxyPassword);/*w w  w. java 2 s  .c  o m*/
            client.getCredentialsProvider().setCredentials(authScope, credentials);
        }
    }
}

From source file:org.xwiki.contrib.authentication.http.XWikiHTTPAuthenticator.java

private boolean checkAuth(String username, String password, URI uri)
        throws ClientProtocolException, IOException {
    DefaultHttpClient httpClient = new DefaultHttpClient();

    httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "XWik");
    httpClient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);
    httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);

    httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials(username, password));

    HttpGet httpget = new HttpGet(uri);
    HttpResponse response = httpClient.execute(httpget);

    return response.getStatusLine().getStatusCode() == HttpStatus.SC_OK;
}

From source file:aajavafx.LoginController.java

public static String getPassword(String userName) throws MalformedURLException, JSONException, IOException {
    String password = "";

    Managers manager = new Managers();
    Managers myManager = new Managers();
    Gson gson = new Gson();

    JSONObject jo = new JSONObject();
    CredentialsProvider provider = new BasicCredentialsProvider();
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("ADMIN", "password");
    provider.setCredentials(AuthScope.ANY, credentials);
    HttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();
    HttpGet get = new HttpGet("http://localhost:8080/MainServerREST/api/managers/username/" + userName);

    HttpResponse response = client.execute(get);
    System.out.println("RESPONSE IS: " + response);
    JSONArray jsonArray = new JSONArray(
            IOUtils.toString(response.getEntity().getContent(), Charset.forName("UTF-8")));
    System.out.println(jsonArray);
    for (int i = 0; i < jsonArray.length(); i++) {
        jo = (JSONObject) jsonArray.getJSONObject(i);
        manager = gson.fromJson(jo.toString(), Managers.class);
        if (manager.getManUsername().equals(userName)) {
            password = manager.getManPassword();
        }//from   w w w  .j av a  2 s  .  c  om

        System.out.println(password);

    }
    return password;
}

From source file:com.icoin.trading.bitcoin.client.BitcoinClientDefaultConfig.java

private CredentialsProvider credentialsProvicer() {
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(host, Integer.valueOf(port)),
            new UsernamePasswordCredentials(user, password));
    return credsProvider;
}

From source file:org.pentaho.di.core.util.HttpClientUtil.java

/**
 * Returns context with AuthCache or null in case of any exception was thrown.
 *
 * @param host//from   ww  w  .j a  v  a  2s .co m
 * @param port
 * @param user
 * @param password
 * @param schema
 * @return {@link org.apache.http.client.protocol.HttpClientContext HttpClientContext}
 */
public static HttpClientContext createPreemptiveBasicAuthentication(String host, int port, String user,
        String password, String schema) {
    HttpClientContext localContext = null;
    try {
        HttpHost target = new HttpHost(host, port, schema);
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(target.getHostName(), target.getPort()),
                new UsernamePasswordCredentials(user, password));

        // Create AuthCache instance
        AuthCache authCache = new BasicAuthCache();
        // Generate BASIC scheme object and add it to the local
        // auth cache
        BasicScheme basicAuth = new BasicScheme();
        authCache.put(target, basicAuth);

        // Add AuthCache to the execution context
        localContext = HttpClientContext.create();
        localContext.setAuthCache(authCache);
    } catch (Exception e) {
        return null;
    }
    return localContext;
}

From source file:com.gsma.mobileconnect.utils.RestClient.java

/**
 * Build a HttpClientContext that will preemptively authenticate using Basic Authentication
 *
 * @param username Username for credentials
 * @param password Password for credentials
 * @param uriForRealm uri used to determine the authentication realm
 * @return A HttpClientContext that will preemptively authenticate using Basic Authentication
 *//*w  w w .j a  va  2  s .c  o m*/
public HttpClientContext getHttpClientContext(String username, String password, URI uriForRealm) {
    String host = URIUtils.extractHost(uriForRealm).getHostName();
    int port = uriForRealm.getPort();
    if (port == -1) {
        if (Constants.HTTP_SCHEME.equalsIgnoreCase(uriForRealm.getScheme())) {
            port = Constants.HTTP_PORT;
        } else if (Constants.HTTPS_SCHEME.equalsIgnoreCase(uriForRealm.getScheme())) {
            port = Constants.HTTPS_PORT;
        }
    }

    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(new AuthScope(host, port),
            new UsernamePasswordCredentials(username, password));

    AuthCache authCache = new BasicAuthCache();
    authCache.put(new HttpHost(host, port, uriForRealm.getScheme()), new BasicScheme());

    HttpClientContext context = HttpClientContext.create();
    context.setCredentialsProvider(credentialsProvider);
    context.setAuthCache(authCache);

    return context;
}