Search in sources :

Example 11 with HttpPost

use of org.apache.http.client.methods.HttpPost in project camel by apache.

the class CxfRsRouterTest method testPostConsumerUniqueResponseCode.

@Test
public void testPostConsumerUniqueResponseCode() throws Exception {
    HttpPost post = new HttpPost("http://localhost:" + getPort() + "/CxfRsRouterTest/route/customerservice/customersUniqueResponseCode");
    post.addHeader("Accept", "text/xml");
    StringEntity entity = new StringEntity(POST_REQUEST, "ISO-8859-1");
    entity.setContentType("text/xml; charset=ISO-8859-1");
    post.setEntity(entity);
    CloseableHttpClient httpclient = HttpClientBuilder.create().build();
    try {
        HttpResponse response = httpclient.execute(post);
        assertEquals(201, response.getStatusLine().getStatusCode());
        assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Customer><id>124</id><name>Jack</name></Customer>", EntityUtils.toString(response.getEntity()));
        HttpDelete del = new HttpDelete("http://localhost:" + getPort() + "/CxfRsRouterTest/route/customerservice/customers/124/");
        response = httpclient.execute(del);
        // need to check the response of delete method
        assertEquals(200, response.getStatusLine().getStatusCode());
    } finally {
        httpclient.close();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpDelete(org.apache.http.client.methods.HttpDelete) HttpResponse(org.apache.http.HttpResponse) Test(org.junit.Test)

Example 12 with HttpPost

use of org.apache.http.client.methods.HttpPost in project camel by apache.

the class HipchatProducer method post.

protected StatusLine post(String urlPath, Map<String, String> postParam) throws IOException {
    HttpPost httpPost = new HttpPost(getConfig().hipChatUrl() + urlPath);
    httpPost.setEntity(new StringEntity(MAPPER.writeValueAsString(postParam), ContentType.APPLICATION_JSON));
    CloseableHttpResponse closeableHttpResponse = HTTP_CLIENT.execute(httpPost);
    try {
        return closeableHttpResponse.getStatusLine();
    } finally {
        closeableHttpResponse.close();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 13 with HttpPost

use of org.apache.http.client.methods.HttpPost 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 14 with HttpPost

use of org.apache.http.client.methods.HttpPost 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)

Example 15 with HttpPost

use of org.apache.http.client.methods.HttpPost in project cas by apereo.

the class SimpleHttpClient method sendMessageToEndPoint.

@Override
public boolean sendMessageToEndPoint(final HttpMessage message) {
    Assert.notNull(this.httpClient);
    try {
        final HttpPost request = new HttpPost(message.getUrl().toURI());
        request.addHeader("Content-Type", message.getContentType());
        final StringEntity entity = new StringEntity(message.getMessage(), ContentType.create(message.getContentType()));
        request.setEntity(entity);
        final ResponseHandler<Boolean> handler = response -> response.getStatusLine().getStatusCode() == HttpStatus.SC_OK;
        LOGGER.debug("Created HTTP post message payload [{}]", request);
        final HttpRequestFutureTask<Boolean> task = this.requestExecutorService.execute(request, HttpClientContext.create(), handler);
        if (message.isAsynchronous()) {
            return true;
        }
        return task.get();
    } catch (final RejectedExecutionException e) {
        LOGGER.warn(e.getMessage(), e);
        return false;
    } catch (final Exception e) {
        LOGGER.debug(e.getMessage(), e);
        return false;
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpRequestFutureTask(org.apache.http.impl.client.HttpRequestFutureTask) URL(java.net.URL) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) LoggerFactory(org.slf4j.LoggerFactory) HttpStatus(org.apache.http.HttpStatus) EntityUtils(org.apache.http.util.EntityUtils) FutureRequestExecutionService(org.apache.http.impl.client.FutureRequestExecutionService) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Logger(org.slf4j.Logger) MalformedURLException(java.net.MalformedURLException) HttpEntity(org.apache.http.HttpEntity) ContentType(org.apache.http.entity.ContentType) StringEntity(org.apache.http.entity.StringEntity) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Serializable(java.io.Serializable) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) HttpGet(org.apache.http.client.methods.HttpGet) DisposableBean(org.springframework.beans.factory.DisposableBean) ResponseHandler(org.apache.http.client.ResponseHandler) Collections(java.util.Collections) Assert(org.springframework.util.Assert) HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) MalformedURLException(java.net.MalformedURLException)

Aggregations

HttpPost (org.apache.http.client.methods.HttpPost)531 HttpResponse (org.apache.http.HttpResponse)238 StringEntity (org.apache.http.entity.StringEntity)220 Test (org.junit.Test)153 IOException (java.io.IOException)143 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)107 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)99 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)90 ArrayList (java.util.ArrayList)86 NameValuePair (org.apache.http.NameValuePair)84 HttpEntity (org.apache.http.HttpEntity)83 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)68 HttpClient (org.apache.http.client.HttpClient)64 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)63 HttpGet (org.apache.http.client.methods.HttpGet)55 TestHttpClient (io.undertow.testutils.TestHttpClient)54 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)49 ClientProtocolException (org.apache.http.client.ClientProtocolException)49 JsonNode (com.fasterxml.jackson.databind.JsonNode)39 InputStream (java.io.InputStream)29