src/jdk.httpserver/share/classes/com/sun/net/httpserver/HttpsConfigurator.java
author herrick
Wed, 16 Oct 2019 10:32:08 -0400
branchJDK-8200758-branch
changeset 58648 3bf53ffa9ae7
parent 47216 71c04702a3d5
permissions -rw-r--r--
8232279 : Improve test helpers #2 Submitted-by: asemenyuk Reviewed-by: aherrick, almatvee
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
45132
db2f2d72cd4f 8179697: Fix Html5 errors in java.naming, java.logging, jdk.httpserver, jdk.net, jdk.sctp
ksrini
parents: 34894
diff changeset
     2
 * Copyright (c) 2005, 2017, 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 com.sun.net.httpserver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * This class is used to configure the https parameters for each incoming
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * https connection on a HttpsServer. Applications need to override
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * the {@link #configure(HttpsParameters)} method in order to change
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * the default configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <p>
45132
db2f2d72cd4f 8179697: Fix Html5 errors in java.naming, java.logging, jdk.httpserver, jdk.net, jdk.sctp
ksrini
parents: 34894
diff changeset
    44
 * The following <a id="example">example</a> shows how this may be done:
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
    45
 *
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
    46
 * <blockquote><pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * SSLContext sslContext = SSLContext.getInstance (....);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * HttpsServer server = HttpsServer.create();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * server.setHttpsConfigurator (new HttpsConfigurator(sslContext) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *     public void configure (HttpsParameters params) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *         // get the remote address if needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *         InetSocketAddress remote = params.getClientAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *         SSLContext c = getSSLContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *         // get the default parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *         SSLParameters sslparams = c.getDefaultSSLParameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *         if (remote.equals (...) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *             // modify the default set for client x
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *         params.setSSLParameters(sslparams);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * });
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
    67
 * </pre></blockquote>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
public class HttpsConfigurator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private SSLContext context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Creates an Https configuration, with the given SSLContext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @param context the SSLContext to use for this configurator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @throws NullPointerException if no SSLContext supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public HttpsConfigurator (SSLContext context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if (context == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            throw new NullPointerException ("null SSLContext");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        this.context = context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * Returns the SSLContext for this HttpsConfigurator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @return the SSLContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public SSLContext getSSLContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        return context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    94
//BEGIN_TIGER_EXCLUDE
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    * Called by the HttpsServer to configure the parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    * for a https connection currently being established.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    * The implementation of configure() must call
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    * {@link HttpsParameters#setSSLParameters(SSLParameters)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    * in order to set the SSL parameters for the connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    * The default implementation of this method uses the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    * SSLParameters returned from <p>
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
   104
    * {@code getSSLContext().getDefaultSSLParameters()}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    * configure() may be overridden in order to modify this behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    * See, the example <a href="#example">above</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    * @param params the HttpsParameters to be configured.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public void configure (HttpsParameters params) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        params.setSSLParameters (getSSLContext().getDefaultSSLParameters());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   115
//END_TIGER_EXCLUDE
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
}