001: /* Licensed to the Apache Software Foundation (ASF) under one or more
002: * contributor license agreements. See the NOTICE file distributed with
003: * this work for additional information regarding copyright ownership.
004: * The ASF licenses this file to You under the Apache License, Version 2.0
005: * (the "License"); you may not use this file except in compliance with
006: * the License. You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package java.net;
017:
018: import org.apache.harmony.luni.util.Msg;
019:
020: /**
021: * This class is about proxy setting. A proxy contains <code>type</code>,
022: * proxy host address information. There are three types of <code>Proxy</code>:
023: * <li>Direct type proxy</li>
024: * <li>HTTP type proxy</li>
025: * <li>SOCKS type proxy</li>
026: *
027: * A <code>Proxy</code> instance is immutable.
028: *
029: */
030: public class Proxy {
031:
032: /**
033: * Represents <code>Proxy.Type.DIRECT</code> type proxy setting. It tells
034: * protocol handlers not to use any proxy.
035: */
036: public static final Proxy NO_PROXY = new Proxy();
037:
038: private Proxy.Type type;
039:
040: private SocketAddress address;
041:
042: /**
043: * New a <code>Proxy</code> instance. SocketAddress must NOT be null when
044: * <code>type</code> is either <code>Proxy.Type.HTTP</code> or
045: * <code>Proxy.Type.SOCKS</code>. For <code>Proxy.Type.DIRECT</code>
046: * type proxy, use <code>Proxy.NO_PROXY</code> directly instead of
047: * constructing it.
048: *
049: * @param type
050: * proxy type
051: * @param sa
052: * proxy address
053: * @throws IllegalArgumentException
054: * when <code>type</code> is <code>Proxy.Type.DIRECT</code>
055: * or SocketAddress is null.
056: */
057: public Proxy(Proxy.Type type, SocketAddress sa) {
058: /*
059: * Don't use DIRECT type to construct a proxy instance directly.
060: * SocketAddress must NOT be null.
061: */
062: if (type == Type.DIRECT || null == sa) {
063: // KA022=Illegal Proxy.Type or SocketAddress argument
064: throw new IllegalArgumentException(Msg.getString("KA022")); //$NON-NLS-1$
065: }
066: this .type = type;
067: address = sa;
068: }
069:
070: /*
071: * Constructs a Proxy instance, which is Proxy.DIRECT type with null
072: * SocketAddress. This constructor is used for NO_PROXY.
073: */
074: private Proxy() {
075: type = Type.DIRECT;
076: address = null;
077: }
078:
079: /**
080: * Gets the proxy type.
081: *
082: * @return the proxy type.
083: */
084: public Proxy.Type type() {
085: return type;
086: }
087:
088: /**
089: * Gets the proxy address.
090: *
091: * @return the proxy address for <code>HTTP</code> and <code>SOCKS</code>
092: * type proxy. Returns null for <code>DIRECT</code> type proxy.
093: */
094: public SocketAddress address() {
095: return address;
096: }
097:
098: /**
099: * <p>
100: * Representing string of the proxy. The string consists of
101: * <code>type.toString()</code> and <code>address.toString()</code> if
102: * <code>type</code> and <code>address</code> are not null.
103: * </p>
104: *
105: * @see java.lang.Object#equals(java.lang.Object)
106: * @return representing string of the proxy.
107: */
108: @Override
109: public String toString() {
110: String proxyString = String.valueOf(type);
111: if (null != address) {
112: proxyString += "/" + address.toString(); //$NON-NLS-1$
113: }
114: return proxyString;
115: }
116:
117: /**
118: * <p>
119: * Compare <code>obj</code> with current proxy. Returns false if the
120: * <code>obj</code> is not a <code>Proxy</code> object. Returns true if
121: * and only if the <code>obj</code> has the same <code>address</code>
122: * and <code>type</code> value as current proxy.
123: * </p>
124: *
125: * @see java.lang.Object#equals(java.lang.Object)
126: * @return true if <code>obj</code> represents the same proxy. Otherwise,
127: * returns false.
128: */
129: @Override
130: public final boolean equals(Object obj) {
131: if (this == obj) {
132: return true;
133: }
134: if (!(obj instanceof Proxy)) {
135: return false;
136: }
137: Proxy another = (Proxy) obj;
138: // address is null when and only when it's NO_PROXY.
139: return (type == another.type)
140: && address.equals(another.address);
141: }
142:
143: /**
144: * gets the hash code of <code>Proxy</code>.
145: *
146: * @see java.lang.Object#hashCode()
147: * @return the hash code of <code>Proxy</code>.
148: */
149: @Override
150: public final int hashCode() {
151: int ret = 0;
152: ret += type.hashCode();
153: if (null != address) {
154: ret += address.hashCode();
155: }
156: return ret;
157: }
158:
159: /**
160: * The proxy type, includes <code>DIRECT</code>, <code>HTTP</code> and
161: * <code>SOCKS</code>.
162: */
163: public enum Type {
164: /**
165: * Direct connection. Connect without any proxy.
166: */
167: DIRECT,
168:
169: /**
170: * HTTP type proxy. It's often used by protocol handlers such as HTTP,
171: * HTTPS and FTP.
172: */
173: HTTP,
174:
175: /**
176: * SOCKS type proxy.
177: */
178: SOCKS
179: }
180: }
|