Search in sources :

Example 1 with NON_SECURE_SERVER_PORT

use of io.servicetalk.examples.http.redirects.RedirectingServer.NON_SECURE_SERVER_PORT in project servicetalk by apple.

the class MultiAddressUrlRedirectClient method main.

public static void main(String... args) throws Exception {
    try (HttpClient client = HttpClients.forMultiAddressUrl().followRedirects(new RedirectConfigBuilder().maxRedirects(3).allowNonRelativeRedirects(true).allowedMethods(GET, POST).redirectPredicate((relative, redirectCount, prevRequest, redirectResponse) -> // allow only relative redirects
    relative || // OR non-relative redirects to a trusted server:
    redirectResponse.headers().get(LOCATION, "").toString().startsWith("https://localhost:" + SECURE_SERVER_PORT)).headersToRedirect(CUSTOM_HEADER).redirectPayloadBody(true).redirectRequestTransformer((relative, prevRequest, redirectResponse, redirectedRequest) -> {
        // prevRequest and redirectResponse: check/copy other headers, modify request method, etc.
        return redirectedRequest;
    }).build()).initializer((scheme, address, builder) -> {
        // already provides default SSL configuration and this step may be skipped.
        if ("https".equalsIgnoreCase(scheme)) {
            builder.sslConfig(new ClientSslConfigBuilder(DefaultTestCerts::loadServerCAPem).build());
        }
    }).build()) {
        final String serverThatRedirects = "http://localhost:" + NON_SECURE_SERVER_PORT;
        System.out.println("- Simple GET request:");
        client.request(client.get(serverThatRedirects + "/relative")).whenOnSuccess(resp -> {
            System.out.println(resp.toString((name, value) -> value));
            System.out.println(resp.payloadBody(textSerializerAscii()));
            System.out.println();
        }).toFuture().get();
        System.out.println("- Relative redirect for POST request with headers and payload body:");
        client.request(client.post(serverThatRedirects + "/relative").addHeader(CUSTOM_HEADER, "value").payloadBody(client.executionContext().bufferAllocator().fromAscii("some_content"))).whenOnSuccess(resp -> {
            System.out.println(resp.toString((name, value) -> value));
            System.out.println(resp.payloadBody(textSerializerAscii()));
            System.out.println();
        }).toFuture().get();
        System.out.println("- Non-relative redirect for POST request with headers and payload body:");
        client.request(client.post(serverThatRedirects + "/non-relative").addHeader(CUSTOM_HEADER, "value").payloadBody(client.executionContext().bufferAllocator().fromAscii("some_content"))).whenOnSuccess(resp -> {
            System.out.println(resp.toString((name, value) -> value));
            System.out.println(resp.payloadBody(textSerializerAscii()));
            System.out.println();
        }).toFuture().get();
    }
}
Also used : HttpSerializers.textSerializerAscii(io.servicetalk.http.api.HttpSerializers.textSerializerAscii) HttpRequestMethod(io.servicetalk.http.api.HttpRequestMethod) CUSTOM_HEADER(io.servicetalk.examples.http.redirects.RedirectingServer.CUSTOM_HEADER) LOCATION(io.servicetalk.http.api.HttpHeaderNames.LOCATION) POST(io.servicetalk.http.api.HttpRequestMethod.POST) MultiAddressHttpClientBuilder(io.servicetalk.http.api.MultiAddressHttpClientBuilder) RedirectConfigBuilder(io.servicetalk.http.api.RedirectConfigBuilder) GET(io.servicetalk.http.api.HttpRequestMethod.GET) 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) RedirectConfig(io.servicetalk.http.api.RedirectConfig) HttpClient(io.servicetalk.http.api.HttpClient) ClientSslConfigBuilder(io.servicetalk.transport.api.ClientSslConfigBuilder) DefaultTestCerts(io.servicetalk.test.resources.DefaultTestCerts) HttpClients(io.servicetalk.http.netty.HttpClients) HttpClient(io.servicetalk.http.api.HttpClient) RedirectConfigBuilder(io.servicetalk.http.api.RedirectConfigBuilder) ClientSslConfigBuilder(io.servicetalk.transport.api.ClientSslConfigBuilder)

Example 2 with NON_SECURE_SERVER_PORT

use of io.servicetalk.examples.http.redirects.RedirectingServer.NON_SECURE_SERVER_PORT in project servicetalk by apple.

the class SingleAddressRedirectClient method main.

public static void main(String... args) throws Exception {
    try (HttpClient client = HttpClients.forSingleAddress("localhost", NON_SECURE_SERVER_PORT).appendClientFilter(new RedirectingHttpRequesterFilter(new RedirectConfigBuilder().allowedMethods(GET, // by default, POST requests don't follow redirects:
    POST).build())).build()) {
        System.out.println("- Simple GET request:");
        client.request(client.get("/relative")).whenOnSuccess(resp -> {
            System.out.println(resp.toString((name, value) -> value));
            System.out.println(resp.payloadBody(textSerializerAscii()));
            System.out.println();
        }).toFuture().get();
        System.out.println("- POST request with headers and payload body:");
        client.request(client.post("/relative").addHeader(CUSTOM_HEADER, "value").payloadBody(client.executionContext().bufferAllocator().fromAscii("some_content"))).whenOnSuccess(resp -> {
            System.out.println(resp.toString((name, value) -> value));
            System.out.println(resp.payloadBody(textSerializerAscii()));
            System.out.println();
        }).toFuture().get();
    }
}
Also used : NON_SECURE_SERVER_PORT(io.servicetalk.examples.http.redirects.RedirectingServer.NON_SECURE_SERVER_PORT) HttpSerializers.textSerializerAscii(io.servicetalk.http.api.HttpSerializers.textSerializerAscii) CUSTOM_HEADER(io.servicetalk.examples.http.redirects.RedirectingServer.CUSTOM_HEADER) HttpClient(io.servicetalk.http.api.HttpClient) POST(io.servicetalk.http.api.HttpRequestMethod.POST) RedirectingHttpRequesterFilter(io.servicetalk.http.utils.RedirectingHttpRequesterFilter) RedirectConfigBuilder(io.servicetalk.http.api.RedirectConfigBuilder) HttpClients(io.servicetalk.http.netty.HttpClients) GET(io.servicetalk.http.api.HttpRequestMethod.GET) HttpClient(io.servicetalk.http.api.HttpClient) RedirectConfigBuilder(io.servicetalk.http.api.RedirectConfigBuilder) RedirectingHttpRequesterFilter(io.servicetalk.http.utils.RedirectingHttpRequesterFilter)

Example 3 with NON_SECURE_SERVER_PORT

use of io.servicetalk.examples.http.redirects.RedirectingServer.NON_SECURE_SERVER_PORT 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

CUSTOM_HEADER (io.servicetalk.examples.http.redirects.RedirectingServer.CUSTOM_HEADER)3 NON_SECURE_SERVER_PORT (io.servicetalk.examples.http.redirects.RedirectingServer.NON_SECURE_SERVER_PORT)3 HttpClient (io.servicetalk.http.api.HttpClient)3 HttpSerializers.textSerializerAscii (io.servicetalk.http.api.HttpSerializers.textSerializerAscii)3 HttpClients (io.servicetalk.http.netty.HttpClients)3 SECURE_SERVER_PORT (io.servicetalk.examples.http.redirects.RedirectingServer.SECURE_SERVER_PORT)2 LOCATION (io.servicetalk.http.api.HttpHeaderNames.LOCATION)2 GET (io.servicetalk.http.api.HttpRequestMethod.GET)2 POST (io.servicetalk.http.api.HttpRequestMethod.POST)2 RedirectConfigBuilder (io.servicetalk.http.api.RedirectConfigBuilder)2 DefaultTestCerts (io.servicetalk.test.resources.DefaultTestCerts)2 ClientSslConfigBuilder (io.servicetalk.transport.api.ClientSslConfigBuilder)2 Single.succeeded (io.servicetalk.concurrent.api.Single.succeeded)1 HttpRequest (io.servicetalk.http.api.HttpRequest)1 HttpRequestMethod (io.servicetalk.http.api.HttpRequestMethod)1 REDIRECTION_3XX (io.servicetalk.http.api.HttpResponseStatus.StatusClass.REDIRECTION_3XX)1 MultiAddressHttpClientBuilder (io.servicetalk.http.api.MultiAddressHttpClientBuilder)1 RedirectConfig (io.servicetalk.http.api.RedirectConfig)1 RedirectingHttpRequesterFilter (io.servicetalk.http.utils.RedirectingHttpRequesterFilter)1 HostAndPort (io.servicetalk.transport.api.HostAndPort)1