1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package org.apache.commons.httpclient;
31
32 import org.apache.commons.httpclient.protocol.Protocol;
33 import org.apache.commons.httpclient.util.LangUtils;
34
35 /***
36 * Holds all of the variables needed to describe an HTTP connection to a host. This includes
37 * remote host, port and protocol.
38 *
39 * @author <a href="mailto:becke@u.washington.edu">Michael Becke</a>
40 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
41 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
42 * @author Laura Werner
43 *
44 * @since 3.0
45 */
46 public class HttpHost implements Cloneable {
47
48 /*** The host to use. */
49 private String hostname = null;
50
51 /*** The port to use. */
52 private int port = -1;
53
54 /*** The protocol */
55 private Protocol protocol = null;
56
57 /***
58 * Constructor for HttpHost.
59 *
60 * @param hostname the hostname (IP or DNS name). Can be <code>null</code>.
61 * @param port the port. Value <code>-1</code> can be used to set default protocol port
62 * @param protocol the protocol. Value <code>null</code> can be used to set default protocol
63 */
64 public HttpHost(final String hostname, int port, final Protocol protocol) {
65 super();
66 if (hostname == null) {
67 throw new IllegalArgumentException("Host name may not be null");
68 }
69 if (protocol == null) {
70 throw new IllegalArgumentException("Protocol may not be null");
71 }
72 this.hostname = hostname;
73 this.protocol = protocol;
74 if (port >= 0) {
75 this.port = port;
76 } else {
77 this.port = this.protocol.getDefaultPort();
78 }
79 }
80
81 /***
82 * Constructor for HttpHost.
83 *
84 * @param hostname the hostname (IP or DNS name). Can be <code>null</code>.
85 * @param port the port. Value <code>-1</code> can be used to set default protocol port
86 */
87 public HttpHost(final String hostname, int port) {
88 this(hostname, port, Protocol.getProtocol("http"));
89 }
90
91 /***
92 * Constructor for HttpHost.
93 *
94 * @param hostname the hostname (IP or DNS name). Can be <code>null</code>.
95 */
96 public HttpHost(final String hostname) {
97 this(hostname, -1, Protocol.getProtocol("http"));
98 }
99
100 /***
101 * URI constructor for HttpHost.
102 *
103 * @param uri the URI.
104 */
105 public HttpHost(final URI uri) throws URIException {
106 this(uri.getHost(), uri.getPort(), Protocol.getProtocol(uri.getScheme()));
107 }
108
109 /***
110 * Copy constructor for HttpHost
111 *
112 * @param httphost the HTTP host to copy details from
113 */
114 public HttpHost (final HttpHost httphost) {
115 super();
116 this.hostname = httphost.hostname;
117 this.port = httphost.port;
118 this.protocol = httphost.protocol;
119 }
120
121 /***
122 * @see java.lang.Object#clone()
123 */
124 public Object clone() {
125 return new HttpHost(this);
126 }
127
128 /***
129 * Returns the host name (IP or DNS name).
130 *
131 * @return the host name (IP or DNS name), or <code>null</code> if not set
132 */
133 public String getHostName() {
134 return this.hostname;
135 }
136
137 /***
138 * Returns the port.
139 *
140 * @return the host port, or <code>-1</code> if not set
141 */
142 public int getPort() {
143 return this.port;
144 }
145
146 /***
147 * Returns the protocol.
148 * @return The protocol.
149 */
150 public Protocol getProtocol() {
151 return this.protocol;
152 }
153
154 /***
155 * Return the host uri.
156 *
157 * @return The host uri.
158 */
159 public String toURI() {
160 StringBuffer buffer = new StringBuffer(50);
161 if (this.protocol != null) {
162 buffer.append(this.protocol.getScheme());
163 buffer.append("://");
164 }
165 buffer.append(this.hostname);
166 if (this.port != this.protocol.getDefaultPort()) {
167 buffer.append(':');
168 buffer.append(this.port);
169 }
170 return buffer.toString();
171 }
172
173 /***
174 * @see java.lang.Object#toString()
175 */
176 public String toString() {
177 StringBuffer buffer = new StringBuffer(50);
178 buffer.append(toURI());
179 return buffer.toString();
180 }
181
182 /***
183 * @see java.lang.Object#equals(java.lang.Object)
184 */
185 public boolean equals(final Object o) {
186
187 if (o instanceof HttpHost) {
188
189 if (o == this) {
190 return true;
191 }
192 HttpHost that = (HttpHost) o;
193 if (!this.hostname.equalsIgnoreCase(that.hostname)) {
194 return false;
195 }
196 if (this.port != that.port) {
197 return false;
198 }
199 if (!this.protocol.equals(that.protocol)) {
200 return false;
201 }
202
203 return true;
204 } else {
205 return false;
206 }
207 }
208
209 /***
210 * @see java.lang.Object#hashCode()
211 */
212 public int hashCode() {
213 int hash = LangUtils.HASH_SEED;
214 hash = LangUtils.hashCode(hash, this.hostname);
215 hash = LangUtils.hashCode(hash, this.port);
216 hash = LangUtils.hashCode(hash, this.protocol);
217 return hash;
218 }
219
220 }