jdk/src/share/classes/javax/net/ssl/HttpsURLConnection.java
author xuelei
Sun, 18 Nov 2012 01:31:44 -0800
changeset 14514 46a8ed03c7fc
parent 10917 becbe249e46b
child 23010 6dadb192ad81
permissions -rw-r--r--
8003587: Warning cleanup in package javax.net.ssl Summary: Removes unnecessary imports and adds missing Override annotations Reviewed-by: xuelei Contributed-by: Florian Weimer <fweimer@redhat.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10916
5a763073fa37 7096936: issue in jsse/runtime
xuelei
parents: 7043
diff changeset
     2
 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.net.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.HttpURLConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * <code>HttpsURLConnection</code> extends <code>HttpURLConnection</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * with support for https-specific features.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * See <A HREF="http://www.w3.org/pub/WWW/Protocols/">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * http://www.w3.org/pub/WWW/Protocols/</A> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <A HREF="http://www.ietf.org/"> RFC 2818 </A>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * for more details on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * https specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * This class uses <code>HostnameVerifier</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <code>SSLSocketFactory</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * There are default implementations defined for both classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * However, the implementations can be replaced on a per-class (static) or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * per-instance basis.  All new <code>HttpsURLConnection</code>s instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * will be assigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * the "default" static values at instance creation, but they can be overriden
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * by calling the appropriate per-instance set method(s) before
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <code>connect</code>ing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
abstract public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
class HttpsURLConnection extends HttpURLConnection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * Creates an <code>HttpsURLConnection</code> using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * URL specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * @param url the URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    protected HttpsURLConnection(URL url) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        super(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Returns the cipher suite in use on this connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @return the cipher suite
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * @throws IllegalStateException if this method is called before
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     *          the connection has been established.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public abstract String getCipherSuite();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Returns the certificate(s) that were sent to the server during
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * handshaking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Note: This method is useful only when using certificate-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * cipher suites.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * When multiple certificates are available for use in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * handshake, the implementation chooses what it considers the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * "best" certificate chain available, and transmits that to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * the other side.  This method allows the caller to know
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * which certificate chain was actually sent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @return an ordered array of certificates,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *          with the client's own certificate first followed by any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *          certificate authorities.  If no certificates were sent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *          then null is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @throws IllegalStateException if this method is called before
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *          the connection has been established.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @see #getLocalPrincipal()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public abstract java.security.cert.Certificate [] getLocalCertificates();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Returns the server's certificate chain which was established
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * as part of defining the session.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Note: This method can be used only when using certificate-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * cipher suites; using it with non-certificate-based cipher suites,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * such as Kerberos, will throw an SSLPeerUnverifiedException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @return an ordered array of server certificates,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *          with the peer's own certificate first followed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *          any certificate authorities.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @throws SSLPeerUnverifiedException if the peer is not verified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @throws IllegalStateException if this method is called before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *          the connection has been established.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @see #getPeerPrincipal()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public abstract java.security.cert.Certificate [] getServerCertificates()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            throws SSLPeerUnverifiedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Returns the server's principal which was established as part of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * defining the session.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Note: Subclasses should override this method. If not overridden, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * will default to returning the X500Principal of the server's end-entity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * certificate for certificate-based ciphersuites, or throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * SSLPeerUnverifiedException for non-certificate based ciphersuites,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * such as Kerberos.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @return the server's principal. Returns an X500Principal of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * end-entity certiticate for X509-based cipher suites, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * KerberosPrincipal for Kerberos cipher suites.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @throws SSLPeerUnverifiedException if the peer was not verified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @throws IllegalStateException if this method is called before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *          the connection has been established.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @see #getServerCertificates()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @see #getLocalPrincipal()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public Principal getPeerPrincipal()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            throws SSLPeerUnverifiedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        java.security.cert.Certificate[] certs = getServerCertificates();
10367
d61bc9ec5dce 7084040: Clearup warning in HttpsURLConnection
xuelei
parents: 7043
diff changeset
   146
        return ((X509Certificate)certs[0]).getSubjectX500Principal();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Returns the principal that was sent to the server during handshaking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Note: Subclasses should override this method. If not overridden, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * will default to returning the X500Principal of the end-entity certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * that was sent to the server for certificate-based ciphersuites or,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * return null for non-certificate based ciphersuites, such as Kerberos.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @return the principal sent to the server. Returns an X500Principal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * of the end-entity certificate for X509-based cipher suites, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * KerberosPrincipal for Kerberos cipher suites. If no principal was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * sent, then null is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @throws IllegalStateException if this method is called before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *          the connection has been established.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @see #getLocalCertificates()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @see #getPeerPrincipal()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public Principal getLocalPrincipal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        java.security.cert.Certificate[] certs = getLocalCertificates();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (certs != null) {
10367
d61bc9ec5dce 7084040: Clearup warning in HttpsURLConnection
xuelei
parents: 7043
diff changeset
   174
            return ((X509Certificate)certs[0]).getSubjectX500Principal();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * <code>HostnameVerifier</code> provides a callback mechanism so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * implementers of this interface can supply a policy for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * handling the case where the host to connect to and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * the server name from the certificate mismatch.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * The default implementation will deny such connections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   188
    private static HostnameVerifier defaultHostnameVerifier =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   189
                                        new DefaultHostnameVerifier();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * The initial default <code>HostnameVerifier</code>.  Should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * updated for another other type of <code>HostnameVerifier</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * that are created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    private static class DefaultHostnameVerifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            implements HostnameVerifier {
14514
46a8ed03c7fc 8003587: Warning cleanup in package javax.net.ssl
xuelei
parents: 10917
diff changeset
   198
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        public boolean verify(String hostname, SSLSession session) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * The <code>hostnameVerifier</code> for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    protected HostnameVerifier hostnameVerifier = defaultHostnameVerifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Sets the default <code>HostnameVerifier</code> inherited by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * new instance of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * If this method is not called, the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * <code>HostnameVerifier</code> assumes the connection should not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * be permitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @param v the default host name verifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @throws IllegalArgumentException if the <code>HostnameVerifier</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *          parameter is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @throws SecurityException if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *         <code>checkPermission</code> method does not allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *         <code>SSLPermission("setHostnameVerifier")</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @see #getDefaultHostnameVerifier()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    public static void setDefaultHostnameVerifier(HostnameVerifier v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        if (v == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                "no default HostnameVerifier specified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            sm.checkPermission(new SSLPermission("setHostnameVerifier"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        defaultHostnameVerifier = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * Gets the default <code>HostnameVerifier</code> that is inherited
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * by new instances of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @return the default host name verifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @see #setDefaultHostnameVerifier(HostnameVerifier)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    public static HostnameVerifier getDefaultHostnameVerifier() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        return defaultHostnameVerifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * Sets the <code>HostnameVerifier</code> for this instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * New instances of this class inherit the default static hostname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * verifier set by {@link #setDefaultHostnameVerifier(HostnameVerifier)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * setDefaultHostnameVerifier}.  Calls to this method replace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * this object's <code>HostnameVerifier</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @param v the host name verifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @throws IllegalArgumentException if the <code>HostnameVerifier</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *  parameter is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @see #getHostnameVerifier()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @see #setDefaultHostnameVerifier(HostnameVerifier)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    public void setHostnameVerifier(HostnameVerifier v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        if (v == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                "no HostnameVerifier specified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        hostnameVerifier = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * Gets the <code>HostnameVerifier</code> in place on this instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @return the host name verifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @see #setHostnameVerifier(HostnameVerifier)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @see #setDefaultHostnameVerifier(HostnameVerifier)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    public HostnameVerifier getHostnameVerifier() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        return hostnameVerifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    private static SSLSocketFactory defaultSSLSocketFactory = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * The <code>SSLSocketFactory</code> inherited when an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * of this class is created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    private SSLSocketFactory sslSocketFactory = getDefaultSSLSocketFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * Sets the default <code>SSLSocketFactory</code> inherited by new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * instances of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * The socket factories are used when creating sockets for secure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * https URL connections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @param sf the default SSL socket factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @throws IllegalArgumentException if the SSLSocketFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *          parameter is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @throws SecurityException if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *         <code>checkSetFactory</code> method does not allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *         a socket factory to be specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @see #getDefaultSSLSocketFactory()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    public static void setDefaultSSLSocketFactory(SSLSocketFactory sf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        if (sf == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                "no default SSLSocketFactory specified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            sm.checkSetFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        defaultSSLSocketFactory = sf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * Gets the default static <code>SSLSocketFactory</code> that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * inherited by new instances of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * The socket factories are used when creating sockets for secure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * https URL connections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * @return the default <code>SSLSocketFactory</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @see #setDefaultSSLSocketFactory(SSLSocketFactory)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    public static SSLSocketFactory getDefaultSSLSocketFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        if (defaultSSLSocketFactory == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            defaultSSLSocketFactory =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                (SSLSocketFactory)SSLSocketFactory.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        return defaultSSLSocketFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * Sets the <code>SSLSocketFactory</code> to be used when this instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * creates sockets for secure https URL connections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * New instances of this class inherit the default static
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * <code>SSLSocketFactory</code> set by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * {@link #setDefaultSSLSocketFactory(SSLSocketFactory)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * setDefaultSSLSocketFactory}.  Calls to this method replace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * this object's <code>SSLSocketFactory</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @param sf the SSL socket factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @throws IllegalArgumentException if the <code>SSLSocketFactory</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *          parameter is null.
10916
5a763073fa37 7096936: issue in jsse/runtime
xuelei
parents: 7043
diff changeset
   350
     * @throws SecurityException if a security manager exists and its
5a763073fa37 7096936: issue in jsse/runtime
xuelei
parents: 7043
diff changeset
   351
     *         <code>checkSetFactory</code> method does not allow
5a763073fa37 7096936: issue in jsse/runtime
xuelei
parents: 7043
diff changeset
   352
     *         a socket factory to be specified.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @see #getSSLSocketFactory()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    public void setSSLSocketFactory(SSLSocketFactory sf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        if (sf == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                "no SSLSocketFactory specified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
10916
5a763073fa37 7096936: issue in jsse/runtime
xuelei
parents: 7043
diff changeset
   361
        SecurityManager sm = System.getSecurityManager();
5a763073fa37 7096936: issue in jsse/runtime
xuelei
parents: 7043
diff changeset
   362
        if (sm != null) {
5a763073fa37 7096936: issue in jsse/runtime
xuelei
parents: 7043
diff changeset
   363
            sm.checkSetFactory();
5a763073fa37 7096936: issue in jsse/runtime
xuelei
parents: 7043
diff changeset
   364
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        sslSocketFactory = sf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * Gets the SSL socket factory to be used when creating sockets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * for secure https URL connections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @return the <code>SSLSocketFactory</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @see #setSSLSocketFactory(SSLSocketFactory)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    public SSLSocketFactory getSSLSocketFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        return sslSocketFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
}