Search in sources :

Example 31 with Response

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

the class NingAsyncHttpClientIT method test.

@Test
public void test() throws Exception {
    AsyncHttpClient client = new AsyncHttpClient();
    try {
        Future<Response> f = client.preparePost("http://www.naver.com/").addParameter("param1", "value1").execute();
        Response response = f.get();
    } finally {
        client.close();
    }
    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.printCache();
    verifier.verifyTrace(event("ASYNC_HTTP_CLIENT", AsyncHttpClient.class.getMethod("executeRequest", Request.class, AsyncHandler.class), null, null, "www.naver.com", annotation("http.url", "http://www.naver.com"), annotation("http.param", "param1=value1")));
    verifier.verifyTraceCount(0);
}
Also used : Response(com.ning.http.client.Response) PluginTestVerifier(com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier) AsyncHttpClient(com.ning.http.client.AsyncHttpClient) Test(org.junit.Test)

Example 32 with Response

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

the class NettyRouterTestBase method testUpload.

@Test
public void testUpload() throws Exception {
    AsyncHttpClientConfig.Builder configBuilder = new AsyncHttpClientConfig.Builder();
    final AsyncHttpClient asyncHttpClient = new AsyncHttpClient(new NettyAsyncHttpProvider(configBuilder.build()), configBuilder.build());
    byte[] requestBody = generatePostData();
    final Request request = new RequestBuilder("POST").setUrl(resolveURI(DEFAULT_SERVICE, "/v1/upload")).setContentLength(requestBody.length).setBody(new ByteEntityWriter(requestBody)).build();
    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    Future<Void> future = asyncHttpClient.executeRequest(request, new AsyncCompletionHandler<Void>() {

        @Override
        public Void onCompleted(Response response) throws Exception {
            return null;
        }

        @Override
        public STATE onBodyPartReceived(HttpResponseBodyPart content) throws Exception {
            //TimeUnit.MILLISECONDS.sleep(RANDOM.nextInt(10));
            content.writeTo(byteArrayOutputStream);
            return super.onBodyPartReceived(content);
        }
    });
    future.get();
    Assert.assertArrayEquals(requestBody, byteArrayOutputStream.toByteArray());
}
Also used : RequestBuilder(com.ning.http.client.RequestBuilder) RequestBuilder(com.ning.http.client.RequestBuilder) Request(com.ning.http.client.Request) HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) NettyAsyncHttpProvider(com.ning.http.client.providers.netty.NettyAsyncHttpProvider) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) Response(com.ning.http.client.Response) HttpResponse(org.apache.http.HttpResponse) AsyncHttpClientConfig(com.ning.http.client.AsyncHttpClientConfig) HttpResponseBodyPart(com.ning.http.client.HttpResponseBodyPart) AsyncHttpClient(com.ning.http.client.AsyncHttpClient) Test(org.junit.Test)

Example 33 with Response

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

the class NettyRouterTestBase method testRouterAsync.

@Test
public void testRouterAsync() throws Exception {
    int numElements = 123;
    AsyncHttpClientConfig.Builder configBuilder = new AsyncHttpClientConfig.Builder();
    final AsyncHttpClient asyncHttpClient = new AsyncHttpClient(new NettyAsyncHttpProvider(configBuilder.build()), configBuilder.build());
    final CountDownLatch latch = new CountDownLatch(numElements);
    final AtomicInteger numSuccessfulRequests = new AtomicInteger(0);
    for (int i = 0; i < numElements; ++i) {
        final int elem = i;
        final Request request = new RequestBuilder("GET").setUrl(resolveURI(DEFAULT_SERVICE, String.format("%s/%s-%d", "/v1/echo", "async", i))).build();
        asyncHttpClient.executeRequest(request, new AsyncCompletionHandler<Void>() {

            @Override
            public Void onCompleted(Response response) throws Exception {
                latch.countDown();
                Assert.assertEquals(HttpResponseStatus.OK.getCode(), response.getStatusCode());
                String responseBody = response.getResponseBody();
                LOG.trace("Got response {}", responseBody);
                Assert.assertEquals("async-" + elem, responseBody);
                numSuccessfulRequests.incrementAndGet();
                return null;
            }

            @Override
            public void onThrowable(Throwable t) {
                LOG.error("Got exception while posting {}", elem, t);
                latch.countDown();
            }
        });
        // Sleep so as not to overrun the server.
        TimeUnit.MILLISECONDS.sleep(1);
    }
    latch.await();
    asyncHttpClient.close();
    Assert.assertEquals(numElements, numSuccessfulRequests.get());
    // we use sticky endpoint strategy so the sum of requests from the two gateways should be NUM_ELEMENTS
    Assert.assertTrue(numElements == (defaultServer1.getNumRequests() + defaultServer2.getNumRequests()));
}
Also used : RequestBuilder(com.ning.http.client.RequestBuilder) RequestBuilder(com.ning.http.client.RequestBuilder) Request(com.ning.http.client.Request) HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) NettyAsyncHttpProvider(com.ning.http.client.providers.netty.NettyAsyncHttpProvider) CountDownLatch(java.util.concurrent.CountDownLatch) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) Response(com.ning.http.client.Response) HttpResponse(org.apache.http.HttpResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AsyncHttpClientConfig(com.ning.http.client.AsyncHttpClientConfig) AsyncHttpClient(com.ning.http.client.AsyncHttpClient) Test(org.junit.Test)

Example 34 with Response

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

the class NettyRouterPipelineTest method testChunkRequestSuccess.

@Test
public void testChunkRequestSuccess() throws Exception {
    AsyncHttpClientConfig.Builder configBuilder = new AsyncHttpClientConfig.Builder();
    final AsyncHttpClient asyncHttpClient = new AsyncHttpClient(new NettyAsyncHttpProvider(configBuilder.build()), configBuilder.build());
    byte[] requestBody = generatePostData();
    final Request request = new RequestBuilder("POST").setUrl(String.format("http://%s:%d%s", HOSTNAME, ROUTER.getServiceMap().get(GATEWAY_NAME), "/v1/upload")).setContentLength(requestBody.length).setBody(new ByteEntityWriter(requestBody)).build();
    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    Future<Void> future = asyncHttpClient.executeRequest(request, new AsyncCompletionHandler<Void>() {

        @Override
        public Void onCompleted(Response response) throws Exception {
            return null;
        }

        @Override
        public STATE onBodyPartReceived(HttpResponseBodyPart content) throws Exception {
            //TimeUnit.MILLISECONDS.sleep(RANDOM.nextInt(10));
            content.writeTo(byteArrayOutputStream);
            return super.onBodyPartReceived(content);
        }
    });
    future.get();
    Assert.assertArrayEquals(requestBody, byteArrayOutputStream.toByteArray());
}
Also used : RequestBuilder(com.ning.http.client.RequestBuilder) RequestBuilder(com.ning.http.client.RequestBuilder) Request(com.ning.http.client.Request) NettyAsyncHttpProvider(com.ning.http.client.providers.netty.NettyAsyncHttpProvider) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Response(com.ning.http.client.Response) AsyncHttpClientConfig(com.ning.http.client.AsyncHttpClientConfig) HttpResponseBodyPart(com.ning.http.client.HttpResponseBodyPart) AsyncHttpClient(com.ning.http.client.AsyncHttpClient) Test(org.junit.Test)

Example 35 with Response

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

the class WorkflowClient method getWorkflowStatus.

public void getWorkflowStatus(String namespaceId, String appId, String workflowId, String runId, final Callback callback) throws IOException {
    // determine the service provider for the given path
    String serviceName = String.format("workflow.%s.%s.%s.%s", namespaceId, appId, workflowId, runId);
    Discoverable discoverable = new RandomEndpointStrategy(discoveryServiceClient.discover(serviceName)).pick();
    if (discoverable == null) {
        LOG.debug("No endpoint for service {}", serviceName);
        callback.handle(new Status(Status.Code.NOT_FOUND, ""));
        return;
    }
    // make HTTP call to workflow service.
    InetSocketAddress endpoint = discoverable.getSocketAddress();
    // Construct request
    String scheme = Arrays.equals(Constants.Security.SSL_URI_SCHEME.getBytes(), discoverable.getPayload()) ? Constants.Security.SSL_URI_SCHEME : Constants.Security.URI_SCHEME;
    String url = String.format("%s%s:%d/status", scheme, endpoint.getHostName(), endpoint.getPort());
    Request workflowRequest = new RequestBuilder("GET").setUrl(url).build();
    httpClient.executeRequest(workflowRequest, new AsyncCompletionHandler<Void>() {

        @Override
        public Void onCompleted(Response response) throws Exception {
            callback.handle(new Status(Status.Code.OK, response.getResponseBody(Charsets.UTF_8.name())));
            return null;
        }

        @Override
        public void onThrowable(Throwable t) {
            LOG.warn("Failed to request for workflow status", t);
            callback.handle(new Status(Status.Code.ERROR, ""));
        }
    });
}
Also used : Discoverable(org.apache.twill.discovery.Discoverable) RequestBuilder(com.ning.http.client.RequestBuilder) InetSocketAddress(java.net.InetSocketAddress) Request(com.ning.http.client.Request) IOException(java.io.IOException) Response(com.ning.http.client.Response) RandomEndpointStrategy(co.cask.cdap.common.discovery.RandomEndpointStrategy)

Aggregations

Response (com.ning.http.client.Response)33 Test (org.junit.Test)10 IOException (java.io.IOException)9 AsyncHttpClient (com.ning.http.client.AsyncHttpClient)7 Test (org.testng.annotations.Test)7 AsyncHttpClientConfig (com.ning.http.client.AsyncHttpClientConfig)4 Request (com.ning.http.client.Request)4 RequestBuilder (com.ning.http.client.RequestBuilder)4 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)4 HttpMethod (io.netty.handler.codec.http.HttpMethod)4 Map (java.util.Map)4 NodesInfoResponse (org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 CircuitBreaker (com.nike.fastbreak.CircuitBreaker)3 Span (com.nike.wingtips.Span)3 AsyncCompletionHandler (com.ning.http.client.AsyncCompletionHandler)3 NettyAsyncHttpProvider (com.ning.http.client.providers.netty.NettyAsyncHttpProvider)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 JsonObject (javax.json.JsonObject)3 JsonReader (javax.json.JsonReader)3