Search in sources :

Example 6 with Request

use of com.ning.http.client.Request in project tez by apache.

the class AsyncHttpConnection method connect.

/**
 * Connect to source
 *
 * @return true if connection was successful
 * false if connection was previously cleaned up
 * @throws IOException upon connection failure
 */
public boolean connect() throws IOException, InterruptedException {
    computeEncHash();
    RequestBuilder rb = new RequestBuilder();
    rb.setHeader(SecureShuffleUtils.HTTP_HEADER_URL_HASH, encHash);
    rb.setHeader(ShuffleHeader.HTTP_HEADER_NAME, ShuffleHeader.DEFAULT_HTTP_HEADER_NAME);
    rb.setHeader(ShuffleHeader.HTTP_HEADER_VERSION, ShuffleHeader.DEFAULT_HTTP_HEADER_VERSION);
    Request request = rb.setUrl(url.toString()).build();
    // for debugging
    LOG.debug("Request url={}, encHash={}, id={}", url, encHash);
    try {
        // Blocks calling thread until it receives headers, but have the option to defer response body
        responseFuture = httpAsyncClient.executeRequest(request, handler);
        // BodyDeferringAsyncHandler would automatically manage producer and consumer frequency mismatch
        dis = new TezBodyDeferringAsyncHandler.BodyDeferringInputStream(responseFuture, handler, pis);
        response = dis.getAsapResponse();
        if (response == null) {
            throw new IOException("Response is null");
        }
    } catch (IOException e) {
        throw e;
    }
    // verify the response
    int rc = response.getStatusCode();
    if (rc != HttpURLConnection.HTTP_OK) {
        LOG.debug("Request url={}, id={}", response.getUri());
        throw new IOException("Got invalid response code " + rc + " from " + url + ": " + response.getStatusText());
    }
    return true;
}
Also used : RequestBuilder(com.ning.http.client.RequestBuilder) Request(com.ning.http.client.Request) IOException(java.io.IOException)

Example 7 with Request

use of com.ning.http.client.Request 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) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) NettyAsyncHttpProvider(com.ning.http.client.providers.netty.NettyAsyncHttpProvider) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) HttpResponse(io.netty.handler.codec.http.HttpResponse) 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 8 with Request

use of com.ning.http.client.Request 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.code(), 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(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) 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 9 with Request

use of com.ning.http.client.Request 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(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) 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 10 with Request

use of com.ning.http.client.Request in project Singularity by HubSpot.

the class AbstractLeaderAwareResource method maybeProxyToLeader.

protected <T, Q> T maybeProxyToLeader(HttpServletRequest request, Class<T> clazz, Q body, Supplier<T> runnable) {
    if (leaderLatch.hasLeadership()) {
        return runnable.get();
    }
    String leaderUri;
    try {
        leaderUri = leaderLatch.getLeader().getId();
    } catch (Exception e) {
        throw new RuntimeException("Could not get leader uri to proxy request");
    }
    if (leaderUri.equals(leaderLatch.getId())) {
        LOG.warn("Got own leader id when not the leader! There is likely no leader, will not proxy");
        return runnable.get();
    }
    String url = "http://" + leaderUri + request.getContextPath() + request.getPathInfo();
    LOG.debug("Not the leader, proxying request to {}", url);
    BoundRequestBuilder requestBuilder;
    switch(request.getMethod().toUpperCase()) {
        case "POST":
            requestBuilder = httpClient.preparePost(url);
            break;
        case "PUT":
            requestBuilder = httpClient.preparePut(url);
            break;
        case "DELETE":
            requestBuilder = httpClient.prepareDelete(url);
            break;
        default:
            throw new WebApplicationException(String.format("Not meant to proxy request of method %s", request.getMethod()), 400);
    }
    try {
        if (body != null) {
            requestBuilder.setBody(objectMapper.writeValueAsBytes(body));
            LOG.trace("Added body {} to reqeust", body);
        }
    } catch (JsonProcessingException jpe) {
        LOG.error("Could not write body from object {}", body);
        throw new WebApplicationException(jpe, 500);
    }
    copyHeadersAndParams(requestBuilder, request);
    Request httpRequest = requestBuilder.build();
    Response response;
    try {
        LOG.trace("Sending request to leader: {}", httpRequest);
        response = httpClient.executeRequest(httpRequest).get();
    } catch (IOException | ExecutionException | InterruptedException e) {
        LOG.error("Could not proxy request {} to leader", e);
        throw new WebApplicationException(e, 500);
    }
    try {
        if (response.getStatusCode() > 399) {
            throw new WebApplicationException(response.getResponseBody(Charsets.UTF_8.toString()), response.getStatusCode());
        } else {
            return objectMapper.readValue(response.getResponseBodyAsStream(), clazz);
        }
    } catch (IOException ioe) {
        String message = String.format("Request to leader succeeded with status %s, but could not interpret response", response.getStatusCode());
        LOG.error(message, ioe);
        throw new WebApplicationException(message, ioe, 500);
    }
}
Also used : Response(com.ning.http.client.Response) BoundRequestBuilder(com.ning.http.client.AsyncHttpClient.BoundRequestBuilder) WebApplicationException(javax.ws.rs.WebApplicationException) Request(com.ning.http.client.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ExecutionException(java.util.concurrent.ExecutionException) WebApplicationException(javax.ws.rs.WebApplicationException)

Aggregations

Request (com.ning.http.client.Request)12 IOException (java.io.IOException)8 RequestBuilder (com.ning.http.client.RequestBuilder)6 Response (com.ning.http.client.Response)5 HttpResponseBodyPart (com.ning.http.client.HttpResponseBodyPart)4 AsyncHttpClient (com.ning.http.client.AsyncHttpClient)3 AsyncHttpClientConfig (com.ning.http.client.AsyncHttpClientConfig)3 NettyAsyncHttpProvider (com.ning.http.client.providers.netty.NettyAsyncHttpProvider)3 HttpRequest (io.netty.handler.codec.http.HttpRequest)3 ExecutionException (java.util.concurrent.ExecutionException)3 Test (org.junit.Test)3 SpanEventRecorder (com.navercorp.pinpoint.bootstrap.context.SpanEventRecorder)2 Trace (com.navercorp.pinpoint.bootstrap.context.Trace)2 HttpResponseHeaders (com.ning.http.client.HttpResponseHeaders)2 HttpResponseStatus (com.ning.http.client.HttpResponseStatus)2 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 URISyntaxException (java.net.URISyntaxException)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2