jdk/src/share/classes/java/net/Authenticator.java
author xdono
Thu, 02 Oct 2008 19:58:32 -0700
changeset 1247 b4c26443dee5
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6754988: Update copyright year Summary: Update for files that have been modified starting July 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2004 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * The class Authenticator represents an object that knows how to obtain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * authentication for a network connection.  Usually, it will do this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * by prompting the user for information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Applications use this class by overriding {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * #getPasswordAuthentication()} in a sub-class. This method will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * typically use the various getXXX() accessor methods to get information
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * about the entity requesting authentication. It must then acquire a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * username and password either by interacting with the user or through
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * some other non-interactive means. The credentials are then returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * as a {@link PasswordAuthentication} return value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * An instance of this concrete sub-class is then registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * with the system by calling {@link #setDefault(Authenticator)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * When authentication is required, the system will invoke one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * requestPasswordAuthentication() methods which in turn will call the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * getPasswordAuthentication() method of the registered object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * All methods that request authentication have a default implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * that fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @see java.net.Authenticator#setDefault(java.net.Authenticator)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @see java.net.Authenticator#getPasswordAuthentication()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @author  Bill Foote
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
// There are no abstract methods, but to be useful the user must
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
// subclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
public abstract
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
class Authenticator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // The system-wide authenticator object.  See setDefault().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private static Authenticator theAuthenticator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private String requestingHost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private InetAddress requestingSite;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private int requestingPort;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private String requestingProtocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private String requestingPrompt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private String requestingScheme;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private URL requestingURL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private RequestorType requestingAuthType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * The type of the entity requesting authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public enum RequestorType {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
         * Entity requesting authentication is a HTTP proxy server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        PROXY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
         * Entity requesting authentication is a HTTP origin server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        SERVER
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        requestingHost = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        requestingSite = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        requestingPort = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        requestingProtocol = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        requestingPrompt = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        requestingScheme = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        requestingURL = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        requestingAuthType = RequestorType.SERVER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Sets the authenticator that will be used by the networking code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * when a proxy or an HTTP server asks for authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * First, if there is a security manager, its <code>checkPermission</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * method is called with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * <code>NetPermission("setDefaultAuthenticator")</code> permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * This may result in a java.lang.SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @param   a       The authenticator to be set. If a is <code>null</code> then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *                  any previously set authenticator is removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @throws SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *        if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *        <code>checkPermission</code> method doesn't allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *        setting the default authenticator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @see SecurityManager#checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @see java.net.NetPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public synchronized static void setDefault(Authenticator a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            NetPermission setDefaultPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                = new NetPermission("setDefaultAuthenticator");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            sm.checkPermission(setDefaultPermission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        theAuthenticator = a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Ask the authenticator that has been registered with the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * for a password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * First, if there is a security manager, its <code>checkPermission</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * method is called with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * <code>NetPermission("requestPasswordAuthentication")</code> permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * This may result in a java.lang.SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @param addr The InetAddress of the site requesting authorization,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *             or null if not known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @param port the port for the requested connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @param protocol The protocol that's requesting the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *          ({@link java.net.Authenticator#getRequestingProtocol()})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @param prompt A prompt string for the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @param scheme The authentication scheme
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @return The username/password, or null if one can't be gotten.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @throws SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *        if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *        <code>checkPermission</code> method doesn't allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *        the password authentication request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @see SecurityManager#checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @see java.net.NetPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public static PasswordAuthentication requestPasswordAuthentication(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                                            InetAddress addr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                                            int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                                            String protocol,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                                            String prompt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                                            String scheme) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            NetPermission requestPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                = new NetPermission("requestPasswordAuthentication");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            sm.checkPermission(requestPermission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        Authenticator a = theAuthenticator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (a == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            synchronized(a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                a.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                a.requestingSite = addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                a.requestingPort = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                a.requestingProtocol = protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                a.requestingPrompt = prompt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                a.requestingScheme = scheme;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                return a.getPasswordAuthentication();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * Ask the authenticator that has been registered with the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * for a password. This is the preferred method for requesting a password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * because the hostname can be provided in cases where the InetAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * is not available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * First, if there is a security manager, its <code>checkPermission</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * method is called with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * <code>NetPermission("requestPasswordAuthentication")</code> permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * This may result in a java.lang.SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @param host The hostname of the site requesting authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @param addr The InetAddress of the site requesting authentication,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *             or null if not known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @param port the port for the requested connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @param protocol The protocol that's requesting the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *          ({@link java.net.Authenticator#getRequestingProtocol()})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @param prompt A prompt string for the user which identifies the authentication realm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @param scheme The authentication scheme
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @return The username/password, or null if one can't be gotten.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @throws SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *        if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *        <code>checkPermission</code> method doesn't allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *        the password authentication request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @see SecurityManager#checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @see java.net.NetPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    public static PasswordAuthentication requestPasswordAuthentication(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                                            String host,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                                            InetAddress addr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                                            int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                                            String protocol,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                                            String prompt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                                            String scheme) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            NetPermission requestPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                = new NetPermission("requestPasswordAuthentication");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            sm.checkPermission(requestPermission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        Authenticator a = theAuthenticator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (a == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            synchronized(a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                a.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                a.requestingHost = host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                a.requestingSite = addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                a.requestingPort = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                a.requestingProtocol = protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                a.requestingPrompt = prompt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                a.requestingScheme = scheme;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                return a.getPasswordAuthentication();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * Ask the authenticator that has been registered with the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * for a password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * First, if there is a security manager, its <code>checkPermission</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * method is called with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * <code>NetPermission("requestPasswordAuthentication")</code> permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * This may result in a java.lang.SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @param host The hostname of the site requesting authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @param addr The InetAddress of the site requesting authorization,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *             or null if not known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @param port the port for the requested connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @param protocol The protocol that's requesting the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *          ({@link java.net.Authenticator#getRequestingProtocol()})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @param prompt A prompt string for the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @param scheme The authentication scheme
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @param url The requesting URL that caused the authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @param reqType The type (server or proxy) of the entity requesting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *              authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @return The username/password, or null if one can't be gotten.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @throws SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *        if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *        <code>checkPermission</code> method doesn't allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *        the password authentication request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @see SecurityManager#checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @see java.net.NetPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    public static PasswordAuthentication requestPasswordAuthentication(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                                    String host,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                                    InetAddress addr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                                    int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                                    String protocol,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                                    String prompt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                                    String scheme,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                                    URL url,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                                    RequestorType reqType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            NetPermission requestPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                = new NetPermission("requestPasswordAuthentication");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            sm.checkPermission(requestPermission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        Authenticator a = theAuthenticator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        if (a == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            synchronized(a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                a.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                a.requestingHost = host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                a.requestingSite = addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                a.requestingPort = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                a.requestingProtocol = protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                a.requestingPrompt = prompt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                a.requestingScheme = scheme;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                a.requestingURL = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                a.requestingAuthType = reqType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                return a.getPasswordAuthentication();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * Gets the <code>hostname</code> of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * site or proxy requesting authentication, or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * if not available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @return the hostname of the connection requiring authentication, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *          if it's not available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    protected final String getRequestingHost() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        return requestingHost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * Gets the <code>InetAddress</code> of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * site requesting authorization, or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * if not available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * @return the InetAddress of the site requesting authorization, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     *          if it's not available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    protected final InetAddress getRequestingSite() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        return requestingSite;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * Gets the port number for the requested connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @return an <code>int</code> indicating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * port for the requested connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    protected final int getRequestingPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        return requestingPort;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * Give the protocol that's requesting the connection.  Often this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * will be based on a URL, but in a future JDK it could be, for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * example, "SOCKS" for a password-protected SOCKS5 firewall.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * @return the protcol, optionally followed by "/version", where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *          version is a version number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @see java.net.URL#getProtocol()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    protected final String getRequestingProtocol() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        return requestingProtocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * Gets the prompt string given by the requestor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @return the prompt string given by the requestor (realm for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *          http requests)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    protected final String getRequestingPrompt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        return requestingPrompt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * Gets the scheme of the requestor (the HTTP scheme
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * for an HTTP firewall, for example).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @return the scheme of the requestor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    protected final String getRequestingScheme() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        return requestingScheme;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * Called when password authorization is needed.  Subclasses should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * override the default implementation, which returns null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * @return The PasswordAuthentication collected from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     *          user, or null if none is provided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    protected PasswordAuthentication getPasswordAuthentication() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * Returns the URL that resulted in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * request for authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * @return the requesting URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    protected URL getRequestingURL () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        return requestingURL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * Returns whether the requestor is a Proxy or a Server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * @return the authentication type of the requestor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    protected RequestorType getRequestorType () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        return requestingAuthType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
}