1 /*
   2  * Copyright (c) 2017, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.internal.net.http;
  27 
  28 import java.io.IOException;
  29 import java.lang.ref.Reference;
  30 import java.net.Authenticator;
  31 import java.net.CookieHandler;
  32 import java.net.ProxySelector;
  33 import java.time.Duration;
  34 import java.util.Optional;
  35 import java.util.concurrent.CompletableFuture;
  36 import java.util.concurrent.Executor;
  37 import javax.net.ssl.SSLContext;
  38 import javax.net.ssl.SSLParameters;
  39 import java.net.http.HttpClient;
  40 import java.net.http.HttpRequest;
  41 import java.net.http.HttpResponse;
  42 import java.net.http.HttpResponse.BodyHandler;
  43 import java.net.http.HttpResponse.PushPromiseHandler;
  44 import java.net.http.WebSocket;
  45 import jdk.internal.net.http.common.OperationTrackers.Trackable;
  46 import jdk.internal.net.http.common.OperationTrackers.Tracker;
  47 
  48 /**
  49  * An HttpClientFacade is a simple class that wraps an HttpClient implementation
  50  * and delegates everything to its implementation delegate.
  51  */
  52 final class HttpClientFacade extends HttpClient implements Trackable {
  53 
  54     final HttpClientImpl impl;
  55 
  56     /**
  57      * Creates an HttpClientFacade.
  58      */
  59     HttpClientFacade(HttpClientImpl impl) {
  60         this.impl = impl;
  61     }
  62 
  63     @Override // for tests
  64     public Tracker getOperationsTracker() {
  65         return impl.getOperationsTracker();
  66     }
  67 
  68     @Override
  69     public Optional<CookieHandler> cookieHandler() {
  70         return impl.cookieHandler();
  71     }
  72 
  73     @Override
  74     public Optional<Duration> connectTimeout() {
  75         return impl.connectTimeout();
  76     }
  77 
  78     @Override
  79     public Redirect followRedirects() {
  80         return impl.followRedirects();
  81     }
  82 
  83     @Override
  84     public Optional<ProxySelector> proxy() {
  85         return impl.proxy();
  86     }
  87 
  88     @Override
  89     public SSLContext sslContext() {
  90         return impl.sslContext();
  91     }
  92 
  93     @Override
  94     public SSLParameters sslParameters() {
  95         return impl.sslParameters();
  96     }
  97 
  98     @Override
  99     public Optional<Authenticator> authenticator() {
 100         return impl.authenticator();
 101     }
 102 
 103     @Override
 104     public HttpClient.Version version() {
 105         return impl.version();
 106     }
 107 
 108     @Override
 109     public Optional<Executor> executor() {
 110         return impl.executor();
 111     }
 112 
 113     @Override
 114     public <T> HttpResponse<T>
 115     send(HttpRequest req, HttpResponse.BodyHandler<T> responseBodyHandler)
 116         throws IOException, InterruptedException
 117     {
 118         try {
 119             return impl.send(req, responseBodyHandler);
 120         } finally {
 121             Reference.reachabilityFence(this);
 122         }
 123     }
 124 
 125     @Override
 126     public <T> CompletableFuture<HttpResponse<T>>
 127     sendAsync(HttpRequest req, HttpResponse.BodyHandler<T> responseBodyHandler) {
 128         try {
 129             return impl.sendAsync(req, responseBodyHandler);
 130         } finally {
 131             Reference.reachabilityFence(this);
 132         }
 133     }
 134 
 135     @Override
 136     public <T> CompletableFuture<HttpResponse<T>>
 137     sendAsync(HttpRequest req,
 138               BodyHandler<T> responseBodyHandler,
 139               PushPromiseHandler<T> pushPromiseHandler){
 140         try {
 141             return impl.sendAsync(req, responseBodyHandler, pushPromiseHandler);
 142         } finally {
 143             Reference.reachabilityFence(this);
 144         }
 145     }
 146 
 147     @Override
 148     public WebSocket.Builder newWebSocketBuilder() {
 149         try {
 150             return impl.newWebSocketBuilder();
 151         } finally {
 152             Reference.reachabilityFence(this);
 153         }
 154     }
 155 
 156     @Override
 157     public String toString() {
 158         // Used by tests to get the client's id.
 159         return impl.toString();
 160     }
 161 }