1 /* HttpsURLConnection.java -- an HTTPS connection.
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax
.net
.ssl
;
41 import java
.io
.IOException
;
42 import java
.net
.HttpURLConnection
;
44 import java
.security
.cert
.Certificate
;
47 * A URL connection that connects via the <i>Secure Socket Layer</i>
48 * (<b>SSL</b>) for HTTPS connections.
50 * <p>This class may be used in the same way as {@link
51 * HttpURLConnection}, and it will transparently negotiate the SSL
54 * @author Casey Marshall (rsdio@metastatic.org)
56 public abstract class HttpsURLConnection
extends HttpURLConnection
60 // ------------------------------------------------------------------
63 * The default verifier.
64 * This is lazily initialized as required.
65 * @see #getDefaultHostnameVerifier
67 private static HostnameVerifier defaultVerifier
;
70 * The default factory.
71 * This is lazily initialized as required.
72 * @see #getDefaultSSLSocketFactory
74 private static SSLSocketFactory defaultFactory
;
77 * The hostname verifier used for this connection.
79 protected HostnameVerifier hostnameVerifier
;
82 * This connection's socket factory.
84 private SSLSocketFactory factory
;
87 // ------------------------------------------------------------------
90 * Creates a new HTTPS URL connection.
92 * @param url The URL of the connection being established.
93 * @throws IOException If the connection cannot be established.
95 protected HttpsURLConnection(URL url
) throws IOException
101 // ------------------------------------------------------------------
104 * Returns the default hostname verifier used in all new
106 * If the default verifier has not been set, a new default one will be
107 * provided by this method.
109 * @return The default hostname verifier.
111 public static synchronized HostnameVerifier
getDefaultHostnameVerifier()
113 if (defaultVerifier
== null)
115 defaultVerifier
= new TrivialHostnameVerifier();
117 return defaultVerifier
;
121 * Sets the default hostname verifier to be used in all new
124 * @param newDefault The new default hostname verifier.
125 * @throws IllegalArgumentException If <i>newDefault</i> is null.
126 * @throws SecurityException If there is a security manager
127 * currently installed and the caller does not have the {@link
128 * SSLPermission} "setHostnameVerifier".
130 public static void setDefaultHostnameVerifier(HostnameVerifier newDefault
)
132 if (newDefault
== null)
133 throw new IllegalArgumentException("default verifier cannot be null");
134 SecurityManager sm
= System
.getSecurityManager();
136 sm
.checkPermission(new SSLPermission("setHostnameVerifier"));
137 synchronized (HttpsURLConnection
.class)
139 defaultVerifier
= newDefault
;
144 * Returns the default SSL socket factory used in all new
146 * If the default SSL socket factory has not been set, a new default one
147 * will be provided by this method.
149 * @return The default SSL socket factory.
151 public static synchronized SSLSocketFactory
getDefaultSSLSocketFactory()
153 if (defaultFactory
== null)
157 defaultFactory
= (SSLSocketFactory
) SSLSocketFactory
.getDefault();
164 return defaultFactory
;
168 * Sets the default SSL socket factory to be used in all new
171 * @param newDefault The new socket factory.
172 * @throws IllegalArgumentException If <i>newDefault</i> is null.
173 * @throws SecurityException If there is a security manager
174 * installed and a call to {@link
175 * SecurityManager#checkSetFactory()} fails.
177 public static void setDefaultSSLSocketFactory(SSLSocketFactory newDefault
)
179 if (newDefault
== null)
180 throw new IllegalArgumentException("default factory cannot be null");
181 SecurityManager sm
= System
.getSecurityManager();
183 sm
.checkSetFactory();
184 synchronized (HttpsURLConnection
.class)
186 defaultFactory
= newDefault
;
191 // ------------------------------------------------------------------
194 * Returns the current hostname verifier for this instance.
196 * @return The hostname verifier.
198 public HostnameVerifier
getHostnameVerifier()
200 if (hostnameVerifier
== null)
202 hostnameVerifier
= getDefaultHostnameVerifier();
204 return hostnameVerifier
;
208 * Sets the hostname verifier for this instance.
210 * @param hostnameVerifier The new verifier.
211 * @throws IllegalArgumentException If <i>hostnameVerifier</i> is
214 public void setHostnameVerifier(HostnameVerifier hostnameVerifier
)
216 if (hostnameVerifier
== null)
217 throw new IllegalArgumentException("verifier cannot be null");
218 this.hostnameVerifier
= hostnameVerifier
;
222 * Returns the current SSL socket factory for this instance.
224 * @return The current SSL socket factory.
226 public SSLSocketFactory
getSSLSocketFactory()
230 factory
= getDefaultSSLSocketFactory();
236 * Sets the SSL socket factory for this instance.
238 * @param factory The new factory.
239 * @throws IllegalArgumentException If <i>factory</i> is null.
241 public void setSSLSocketFactory(SSLSocketFactory factory
)
244 throw new IllegalArgumentException("factory cannot be null");
245 this.factory
= factory
;
249 // -------------------------------------------------------------------
252 * Returns the cipher name negotiated for this connection.
254 * @return The cipher name.
255 * @throws IllegalStateException If the connection has not yet been
258 public abstract String
getCipherSuite();
261 * Returns the certificates used on the local side in this
264 * @return The local certificates.
265 * @throws IllegalStateException If the connection has not yet been
268 public abstract Certificate
[] getLocalCertificates();
271 * Returns the certificates sent by the other party.
273 * @return The peer's certificates.
274 * @throws IllegalStateException If the connection has not yet been
276 * @throws SSLPeerUnverifiedException If the peer could not be
279 public abstract Certificate
[] getServerCertificates() throws SSLPeerUnverifiedException
;