View Javadoc

1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/oac.hc3x/trunk/src/java/org/apache/commons/httpclient/HttpHost.java $
3    * $Revision$
4    * $Date$
5    *
6    * ====================================================================
7    *
8    *  Licensed to the Apache Software Foundation (ASF) under one or more
9    *  contributor license agreements.  See the NOTICE file distributed with
10   *  this work for additional information regarding copyright ownership.
11   *  The ASF licenses this file to You under the Apache License, Version 2.0
12   *  (the "License"); you may not use this file except in compliance with
13   *  the License.  You may obtain a copy of the License at
14   *
15   *      http://www.apache.org/licenses/LICENSE-2.0
16   *
17   *  Unless required by applicable law or agreed to in writing, software
18   *  distributed under the License is distributed on an "AS IS" BASIS,
19   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   *  See the License for the specific language governing permissions and
21   *  limitations under the License.
22   * ====================================================================
23   *
24   * This software consists of voluntary contributions made by many
25   * individuals on behalf of the Apache Software Foundation.  For more
26   * information on the Apache Software Foundation, please see
27   * <http://www.apache.org/>.
28   *
29   */
30  
31  package org.apache.commons.httpclient;
32  
33  import org.apache.commons.httpclient.protocol.Protocol;
34  import org.apache.commons.httpclient.util.LangUtils;
35  
36  /***
37   * Holds all of the variables needed to describe an HTTP connection to a host. This includes 
38   * remote host, port and protocol.
39   * 
40   * @author <a href="mailto:becke@u.washington.edu">Michael Becke</a>
41   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
42   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
43   * @author Laura Werner
44   * 
45   * @since 3.0 
46   */
47  public class HttpHost implements Cloneable {
48  
49      /*** The host to use. */
50      private String hostname = null;
51  
52      /*** The port to use. */
53      private int port = -1;
54  
55      /*** The protocol */
56      private Protocol protocol = null;
57  
58      /***
59       * Constructor for HttpHost.
60       *   
61       * @param hostname the hostname (IP or DNS name). Can be <code>null</code>.
62       * @param port the port. Value <code>-1</code> can be used to set default protocol port
63       * @param protocol the protocol. Value <code>null</code> can be used to set default protocol
64       */
65      public HttpHost(final String hostname, int port, final Protocol protocol) {
66          super();
67          if (hostname == null) {
68              throw new IllegalArgumentException("Host name may not be null");
69          }
70          if (protocol == null) {
71              throw new IllegalArgumentException("Protocol may not be null");
72          }
73          this.hostname = hostname;
74          this.protocol = protocol;
75          if (port >= 0) {
76              this.port = port;
77          } else {
78              this.port = this.protocol.getDefaultPort();
79          }
80      }
81  
82      /***
83       * Constructor for HttpHost.
84       *   
85       * @param hostname the hostname (IP or DNS name). Can be <code>null</code>.
86       * @param port the port. Value <code>-1</code> can be used to set default protocol port
87       */
88      public HttpHost(final String hostname, int port) {
89          this(hostname, port, Protocol.getProtocol("http"));
90      }
91      
92      /***
93       * Constructor for HttpHost.
94       *   
95       * @param hostname the hostname (IP or DNS name). Can be <code>null</code>.
96       */
97      public HttpHost(final String hostname) {
98          this(hostname, -1, Protocol.getProtocol("http"));
99      }
100     
101     /***
102      * URI constructor for HttpHost.
103      *   
104      * @param uri the URI.
105      */
106     public  HttpHost(final URI uri) throws URIException {
107         this(uri.getHost(), uri.getPort(), Protocol.getProtocol(uri.getScheme()));
108     }
109 
110     /***
111      * Copy constructor for HttpHost
112      * 
113      * @param httphost the HTTP host to copy details from
114      */
115     public HttpHost (final HttpHost httphost) {
116         super();
117         init(httphost);
118     }
119 
120 	private void init(final HttpHost httphost) {
121 		this.hostname = httphost.hostname;
122         this.port = httphost.port;
123         this.protocol = httphost.protocol;
124 	}
125 
126     /***
127      * @throws CloneNotSupportedException 
128      * @see java.lang.Object#clone()
129      */
130     public Object clone() throws CloneNotSupportedException {
131     	HttpHost copy = (HttpHost) super.clone();
132     	copy.init(this);
133         return copy;
134     }    
135     
136     /***
137      * Returns the host name (IP or DNS name).
138      * 
139      * @return the host name (IP or DNS name), or <code>null</code> if not set
140      */
141     public String getHostName() {
142         return this.hostname;
143     }
144 
145     /***
146      * Returns the port.
147      * 
148      * @return the host port, or <code>-1</code> if not set
149      */
150     public int getPort() {
151         return this.port;
152     }
153 
154     /***
155      * Returns the protocol.
156      * @return The protocol.
157      */
158     public Protocol getProtocol() {
159         return this.protocol;
160     }
161 
162     /***
163      * Return the host uri.
164      * 
165      * @return The host uri.
166      */
167     public String toURI() {
168         StringBuffer buffer = new StringBuffer(50);        
169         buffer.append(this.protocol.getScheme());
170         buffer.append("://");
171         buffer.append(this.hostname);
172         if (this.port != this.protocol.getDefaultPort()) {
173             buffer.append(':');
174             buffer.append(this.port);
175         }
176         return buffer.toString();
177     }
178 
179     /***
180      * @see java.lang.Object#toString()
181      */
182     public String toString() {
183         StringBuffer buffer = new StringBuffer(50);        
184         buffer.append(toURI());
185         return buffer.toString();
186     }    
187     
188     /***
189      * @see java.lang.Object#equals(java.lang.Object)
190      */
191     public boolean equals(final Object o) {
192         
193         if (o instanceof HttpHost) {
194             // shortcut if we're comparing with ourselves
195             if (o == this) { 
196                 return true;
197             }
198             HttpHost that = (HttpHost) o;
199             if (!this.hostname.equalsIgnoreCase(that.hostname)) {
200                 return false;
201             }
202             if (this.port != that.port) {
203                 return false;
204             }
205             if (!this.protocol.equals(that.protocol)) {
206                 return false;
207             }
208             // everything matches
209             return true;
210         } else {
211             return false;
212         }
213     }
214 
215     /***
216      * @see java.lang.Object#hashCode()
217      */
218     public int hashCode() {
219         int hash = LangUtils.HASH_SEED;
220         hash = LangUtils.hashCode(hash, this.hostname);
221         hash = LangUtils.hashCode(hash, this.port);
222         hash = LangUtils.hashCode(hash, this.protocol);
223         return hash;
224     }
225 
226 }