Search in sources :

Example 1 with StatusLine

use of org.apache.http.StatusLine in project camel by apache.

the class HipchatProducer method sendUserMessage.

private StatusLine sendUserMessage(String user, Exchange exchange) throws IOException, InvalidPayloadException {
    String urlPath = String.format(getConfig().withAuthToken(HipchatApiConstants.URI_PATH_USER_MESSAGE), user);
    Map<String, String> jsonParam = getCommonHttpPostParam(exchange);
    LOG.info("Sending message to user: " + user + ", " + MAPPER.writeValueAsString(jsonParam));
    StatusLine statusLine = post(urlPath, jsonParam);
    LOG.debug("Response status for send user message: " + statusLine);
    return statusLine;
}
Also used : StatusLine(org.apache.http.StatusLine)

Example 2 with StatusLine

use of org.apache.http.StatusLine in project camel by apache.

the class HipchatConsumerIntegrationTest method sendInOnly.

@Test
public void sendInOnly() throws Exception {
    result.expectedMessageCount(1);
    result.expectedMessagesMatches(new Predicate() {

        @Override
        public boolean matches(Exchange exchange) {
            StatusLine status = (StatusLine) exchange.getIn().getHeader(HipchatConstants.FROM_USER_RESPONSE_STATUS);
            return 200 == status.getStatusCode();
        }
    });
    assertMockEndpointsSatisfied();
}
Also used : Exchange(org.apache.camel.Exchange) StatusLine(org.apache.http.StatusLine) Predicate(org.apache.camel.Predicate) Test(org.junit.Test)

Example 3 with StatusLine

use of org.apache.http.StatusLine 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 4 with StatusLine

use of org.apache.http.StatusLine in project hive by apache.

the class PTestClient method downloadTestResults.

private void downloadTestResults(String testHandle, String testOutputDir) throws Exception {
    HttpGet request = new HttpGet(mLogsEndpoint + testHandle + "/test-results.tar.gz");
    FileOutputStream output = null;
    try {
        HttpResponse httpResponse = mHttpClient.execute(request);
        StatusLine statusLine = httpResponse.getStatusLine();
        if (statusLine.getStatusCode() != 200) {
            throw new RuntimeException(statusLine.getStatusCode() + " " + statusLine.getReasonPhrase());
        }
        output = new FileOutputStream(new File(testOutputDir, "test-results.tar.gz"));
        IOUtils.copyLarge(httpResponse.getEntity().getContent(), output);
        output.flush();
    } finally {
        request.abort();
        if (output != null) {
            output.close();
        }
    }
}
Also used : StatusLine(org.apache.http.StatusLine) HttpGet(org.apache.http.client.methods.HttpGet) FileOutputStream(java.io.FileOutputStream) HttpResponse(org.apache.http.HttpResponse) File(java.io.File)

Example 5 with StatusLine

use of org.apache.http.StatusLine in project hive by apache.

the class PTestClient method post.

private <S extends GenericResponse> S post(Object payload, boolean agressiveRetry) throws Exception {
    EndPointResponsePair endPointResponse = Preconditions.checkNotNull(REQUEST_TO_ENDPOINT.get(payload.getClass()), payload.getClass().getName());
    HttpPost request = new HttpPost(mApiEndPoint + endPointResponse.getEndpoint());
    try {
        String payloadString = mMapper.writeValueAsString(payload);
        StringEntity params = new StringEntity(payloadString);
        request.addHeader("content-type", "application/json");
        request.setEntity(params);
        if (agressiveRetry) {
            mHttpClient.setHttpRequestRetryHandler(new PTestHttpRequestRetryHandler());
        }
        HttpResponse httpResponse = mHttpClient.execute(request);
        StatusLine statusLine = httpResponse.getStatusLine();
        if (statusLine.getStatusCode() != 200) {
            throw new IllegalStateException(statusLine.getStatusCode() + " " + statusLine.getReasonPhrase());
        }
        String response = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
        @SuppressWarnings("unchecked") S result = (S) endPointResponse.getResponseClass().cast(mMapper.readValue(response, endPointResponse.getResponseClass()));
        Status.assertOK(result.getStatus());
        if (System.getProperty("DEBUG_PTEST_CLIENT") != null) {
            System.err.println("payload " + payloadString);
            if (result instanceof TestLogResponse) {
                System.err.println("response " + ((TestLogResponse) result).getOffset() + " " + ((TestLogResponse) result).getStatus());
            } else {
                System.err.println("response " + response);
            }
        }
        Thread.sleep(1000);
        return result;
    } finally {
        request.abort();
    }
}
Also used : StatusLine(org.apache.http.StatusLine) HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) TestLogResponse(org.apache.hive.ptest.api.response.TestLogResponse) HttpResponse(org.apache.http.HttpResponse)

Aggregations

StatusLine (org.apache.http.StatusLine)185 IOException (java.io.IOException)77 HttpResponse (org.apache.http.HttpResponse)72 HttpEntity (org.apache.http.HttpEntity)61 BasicStatusLine (org.apache.http.message.BasicStatusLine)44 ProtocolVersion (org.apache.http.ProtocolVersion)42 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)39 Header (org.apache.http.Header)36 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)35 HttpGet (org.apache.http.client.methods.HttpGet)34 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)31 Test (org.junit.Test)31 HttpPost (org.apache.http.client.methods.HttpPost)26 HashMap (java.util.HashMap)23 HttpResponseException (org.apache.http.client.HttpResponseException)23 StringEntity (org.apache.http.entity.StringEntity)22 URL (java.net.URL)20 BasicHeader (org.apache.http.message.BasicHeader)16 HttpURLConnection (java.net.HttpURLConnection)15 List (java.util.List)15