Search in sources :

Example 16 with StringEntity

use of org.apache.http.entity.StringEntity 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 17 with StringEntity

use of org.apache.http.entity.StringEntity 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)

Example 18 with StringEntity

use of org.apache.http.entity.StringEntity in project crate by crate.

the class BlobHttpIntegrationTest method put.

protected CloseableHttpResponse put(String uri, String body) throws IOException {
    HttpPut httpPut = new HttpPut(Blobs.url(address, uri));
    if (body != null) {
        StringEntity bodyEntity = new StringEntity(body);
        httpPut.setEntity(bodyEntity);
    }
    return executeAndDefaultAssertions(httpPut);
}
Also used : StringEntity(org.apache.http.entity.StringEntity)

Example 19 with StringEntity

use of org.apache.http.entity.StringEntity in project weixin-java-tools by chanjarster.

the class MaterialDeleteRequestExecutor method execute.

public Boolean execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String materialId) throws WxErrorException, ClientProtocolException, IOException {
    HttpPost httpPost = new HttpPost(uri);
    if (httpProxy != null) {
        RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
        httpPost.setConfig(config);
    }
    Map<String, String> params = new HashMap<>();
    params.put("media_id", materialId);
    httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params)));
    CloseableHttpResponse response = httpclient.execute(httpPost);
    String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
    WxError error = WxError.fromJson(responseContent);
    if (error.getErrorCode() != 0) {
        throw new WxErrorException(error);
    } else {
        return true;
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) RequestConfig(org.apache.http.client.config.RequestConfig) StringEntity(org.apache.http.entity.StringEntity) WxError(me.chanjar.weixin.common.bean.result.WxError) HashMap(java.util.HashMap) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException)

Example 20 with StringEntity

use of org.apache.http.entity.StringEntity in project weixin-java-tools by chanjarster.

the class MaterialNewsInfoRequestExecutor method execute.

public WxMpMaterialNews execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String materialId) throws WxErrorException, ClientProtocolException, IOException {
    HttpPost httpPost = new HttpPost(uri);
    if (httpProxy != null) {
        RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
        httpPost.setConfig(config);
    }
    Map<String, String> params = new HashMap<>();
    params.put("media_id", materialId);
    httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params)));
    CloseableHttpResponse response = httpclient.execute(httpPost);
    String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
    WxError error = WxError.fromJson(responseContent);
    if (error.getErrorCode() != 0) {
        throw new WxErrorException(error);
    } else {
        return WxMpGsonBuilder.create().fromJson(responseContent, WxMpMaterialNews.class);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) RequestConfig(org.apache.http.client.config.RequestConfig) StringEntity(org.apache.http.entity.StringEntity) WxError(me.chanjar.weixin.common.bean.result.WxError) HashMap(java.util.HashMap) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException)

Aggregations

StringEntity (org.apache.http.entity.StringEntity)409 HttpPost (org.apache.http.client.methods.HttpPost)223 HttpResponse (org.apache.http.HttpResponse)136 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)129 HttpPut (org.apache.http.client.methods.HttpPut)89 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)88 Test (org.junit.Test)84 IOException (java.io.IOException)78 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)60 HttpEntity (org.apache.http.HttpEntity)55 JsonNode (com.fasterxml.jackson.databind.JsonNode)54 Deployment (org.activiti.engine.test.Deployment)50 TestHttpClient (io.undertow.testutils.TestHttpClient)30 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)27 StatusLine (org.apache.http.StatusLine)26 HttpGet (org.apache.http.client.methods.HttpGet)25 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)23 Task (org.activiti.engine.task.Task)21 HttpRequest (org.apache.http.HttpRequest)21 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)20