Search in sources :

Example 21 with StringEntity

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

the class WxMpServiceImpl method sendRedpack.

@Override
public WxRedpackResult sendRedpack(Map<String, String> parameters) throws WxErrorException {
    String nonce_str = System.currentTimeMillis() + "";
    SortedMap<String, String> packageParams = new TreeMap<String, String>(parameters);
    packageParams.put("wxappid", wxMpConfigStorage.getAppId());
    packageParams.put("mch_id", wxMpConfigStorage.getPartnerId());
    packageParams.put("nonce_str", nonce_str);
    String sign = WxCryptUtil.createSign(packageParams, wxMpConfigStorage.getPartnerKey());
    packageParams.put("sign", sign);
    StringBuilder request = new StringBuilder("<xml>");
    for (Entry<String, String> para : packageParams.entrySet()) {
        request.append(String.format("<%s>%s</%s>", para.getKey(), para.getValue(), para.getKey()));
    }
    request.append("</xml>");
    HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack");
    if (httpProxy != null) {
        RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
        httpPost.setConfig(config);
    }
    StringEntity entity = new StringEntity(request.toString(), Consts.UTF_8);
    httpPost.setEntity(entity);
    try {
        CloseableHttpResponse response = getHttpclient().execute(httpPost);
        String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
        XStream xstream = XStreamInitializer.getInstance();
        xstream.processAnnotations(WxRedpackResult.class);
        WxRedpackResult wxMpRedpackResult = (WxRedpackResult) xstream.fromXML(responseContent);
        return wxMpRedpackResult;
    } catch (IOException e) {
        log.error(MessageFormatter.format("The exception was happened when sending redpack '{}'.", request.toString()).getMessage(), e);
        WxError error = new WxError();
        error.setErrorCode(-1);
        throw new WxErrorException(error);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) RequestConfig(org.apache.http.client.config.RequestConfig) WxError(me.chanjar.weixin.common.bean.result.WxError) XStream(com.thoughtworks.xstream.XStream) IOException(java.io.IOException) TreeMap(java.util.TreeMap) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException) StringEntity(org.apache.http.entity.StringEntity) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) WxRedpackResult(me.chanjar.weixin.mp.bean.result.WxRedpackResult)

Example 22 with StringEntity

use of org.apache.http.entity.StringEntity in project elasticsearch by elastic.

the class WaitForRefreshAndCloseTests method testUpdateAndThenClose.

public void testUpdateAndThenClose() throws Exception {
    client().performRequest("PUT", docPath(), emptyMap(), new StringEntity("{\"test\":\"test\"}", ContentType.APPLICATION_JSON));
    closeWhileListenerEngaged(start("POST", "/_update", new StringEntity("{\"doc\":{\"name\":\"test\"}}", ContentType.APPLICATION_JSON)));
}
Also used : StringEntity(org.apache.http.entity.StringEntity)

Example 23 with StringEntity

use of org.apache.http.entity.StringEntity in project elasticsearch by elastic.

the class WaitForRefreshAndCloseTests method testDeleteAndThenClose.

public void testDeleteAndThenClose() throws Exception {
    client().performRequest("PUT", docPath(), emptyMap(), new StringEntity("{\"test\":\"test\"}", ContentType.APPLICATION_JSON));
    closeWhileListenerEngaged(start("DELETE", "", null));
}
Also used : StringEntity(org.apache.http.entity.StringEntity)

Example 24 with StringEntity

use of org.apache.http.entity.StringEntity in project elasticsearch by elastic.

the class RemoteScrollableHitSourceTests method testWrapExceptionToPreserveStatus.

public void testWrapExceptionToPreserveStatus() throws IOException {
    Exception cause = new Exception();
    // Successfully get the status without a body
    RestStatus status = randomFrom(RestStatus.values());
    ElasticsearchStatusException wrapped = RemoteScrollableHitSource.wrapExceptionToPreserveStatus(status.getStatus(), null, cause);
    assertEquals(status, wrapped.status());
    assertEquals(cause, wrapped.getCause());
    assertEquals("No error body.", wrapped.getMessage());
    // Successfully get the status without a body
    HttpEntity okEntity = new StringEntity("test body", ContentType.TEXT_PLAIN);
    wrapped = RemoteScrollableHitSource.wrapExceptionToPreserveStatus(status.getStatus(), okEntity, cause);
    assertEquals(status, wrapped.status());
    assertEquals(cause, wrapped.getCause());
    assertEquals("body=test body", wrapped.getMessage());
    // Successfully get the status with a broken body
    IOException badEntityException = new IOException();
    HttpEntity badEntity = mock(HttpEntity.class);
    when(badEntity.getContent()).thenThrow(badEntityException);
    wrapped = RemoteScrollableHitSource.wrapExceptionToPreserveStatus(status.getStatus(), badEntity, cause);
    assertEquals(status, wrapped.status());
    assertEquals(cause, wrapped.getCause());
    assertEquals("Failed to extract body.", wrapped.getMessage());
    assertEquals(badEntityException, wrapped.getSuppressed()[0]);
    // Fail to get the status without a body
    int notAnHttpStatus = -1;
    assertNull(RestStatus.fromCode(notAnHttpStatus));
    wrapped = RemoteScrollableHitSource.wrapExceptionToPreserveStatus(notAnHttpStatus, null, cause);
    assertEquals(RestStatus.INTERNAL_SERVER_ERROR, wrapped.status());
    assertEquals(cause, wrapped.getCause());
    assertEquals("Couldn't extract status [" + notAnHttpStatus + "]. No error body.", wrapped.getMessage());
    // Fail to get the status without a body
    wrapped = RemoteScrollableHitSource.wrapExceptionToPreserveStatus(notAnHttpStatus, okEntity, cause);
    assertEquals(RestStatus.INTERNAL_SERVER_ERROR, wrapped.status());
    assertEquals(cause, wrapped.getCause());
    assertEquals("Couldn't extract status [" + notAnHttpStatus + "]. body=test body", wrapped.getMessage());
    // Fail to get the status with a broken body
    wrapped = RemoteScrollableHitSource.wrapExceptionToPreserveStatus(notAnHttpStatus, badEntity, cause);
    assertEquals(RestStatus.INTERNAL_SERVER_ERROR, wrapped.status());
    assertEquals(cause, wrapped.getCause());
    assertEquals("Couldn't extract status [" + notAnHttpStatus + "]. Failed to extract body.", wrapped.getMessage());
    assertEquals(badEntityException, wrapped.getSuppressed()[0]);
}
Also used : StringEntity(org.apache.http.entity.StringEntity) RestStatus(org.elasticsearch.rest.RestStatus) HttpEntity(org.apache.http.HttpEntity) IOException(java.io.IOException) ElasticsearchStatusException(org.elasticsearch.ElasticsearchStatusException) ContentTooLongException(org.apache.http.ContentTooLongException) ParsingException(org.elasticsearch.common.ParsingException) IOException(java.io.IOException) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException) ElasticsearchStatusException(org.elasticsearch.ElasticsearchStatusException)

Example 25 with StringEntity

use of org.apache.http.entity.StringEntity in project elasticsearch by elastic.

the class Netty4HeadBodyIsEmptyIT method testGetSourceAction.

public void testGetSourceAction() throws IOException {
    createTestDoc();
    headTestCase("/test/test/1/_source", emptyMap(), greaterThan(0));
    headTestCase("/test/test/2/_source", emptyMap(), NOT_FOUND.getStatus(), equalTo(0));
    try (XContentBuilder builder = jsonBuilder()) {
        builder.startObject();
        {
            builder.startObject("mappings");
            {
                builder.startObject("test-no-source");
                {
                    builder.startObject("_source");
                    {
                        builder.field("enabled", false);
                    }
                    builder.endObject();
                }
                builder.endObject();
            }
            builder.endObject();
        }
        builder.endObject();
        client().performRequest("PUT", "/test-no-source", emptyMap(), new StringEntity(builder.string(), ContentType.APPLICATION_JSON));
        createTestDoc("test-no-source", "test-no-source");
        headTestCase("/test-no-source/test-no-source/1/_source", emptyMap(), NOT_FOUND.getStatus(), equalTo(0));
    }
}
Also used : StringEntity(org.apache.http.entity.StringEntity) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

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