jdk/src/share/classes/java/net/ProxySelector.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 2003-2006 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
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.security.util.SecurityConstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Selects the proxy server to use, if any, when connecting to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * network resource referenced by a URL. A proxy selector is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * concrete sub-class of this class and is registered by invoking the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * {@link java.net.ProxySelector#setDefault setDefault} method. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * currently registered proxy selector can be retrieved by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * {@link java.net.ProxySelector#getDefault getDefault} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p> When a proxy selector is registered, for instance, a subclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * of URLConnection class should call the {@link #select select}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * method for each URL request so that the proxy selector can decide
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * if a direct, or proxied connection should be used. The {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * #select select} method returns an iterator over a collection with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * the preferred connection approach.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p> If a connection cannot be established to a proxy (PROXY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * SOCKS) servers then the caller should call the proxy selector's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * {@link #connectFailed connectFailed} method to notify the proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * selector that the proxy server is unavailable. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <P>The default proxy selector does enforce a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <a href="doc-files/net-properties.html#Proxies">set of System Properties</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * related to proxy settings.</P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @author Yingxian Wang
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @author Jean-Christophe Collet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
public abstract class ProxySelector {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * The system wide proxy selector that selects the proxy server to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * use, if any, when connecting to a remote object referenced by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * an URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * @see #setDefault(ProxySelector)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private static ProxySelector theProxySelector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            Class c = Class.forName("sun.net.spi.DefaultProxySelector");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            if (c != null && ProxySelector.class.isAssignableFrom(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                theProxySelector = (ProxySelector) c.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            theProxySelector = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Gets the system-wide proxy selector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *          If a security manager has been installed and it denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * {@link NetPermission}<tt>("getProxySelector")</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @see #setDefault(ProxySelector)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @return the system-wide <code>ProxySelector</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public static ProxySelector getDefault() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            sm.checkPermission(SecurityConstants.GET_PROXYSELECTOR_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        return theProxySelector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * Sets (or unsets) the system-wide proxy selector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Note: non-standard protocol handlers may ignore this setting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @param ps The HTTP proxy selector, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *          <code>null</code> to unset the proxy selector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *          If a security manager has been installed and it denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * {@link NetPermission}<tt>("setProxySelector")</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @see #getDefault()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public static void setDefault(ProxySelector ps) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            sm.checkPermission(SecurityConstants.SET_PROXYSELECTOR_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        theProxySelector = ps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Selects all the applicable proxies based on the protocol to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * access the resource with and a destination address to access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * the resource at.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * The format of the URI is defined as follow:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * <UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * <LI>http URI for http connections</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * <LI>https URI for https connections
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * <LI>ftp URI for ftp connections</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * <LI><code>socket://host:port</code><br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *     for tcp client sockets connections</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * </UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param   uri
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *          The URI that a connection is required to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @return  a List of Proxies. Each element in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *          the List is of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *          {@link java.net.Proxy Proxy};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *          when no proxy is available, the list will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *          contain one element of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *          {@link java.net.Proxy Proxy}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *          that represents a direct connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @throws IllegalArgumentException if the argument is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public abstract List<Proxy> select(URI uri);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Called to indicate that a connection could not be established
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * to a proxy/socks server. An implementation of this method can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * temporarily remove the proxies or reorder the sequence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * proxies returned by {@link #select(URI)}, using the address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * and the IOException caught when trying to connect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @param   uri
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *          The URI that the proxy at sa failed to serve.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @param   sa
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *          The socket address of the proxy/SOCKS server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @param   ioe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *          The I/O exception thrown when the connect failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @throws IllegalArgumentException if either argument is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public abstract void connectFailed(URI uri, SocketAddress sa, IOException ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
}