Path: blob/master/src/java.base/share/classes/java/net/Proxy.java
41152 views
/*1* Copyright (c) 2003, 2020, 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;2627/**28* This class represents a proxy setting, typically a type (http, socks) and29* a socket address.30* A {@code Proxy} is an immutable object.31*32* @see java.net.ProxySelector33* @author Yingxian Wang34* @author Jean-Christophe Collet35* @since 1.536*/37public class Proxy {3839/**40* Represents the proxy type.41*42* @since 1.543*/44public enum Type {45/**46* Represents a direct connection, or the absence of a proxy.47*/48DIRECT,49/**50* Represents proxy for high level protocols such as HTTP or FTP.51*/52HTTP,53/**54* Represents a SOCKS (V4 or V5) proxy.55*/56SOCKS57};5859private Type type;60private SocketAddress sa;6162/**63* A proxy setting that represents a {@code DIRECT} connection,64* basically telling the protocol handler not to use any proxying.65* Used, for instance, to create sockets bypassing any other global66* proxy settings (like SOCKS):67* <P>68* {@code Socket s = new Socket(Proxy.NO_PROXY);}69*70*/71public static final Proxy NO_PROXY = new Proxy();7273// Creates the proxy that represents a {@code DIRECT} connection.74private Proxy() {75type = Type.DIRECT;76sa = null;77}7879/**80* Creates an entry representing a PROXY connection.81* Certain combinations are illegal. For instance, for types Http, and82* Socks, a SocketAddress <b>must</b> be provided.83* <P>84* Use the {@code Proxy.NO_PROXY} constant85* for representing a direct connection.86*87* @param type the {@code Type} of the proxy88* @param sa the {@code SocketAddress} for that proxy89* @throws IllegalArgumentException when the type and the address are90* incompatible91*/92public Proxy(Type type, SocketAddress sa) {93if ((type == Type.DIRECT) || !(sa instanceof InetSocketAddress))94throw new IllegalArgumentException("type " + type + " is not compatible with address " + sa);95this.type = type;96this.sa = sa;97}9899/**100* Returns the proxy type.101*102* @return a Type representing the proxy type103*/104public Type type() {105return type;106}107108/**109* Returns the socket address of the proxy, or110* {@code null} if its a direct connection.111*112* @return a {@code SocketAddress} representing the socket end113* point of the proxy114*/115public SocketAddress address() {116return sa;117}118119/**120* Constructs a string representation of this Proxy.121* This String is constructed by calling toString() on its type122* and concatenating " @ " and the toString() result from its address123* if its type is not {@code DIRECT}.124*125* @return a string representation of this object.126*/127public String toString() {128if (type() == Type.DIRECT)129return "DIRECT";130return type() + " @ " + address();131}132133/**134* Compares this object against the specified object.135* The result is {@code true} if and only if the argument is136* not {@code null} and it represents the same proxy as137* this object.138* <p>139* Two instances of {@code Proxy} represent the same140* address if both the SocketAddresses and type are equal.141*142* @param obj the object to compare against.143* @return {@code true} if the objects are the same;144* {@code false} otherwise.145* @see java.net.InetSocketAddress#equals(java.lang.Object)146*/147public final boolean equals(Object obj) {148if (!(obj instanceof Proxy p))149return false;150if (p.type() == type()) {151if (address() == null) {152return (p.address() == null);153} else154return address().equals(p.address());155}156return false;157}158159/**160* Returns a hashcode for this Proxy.161*162* @return a hash code value for this Proxy.163*/164public final int hashCode() {165if (address() == null)166return type().hashCode();167return type().hashCode() + address().hashCode();168}169}170171172