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

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

Introduction

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

Prototype

public String getUserName() 

Source Link

Usage

From source file:com.google.code.maven.plugin.http.client.CredentialsTest.java

@Test
public void testToUsernamePasswordCredentials() {
    try {//w w  w . j  a v a2  s .  c o m
        credentials.toUsernamePasswordCredentials();
        Assert.fail("unexpected");
    } catch (IllegalArgumentException iae) {
        //   
    }
    String expectedLogin = "a login";
    String expectedPassword = "a password";
    credentials.setLogin(expectedLogin);
    UsernamePasswordCredentials usernamePasswordCredentials = credentials.toUsernamePasswordCredentials();
    Assert.assertNotNull(usernamePasswordCredentials);
    Assert.assertEquals(expectedLogin, usernamePasswordCredentials.getUserName());
    Assert.assertNull(usernamePasswordCredentials.getPassword());
    //
    credentials.setPassword(expectedPassword);
    usernamePasswordCredentials = credentials.toUsernamePasswordCredentials();
    Assert.assertNotNull(usernamePasswordCredentials);
    Assert.assertEquals(expectedLogin, usernamePasswordCredentials.getUserName());
    Assert.assertEquals(expectedPassword, usernamePasswordCredentials.getPassword());
}

From source file:com.google.code.maven.plugin.http.client.FormCredentials.java

/**
 * @param httpclient//  ww w. j av  a2  s. co m
 * @param beanResolver
 * @return
 * @throws IOException
 */
public void authenticate(DefaultHttpClient httpclient, Log log) throws IOException {
    HttpPost httpPost = new HttpPost(formUrl);
    UsernamePasswordCredentials credentials = toUsernamePasswordCredentials();
    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    nvps.add(new Parameter(loginParameterName, credentials.getUserName()).toNameValuePair());
    nvps.add(new Parameter(passwordParameterName, credentials.getPassword()).toNameValuePair());
    for (Parameter parameter : parameters) {
        nvps.add(parameter.toNameValuePair());
    }
    httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
    HttpResponse response = httpclient.execute(httpPost);
    EntityUtils.consume(response.getEntity());
    log.info("form authentication submitted to " + formUrl);
}

From source file:at.ac.univie.isc.asio.integration.IntegrationTest.java

/**
 * @return an observable yielding all events from the server on subscription
 *///  www.j a  v a2s.co m
protected final Observable<InboundEvent> eventStream() {
    final ClientBuilder clientConfig = ClientBuilder.newBuilder();
    Unchecked.run(new Unchecked.Action() {
        @Override
        public void call() throws Exception {
            final SSLContext ssl = SSLContext.getInstance("TLS");
            ssl.init(null, NoopTrustManager.asArray(), null);
            clientConfig.hostnameVerifier(AllowAllVerifier.instance()).sslContext(ssl);
        }
    });
    final Client client = clientConfig.build();

    final UsernamePasswordCredentials credentials = config.rootCredentials;
    final HttpAuthenticationFeature authentication = HttpAuthenticationFeature.basic(credentials.getUserName(),
            credentials.getPassword());
    client.register(authentication);

    client.register(new JacksonFeature());

    return EventStream.listenTo(EventSource.target(client.target(eventsEndpoint())).build())
            //        .doOnEach(new EventLogger())
            .doOnEach(interactions.attached(new EventReport())).finallyDo(new Action0() {
                @Override
                public void call() {
                    client.close();
                }
            });
}

From source file:org.artifactory.util.HttpClientConfigurator.java

/**
 * Ignores null credentials//  w  ww  .j  a  v  a 2s . c  o  m
 */
public HttpClientConfigurator authentication(UsernamePasswordCredentials creds) {
    if (creds != null) {
        authentication(creds.getUserName(), creds.getPassword());
    }

    return this;
}

From source file:at.ac.uniklu.mobile.sportal.api.UnikluApiClient.java

private synchronized String get(String apiMethod, boolean useCache, boolean needsAuthentication,
        NameValuePair... queryParams) throws ApiClientException, ApiServerException {
    try {//from  w  ww  .j  a v a 2  s.co  m
        // execute an ordinary request
        return get(apiMethod, useCache, queryParams);
    } catch (ApiServerException e) {
        /* if the request fails, and the reason is a missing authorization, 
         * and the method needs authorization, log in and retry the request
         */
        if (e.getError().getCode() == 401 && needsAuthentication) {
            UsernamePasswordCredentials credentials = Studentportal.getUsernamePasswordCredentials();
            logDebug("re-login...");
            if (login(credentials.getUserName(), credentials.getPassword(), null).isLoggedIn()) {
                return get(apiMethod, useCache, queryParams);
            }
        }
        // if the exception has another reason, continue with the exception
        throw e;
    }
}

From source file:at.ac.uniklu.mobile.sportal.api.UnikluApiClient.java

private synchronized void post(String apiMethod, String data, boolean needsAuthentication,
        NameValuePair... queryParams) throws ApiClientException, ApiServerException {
    try {//from   w w w  . j a  v a 2 s  .  c  o  m
        // execute an ordinary request
        post(apiMethod, data, queryParams);
    } catch (ApiServerException e) {
        /* if the request fails, and the reason is a missing authorization, 
         * and the method needs authorization, log in and retry the request
         */
        if (e.getError().getCode() == 401 && needsAuthentication) {
            UsernamePasswordCredentials credentials = Studentportal.getUsernamePasswordCredentials();
            logDebug("re-login...");
            if (login(credentials.getUserName(), credentials.getPassword(), null).isLoggedIn()) {
                post(apiMethod, data, queryParams);
            }
        }
        // if the exception has another reason, continue with the exception
        throw e;
    }
}

From source file:at.ac.uniklu.mobile.sportal.api.UnikluApiClient.java

public synchronized void delete(String apiMethod, boolean needsAuthentication, NameValuePair... queryParams)
        throws ApiClientException, ApiServerException {
    try {/*from   w w  w.ja  v  a 2 s.  c o  m*/
        // execute an ordinary request
        delete(apiMethod, queryParams);
    } catch (ApiServerException e) {
        /* if the request fails, and the reason is a missing authorization, 
         * and the method needs authorization, log in and retry the request
         */
        if (e.getError().getCode() == 401 && needsAuthentication) {
            UsernamePasswordCredentials credentials = Studentportal.getUsernamePasswordCredentials();
            logDebug("re-login...");
            if (login(credentials.getUserName(), credentials.getPassword(), null).isLoggedIn()) {
                delete(apiMethod, queryParams);
            }
        }
        // if the exception has another reason, continue with the exception
        throw e;
    }
}

From source file:eu.europa.ec.markt.dss.validation102853.https.CommonDataLoader.java

/**
 * This method allows to propagate the authentication information from the current object.
 *
 * @param commonDataLoader {@code CommonsDataLoader} to be initialised with authentication information
 *//*  w w w.j  av a  2  s .co m*/
public void propagateAuthentication(final CommonDataLoader commonDataLoader) {

    for (final Map.Entry<HttpHost, UsernamePasswordCredentials> credentialsEntry : authenticationMap
            .entrySet()) {

        final HttpHost httpHost = credentialsEntry.getKey();
        final UsernamePasswordCredentials credentials = credentialsEntry.getValue();
        commonDataLoader.addAuthentication(httpHost.getHostName(), httpHost.getPort(), httpHost.getSchemeName(),
                credentials.getUserName(), credentials.getPassword());
    }
}