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.sonar.plugins.buildstability.ci.api.AbstractServer.java

public void doLogin(DefaultHttpClient client) throws IOException {
    if (StringUtils.isNotBlank(getUsername()) && StringUtils.isNotBlank(getPassword())) {
        client.getCredentialsProvider().setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(getUsername(), getPassword()));
    }/*w  w w. j  av a 2s.co  m*/
}

From source file:com.aperigeek.dropvault.dav.DAVClient.java

public DAVClient(String username, String password) {
    client = new DefaultHttpClient();

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

    dateFormat = new SimpleDateFormat(DATE_FORMAT);
}

From source file:nl.surfnet.sab.HttpClientTransportTest.java

@Before
public void setUp() throws Exception {
    server = new LocalTestServer(null, null);

    server.register("/test", new HttpRequestHandler() {
        @Override/*w w  w . j  ava2s . co m*/
        public void handle(HttpRequest request, HttpResponse response, HttpContext context)
                throws HttpException, IOException {
            response.setEntity(new StringEntity("This is the response"));
            response.setStatusCode(200);
        }
    });

    server.start();
    endPoint = URI.create("http:/" + server.getServiceAddress().toString() + "/test");
    credentials = new UsernamePasswordCredentials("theuser", "thepass");
    transport = new HttpClientTransport(credentials, credentials, endPoint, endPoint);
}

From source file:org.syncope.core.rest.AbstractTest.java

@Before
public void setupRestTemplate() {
    PreemptiveAuthHttpRequestFactory requestFactory = ((PreemptiveAuthHttpRequestFactory) restTemplate
            .getRequestFactory());/*from  w  w w .ja  v  a  2  s  .com*/
    ((DefaultHttpClient) requestFactory.getHttpClient()).getCredentialsProvider().setCredentials(
            requestFactory.getAuthScope(), new UsernamePasswordCredentials("admin", "password"));
}

From source file:io.n7.calendar.caldav.BaseTestCase.java

public static HttpClient createHttpClient(CaldavCredential caldavCredential) {
    HttpClient http = new HttpClient();

    Credentials credentials = new UsernamePasswordCredentials(caldavCredential.user, caldavCredential.password);
    http.getState().setCredentials(new AuthScope(caldavCredential.host, caldavCredential.port), credentials);
    http.getParams().setAuthenticationPreemptive(true);
    return http;/*from w ww  . ja  va 2  s  .co  m*/
}

From source file:cn.edu.pku.lib.dataverse.doi.DataCiteRESTfullClient.java

public DataCiteRESTfullClient(String url, String username, String password) {
    this.url = url;
    try {//  w  ww .ja  v a2s.c  o m
        context = HttpClientContext.create();
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(null, -1),
                new UsernamePasswordCredentials(username, password));
        context.setCredentialsProvider(credsProvider);

        httpClient = HttpClients.createDefault();
    } catch (Exception ioe) {
        close();
        logger.log(Level.SEVERE, "Fail to init Client", ioe);
        throw new RuntimeException("Fail to init Client", ioe);
    }
}

From source file:com.ibm.watson.developer_cloud.retrieve_and_rank.v1.utils.HttpSolrClientUtils.java

/**
 * Creates the {@link HttpClient} to use with the Solrj
 *
 * @param url the Solr server url/* www. j  a  va2  s  .c  o m*/
 * @param username the {@link RetrieveAndRank} service username
 * @param password the {@link RetrieveAndRank} service password
 * @return the {@link HttpClient}
 */
public static HttpClient createHttpClient(String url, String username, String password) {
    URI scopeUri = URI.create(url);

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

    HttpClientBuilder builder = HttpClientBuilder.create().setMaxConnTotal(128).setMaxConnPerRoute(32)
            .setDefaultRequestConfig(
                    RequestConfig.copy(RequestConfig.DEFAULT).setRedirectsEnabled(true).build())
            .setDefaultCredentialsProvider(credentialsProvider)
            .addInterceptorFirst(new PreemptiveAuthInterceptor());
    return builder.build();
}

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

@Test
public void testServer_2() {
    BasicCredentialsProvider credsProv = new BasicCredentialsProvider();
    credsProv.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("USER", "PASSWORD"));
    HttpClient client = HttpClients.custom().setDefaultCredentialsProvider(credsProv).build();

    // No auth set - should work.
    try (TypedInputStream in = HttpOp.execHttpGet(FusekiTestServer.urlDataset(), "*/*")) {
    } catch (HttpException ex) {
        Assert.assertTrue(ex.getResponseCode() == HttpSC.FORBIDDEN_403
                || ex.getResponseCode() == HttpSC.UNAUTHORIZED_401);
        throw ex;
    }//from w  ww. java 2 s  . co m
}

From source file:jetbrains.buildServer.commitPublisher.github.api.impl.GitHubApiFactoryImpl.java

@NotNull
public GitHubApi openGitHubForToken(@NotNull final String url, @NotNull final String token) {
    return new GitHubApiImpl(myWrapper, new GitHubApiPaths(url)) {
        @Override/*from  www .  jav a 2s .  c om*/
        protected void setAuthentication(@NotNull HttpRequest request) throws AuthenticationException {
            //NOTE: This auth could also be done via HTTP header
            request.addHeader(new BasicScheme()
                    .authenticate(new UsernamePasswordCredentials(token, "x-oauth-basic"), request));
        }
    };
}

From source file:com.liferay.jenkins.tools.RemoteStringGetter.java

@Override
public String getString(String url) throws Exception {
    url = convertURL(url);//from w  w  w.  ja  v a  2s .c o m

    logger.debug("Fetching string from {}", url);

    CredentialsProvider provider = new BasicCredentialsProvider();

    Credentials credentials = new UsernamePasswordCredentials(username, password);

    provider.setCredentials(AuthScope.ANY, credentials);

    RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).build();

    HttpClient httpClient = HttpClientBuilder.create().setDefaultCredentialsProvider(provider)
            .setDefaultRequestConfig(requestConfig).build();

    HttpResponse httpResponse = httpClient.execute(new HttpGet(url));

    int statusCode = httpResponse.getStatusLine().getStatusCode();

    logger.debug("Successfully fetched {}", url);

    return IOUtils.toString(httpResponse.getEntity().getContent(), Charset.defaultCharset());
}