HttpsConfigurator.java revision 11818:4682500c3098
1163533Simp/* 2163533Simp * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. 3163533Simp * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4163533Simp * 5163533Simp * This code is free software; you can redistribute it and/or modify it 6163533Simp * under the terms of the GNU General Public License version 2 only, as 7163533Simp * published by the Free Software Foundation. Oracle designates this 8163533Simp * particular file as subject to the "Classpath" exception as provided 9163533Simp * by Oracle in the LICENSE file that accompanied this code. 10163533Simp * 11163533Simp * This code is distributed in the hope that it will be useful, but WITHOUT 12163533Simp * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13163533Simp * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14163533Simp * version 2 for more details (a copy is included in the LICENSE file that 15163533Simp * accompanied this code). 16163533Simp * 17163533Simp * You should have received a copy of the GNU General Public License version 18163533Simp * 2 along with this work; if not, write to the Free Software Foundation, 19163533Simp * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20163533Simp * 21163533Simp * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22163533Simp * or visit www.oracle.com if you need additional information or have any 23163533Simp * questions. 24163533Simp */ 25163533Simp 26164137Simppackage com.sun.net.httpserver; 27164137Simp 28164137Simpimport java.net.*; 29164137Simpimport java.io.*; 30164137Simpimport java.nio.*; 31164137Simpimport java.security.*; 32164137Simpimport java.nio.channels.*; 33164137Simpimport java.util.*; 34164137Simpimport java.util.concurrent.*; 35164137Simpimport javax.net.ssl.*; 36164137Simp 37164137Simp 38164137Simp/** 39164137Simp * This class is used to configure the https parameters for each incoming 40164137Simp * https connection on a HttpsServer. Applications need to override 41164137Simp * the {@link #configure(HttpsParameters)} method in order to change 42164137Simp * the default configuration. 43164137Simp * <p> 44164137Simp * The following <a name="example">example</a> shows how this may be done: 45164137Simp * 46164137Simp * <blockquote><pre> 47164137Simp * SSLContext sslContext = SSLContext.getInstance (....); 48164137Simp * HttpsServer server = HttpsServer.create(); 49164137Simp * 50164137Simp * server.setHttpsConfigurator (new HttpsConfigurator(sslContext) { 51164137Simp * public void configure (HttpsParameters params) { 52163533Simp * 53163533Simp * // get the remote address if needed 54163533Simp * InetSocketAddress remote = params.getClientAddress(); 55163533Simp * 56163533Simp * SSLContext c = getSSLContext(); 57163533Simp * 58163533Simp * // get the default parameters 59163533Simp * SSLParameters sslparams = c.getDefaultSSLParameters(); 60163533Simp * if (remote.equals (...) ) { 61163533Simp * // modify the default set for client x 62163533Simp * } 63163533Simp * 64163533Simp * params.setSSLParameters(sslparams); 65163533Simp * } 66163533Simp * }); 67168005Simp * </pre></blockquote> 68163533Simp * @since 1.6 69 */ 70@jdk.Exported 71public class HttpsConfigurator { 72 73 private SSLContext context; 74 75 /** 76 * Creates an Https configuration, with the given SSLContext. 77 * @param context the SSLContext to use for this configurator 78 * @throws NullPointerException if no SSLContext supplied 79 */ 80 public HttpsConfigurator (SSLContext context) { 81 if (context == null) { 82 throw new NullPointerException ("null SSLContext"); 83 } 84 this.context = context; 85 } 86 87 /** 88 * Returns the SSLContext for this HttpsConfigurator. 89 * @return the SSLContext 90 */ 91 public SSLContext getSSLContext() { 92 return context; 93 } 94 95//BEGIN_TIGER_EXCLUDE 96 /** 97 * Called by the HttpsServer to configure the parameters 98 * for a https connection currently being established. 99 * The implementation of configure() must call 100 * {@link HttpsParameters#setSSLParameters(SSLParameters)} 101 * in order to set the SSL parameters for the connection. 102 * <p> 103 * The default implementation of this method uses the 104 * SSLParameters returned from <p> 105 * {@code getSSLContext().getDefaultSSLParameters()} 106 * <p> 107 * configure() may be overridden in order to modify this behavior. 108 * See, the example <a href="#example">above</a>. 109 * @param params the HttpsParameters to be configured. 110 * 111 * @since 1.6 112 */ 113 public void configure (HttpsParameters params) { 114 params.setSSLParameters (getSSLContext().getDefaultSSLParameters()); 115 } 116//END_TIGER_EXCLUDE 117} 118