Search in sources :

Example 1 with UsernamePasswordCredentials

use of org.apache.http.auth.UsernamePasswordCredentials in project hive by apache.

the class JIRAService method publishComments.

void publishComments(String comments) {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    try {
        String url = String.format("%s/rest/api/2/issue/%s/comment", mUrl, mName);
        URL apiURL = new URL(mUrl);
        httpClient.getCredentialsProvider().setCredentials(new AuthScope(apiURL.getHost(), apiURL.getPort(), AuthScope.ANY_REALM), new UsernamePasswordCredentials(mUser, mPassword));
        BasicHttpContext localcontext = new BasicHttpContext();
        localcontext.setAttribute("preemptive-auth", new BasicScheme());
        httpClient.addRequestInterceptor(new PreemptiveAuth(), 0);
        HttpPost request = new HttpPost(url);
        ObjectMapper mapper = new ObjectMapper();
        StringEntity params = new StringEntity(mapper.writeValueAsString(new Body(comments)));
        request.addHeader("Content-Type", "application/json");
        request.setEntity(params);
        HttpResponse httpResponse = httpClient.execute(request, localcontext);
        StatusLine statusLine = httpResponse.getStatusLine();
        if (statusLine.getStatusCode() != 201) {
            throw new RuntimeException(statusLine.getStatusCode() + " " + statusLine.getReasonPhrase());
        }
        mLogger.info("JIRA Response Metadata: " + httpResponse);
    } catch (Exception e) {
        mLogger.error("Encountered error attempting to post comment to " + mName, e);
    } finally {
        httpClient.getConnectionManager().shutdown();
    }
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) HttpPost(org.apache.http.client.methods.HttpPost) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpResponse(org.apache.http.HttpResponse) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) URL(java.net.URL) IOException(java.io.IOException) HttpException(org.apache.http.HttpException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) StatusLine(org.apache.http.StatusLine) StringEntity(org.apache.http.entity.StringEntity) AuthScope(org.apache.http.auth.AuthScope) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 2 with UsernamePasswordCredentials

use of org.apache.http.auth.UsernamePasswordCredentials in project jersey by jersey.

the class AuthTest method testPreemptiveAuth.

@Test
public void testPreemptiveAuth() {
    CredentialsProvider credentialsProvider = new org.apache.http.impl.client.BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("name", "password"));
    ClientConfig cc = new ClientConfig();
    cc.property(ApacheClientProperties.CREDENTIALS_PROVIDER, credentialsProvider).property(ApacheClientProperties.PREEMPTIVE_BASIC_AUTHENTICATION, true);
    cc.connectorProvider(new ApacheConnectorProvider());
    Client client = ClientBuilder.newClient(cc);
    WebTarget r = client.target(getBaseUri());
    assertEquals("GET", r.request().get(String.class));
}
Also used : CredentialsProvider(org.apache.http.client.CredentialsProvider) WebTarget(javax.ws.rs.client.WebTarget) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 3 with UsernamePasswordCredentials

use of org.apache.http.auth.UsernamePasswordCredentials in project jersey by jersey.

the class AuthTest method testAuthInteractiveGet.

@Test
public void testAuthInteractiveGet() {
    CredentialsProvider credentialsProvider = new org.apache.http.impl.client.BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("name", "password"));
    ClientConfig cc = new ClientConfig();
    cc.property(ApacheClientProperties.CREDENTIALS_PROVIDER, credentialsProvider);
    cc.connectorProvider(new ApacheConnectorProvider());
    Client client = ClientBuilder.newClient(cc);
    WebTarget r = client.target(getBaseUri()).path("test");
    assertEquals("GET", r.request().get(String.class));
}
Also used : CredentialsProvider(org.apache.http.client.CredentialsProvider) WebTarget(javax.ws.rs.client.WebTarget) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 4 with UsernamePasswordCredentials

use of org.apache.http.auth.UsernamePasswordCredentials in project jersey by jersey.

the class AuthTest method testAuthDelete.

@Test
public void testAuthDelete() {
    CredentialsProvider credentialsProvider = new org.apache.http.impl.client.BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("name", "password"));
    ClientConfig cc = new ClientConfig();
    cc.property(ApacheClientProperties.CREDENTIALS_PROVIDER, credentialsProvider);
    cc.connectorProvider(new ApacheConnectorProvider());
    Client client = ClientBuilder.newClient(cc);
    WebTarget r = client.target(getBaseUri()).path("test");
    Response response = r.request().delete();
    assertEquals(response.getStatus(), 204);
}
Also used : Response(javax.ws.rs.core.Response) CredentialsProvider(org.apache.http.client.CredentialsProvider) WebTarget(javax.ws.rs.client.WebTarget) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 5 with UsernamePasswordCredentials

use of org.apache.http.auth.UsernamePasswordCredentials in project android_frameworks_base by ParanoidAndroid.

the class DefaultHttpClientTest method authenticateDigestAlgorithm.

private void authenticateDigestAlgorithm(String algorithm) throws Exception {
    String challenge = "Digest realm=\"protected area\", " + "nonce=\"dcd98b7102dd2f0e8b11d0f600bfb0c093\", " + "algorithm=" + algorithm;
    DigestScheme digestScheme = new DigestScheme();
    digestScheme.processChallenge(new BasicHeader("WWW-Authenticate", challenge));
    HttpGet get = new HttpGet();
    digestScheme.authenticate(new UsernamePasswordCredentials("username", "password"), get);
}
Also used : DigestScheme(org.apache.http.impl.auth.DigestScheme) HttpGet(org.apache.http.client.methods.HttpGet) BasicHeader(org.apache.http.message.BasicHeader) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Aggregations

UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)337 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)212 CredentialsProvider (org.apache.http.client.CredentialsProvider)189 AuthScope (org.apache.http.auth.AuthScope)163 HttpHost (org.apache.http.HttpHost)95 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)78 HttpGet (org.apache.http.client.methods.HttpGet)73 IOException (java.io.IOException)54 BasicScheme (org.apache.http.impl.auth.BasicScheme)53 Test (org.junit.Test)53 HttpResponse (org.apache.http.HttpResponse)52 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)46 Credentials (org.apache.http.auth.Credentials)44 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)43 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)37 URI (java.net.URI)34 AuthCache (org.apache.http.client.AuthCache)32 HttpClient (org.apache.http.client.HttpClient)31 BasicAuthCache (org.apache.http.impl.client.BasicAuthCache)31 HttpPost (org.apache.http.client.methods.HttpPost)29