KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > net > Proxy


1 /*
2  * @(#)Proxy.java 1.3 03/08/09
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.net;
9
10 /**
11  * This class represents a proxy setting, typically a type (http, socks) and
12  * a socket address.
13  * A <code>Proxy</code> is an immutable object.
14  *
15  * @version 1.3, 08/09/03
16  * @see java.net.ProxySelector
17  * @author Yingxian Wang
18  * @author Jean-Christophe Collet
19  * @since 1.5
20  */

21 public class Proxy {
22
23     /**
24      * Represents the proxy type.
25      *
26      * @since 1.5
27      */

28     public enum Type {
29     /**
30      * Represents a direct connection, or the absence of a proxy.
31      */

32     DIRECT,
33     /**
34      * Represents proxy for high level protocols such as HTTP or FTP.
35      */

36     HTTP,
37     /**
38      * Represents a SOCKS (V4 or V5) proxy.
39      */

40     SOCKS
41     };
42
43     private Type type;
44     private SocketAddress JavaDoc sa;
45
46     /**
47      * A proxy setting that represents a <code>DIRECT</code> connection,
48      * basically telling the protocol handler not to use any proxying.
49      * Used, for instance, to create sockets bypassing any other global
50      * proxy settings (like SOCKS):
51      * <P>
52      * <code>Socket s = new Socket(Proxy.NO_PROXY);</code><br>
53      * <P>
54      */

55     public final static Proxy JavaDoc NO_PROXY = new Proxy JavaDoc();
56
57     // Creates the proxy that represents a <code>DIRECT</code> connection.
58
private Proxy() {
59     type = type.DIRECT;
60     sa = null;
61     }
62
63     /**
64      * Creates an entry representing a PROXY connection.
65      * Certain combinations are illegal. For instance, for types Http, and
66      * Socks, a SocketAddress <b>must</b> be provided.
67      * <P>
68      * Use the <code>Proxy.NO_PROXY</code> constant
69      * for representing a direct connection.
70      *
71      * @param type the <code>Type</code> of the proxy
72      * @param sa the <code>SocketAddress</code> for that proxy
73      * @throws IllegalArgumentException when the type and the address are
74      * incompatible
75      */

76     public Proxy(Type type, SocketAddress JavaDoc sa) {
77     if ((type == Type.DIRECT) || !(sa instanceof InetSocketAddress JavaDoc))
78         throw new IllegalArgumentException JavaDoc("type " + type + " is not compatible with address " + sa);
79     this.type = type;
80     this.sa = sa;
81     }
82
83     /**
84      * Returns the proxy type.
85      *
86      * @return a Type representing the proxy type
87      */

88     public Type type() {
89     return type;
90     }
91
92     /**
93      * Returns the socket address of the proxy, or
94      * <code>null</code> if its a direct connection.
95      *
96      * @return a <code>SocketAddress</code> representing the socket end
97      * point of the proxy
98      */

99     public SocketAddress JavaDoc address() {
100     return sa;
101     }
102
103     /**
104      * Constructs a string representation of this Proxy.
105      * This String is constructed by calling toString() on its type
106      * and concatenating " @ " and the toString() result from its address
107      * if its type is not <code>DIRECT</code>.
108      *
109      * @return a string representation of this object.
110      */

111     public String JavaDoc toString() {
112     if (type() == Type.DIRECT)
113         return "DIRECT";
114     return type() + " @ " + address();
115     }
116
117         /**
118      * Compares this object against the specified object.
119      * The result is <code>true</code> if and only if the argument is
120      * not <code>null</code> and it represents the same proxy as
121      * this object.
122      * <p>
123      * Two instances of <code>Proxy</code> represent the same
124      * address if both the SocketAddresses and type are equal.
125      *
126      * @param obj the object to compare against.
127      * @return <code>true</code> if the objects are the same;
128      * <code>false</code> otherwise.
129      * @see java.net.InetSocketAddress#equals(java.lang.Object)
130      */

131     public final boolean equals(Object JavaDoc obj) {
132     if (obj == null || !(obj instanceof Proxy JavaDoc))
133         return false;
134     Proxy JavaDoc p = (Proxy JavaDoc) obj;
135     if (p.type() == type()) {
136         if (address() == null) {
137         return (p.address() == null);
138         } else
139         return address().equals(p.address());
140     }
141     return false;
142     }
143
144     /**
145      * Returns a hashcode for this Proxy.
146      *
147      * @return a hash code value for this Proxy.
148      */

149     public final int hashCode() {
150     if (address() == null)
151         return type().hashCode();
152     return type().hashCode() + address().hashCode();
153     }
154 }
155
Popular Tags