001: /*
002: * Copyright 2003-2005 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package java.net;
027:
028: /**
029: * This class represents a proxy setting, typically a type (http, socks) and
030: * a socket address.
031: * A <code>Proxy</code> is an immutable object.
032: *
033: * @version 1.3, 08/09/03
034: * @see java.net.ProxySelector
035: * @author Yingxian Wang
036: * @author Jean-Christophe Collet
037: * @since 1.5
038: */
039: public class Proxy {
040:
041: /**
042: * Represents the proxy type.
043: *
044: * @since 1.5
045: */
046: public enum Type {
047: /**
048: * Represents a direct connection, or the absence of a proxy.
049: */
050: DIRECT,
051: /**
052: * Represents proxy for high level protocols such as HTTP or FTP.
053: */
054: HTTP,
055: /**
056: * Represents a SOCKS (V4 or V5) proxy.
057: */
058: SOCKS
059: };
060:
061: private Type type;
062: private SocketAddress sa;
063:
064: /**
065: * A proxy setting that represents a <code>DIRECT</code> connection,
066: * basically telling the protocol handler not to use any proxying.
067: * Used, for instance, to create sockets bypassing any other global
068: * proxy settings (like SOCKS):
069: * <P>
070: * <code>Socket s = new Socket(Proxy.NO_PROXY);</code><br>
071: * <P>
072: */
073: public final static Proxy NO_PROXY = new Proxy();
074:
075: // Creates the proxy that represents a <code>DIRECT</code> connection.
076: private Proxy() {
077: type = type.DIRECT;
078: sa = null;
079: }
080:
081: /**
082: * Creates an entry representing a PROXY connection.
083: * Certain combinations are illegal. For instance, for types Http, and
084: * Socks, a SocketAddress <b>must</b> be provided.
085: * <P>
086: * Use the <code>Proxy.NO_PROXY</code> constant
087: * for representing a direct connection.
088: *
089: * @param type the <code>Type</code> of the proxy
090: * @param sa the <code>SocketAddress</code> for that proxy
091: * @throws IllegalArgumentException when the type and the address are
092: * incompatible
093: */
094: public Proxy(Type type, SocketAddress sa) {
095: if ((type == Type.DIRECT) || !(sa instanceof InetSocketAddress))
096: throw new IllegalArgumentException("type " + type
097: + " is not compatible with address " + sa);
098: this .type = type;
099: this .sa = sa;
100: }
101:
102: /**
103: * Returns the proxy type.
104: *
105: * @return a Type representing the proxy type
106: */
107: public Type type() {
108: return type;
109: }
110:
111: /**
112: * Returns the socket address of the proxy, or
113: * <code>null</code> if its a direct connection.
114: *
115: * @return a <code>SocketAddress</code> representing the socket end
116: * point of the proxy
117: */
118: public SocketAddress address() {
119: return sa;
120: }
121:
122: /**
123: * Constructs a string representation of this Proxy.
124: * This String is constructed by calling toString() on its type
125: * and concatenating " @ " and the toString() result from its address
126: * if its type is not <code>DIRECT</code>.
127: *
128: * @return a string representation of this object.
129: */
130: public String toString() {
131: if (type() == Type.DIRECT)
132: return "DIRECT";
133: return type() + " @ " + address();
134: }
135:
136: /**
137: * Compares this object against the specified object.
138: * The result is <code>true</code> if and only if the argument is
139: * not <code>null</code> and it represents the same proxy as
140: * this object.
141: * <p>
142: * Two instances of <code>Proxy</code> represent the same
143: * address if both the SocketAddresses and type are equal.
144: *
145: * @param obj the object to compare against.
146: * @return <code>true</code> if the objects are the same;
147: * <code>false</code> otherwise.
148: * @see java.net.InetSocketAddress#equals(java.lang.Object)
149: */
150: public final boolean equals(Object obj) {
151: if (obj == null || !(obj instanceof Proxy))
152: return false;
153: Proxy p = (Proxy) obj;
154: if (p.type() == type()) {
155: if (address() == null) {
156: return (p.address() == null);
157: } else
158: return address().equals(p.address());
159: }
160: return false;
161: }
162:
163: /**
164: * Returns a hashcode for this Proxy.
165: *
166: * @return a hash code value for this Proxy.
167: */
168: public final int hashCode() {
169: if (address() == null)
170: return type().hashCode();
171: return type().hashCode() + address().hashCode();
172: }
173: }
|