View Javadoc

1   /*
2    * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/HttpHost.java,v 1.3 2005/01/14 21:16:40 olegk Exp $
3    * $Revision: 1.3 $
4    * $Date: 2005-01-14 16:16:40 -0500 (Fri, 14 Jan 2005) $
5    *
6    * ====================================================================
7    *
8    *  Copyright 2002-2004 The Apache Software Foundation
9    *
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
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             // shortcut if we're comparing with ourselves
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             // everything matches
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 }