HttpsURLConnection.java revision 12745:f068a4ffddd2
1/*
2 * Copyright (c) 2000, 2015, 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 file was copied from javax.net.ssl.HttpsURLConnection
28 */
29
30package com.sun.net.ssl;
31
32import java.net.URL;
33import java.net.HttpURLConnection;
34import java.io.IOException;
35import java.security.cert.Certificate;
36import javax.net.SocketFactory;
37import javax.net.ssl.SSLSocketFactory;
38import javax.net.ssl.SSLPeerUnverifiedException;
39
40/**
41 * HTTP URL connection with support for HTTPS-specific features. See
42 * <A HREF="http://www.w3.org/pub/WWW/Protocols/"> the spec </A> for
43 * details.
44 *
45 * @deprecated As of JDK 1.4, this implementation-specific class was
46 *      replaced by {@link javax.net.ssl.HttpsURLConnection}.
47 */
48@Deprecated
49public abstract
50class HttpsURLConnection extends HttpURLConnection
51{
52    /*
53     * Initialize an HTTPS URLConnection ... could check that the URL
54     * is an "https" URL, and that the handler is also an HTTPS one,
55     * but that's established by other code in this package.
56     * @param url the URL
57     */
58    public HttpsURLConnection(URL url) throws IOException {
59        super(url);
60    }
61
62    /**
63     * Returns the cipher suite in use on this connection.
64     * @return the cipher suite
65     */
66    public abstract String getCipherSuite();
67
68    /**
69     * Returns the server's X.509 certificate chain, or null if
70     * the server did not authenticate.
71     * @return the server certificate chain
72     */
73    public abstract Certificate[] getServerCertificates()
74        throws SSLPeerUnverifiedException;
75
76    /**
77     * HostnameVerifier provides a callback mechanism so that
78     * implementers of this interface can supply a policy for
79     * handling the case where the host to connect to and
80     * the server name from the certificate mismatch.
81     *
82     * The default implementation will deny such connections.
83     */
84    private static HostnameVerifier defaultHostnameVerifier =
85        new HostnameVerifier() {
86            public boolean verify(String urlHostname, String certHostname) {
87                return false;
88            }
89        };
90
91    protected HostnameVerifier hostnameVerifier = defaultHostnameVerifier;
92
93    /**
94     * Sets the default HostnameVerifier inherited when an instance
95     * of this class is created.
96     * @param v the default host name verifier
97     */
98    public static void setDefaultHostnameVerifier(HostnameVerifier v) {
99        if (v == null) {
100            throw new IllegalArgumentException(
101                "no default HostnameVerifier specified");
102        }
103
104        SecurityManager sm = System.getSecurityManager();
105        if (sm != null) {
106            sm.checkPermission(new SSLPermission("setHostnameVerifier"));
107        }
108        defaultHostnameVerifier = v;
109    }
110
111    /**
112     * Gets the default HostnameVerifier.
113     * @return the default host name verifier
114     */
115    public static HostnameVerifier getDefaultHostnameVerifier() {
116        return defaultHostnameVerifier;
117    }
118
119    /**
120     * Sets the HostnameVerifier.
121     * @param v the host name verifier
122     */
123    public void setHostnameVerifier(HostnameVerifier v) {
124        if (v == null) {
125            throw new IllegalArgumentException(
126                "no HostnameVerifier specified");
127        }
128
129        hostnameVerifier = v;
130    }
131
132    /**
133     * Gets the HostnameVerifier.
134     * @return the host name verifier
135     */
136    public HostnameVerifier getHostnameVerifier() {
137        return hostnameVerifier;
138    }
139
140    private static SSLSocketFactory defaultSSLSocketFactory = null;
141
142    private SSLSocketFactory sslSocketFactory = getDefaultSSLSocketFactory();
143
144    /**
145     * Sets the default SSL socket factory inherited when an instance
146     * of this class is created.
147     * @param sf the default SSL socket factory
148     */
149    public static void setDefaultSSLSocketFactory(SSLSocketFactory sf) {
150        if (sf == null) {
151            throw new IllegalArgumentException(
152                "no default SSLSocketFactory specified");
153        }
154
155        SecurityManager sm = System.getSecurityManager();
156        if (sm != null) {
157            sm.checkSetFactory();
158        }
159        defaultSSLSocketFactory = sf;
160    }
161
162    /**
163     * Gets the default SSL socket factory.
164     * @return the default SSL socket factory
165     */
166    public static SSLSocketFactory getDefaultSSLSocketFactory() {
167        if (defaultSSLSocketFactory == null) {
168            defaultSSLSocketFactory =
169                (SSLSocketFactory)SSLSocketFactory.getDefault();
170        }
171        return defaultSSLSocketFactory;
172    }
173
174    /**
175     * Sets the SSL socket factory.
176     * @param sf the SSL socket factory
177     */
178    public void setSSLSocketFactory(SSLSocketFactory sf) {
179        if (sf == null) {
180            throw new IllegalArgumentException(
181                "no SSLSocketFactory specified");
182        }
183
184        SecurityManager sm = System.getSecurityManager();
185        if (sm != null) {
186            sm.checkSetFactory();
187        }
188
189        sslSocketFactory = sf;
190    }
191
192    /**
193     * Gets the SSL socket factory.
194     * @return the SSL socket factory
195     */
196    public SSLSocketFactory getSSLSocketFactory() {
197        return sslSocketFactory;
198    }
199}
200