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