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