Search in sources :

Example 1 with Response

use of org.apache.apex.shaded.ning19.com.ning.http.client.Response in project killbill by killbill.

the class TestPaymentGateway method testProcessNotification.

@Test(groups = "slow")
public void testProcessNotification() throws Exception {
    final Response response = killBillClient.processNotification("TOTO", PLUGIN_NAME, ImmutableMap.<String, String>of(), createdBy, reason, comment);
    Assert.assertEquals(response.getStatusCode(), 200);
}
Also used : Response(com.ning.http.client.Response) Test(org.testng.annotations.Test)

Example 2 with Response

use of org.apache.apex.shaded.ning19.com.ning.http.client.Response in project Singularity by HubSpot.

the class SingularityWebhookSender method executeWebhookAsync.

// TODO handle retries, errors.
private <T> CompletableFuture<Response> executeWebhookAsync(SingularityWebhook webhook, Object payload, AbstractSingularityWebhookAsyncHandler<T> handler) {
    LOG.trace("Sending {} to {}", payload, webhook.getUri());
    BoundRequestBuilder postRequest = http.preparePost(webhook.getUri());
    postRequest.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
    try {
        postRequest.setBody(objectMapper.writeValueAsBytes(payload));
    } catch (JsonProcessingException e) {
        throw Throwables.propagate(e);
    }
    CompletableFuture<Response> webhookFuture = new CompletableFuture<>();
    try {
        handler.setCompletableFuture(webhookFuture);
        postRequest.execute(handler);
    } catch (IOException e) {
        LOG.warn("Couldn't execute webhook to {}", webhook.getUri(), e);
        if (handler.shouldDeleteUpdateDueToQueueAboveCapacity()) {
            handler.deleteWebhookUpdate();
        }
        webhookFuture.completeExceptionally(e);
    }
    return webhookFuture;
}
Also used : Response(com.ning.http.client.Response) BoundRequestBuilder(com.ning.http.client.AsyncHttpClient.BoundRequestBuilder) CompletableFuture(java.util.concurrent.CompletableFuture) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 3 with Response

use of org.apache.apex.shaded.ning19.com.ning.http.client.Response in project protools by SeanDragon.

the class ToolHttp method sendHttp.

/**
 * 用于请求http
 *
 * @param send
 *         里面包含请求的信息
 *
 * @return 响应的信息
 */
public static HttpReceive sendHttp(HttpSend send, HttpBuilder httpBuilder) {
    HttpReceive httpReceive = new HttpReceive();
    httpReceive.setHaveError(true);
    String url = send.getUrl();
    URI uri;
    try {
        uri = new URL(url).toURI();
    } catch (MalformedURLException | URISyntaxException e) {
        httpReceive.setErrMsg("不是一个有效的URL").setThrowable(e);
        return httpReceive;
    }
    String scheme = uri.getScheme();
    String host = uri.getHost();
    int port = uri.getPort();
    if (port == -1) {
        if (HttpScheme.HTTP.equalsIgnoreCase(scheme)) {
            port = 80;
        } else if (HttpScheme.HTTPS.equalsIgnoreCase(scheme)) {
            port = 443;
        }
    }
    Map<String, Object> param = send.getParams();
    Map<String, Object> sendHeaders = send.getHeaders();
    HttpMethod method = send.getMethod();
    Charset charset = send.getCharset();
    AsyncHttpClient asyncHttpClient = httpBuilder.buildDefaultClient();
    RequestBuilder builder = new RequestBuilder(method.name());
    AsyncHttpClient.BoundRequestBuilder requestBuilder = asyncHttpClient.prepareRequest(builder.build());
    // 设置编码
    requestBuilder.setBodyEncoding(charset.toString());
    if (param != null) {
        param.forEach((key, value) -> requestBuilder.addQueryParam(key, value.toString()));
    }
    // 设置基本请求头
    requestBuilder.addHeader(HttpHeaderNames.CONTENT_TYPE.toString(), HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED + ";charset" + charset.toString()).addHeader(HttpHeaderNames.CONNECTION.toString(), HttpHeaderValues.KEEP_ALIVE.toString()).addHeader(HttpHeaderNames.ACCEPT_ENCODING.toString(), HttpHeaderValues.GZIP_DEFLATE.toString()).addHeader(HttpHeaderNames.USER_AGENT.toString(), "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)").addHeader(HttpHeaderNames.CACHE_CONTROL.toString(), "max-age=0").addHeader(HttpHeaderNames.HOST.toString(), host + ":" + port).addHeader("DNT", "1");
    if (sendHeaders != null) {
        sendHeaders.forEach((key, value) -> requestBuilder.addHeader(key, value.toString()));
    }
    // TODO: 2017/7/27 Cookie未加入
    ListenableFuture<Response> future = requestBuilder.execute();
    try {
        Response response = future.get();
        Map<String, String> responseHeaderMap = Maps.newHashMap();
        if (send.getNeedReceiveHeaders()) {
            FluentCaseInsensitiveStringsMap responseHeaders = response.getHeaders();
            responseHeaders.forEach((k, v) -> {
                if (v.size() == 1) {
                    responseHeaderMap.put(k, v.get(0));
                } else {
                    responseHeaderMap.put(k, ToolJson.anyToJson(v));
                }
            });
        }
        int responseStatusCode = response.getStatusCode();
        if (responseStatusCode != 200) {
            throw new HttpException("本次请求响应码不是200,是" + responseStatusCode);
        }
        String responseBody = response.getResponseBody();
        if (log.isDebugEnabled()) {
            log.debug(responseBody);
        }
        httpReceive.setStatusCode(responseStatusCode).setStatusText(response.getStatusText()).setResponseBody(responseBody).setResponseHeader(responseHeaderMap).setHaveError(false);
    } catch (InterruptedException e) {
        httpReceive.setErrMsg("http组件出现问题!").setThrowable(e);
    } catch (IOException e) {
        httpReceive.setErrMsg("获取返回内容失败!").setThrowable(e);
    } catch (ExecutionException e) {
        httpReceive.setErrMsg("访问URL失败!").setThrowable(e);
    } catch (HttpException e) {
        httpReceive.setErrMsg(e.getMessage()).setThrowable(e);
    }
    if (httpReceive.getHaveError()) {
        if (log.isWarnEnabled()) {
            Throwable throwable = httpReceive.getThrowable();
            log.warn(ToolFormat.toException(throwable), throwable);
        }
    }
    httpReceive.setIsDone(true);
    return httpReceive;
}
Also used : HttpReceive(pro.tools.http.pojo.HttpReceive) MalformedURLException(java.net.MalformedURLException) RequestBuilder(com.ning.http.client.RequestBuilder) Charset(java.nio.charset.Charset) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) URL(java.net.URL) Response(com.ning.http.client.Response) FluentCaseInsensitiveStringsMap(com.ning.http.client.FluentCaseInsensitiveStringsMap) HttpException(pro.tools.http.pojo.HttpException) ExecutionException(java.util.concurrent.ExecutionException) HttpMethod(pro.tools.http.pojo.HttpMethod) AsyncHttpClient(com.ning.http.client.AsyncHttpClient)

Example 4 with Response

use of org.apache.apex.shaded.ning19.com.ning.http.client.Response in project rxrabbit by meltwater.

the class RxRabbitMultiNodeTest method handles_broker_failover_with_ha_queues.

@Test
public void handles_broker_failover_with_ha_queues() throws Exception {
    int nrMessages = 1_00;
    log.infoWithParams("Setting ha-mode to 'all' for queues");
    Response setPolicyResponse = httpClient.preparePut("http://localhost:" + rabbitAdminPort + "/api/policies/%2f/ha-all").setBody("{\"pattern\":\"(.*?)\", \"definition\":{\"ha-mode\":\"all\"}}").setRealm(realm).setHeader("Content-Type", "application/json").execute().get();
    assertThat(setPolicyResponse.getStatusCode(), Matchers.is(204));
    log.infoWithParams("Sending messages");
    SortedSet<Integer> sent = sendNMessages(nrMessages, publisher);
    log.infoWithParams("Killing the primary rabbit broker");
    dockerContainers.rabbit(RABBIT_1).kill();
    SortedSet<Integer> received = consumeAndGetIds(nrMessages, createConsumer(consumerFactory, inputQueue));
    assertThat(received.size(), equalTo(nrMessages));
    assertEquals(received, sent);
}
Also used : Response(com.ning.http.client.Response) Test(org.junit.Test)

Example 5 with Response

use of org.apache.apex.shaded.ning19.com.ning.http.client.Response in project rxrabbit by meltwater.

the class RabbitTestUtils method getQueueNames.

public static List<String> getQueueNames(AsyncHttpClient httpClient, String rabbitAdminPort) throws Exception {
    final Response response = httpClient.prepareGet("http://localhost:" + rabbitAdminPort + "/api/queues").setRealm(realm).execute().get();
    ObjectMapper mapper = new ObjectMapper();
    List<String> queues = new ArrayList<>();
    final List<Map<String, Object>> list = mapper.readValue(response.getResponseBody(), List.class);
    for (Map<String, Object> entry : list) {
        queues.add(entry.get("name").toString());
    }
    return queues;
}
Also used : Response(com.ning.http.client.Response) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

Response (com.ning.http.client.Response)42 IOException (java.io.IOException)14 Test (org.junit.Test)11 AsyncHttpClient (com.ning.http.client.AsyncHttpClient)9 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 BoundRequestBuilder (com.ning.http.client.AsyncHttpClient.BoundRequestBuilder)6 Map (java.util.Map)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 Request (com.ning.http.client.Request)5 ExecutionException (java.util.concurrent.ExecutionException)5 AsyncHttpClientConfig (com.ning.http.client.AsyncHttpClientConfig)4 RequestBuilder (com.ning.http.client.RequestBuilder)4 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)4 HttpMethod (io.netty.handler.codec.http.HttpMethod)4 TimeoutException (java.util.concurrent.TimeoutException)4 NodesInfoResponse (org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse)4 Span (com.nike.wingtips.Span)3 AsyncCompletionHandler (com.ning.http.client.AsyncCompletionHandler)3 NettyAsyncHttpProvider (com.ning.http.client.providers.netty.NettyAsyncHttpProvider)3