Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/net/ProxySelector.java
38829 views
/*1* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.net;2627import java.io.IOException;28import java.util.List;29import sun.security.util.SecurityConstants;3031/**32* Selects the proxy server to use, if any, when connecting to the33* network resource referenced by a URL. A proxy selector is a34* concrete sub-class of this class and is registered by invoking the35* {@link java.net.ProxySelector#setDefault setDefault} method. The36* currently registered proxy selector can be retrieved by calling37* {@link java.net.ProxySelector#getDefault getDefault} method.38*39* <p> When a proxy selector is registered, for instance, a subclass40* of URLConnection class should call the {@link #select select}41* method for each URL request so that the proxy selector can decide42* if a direct, or proxied connection should be used. The {@link43* #select select} method returns an iterator over a collection with44* the preferred connection approach.45*46* <p> If a connection cannot be established to a proxy (PROXY or47* SOCKS) servers then the caller should call the proxy selector's48* {@link #connectFailed connectFailed} method to notify the proxy49* selector that the proxy server is unavailable. </p>50*51* <P>The default proxy selector does enforce a52* <a href="doc-files/net-properties.html#Proxies">set of System Properties</a>53* related to proxy settings.</P>54*55* @author Yingxian Wang56* @author Jean-Christophe Collet57* @since 1.558*/59public abstract class ProxySelector {60/**61* The system wide proxy selector that selects the proxy server to62* use, if any, when connecting to a remote object referenced by63* an URL.64*65* @see #setDefault(ProxySelector)66*/67private static ProxySelector theProxySelector;6869static {70try {71Class<?> c = Class.forName("sun.net.spi.DefaultProxySelector");72if (c != null && ProxySelector.class.isAssignableFrom(c)) {73theProxySelector = (ProxySelector) c.newInstance();74}75} catch (Exception e) {76theProxySelector = null;77}78}7980/**81* Gets the system-wide proxy selector.82*83* @throws SecurityException84* If a security manager has been installed and it denies85* {@link NetPermission}{@code ("getProxySelector")}86* @see #setDefault(ProxySelector)87* @return the system-wide {@code ProxySelector}88* @since 1.589*/90public static ProxySelector getDefault() {91SecurityManager sm = System.getSecurityManager();92if (sm != null) {93sm.checkPermission(SecurityConstants.GET_PROXYSELECTOR_PERMISSION);94}95return theProxySelector;96}9798/**99* Sets (or unsets) the system-wide proxy selector.100*101* Note: non-standard protocol handlers may ignore this setting.102*103* @param ps The HTTP proxy selector, or104* {@code null} to unset the proxy selector.105*106* @throws SecurityException107* If a security manager has been installed and it denies108* {@link NetPermission}{@code ("setProxySelector")}109*110* @see #getDefault()111* @since 1.5112*/113public static void setDefault(ProxySelector ps) {114SecurityManager sm = System.getSecurityManager();115if (sm != null) {116sm.checkPermission(SecurityConstants.SET_PROXYSELECTOR_PERMISSION);117}118theProxySelector = ps;119}120121/**122* Selects all the applicable proxies based on the protocol to123* access the resource with and a destination address to access124* the resource at.125* The format of the URI is defined as follow:126* <UL>127* <LI>http URI for http connections</LI>128* <LI>https URI for https connections129* <LI>{@code socket://host:port}<br>130* for tcp client sockets connections</LI>131* </UL>132*133* @param uri134* The URI that a connection is required to135*136* @return a List of Proxies. Each element in the137* the List is of type138* {@link java.net.Proxy Proxy};139* when no proxy is available, the list will140* contain one element of type141* {@link java.net.Proxy Proxy}142* that represents a direct connection.143* @throws IllegalArgumentException if the argument is null144*/145public abstract List<Proxy> select(URI uri);146147/**148* Called to indicate that a connection could not be established149* to a proxy/socks server. An implementation of this method can150* temporarily remove the proxies or reorder the sequence of151* proxies returned by {@link #select(URI)}, using the address152* and the IOException caught when trying to connect.153*154* @param uri155* The URI that the proxy at sa failed to serve.156* @param sa157* The socket address of the proxy/SOCKS server158*159* @param ioe160* The I/O exception thrown when the connect failed.161* @throws IllegalArgumentException if either argument is null162*/163public abstract void connectFailed(URI uri, SocketAddress sa, IOException ioe);164}165166167