Search in sources :

Example 1 with REDIRECTION_3XX

use of io.servicetalk.http.api.HttpResponseStatus.StatusClass.REDIRECTION_3XX in project servicetalk by apple.

the class ManualRedirectClient method main.

public static void main(String... args) throws Exception {
    try (HttpClient secureClient = HttpClients.forSingleAddress("localhost", SECURE_SERVER_PORT).sslConfig(new ClientSslConfigBuilder(DefaultTestCerts::loadServerCAPem).build()).build()) {
        try (HttpClient client = HttpClients.forSingleAddress("localhost", NON_SECURE_SERVER_PORT).build()) {
            System.out.println("- Redirect of a GET request with a custom header:");
            HttpRequest originalGet = client.get("/non-relative").addHeader(CUSTOM_HEADER, "value");
            client.request(originalGet).flatMap(response -> {
                if (response.status().statusClass() == REDIRECTION_3XX) {
                    CharSequence location = response.headers().get(LOCATION);
                    HttpClient redirectClient = lookupClient(location, client, secureClient);
                    return redirectClient.request(redirectClient.newRequest(originalGet.method(), location.toString()).addHeader(CUSTOM_HEADER, originalGet.headers().get(CUSTOM_HEADER)));
                }
                // Decided not to follow redirect, return the original response or an error:
                return succeeded(response);
            }).whenOnSuccess(resp -> {
                System.out.println(resp.toString((name, value) -> value));
                System.out.println(resp.payloadBody(textSerializerAscii()));
                System.out.println();
            }).toFuture().get();
            System.out.println("- Redirect of a POST request with a payload body:");
            HttpRequest originalPost = client.post("/non-relative").payloadBody(client.executionContext().bufferAllocator().fromAscii("some_content"));
            client.request(originalPost).flatMap(response -> {
                if (response.status().statusClass() == REDIRECTION_3XX) {
                    CharSequence location = response.headers().get(LOCATION);
                    HttpClient redirectClient = lookupClient(location, client, secureClient);
                    return redirectClient.request(redirectClient.newRequest(originalPost.method(), location.toString()).payloadBody(originalPost.payloadBody()));
                }
                // Decided not to follow redirect, return the original response or an error:
                return succeeded(response);
            }).whenOnSuccess(resp -> {
                System.out.println(resp.toString((name, value) -> value));
                System.out.println(resp.payloadBody(textSerializerAscii()));
            }).toFuture().get();
        }
    }
}
Also used : HttpRequest(io.servicetalk.http.api.HttpRequest) HttpSerializers.textSerializerAscii(io.servicetalk.http.api.HttpSerializers.textSerializerAscii) REDIRECTION_3XX(io.servicetalk.http.api.HttpResponseStatus.StatusClass.REDIRECTION_3XX) CUSTOM_HEADER(io.servicetalk.examples.http.redirects.RedirectingServer.CUSTOM_HEADER) LOCATION(io.servicetalk.http.api.HttpHeaderNames.LOCATION) SECURE_SERVER_PORT(io.servicetalk.examples.http.redirects.RedirectingServer.SECURE_SERVER_PORT) NON_SECURE_SERVER_PORT(io.servicetalk.examples.http.redirects.RedirectingServer.NON_SECURE_SERVER_PORT) HttpRequest(io.servicetalk.http.api.HttpRequest) Single.succeeded(io.servicetalk.concurrent.api.Single.succeeded) HttpClient(io.servicetalk.http.api.HttpClient) ClientSslConfigBuilder(io.servicetalk.transport.api.ClientSslConfigBuilder) DefaultTestCerts(io.servicetalk.test.resources.DefaultTestCerts) HttpClients(io.servicetalk.http.netty.HttpClients) Nullable(javax.annotation.Nullable) HostAndPort(io.servicetalk.transport.api.HostAndPort) HttpClient(io.servicetalk.http.api.HttpClient) ClientSslConfigBuilder(io.servicetalk.transport.api.ClientSslConfigBuilder)

Aggregations

Single.succeeded (io.servicetalk.concurrent.api.Single.succeeded)1 CUSTOM_HEADER (io.servicetalk.examples.http.redirects.RedirectingServer.CUSTOM_HEADER)1 NON_SECURE_SERVER_PORT (io.servicetalk.examples.http.redirects.RedirectingServer.NON_SECURE_SERVER_PORT)1 SECURE_SERVER_PORT (io.servicetalk.examples.http.redirects.RedirectingServer.SECURE_SERVER_PORT)1 HttpClient (io.servicetalk.http.api.HttpClient)1 LOCATION (io.servicetalk.http.api.HttpHeaderNames.LOCATION)1 HttpRequest (io.servicetalk.http.api.HttpRequest)1 REDIRECTION_3XX (io.servicetalk.http.api.HttpResponseStatus.StatusClass.REDIRECTION_3XX)1 HttpSerializers.textSerializerAscii (io.servicetalk.http.api.HttpSerializers.textSerializerAscii)1 HttpClients (io.servicetalk.http.netty.HttpClients)1 DefaultTestCerts (io.servicetalk.test.resources.DefaultTestCerts)1 ClientSslConfigBuilder (io.servicetalk.transport.api.ClientSslConfigBuilder)1 HostAndPort (io.servicetalk.transport.api.HostAndPort)1 Nullable (javax.annotation.Nullable)1