Path: blob/master/src/java.base/share/classes/java/net/ProxySelector.java
41152 views
/*1* Copyright (c) 2003, 2021, 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 volatile ProxySelector theProxySelector;6869static {70try {71Class<?> c = Class.forName("sun.net.spi.DefaultProxySelector");72if (c != null && ProxySelector.class.isAssignableFrom(c)) {73@SuppressWarnings("deprecation")74ProxySelector tmp = (ProxySelector) c.newInstance();75theProxySelector = tmp;76}77} catch (Exception e) {78theProxySelector = null;79}80}8182/**83* Constructor for subclasses to call.84*/85public ProxySelector() {}8687/**88* Gets the system-wide proxy selector.89*90* @throws SecurityException91* If a security manager has been installed and it denies92* {@link NetPermission}{@code ("getProxySelector")}93* @see #setDefault(ProxySelector)94* @return the system-wide {@code ProxySelector}95* @since 1.596*/97public static ProxySelector getDefault() {98@SuppressWarnings("removal")99SecurityManager sm = System.getSecurityManager();100if (sm != null) {101sm.checkPermission(SecurityConstants.GET_PROXYSELECTOR_PERMISSION);102}103return theProxySelector;104}105106/**107* Sets (or unsets) the system-wide proxy selector.108*109* Note: non-standard protocol handlers may ignore this setting.110*111* @param ps The HTTP proxy selector, or112* {@code null} to unset the proxy selector.113*114* @throws SecurityException115* If a security manager has been installed and it denies116* {@link NetPermission}{@code ("setProxySelector")}117*118* @see #getDefault()119* @since 1.5120*/121public static void setDefault(ProxySelector ps) {122@SuppressWarnings("removal")123SecurityManager sm = System.getSecurityManager();124if (sm != null) {125sm.checkPermission(SecurityConstants.SET_PROXYSELECTOR_PERMISSION);126}127theProxySelector = ps;128}129130/**131* Selects all the applicable proxies based on the protocol to132* access the resource with and a destination address to access133* the resource at.134* The format of the URI is defined as follow:135* <UL>136* <LI>http URI for http connections</LI>137* <LI>https URI for https connections138* <LI>{@code socket://host:port}<br>139* for tcp client sockets connections</LI>140* </UL>141*142* @param uri143* The URI that a connection is required to144*145* @return a List of Proxies. Each element in146* the List is of type147* {@link java.net.Proxy Proxy};148* when no proxy is available, the list will149* contain one element of type150* {@link java.net.Proxy Proxy}151* that represents a direct connection.152* @throws IllegalArgumentException if the argument is null or if153* the protocol or host cannot be determined from the provided154* {@code uri}155*/156public abstract List<Proxy> select(URI uri);157158/**159* Called to indicate that a connection could not be established160* to a proxy/socks server. An implementation of this method can161* temporarily remove the proxies or reorder the sequence of162* proxies returned by {@link #select(URI)}, using the address163* and the IOException caught when trying to connect.164*165* @param uri166* The URI that the proxy at sa failed to serve.167* @param sa168* The socket address of the proxy/SOCKS server169*170* @param ioe171* The I/O exception thrown when the connect failed.172* @throws IllegalArgumentException if either argument is null173*/174public abstract void connectFailed(URI uri, SocketAddress sa, IOException ioe);175176/**177* Returns a ProxySelector which uses the given proxy address for all HTTP178* and HTTPS requests. If proxy is {@code null} then proxying is disabled.179*180* @param proxyAddress181* The address of the proxy182*183* @return a ProxySelector184*185* @since 9186*/187public static ProxySelector of(InetSocketAddress proxyAddress) {188return new StaticProxySelector(proxyAddress);189}190191static class StaticProxySelector extends ProxySelector {192private static final List<Proxy> NO_PROXY_LIST = List.of(Proxy.NO_PROXY);193final List<Proxy> list;194195StaticProxySelector(InetSocketAddress address){196Proxy p;197if (address == null) {198p = Proxy.NO_PROXY;199} else {200p = new Proxy(Proxy.Type.HTTP, address);201}202list = List.of(p);203}204205@Override206public void connectFailed(URI uri, SocketAddress sa, IOException e) {207/* ignore */208}209210@Override211public synchronized List<Proxy> select(URI uri) {212String scheme = uri.getScheme().toLowerCase();213if (scheme.equals("http") || scheme.equals("https")) {214return list;215} else {216return NO_PROXY_LIST;217}218}219}220}221222223