Search in sources :

Example 1 with StaticBodyGenerator

use of io.airlift.http.client.StaticBodyGenerator in project airlift by airlift.

the class JettyHttpClient method buildJettyRequest.

private HttpRequest buildJettyRequest(Request finalRequest) {
    HttpRequest jettyRequest = (HttpRequest) httpClient.newRequest(finalRequest.getUri());
    JettyRequestListener listener = new JettyRequestListener(finalRequest.getUri());
    jettyRequest.onRequestBegin(request -> listener.onRequestBegin());
    jettyRequest.onRequestSuccess(request -> listener.onRequestEnd());
    jettyRequest.onResponseBegin(response -> listener.onResponseBegin());
    jettyRequest.onComplete(result -> listener.onFinish());
    jettyRequest.attribute(PRESTO_STATS_KEY, listener);
    // jetty client always adds the user agent header
    // todo should there be a default?
    jettyRequest.getHeaders().remove(HttpHeader.USER_AGENT);
    jettyRequest.method(finalRequest.getMethod());
    for (Entry<String, String> entry : finalRequest.getHeaders().entries()) {
        jettyRequest.header(entry.getKey(), entry.getValue());
    }
    BodyGenerator bodyGenerator = finalRequest.getBodyGenerator();
    if (bodyGenerator != null) {
        if (bodyGenerator instanceof StaticBodyGenerator) {
            StaticBodyGenerator staticBodyGenerator = (StaticBodyGenerator) bodyGenerator;
            jettyRequest.content(new BytesContentProvider(staticBodyGenerator.getBody()));
        } else if (bodyGenerator instanceof FileBodyGenerator) {
            Path path = ((FileBodyGenerator) bodyGenerator).getPath();
            jettyRequest.content(fileContentProvider(path));
        } else {
            jettyRequest.content(new BodyGeneratorContentProvider(bodyGenerator, httpClient.getExecutor()));
        }
    }
    // timeouts
    jettyRequest.timeout(requestTimeoutMillis, MILLISECONDS);
    jettyRequest.idleTimeout(idleTimeoutMillis, MILLISECONDS);
    return jettyRequest;
}
Also used : HttpRequest(org.eclipse.jetty.client.HttpRequest) Path(java.nio.file.Path) BodyGenerator(io.airlift.http.client.BodyGenerator) StaticBodyGenerator(io.airlift.http.client.StaticBodyGenerator) FileBodyGenerator(io.airlift.http.client.FileBodyGenerator) FileBodyGenerator(io.airlift.http.client.FileBodyGenerator) StaticBodyGenerator(io.airlift.http.client.StaticBodyGenerator) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider)

Example 2 with StaticBodyGenerator

use of io.airlift.http.client.StaticBodyGenerator in project airlift by airlift.

the class JettyHttpClient method buildJettyRequest.

private HttpRequest buildJettyRequest(Request finalRequest, JettyRequestListener listener) {
    HttpRequest jettyRequest = (HttpRequest) httpClient.newRequest(finalRequest.getUri());
    jettyRequest.onRequestBegin(request -> listener.onRequestBegin());
    jettyRequest.onRequestSuccess(request -> listener.onRequestEnd());
    jettyRequest.onResponseBegin(response -> listener.onResponseBegin());
    jettyRequest.onComplete(result -> listener.onFinish());
    jettyRequest.onComplete(result -> {
        if (result.isFailed() && result.getFailure() instanceof TimeoutException) {
            clientDiagnostics.logDiagnosticsInfo(httpClient);
        }
    });
    jettyRequest.attribute(STATS_KEY, listener);
    jettyRequest.method(finalRequest.getMethod());
    for (Entry<String, String> entry : finalRequest.getHeaders().entries()) {
        jettyRequest.header(entry.getKey(), entry.getValue());
    }
    BodyGenerator bodyGenerator = finalRequest.getBodyGenerator();
    if (bodyGenerator != null) {
        if (bodyGenerator instanceof StaticBodyGenerator) {
            StaticBodyGenerator staticBodyGenerator = (StaticBodyGenerator) bodyGenerator;
            jettyRequest.content(new BytesContentProvider(staticBodyGenerator.getBody()));
        } else if (bodyGenerator instanceof FileBodyGenerator) {
            Path path = ((FileBodyGenerator) bodyGenerator).getPath();
            jettyRequest.content(fileContentProvider(path));
        } else {
            jettyRequest.content(new BodyGeneratorContentProvider(bodyGenerator, httpClient.getExecutor()));
        }
    }
    jettyRequest.followRedirects(finalRequest.isFollowRedirects());
    setPreserveAuthorization(jettyRequest, finalRequest.isPreserveAuthorizationOnRedirect());
    // timeouts
    jettyRequest.timeout(requestTimeoutMillis, MILLISECONDS);
    jettyRequest.idleTimeout(idleTimeoutMillis, MILLISECONDS);
    return jettyRequest;
}
Also used : HttpRequest(org.eclipse.jetty.client.HttpRequest) Path(java.nio.file.Path) BodyGenerator(io.airlift.http.client.BodyGenerator) StaticBodyGenerator(io.airlift.http.client.StaticBodyGenerator) FileBodyGenerator(io.airlift.http.client.FileBodyGenerator) FileBodyGenerator(io.airlift.http.client.FileBodyGenerator) StaticBodyGenerator(io.airlift.http.client.StaticBodyGenerator) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

BodyGenerator (io.airlift.http.client.BodyGenerator)2 FileBodyGenerator (io.airlift.http.client.FileBodyGenerator)2 StaticBodyGenerator (io.airlift.http.client.StaticBodyGenerator)2 Path (java.nio.file.Path)2 HttpRequest (org.eclipse.jetty.client.HttpRequest)2 BytesContentProvider (org.eclipse.jetty.client.util.BytesContentProvider)2 TimeoutException (java.util.concurrent.TimeoutException)1