summaryrefslogtreecommitdiffstats
path: root/jdisc_http_service/src/main/java/com/yahoo/jdisc/http/client/ChunkedRequest.java
blob: 6da40f3c4436f368ed93de7cb1edd3382bc2d325 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.http.client;

import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.RequestBuilder;
import com.yahoo.jdisc.Metric;
import com.yahoo.jdisc.Request;
import com.yahoo.jdisc.handler.ContentChannel;
import com.yahoo.jdisc.handler.ResponseHandler;
import com.yahoo.jdisc.http.HttpRequest;

import java.io.IOException;

/**
 * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen Hult</a>
 */
final class ChunkedRequest {

    private ChunkedRequest() {
        // hide
    }

    public static ContentChannel executeRequest(AsyncHttpClient ningClient, Request request, HttpRequest.Method method,
                                                ResponseHandler handler, Metric metric, Metric.Context ctx)
    {
        RequestBuilder builder = RequestBuilderFactory.newInstance(request, method);
        ChunkedRequestContent content = new ChunkedRequestContent(request);
        builder.setBody(content);
        try {
            ningClient.executeRequest(builder.build(), new AsyncResponseHandler(request, handler, metric, ctx));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return content;
    }
}