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