Example usage for org.apache.http.impl.client DefaultHttpClient addRequestInterceptor

List of usage examples for org.apache.http.impl.client DefaultHttpClient addRequestInterceptor

Introduction

In this page you can find the example usage for org.apache.http.impl.client DefaultHttpClient addRequestInterceptor.

Prototype

public synchronized void addRequestInterceptor(final HttpRequestInterceptor itcp, final int index) 

Source Link

Usage

From source file:org.apache.hive.ptest.execution.JIRAService.java

void publishComments(String comments) {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    try {// w w w  .  ja v a 2 s  .  c  o  m
        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();
    }
}