1 /*
   2  * Copyright (c) 1996, 2013, 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 /*
  27  * NOTE: This class lives in the package sun.net.www.protocol.https.
  28  * There is a copy in com.sun.net.ssl.internal.www.protocol.https for JSSE
  29  * 1.0.2 compatibility. It is 100% identical except the package and extends
  30  * lines. Any changes should be made to be class in sun.net.* and then copied
  31  * to com.sun.net.*.
  32  */
  33 
  34 // For both copies of the file, uncomment one line and comment the other
  35 // package sun.net.www.protocol.https;
  36 package com.sun.net.ssl.internal.www.protocol.https;
  37 
  38 import java.net.URL;
  39 import java.net.Proxy;
  40 import java.net.ProtocolException;
  41 import java.io.*;
  42 import javax.net.ssl.*;
  43 import java.security.Permission;
  44 import java.util.Map;
  45 import java.util.List;
  46 import sun.net.www.http.HttpClient;
  47 
  48 /**
  49  * A class to represent an HTTP connection to a remote object.
  50  *
  51  * Ideally, this class should subclass and inherit the http handler
  52  * implementation, but it can't do so because that class have the
  53  * wrong Java Type.  Thus it uses the delegate (aka, the
  54  * Adapter/Wrapper design pattern) to reuse code from the http
  55  * handler.
  56  *
  57  * Since it would use a delegate to access
  58  * sun.net.www.protocol.http.HttpURLConnection functionalities, it
  59  * needs to implement all public methods in it's super class and all
  60  * the way to Object.
  61  *
  62  */
  63 
  64 // For both copies of the file, uncomment one line and comment the other
  65 // public class HttpsURLConnectionImpl
  66 //      extends javax.net.ssl.HttpsURLConnection {
  67 public class HttpsURLConnectionOldImpl
  68         extends com.sun.net.ssl.HttpsURLConnection {
  69 
  70     private DelegateHttpsURLConnection delegate;
  71 
  72 // For both copies of the file, uncomment one line and comment the other
  73 //    HttpsURLConnectionImpl(URL u, Handler handler) throws IOException {
  74     HttpsURLConnectionOldImpl(URL u, Handler handler) throws IOException {
  75         this(u, null, handler);
  76     }
  77 
  78 // For both copies of the file, uncomment one line and comment the other
  79 //    HttpsURLConnectionImpl(URL u, Handler handler) throws IOException {
  80     HttpsURLConnectionOldImpl(URL u, Proxy p, Handler handler) throws IOException {
  81         super(u);
  82         delegate = new DelegateHttpsURLConnection(url, p, handler, this);
  83     }
  84 
  85     /**
  86      * Create a new HttpClient object, bypassing the cache of
  87      * HTTP client objects/connections.
  88      *
  89      * @param url       the URL being accessed
  90      */
  91     protected void setNewClient(URL url) throws IOException {
  92         delegate.setNewClient(url, false);
  93     }
  94 
  95     /**
  96      * Obtain a HttpClient object. Use the cached copy if specified.
  97      *
  98      * @param url       the URL being accessed
  99      * @param useCache  whether the cached connection should be used
 100      *                  if present
 101      */
 102     protected void setNewClient(URL url, boolean useCache)
 103             throws IOException {
 104         delegate.setNewClient(url, useCache);
 105     }
 106 
 107     /**
 108      * Create a new HttpClient object, set up so that it uses
 109      * per-instance proxying to the given HTTP proxy.  This
 110      * bypasses the cache of HTTP client objects/connections.
 111      *
 112      * @param url       the URL being accessed
 113      * @param proxyHost the proxy host to use
 114      * @param proxyPort the proxy port to use
 115      */
 116     protected void setProxiedClient(URL url, String proxyHost, int proxyPort)
 117             throws IOException {
 118         delegate.setProxiedClient(url, proxyHost, proxyPort);
 119     }
 120 
 121     /**
 122      * Obtain a HttpClient object, set up so that it uses per-instance
 123      * proxying to the given HTTP proxy. Use the cached copy of HTTP
 124      * client objects/connections if specified.
 125      *
 126      * @param url       the URL being accessed
 127      * @param proxyHost the proxy host to use
 128      * @param proxyPort the proxy port to use
 129      * @param useCache  whether the cached connection should be used
 130      *                  if present
 131      */
 132     protected void setProxiedClient(URL url, String proxyHost, int proxyPort,
 133             boolean useCache) throws IOException {
 134         delegate.setProxiedClient(url, proxyHost, proxyPort, useCache);
 135     }
 136 
 137     /**
 138      * Implements the HTTP protocol handler's "connect" method,
 139      * establishing an SSL connection to the server as necessary.
 140      */
 141     public void connect() throws IOException {
 142         delegate.connect();
 143     }
 144 
 145     /**
 146      * Used by subclass to access "connected" variable.  Since we are
 147      * delegating the actual implementation to "delegate", we need to
 148      * delegate the access of "connected" as well.
 149      */
 150     protected boolean isConnected() {
 151         return delegate.isConnected();
 152     }
 153 
 154     /**
 155      * Used by subclass to access "connected" variable.  Since we are
 156      * delegating the actual implementation to "delegate", we need to
 157      * delegate the access of "connected" as well.
 158      */
 159     protected void setConnected(boolean conn) {
 160         delegate.setConnected(conn);
 161     }
 162 
 163     /**
 164      * Returns the cipher suite in use on this connection.
 165      */
 166     public String getCipherSuite() {
 167         return delegate.getCipherSuite();
 168     }
 169 
 170     /**
 171      * Returns the certificate chain the client sent to the
 172      * server, or null if the client did not authenticate.
 173      */
 174     public java.security.cert.Certificate []
 175         getLocalCertificates() {
 176         return delegate.getLocalCertificates();
 177     }
 178 
 179     /**
 180      * Returns the server's certificate chain, or throws
 181      * SSLPeerUnverified Exception if
 182      * the server did not authenticate.
 183      */
 184     public java.security.cert.Certificate []
 185         getServerCertificates() throws SSLPeerUnverifiedException {
 186         return delegate.getServerCertificates();
 187     }
 188 
 189     /**
 190      * Returns the server's X.509 certificate chain, or null if
 191      * the server did not authenticate.
 192      *
 193      * NOTE: This method is not necessary for the version of this class
 194      * implementing javax.net.ssl.HttpsURLConnection, but provided for
 195      * compatibility with the com.sun.net.ssl.HttpsURLConnection version.
 196      */
 197     public javax.security.cert.X509Certificate[] getServerCertificateChain() {
 198         try {
 199             return delegate.getServerCertificateChain();
 200         } catch (SSLPeerUnverifiedException e) {
 201             // this method does not throw an exception as declared in
 202             // com.sun.net.ssl.HttpsURLConnection.
 203             // Return null for compatibility.
 204             return null;
 205         }
 206     }
 207 
 208     /*
 209      * Allowable input/output sequences:
 210      * [interpreted as POST/PUT]
 211      * - get output, [write output,] get input, [read input]
 212      * - get output, [write output]
 213      * [interpreted as GET]
 214      * - get input, [read input]
 215      * Disallowed:
 216      * - get input, [read input,] get output, [write output]
 217      */
 218 
 219     public synchronized OutputStream getOutputStream() throws IOException {
 220         return delegate.getOutputStream();
 221     }
 222 
 223     public synchronized InputStream getInputStream() throws IOException {
 224         return delegate.getInputStream();
 225     }
 226 
 227     public InputStream getErrorStream() {
 228         return delegate.getErrorStream();
 229     }
 230 
 231     /**
 232      * Disconnect from the server.
 233      */
 234     public void disconnect() {
 235         delegate.disconnect();
 236     }
 237 
 238     public boolean usingProxy() {
 239         return delegate.usingProxy();
 240     }
 241 
 242     /**
 243      * Returns an unmodifiable Map of the header fields.
 244      * The Map keys are Strings that represent the
 245      * response-header field names. Each Map value is an
 246      * unmodifiable List of Strings that represents
 247      * the corresponding field values.
 248      *
 249      * @return a Map of header fields
 250      * @since 1.4
 251      */
 252     public Map<String,List<String>> getHeaderFields() {
 253         return delegate.getHeaderFields();
 254     }
 255 
 256     /**
 257      * Gets a header field by name. Returns null if not known.
 258      * @param name the name of the header field
 259      */
 260     public String getHeaderField(String name) {
 261         return delegate.getHeaderField(name);
 262     }
 263 
 264     /**
 265      * Gets a header field by index. Returns null if not known.
 266      * @param n the index of the header field
 267      */
 268     public String getHeaderField(int n) {
 269         return delegate.getHeaderField(n);
 270     }
 271 
 272     /**
 273      * Gets a header field by index. Returns null if not known.
 274      * @param n the index of the header field
 275      */
 276     public String getHeaderFieldKey(int n) {
 277         return delegate.getHeaderFieldKey(n);
 278     }
 279 
 280     /**
 281      * Sets request property. If a property with the key already
 282      * exists, overwrite its value with the new value.
 283      * @param value the value to be set
 284      */
 285     public void setRequestProperty(String key, String value) {
 286         delegate.setRequestProperty(key, value);
 287     }
 288 
 289     /**
 290      * Adds a general request property specified by a
 291      * key-value pair.  This method will not overwrite
 292      * existing values associated with the same key.
 293      *
 294      * @param   key     the keyword by which the request is known
 295      *                  (e.g., "<code>accept</code>").
 296      * @param   value  the value associated with it.
 297      * @see #getRequestProperties(java.lang.String)
 298      * @since 1.4
 299      */
 300     public void addRequestProperty(String key, String value) {
 301         delegate.addRequestProperty(key, value);
 302     }
 303 
 304     /**
 305      * Overwrite super class method
 306      */
 307     public int getResponseCode() throws IOException {
 308         return delegate.getResponseCode();
 309     }
 310 
 311     public String getRequestProperty(String key) {
 312         return delegate.getRequestProperty(key);
 313     }
 314 
 315     /**
 316      * Returns an unmodifiable Map of general request
 317      * properties for this connection. The Map keys
 318      * are Strings that represent the request-header
 319      * field names. Each Map value is a unmodifiable List
 320      * of Strings that represents the corresponding
 321      * field values.
 322      *
 323      * @return  a Map of the general request properties for this connection.
 324      * @throws IllegalStateException if already connected
 325      * @since 1.4
 326      */
 327     public Map<String,List<String>> getRequestProperties() {
 328         return delegate.getRequestProperties();
 329     }
 330 
 331     /*
 332      * We support JDK 1.2.x so we can't count on these from JDK 1.3.
 333      * We override and supply our own version.
 334      */
 335     public void setInstanceFollowRedirects(boolean shouldFollow) {
 336         delegate.setInstanceFollowRedirects(shouldFollow);
 337     }
 338 
 339     public boolean getInstanceFollowRedirects() {
 340         return delegate.getInstanceFollowRedirects();
 341     }
 342 
 343     public void setRequestMethod(String method) throws ProtocolException {
 344         delegate.setRequestMethod(method);
 345     }
 346 
 347     public String getRequestMethod() {
 348         return delegate.getRequestMethod();
 349     }
 350 
 351     public String getResponseMessage() throws IOException {
 352         return delegate.getResponseMessage();
 353     }
 354 
 355     public long getHeaderFieldDate(String name, long Default) {
 356         return delegate.getHeaderFieldDate(name, Default);
 357     }
 358 
 359     public Permission getPermission() throws IOException {
 360         return delegate.getPermission();
 361     }
 362 
 363     public URL getURL() {
 364         return delegate.getURL();
 365     }
 366 
 367     public int getContentLength() {
 368         return delegate.getContentLength();
 369     }
 370 
 371     public long getContentLengthLong() {
 372         return delegate.getContentLengthLong();
 373     }
 374 
 375     public String getContentType() {
 376         return delegate.getContentType();
 377     }
 378 
 379     public String getContentEncoding() {
 380         return delegate.getContentEncoding();
 381     }
 382 
 383     public long getExpiration() {
 384         return delegate.getExpiration();
 385     }
 386 
 387     public long getDate() {
 388         return delegate.getDate();
 389     }
 390 
 391     public long getLastModified() {
 392         return delegate.getLastModified();
 393     }
 394 
 395     public int getHeaderFieldInt(String name, int Default) {
 396         return delegate.getHeaderFieldInt(name, Default);
 397     }
 398 
 399     public long getHeaderFieldLong(String name, long Default) {
 400         return delegate.getHeaderFieldLong(name, Default);
 401     }
 402 
 403     public Object getContent() throws IOException {
 404         return delegate.getContent();
 405     }
 406 
 407     @SuppressWarnings("rawtypes")
 408     public Object getContent(Class[] classes) throws IOException {
 409         return delegate.getContent(classes);
 410     }
 411 
 412     public String toString() {
 413         return delegate.toString();
 414     }
 415 
 416     public void setDoInput(boolean doinput) {
 417         delegate.setDoInput(doinput);
 418     }
 419 
 420     public boolean getDoInput() {
 421         return delegate.getDoInput();
 422     }
 423 
 424     public void setDoOutput(boolean dooutput) {
 425         delegate.setDoOutput(dooutput);
 426     }
 427 
 428     public boolean getDoOutput() {
 429         return delegate.getDoOutput();
 430     }
 431 
 432     public void setAllowUserInteraction(boolean allowuserinteraction) {
 433         delegate.setAllowUserInteraction(allowuserinteraction);
 434     }
 435 
 436     public boolean getAllowUserInteraction() {
 437         return delegate.getAllowUserInteraction();
 438     }
 439 
 440     public void setUseCaches(boolean usecaches) {
 441         delegate.setUseCaches(usecaches);
 442     }
 443 
 444     public boolean getUseCaches() {
 445         return delegate.getUseCaches();
 446     }
 447 
 448     public void setIfModifiedSince(long ifmodifiedsince) {
 449         delegate.setIfModifiedSince(ifmodifiedsince);
 450     }
 451 
 452     public long getIfModifiedSince() {
 453         return delegate.getIfModifiedSince();
 454     }
 455 
 456     public boolean getDefaultUseCaches() {
 457         return delegate.getDefaultUseCaches();
 458     }
 459 
 460     public void setDefaultUseCaches(boolean defaultusecaches) {
 461         delegate.setDefaultUseCaches(defaultusecaches);
 462     }
 463 
 464     /*
 465      * finalize (dispose) the delegated object.  Otherwise
 466      * sun.net.www.protocol.http.HttpURLConnection's finalize()
 467      * would have to be made public.
 468      */
 469     protected void finalize() throws Throwable {
 470         delegate.dispose();
 471     }
 472 
 473     public boolean equals(Object obj) {
 474         return delegate.equals(obj);
 475     }
 476 
 477     public int hashCode() {
 478         return delegate.hashCode();
 479     }
 480 
 481     public void setConnectTimeout(int timeout) {
 482         delegate.setConnectTimeout(timeout);
 483     }
 484 
 485     public int getConnectTimeout() {
 486         return delegate.getConnectTimeout();
 487     }
 488 
 489     public void setReadTimeout(int timeout) {
 490         delegate.setReadTimeout(timeout);
 491     }
 492 
 493     public int getReadTimeout() {
 494         return delegate.getReadTimeout();
 495     }
 496 
 497     public void setFixedLengthStreamingMode (int contentLength) {
 498         delegate.setFixedLengthStreamingMode(contentLength);
 499     }
 500 
 501     public void setFixedLengthStreamingMode(long contentLength) {
 502         delegate.setFixedLengthStreamingMode(contentLength);
 503     }
 504 
 505     public void setChunkedStreamingMode (int chunklen) {
 506         delegate.setChunkedStreamingMode(chunklen);
 507     }
 508 }