1 /*
   2  * Copyright (c) 2017, 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.incubator.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.net.URI;
  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 
  40 /**
  41  * An HttpClientFacade is a simple class that wraps an HttpClient implementation
  42  * and delegates everything to its implementation delegate.
  43  */
  44 final class HttpClientFacade extends HttpClient {
  45 
  46     final HttpClientImpl impl;
  47 
  48     /**
  49      * Creates an HttpClientFacade.
  50      */
  51     HttpClientFacade(HttpClientImpl impl) {
  52         this.impl = impl;
  53     }
  54 
  55     @Override
  56     public Optional<CookieHandler> cookieHandler() {
  57         return impl.cookieHandler();
  58     }
  59 
  60     @Override
  61     public Redirect followRedirects() {
  62         return impl.followRedirects();
  63     }
  64 
  65     @Override
  66     public Optional<ProxySelector> proxy() {
  67         return impl.proxy();
  68     }
  69 
  70     @Override
  71     public SSLContext sslContext() {
  72         return impl.sslContext();
  73     }
  74 
  75     @Override
  76     public SSLParameters sslParameters() {
  77         return impl.sslParameters();
  78     }
  79 
  80     @Override
  81     public Optional<Authenticator> authenticator() {
  82         return impl.authenticator();
  83     }
  84 
  85     @Override
  86     public HttpClient.Version version() {
  87         return impl.version();
  88     }
  89 
  90     @Override
  91     public Optional<Executor> executor() {
  92         return impl.executor();
  93     }
  94 
  95     @Override
  96     public <T> HttpResponse<T>
  97     send(HttpRequest req, HttpResponse.BodyHandler<T> responseBodyHandler)
  98         throws IOException, InterruptedException
  99     {
 100         try {
 101             return impl.send(req, responseBodyHandler);
 102         } finally {
 103             Reference.reachabilityFence(this);
 104         }
 105     }
 106 
 107     @Override
 108     public <T> CompletableFuture<HttpResponse<T>>
 109     sendAsync(HttpRequest req, HttpResponse.BodyHandler<T> responseBodyHandler) {
 110         try {
 111             return impl.sendAsync(req, responseBodyHandler);
 112         } finally {
 113             Reference.reachabilityFence(this);
 114         }
 115     }
 116 
 117     @Override
 118     public <U, T> CompletableFuture<U>
 119     sendAsync(HttpRequest req, HttpResponse.MultiSubscriber<U, T> multiSubscriber) {
 120         try {
 121             return impl.sendAsync(req, multiSubscriber);
 122         } finally {
 123             Reference.reachabilityFence(this);
 124         }
 125     }
 126 
 127     @Override
 128     public WebSocket.Builder newWebSocketBuilder() {
 129         try {
 130             return impl.newWebSocketBuilder();
 131         } finally {
 132             Reference.reachabilityFence(this);
 133         }
 134     }
 135 
 136     @Override
 137     public String toString() {
 138         // Used by tests to get the client's id.
 139         return impl.toString();
 140     }
 141 }