test/jdk/java/net/httpclient/offline/DelegatingHttpClient.java
author chegar
Thu, 18 Jan 2018 11:26:39 +0000
branchhttp-client-branch
changeset 56023 fa36a61f4cbf
child 56089 42208b2f224e
permissions -rw-r--r--
http-client-branch: add offline testing example
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56023
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
     1
/*
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
     4
 *
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
     8
 *
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    13
 * accompanied this code).
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    14
 *
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    18
 *
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    21
 * questions.
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    22
 */
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    23
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    24
import javax.net.ssl.SSLContext;
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    25
import javax.net.ssl.SSLParameters;
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    26
import java.io.IOException;
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    27
import java.net.Authenticator;
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    28
import java.net.CookieHandler;
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    29
import java.net.ProxySelector;
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    30
import java.util.Optional;
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    31
import java.util.concurrent.CompletableFuture;
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    32
import java.util.concurrent.Executor;
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    33
import jdk.incubator.http.HttpClient;
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    34
import jdk.incubator.http.HttpRequest;
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    35
import jdk.incubator.http.HttpResponse;
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    36
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    37
/**
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    38
 * An HttpClient that delegates all its operations to the given client.
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    39
 */
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    40
public class DelegatingHttpClient extends HttpClient {
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    41
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    42
    private final HttpClient client;
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    43
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    44
    public DelegatingHttpClient(HttpClient client) {
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    45
        this.client = client;
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    46
    }
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    47
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    48
    @Override
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    49
    public Optional<CookieHandler> cookieHandler() {
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    50
        return client.cookieHandler();
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    51
    }
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    52
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    53
    @Override
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    54
    public Redirect followRedirects() {
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    55
        return client.followRedirects();
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    56
    }
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    57
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    58
    @Override
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    59
    public Optional<ProxySelector> proxy() {
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    60
        return client.proxy();
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    61
    }
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    62
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    63
    @Override
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    64
    public SSLContext sslContext() {
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    65
        return client.sslContext();
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    66
    }
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    67
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    68
    @Override
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    69
    public SSLParameters sslParameters() {
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    70
        return client.sslParameters();
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    71
    }
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    72
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    73
    @Override
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    74
    public Optional<Authenticator> authenticator() {
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    75
        return client.authenticator();
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    76
    }
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    77
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    78
    @Override
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    79
    public Version version() {
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    80
        return client.version();
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    81
    }
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    82
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    83
    @Override
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    84
    public Optional<Executor> executor() {
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    85
        return client.executor();
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    86
    }
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    87
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    88
    @Override
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    89
    public <T> HttpResponse<T> send(HttpRequest request,
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    90
                                    HttpResponse.BodyHandler<T> responseBodyHandler)
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    91
            throws IOException, InterruptedException {
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    92
        return client.send(request, responseBodyHandler);
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    93
    }
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    94
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    95
    @Override
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    96
    public <T> CompletableFuture<HttpResponse<T>>
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    97
    sendAsync(HttpRequest request,
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    98
              HttpResponse.BodyHandler<T> responseBodyHandler) {
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    99
        return client.sendAsync(request, responseBodyHandler);
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   100
    }
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   101
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   102
    @Override
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   103
    public <T> CompletableFuture<HttpResponse<T>>
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   104
    sendAsync(HttpRequest request,
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   105
              HttpResponse.BodyHandler<T> responseBodyHandler,
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   106
              HttpResponse.PushPromiseHandler<T> pushPromiseHandler) {
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   107
        return client.sendAsync(request, responseBodyHandler, pushPromiseHandler);
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   108
    }
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   109
}