src/java.base/share/classes/java/net/Authenticator.java
author chegar
Fri, 18 Oct 2019 21:25:01 +0100
branchdatagramsocketimpl-branch
changeset 58697 e3ff12d14d43
parent 47216 71c04702a3d5
permissions -rw-r--r--
datagramsocketimpl-branch: minor refactoring
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
42152
5c183e1ba44e 8169068: Add a new method: java.net.Authenticator.getDefault()
dfuchs
parents: 36131
diff changeset
     2
 * Copyright (c) 1997, 2016, 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 java.net;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
    28
import sun.net.www.protocol.http.AuthenticatorKeys;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
    29
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * The class Authenticator represents an object that knows how to obtain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * authentication for a network connection.  Usually, it will do this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * by prompting the user for information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Applications use this class by overriding {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * #getPasswordAuthentication()} in a sub-class. This method will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * typically use the various getXXX() accessor methods to get information
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * about the entity requesting authentication. It must then acquire a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * username and password either by interacting with the user or through
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * some other non-interactive means. The credentials are then returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * as a {@link PasswordAuthentication} return value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * An instance of this concrete sub-class is then registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * with the system by calling {@link #setDefault(Authenticator)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * When authentication is required, the system will invoke one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * requestPasswordAuthentication() methods which in turn will call the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * getPasswordAuthentication() method of the registered object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * All methods that request authentication have a default implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * that fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @see java.net.Authenticator#setDefault(java.net.Authenticator)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @see java.net.Authenticator#getPasswordAuthentication()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @author  Bill Foote
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
// There are no abstract methods, but to be useful the user must
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
// subclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
public abstract
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
class Authenticator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // The system-wide authenticator object.  See setDefault().
27728
cbde148d040c 8065096: java.net.Authenticator.theAuthenticator should be properly synchronized
shade
parents: 25859
diff changeset
    65
    private static volatile Authenticator theAuthenticator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private String requestingHost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private InetAddress requestingSite;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private int requestingPort;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private String requestingProtocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private String requestingPrompt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private String requestingScheme;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private URL requestingURL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private RequestorType requestingAuthType;
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
    75
    private final String key = AuthenticatorKeys.computeKey(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * The type of the entity requesting authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public enum RequestorType {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
         * Entity requesting authentication is a HTTP proxy server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        PROXY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
         * Entity requesting authentication is a HTTP origin server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        SERVER
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        requestingHost = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        requestingSite = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        requestingPort = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        requestingProtocol = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        requestingPrompt = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        requestingScheme = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        requestingURL = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        requestingAuthType = RequestorType.SERVER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Sets the authenticator that will be used by the networking code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * when a proxy or an HTTP server asks for authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
   109
     * First, if there is a security manager, its {@code checkPermission}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * method is called with a
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
   111
     * {@code NetPermission("setDefaultAuthenticator")} permission.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * This may result in a java.lang.SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
   114
     * @param   a       The authenticator to be set. If a is {@code null} then
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *                  any previously set authenticator is removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @throws SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *        if a security manager exists and its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
   119
     *        {@code checkPermission} method doesn't allow
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *        setting the default authenticator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @see SecurityManager#checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @see java.net.NetPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 27728
diff changeset
   125
    public static synchronized void setDefault(Authenticator a) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            NetPermission setDefaultPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                = new NetPermission("setDefaultAuthenticator");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            sm.checkPermission(setDefaultPermission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        theAuthenticator = a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
42152
5c183e1ba44e 8169068: Add a new method: java.net.Authenticator.getDefault()
dfuchs
parents: 36131
diff changeset
   137
     * Gets the default authenticator.
5c183e1ba44e 8169068: Add a new method: java.net.Authenticator.getDefault()
dfuchs
parents: 36131
diff changeset
   138
     * First, if there is a security manager, its {@code checkPermission}
5c183e1ba44e 8169068: Add a new method: java.net.Authenticator.getDefault()
dfuchs
parents: 36131
diff changeset
   139
     * method is called with a
5c183e1ba44e 8169068: Add a new method: java.net.Authenticator.getDefault()
dfuchs
parents: 36131
diff changeset
   140
     * {@code NetPermission("requestPasswordAuthentication")} permission.
5c183e1ba44e 8169068: Add a new method: java.net.Authenticator.getDefault()
dfuchs
parents: 36131
diff changeset
   141
     * This may result in a java.lang.SecurityException.
5c183e1ba44e 8169068: Add a new method: java.net.Authenticator.getDefault()
dfuchs
parents: 36131
diff changeset
   142
     * Then the default authenticator, if set, is returned.
5c183e1ba44e 8169068: Add a new method: java.net.Authenticator.getDefault()
dfuchs
parents: 36131
diff changeset
   143
     * Otherwise, {@code null} is returned.
5c183e1ba44e 8169068: Add a new method: java.net.Authenticator.getDefault()
dfuchs
parents: 36131
diff changeset
   144
     *
5c183e1ba44e 8169068: Add a new method: java.net.Authenticator.getDefault()
dfuchs
parents: 36131
diff changeset
   145
     * @return The default authenticator, if set, {@code null} otherwise.
5c183e1ba44e 8169068: Add a new method: java.net.Authenticator.getDefault()
dfuchs
parents: 36131
diff changeset
   146
     *
5c183e1ba44e 8169068: Add a new method: java.net.Authenticator.getDefault()
dfuchs
parents: 36131
diff changeset
   147
     * @throws SecurityException
5c183e1ba44e 8169068: Add a new method: java.net.Authenticator.getDefault()
dfuchs
parents: 36131
diff changeset
   148
     *        if a security manager exists and its
5c183e1ba44e 8169068: Add a new method: java.net.Authenticator.getDefault()
dfuchs
parents: 36131
diff changeset
   149
     *        {@code checkPermission} method doesn't allow
5c183e1ba44e 8169068: Add a new method: java.net.Authenticator.getDefault()
dfuchs
parents: 36131
diff changeset
   150
     *        requesting password authentication.
5c183e1ba44e 8169068: Add a new method: java.net.Authenticator.getDefault()
dfuchs
parents: 36131
diff changeset
   151
     * @since 9
5c183e1ba44e 8169068: Add a new method: java.net.Authenticator.getDefault()
dfuchs
parents: 36131
diff changeset
   152
     * @see SecurityManager#checkPermission
5c183e1ba44e 8169068: Add a new method: java.net.Authenticator.getDefault()
dfuchs
parents: 36131
diff changeset
   153
     * @see java.net.NetPermission
5c183e1ba44e 8169068: Add a new method: java.net.Authenticator.getDefault()
dfuchs
parents: 36131
diff changeset
   154
     */
5c183e1ba44e 8169068: Add a new method: java.net.Authenticator.getDefault()
dfuchs
parents: 36131
diff changeset
   155
    public static Authenticator getDefault() {
5c183e1ba44e 8169068: Add a new method: java.net.Authenticator.getDefault()
dfuchs
parents: 36131
diff changeset
   156
        SecurityManager sm = System.getSecurityManager();
5c183e1ba44e 8169068: Add a new method: java.net.Authenticator.getDefault()
dfuchs
parents: 36131
diff changeset
   157
        if (sm != null) {
5c183e1ba44e 8169068: Add a new method: java.net.Authenticator.getDefault()
dfuchs
parents: 36131
diff changeset
   158
            NetPermission requestPermission
5c183e1ba44e 8169068: Add a new method: java.net.Authenticator.getDefault()
dfuchs
parents: 36131
diff changeset
   159
                = new NetPermission("requestPasswordAuthentication");
5c183e1ba44e 8169068: Add a new method: java.net.Authenticator.getDefault()
dfuchs
parents: 36131
diff changeset
   160
            sm.checkPermission(requestPermission);
5c183e1ba44e 8169068: Add a new method: java.net.Authenticator.getDefault()
dfuchs
parents: 36131
diff changeset
   161
        }
5c183e1ba44e 8169068: Add a new method: java.net.Authenticator.getDefault()
dfuchs
parents: 36131
diff changeset
   162
        return theAuthenticator;
5c183e1ba44e 8169068: Add a new method: java.net.Authenticator.getDefault()
dfuchs
parents: 36131
diff changeset
   163
    }
5c183e1ba44e 8169068: Add a new method: java.net.Authenticator.getDefault()
dfuchs
parents: 36131
diff changeset
   164
5c183e1ba44e 8169068: Add a new method: java.net.Authenticator.getDefault()
dfuchs
parents: 36131
diff changeset
   165
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * Ask the authenticator that has been registered with the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * for a password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
   169
     * First, if there is a security manager, its {@code checkPermission}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * method is called with a
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
   171
     * {@code NetPermission("requestPasswordAuthentication")} permission.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * This may result in a java.lang.SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @param addr The InetAddress of the site requesting authorization,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *             or null if not known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @param port the port for the requested connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @param protocol The protocol that's requesting the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *          ({@link java.net.Authenticator#getRequestingProtocol()})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @param prompt A prompt string for the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @param scheme The authentication scheme
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @return The username/password, or null if one can't be gotten.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @throws SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *        if a security manager exists and its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
   186
     *        {@code checkPermission} method doesn't allow
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *        the password authentication request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @see SecurityManager#checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @see java.net.NetPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public static PasswordAuthentication requestPasswordAuthentication(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                                            InetAddress addr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                                            int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                                            String protocol,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                                            String prompt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                                            String scheme) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            NetPermission requestPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                = new NetPermission("requestPasswordAuthentication");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            sm.checkPermission(requestPermission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        Authenticator a = theAuthenticator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        if (a == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            synchronized(a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                a.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                a.requestingSite = addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                a.requestingPort = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                a.requestingProtocol = protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                a.requestingPrompt = prompt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                a.requestingScheme = scheme;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                return a.getPasswordAuthentication();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * Ask the authenticator that has been registered with the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * for a password. This is the preferred method for requesting a password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * because the hostname can be provided in cases where the InetAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * is not available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
   228
     * First, if there is a security manager, its {@code checkPermission}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * method is called with a
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
   230
     * {@code NetPermission("requestPasswordAuthentication")} permission.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * This may result in a java.lang.SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @param host The hostname of the site requesting authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @param addr The InetAddress of the site requesting authentication,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *             or null if not known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @param port the port for the requested connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @param protocol The protocol that's requesting the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *          ({@link java.net.Authenticator#getRequestingProtocol()})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @param prompt A prompt string for the user which identifies the authentication realm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @param scheme The authentication scheme
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @return The username/password, or null if one can't be gotten.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @throws SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *        if a security manager exists and its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
   246
     *        {@code checkPermission} method doesn't allow
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *        the password authentication request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @see SecurityManager#checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @see java.net.NetPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    public static PasswordAuthentication requestPasswordAuthentication(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                                            String host,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                                            InetAddress addr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                                            int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                                            String protocol,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                                            String prompt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                                            String scheme) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            NetPermission requestPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                = new NetPermission("requestPasswordAuthentication");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            sm.checkPermission(requestPermission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        Authenticator a = theAuthenticator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        if (a == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            synchronized(a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                a.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                a.requestingHost = host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                a.requestingSite = addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                a.requestingPort = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                a.requestingProtocol = protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                a.requestingPrompt = prompt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                a.requestingScheme = scheme;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                return a.getPasswordAuthentication();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * Ask the authenticator that has been registered with the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * for a password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
   289
     * First, if there is a security manager, its {@code checkPermission}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * method is called with a
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
   291
     * {@code NetPermission("requestPasswordAuthentication")} permission.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * This may result in a java.lang.SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @param host The hostname of the site requesting authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @param addr The InetAddress of the site requesting authorization,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *             or null if not known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @param port the port for the requested connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @param protocol The protocol that's requesting the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     *          ({@link java.net.Authenticator#getRequestingProtocol()})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * @param prompt A prompt string for the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @param scheme The authentication scheme
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @param url The requesting URL that caused the authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @param reqType The type (server or proxy) of the entity requesting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *              authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @return The username/password, or null if one can't be gotten.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @throws SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *        if a security manager exists and its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
   310
     *        {@code checkPermission} method doesn't allow
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *        the password authentication request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @see SecurityManager#checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @see java.net.NetPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    public static PasswordAuthentication requestPasswordAuthentication(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                                    String host,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                                    InetAddress addr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                                    int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                                    String protocol,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                                    String prompt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                                    String scheme,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                                    URL url,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                                    RequestorType reqType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            NetPermission requestPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                = new NetPermission("requestPasswordAuthentication");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            sm.checkPermission(requestPermission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        Authenticator a = theAuthenticator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        if (a == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            synchronized(a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                a.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                a.requestingHost = host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                a.requestingSite = addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                a.requestingPort = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                a.requestingProtocol = protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                a.requestingPrompt = prompt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                a.requestingScheme = scheme;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                a.requestingURL = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                a.requestingAuthType = reqType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                return a.getPasswordAuthentication();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    /**
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   355
     * Ask the given {@code authenticator} for a password. If the given
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   356
     * {@code authenticator} is null, the authenticator, if any, that has been
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   357
     * registered with the system using {@link #setDefault(java.net.Authenticator)
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   358
     * setDefault} is used.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   359
     * <p>
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   360
     * First, if there is a security manager, its {@code checkPermission}
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   361
     * method is called with a
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   362
     * {@code NetPermission("requestPasswordAuthentication")} permission.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   363
     * This may result in a java.lang.SecurityException.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   364
     *
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   365
     * @param authenticator the authenticator, or {@code null}.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   366
     * @param host The hostname of the site requesting authentication.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   367
     * @param addr The InetAddress of the site requesting authorization,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   368
     *             or null if not known.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   369
     * @param port the port for the requested connection
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   370
     * @param protocol The protocol that's requesting the connection
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   371
     *          ({@link java.net.Authenticator#getRequestingProtocol()})
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   372
     * @param prompt A prompt string for the user
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   373
     * @param scheme The authentication scheme
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   374
     * @param url The requesting URL that caused the authentication
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   375
     * @param reqType The type (server or proxy) of the entity requesting
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   376
     *              authentication.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   377
     *
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   378
     * @return The username/password, or {@code null} if one can't be gotten.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   379
     *
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   380
     * @throws SecurityException
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   381
     *        if a security manager exists and its
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   382
     *        {@code checkPermission} method doesn't allow
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   383
     *        the password authentication request.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   384
     *
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   385
     * @see SecurityManager#checkPermission
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   386
     * @see java.net.NetPermission
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   387
     *
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   388
     * @since 9
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   389
     */
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   390
    public static PasswordAuthentication requestPasswordAuthentication(
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   391
                                    Authenticator authenticator,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   392
                                    String host,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   393
                                    InetAddress addr,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   394
                                    int port,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   395
                                    String protocol,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   396
                                    String prompt,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   397
                                    String scheme,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   398
                                    URL url,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   399
                                    RequestorType reqType) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   400
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   401
        SecurityManager sm = System.getSecurityManager();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   402
        if (sm != null) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   403
            NetPermission requestPermission
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   404
                = new NetPermission("requestPasswordAuthentication");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   405
            sm.checkPermission(requestPermission);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   406
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   407
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   408
        Authenticator a = authenticator == null ? theAuthenticator : authenticator;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   409
        if (a == null) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   410
            return null;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   411
        } else {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   412
            return a.requestPasswordAuthenticationInstance(host,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   413
                                                           addr,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   414
                                                           port,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   415
                                                           protocol,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   416
                                                           prompt,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   417
                                                           scheme,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   418
                                                           url,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   419
                                                           reqType);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   420
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   421
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   422
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   423
    /**
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 32649
diff changeset
   424
     * Ask this authenticator for a password.
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 32649
diff changeset
   425
     *
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 32649
diff changeset
   426
     * @param host The hostname of the site requesting authentication.
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 32649
diff changeset
   427
     * @param addr The InetAddress of the site requesting authorization,
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 32649
diff changeset
   428
     *             or null if not known.
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 32649
diff changeset
   429
     * @param port the port for the requested connection
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 32649
diff changeset
   430
     * @param protocol The protocol that's requesting the connection
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 32649
diff changeset
   431
     *          ({@link java.net.Authenticator#getRequestingProtocol()})
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 32649
diff changeset
   432
     * @param prompt A prompt string for the user
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 32649
diff changeset
   433
     * @param scheme The authentication scheme
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 32649
diff changeset
   434
     * @param url The requesting URL that caused the authentication
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 32649
diff changeset
   435
     * @param reqType The type (server or proxy) of the entity requesting
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 32649
diff changeset
   436
     *              authentication.
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 32649
diff changeset
   437
     *
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 32649
diff changeset
   438
     * @return The username/password, or null if one can't be gotten
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 32649
diff changeset
   439
     *
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 32649
diff changeset
   440
     * @since 9
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 32649
diff changeset
   441
     */
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 32649
diff changeset
   442
    public PasswordAuthentication
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 32649
diff changeset
   443
    requestPasswordAuthenticationInstance(String host,
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 32649
diff changeset
   444
                                          InetAddress addr,
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 32649
diff changeset
   445
                                          int port,
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 32649
diff changeset
   446
                                          String protocol,
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 32649
diff changeset
   447
                                          String prompt,
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 32649
diff changeset
   448
                                          String scheme,
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 32649
diff changeset
   449
                                          URL url,
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 32649
diff changeset
   450
                                          RequestorType reqType) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 32649
diff changeset
   451
        synchronized (this) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 32649
diff changeset
   452
            this.reset();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 32649
diff changeset
   453
            this.requestingHost = host;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 32649
diff changeset
   454
            this.requestingSite = addr;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 32649
diff changeset
   455
            this.requestingPort = port;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 32649
diff changeset
   456
            this.requestingProtocol = protocol;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 32649
diff changeset
   457
            this.requestingPrompt = prompt;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 32649
diff changeset
   458
            this.requestingScheme = scheme;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 32649
diff changeset
   459
            this.requestingURL = url;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 32649
diff changeset
   460
            this.requestingAuthType = reqType;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 32649
diff changeset
   461
            return this.getPasswordAuthentication();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 32649
diff changeset
   462
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 32649
diff changeset
   463
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 32649
diff changeset
   464
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 32649
diff changeset
   465
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
   466
     * Gets the {@code hostname} of the
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
   467
     * site or proxy requesting authentication, or {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * if not available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * @return the hostname of the connection requiring authentication, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     *          if it's not available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    protected final String getRequestingHost() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        return requestingHost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
   479
     * Gets the {@code InetAddress} of the
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
   480
     * site requesting authorization, or {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * if not available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * @return the InetAddress of the site requesting authorization, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     *          if it's not available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    protected final InetAddress getRequestingSite() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        return requestingSite;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * Gets the port number for the requested connection.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
   492
     * @return an {@code int} indicating the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * port for the requested connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    protected final int getRequestingPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        return requestingPort;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * Give the protocol that's requesting the connection.  Often this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * will be based on a URL, but in a future JDK it could be, for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * example, "SOCKS" for a password-protected SOCKS5 firewall.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     *
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 19069
diff changeset
   504
     * @return the protocol, optionally followed by "/version", where
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     *          version is a version number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * @see java.net.URL#getProtocol()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    protected final String getRequestingProtocol() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        return requestingProtocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * Gets the prompt string given by the requestor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * @return the prompt string given by the requestor (realm for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     *          http requests)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    protected final String getRequestingPrompt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        return requestingPrompt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * Gets the scheme of the requestor (the HTTP scheme
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * for an HTTP firewall, for example).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * @return the scheme of the requestor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    protected final String getRequestingScheme() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        return requestingScheme;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * Called when password authorization is needed.  Subclasses should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * override the default implementation, which returns null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * @return The PasswordAuthentication collected from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     *          user, or null if none is provided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    protected PasswordAuthentication getPasswordAuthentication() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * Returns the URL that resulted in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * request for authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * @return the requesting URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    protected URL getRequestingURL () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        return requestingURL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * Returns whether the requestor is a Proxy or a Server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * @return the authentication type of the requestor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    protected RequestorType getRequestorType () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        return requestingAuthType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    }
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   568
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   569
    static String getKey(Authenticator a) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   570
        return a.key;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   571
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   572
    static {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   573
        AuthenticatorKeys.setAuthenticatorKeyAccess(Authenticator::getKey);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 42152
diff changeset
   574
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
}