Index: contrib/org/apache/commons/httpclient/contrib/utils/HttpMethodCloner.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/contrib/org/apache/commons/httpclient/contrib/utils/HttpMethodCloner.java,v retrieving revision 1.6 diff -u -r1.6 HttpMethodCloner.java --- contrib/org/apache/commons/httpclient/contrib/utils/HttpMethodCloner.java 4 May 2004 21:24:51 -0000 1.6 +++ contrib/org/apache/commons/httpclient/contrib/utils/HttpMethodCloner.java 1 Oct 2004 15:24:09 -0000 @@ -28,7 +28,6 @@ package org.apache.commons.httpclient.contrib.utils; import org.apache.commons.httpclient.Header; -import org.apache.commons.httpclient.HostConfiguration; import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.HttpMethodBase; import org.apache.commons.httpclient.methods.EntityEnclosingMethod; @@ -58,10 +57,6 @@ private static void copyHttpMethodBase( HttpMethodBase m, HttpMethodBase copy) { - if (m.getHostConfiguration() != null) { - copy.setHostConfiguration( - new HostConfiguration(m.getHostConfiguration())); - } try { copy.setParams((HttpMethodParams)m.getParams().clone()); } catch (CloneNotSupportedException e) { Index: java/org/apache/commons/httpclient/HostConfiguration.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HostConfiguration.java,v retrieving revision 1.19 diff -u -r1.19 HostConfiguration.java --- java/org/apache/commons/httpclient/HostConfiguration.java 17 Jul 2004 18:58:33 -0000 1.19 +++ java/org/apache/commons/httpclient/HostConfiguration.java 1 Oct 2004 15:24:09 -0000 @@ -55,51 +55,22 @@ public static final HostConfiguration ANY_HOST_CONFIGURATION = new HostConfiguration(); /** The host to use. */ - private String host; - - /** The virtual host to use. */ - private String virtualHost; - - /** The port to use. */ - private int port; - - /** The protocol */ - private Protocol protocol; - - /** True if a host has been set */ - private boolean hostSet; + private HttpHost host = null; /** The host name of the proxy server */ - private String proxyHost; - - /** The port number of the proxy server */ - private int proxyPort; + private ProxyHost proxyHost = null; - /** True if a proxy server has been set */ - private boolean proxySet; - /** The local address to use when creating the socket, or null to use the default */ - private InetAddress localAddress; + private InetAddress localAddress = null; /** Parameters specific to this host */ - private HostParams params; + private HostParams params = new HostParams(); /** * Constructor for HostConfiguration. */ public HostConfiguration() { - - this.host = null; - this.virtualHost = null; - this.port = -1; - this.protocol = null; - this.hostSet = false; - - this.proxyHost = null; - this.proxyPort = -1; - this.proxySet = false; - this.localAddress = null; - this.params = new HostParams(); + super(); } /** @@ -107,28 +78,27 @@ * * @param hostConfiguration the hostConfiguration to copy */ - public HostConfiguration (HostConfiguration hostConfiguration) { - + public HostConfiguration (final HostConfiguration hostConfiguration) { // wrap all of the assignments in a synchronized block to avoid // having to negotiate the monitor for each method call - synchronized (hostConfiguration) { - this.host = hostConfiguration.getHost(); - this.virtualHost = hostConfiguration.getVirtualHost(); - this.port = hostConfiguration.getPort(); - this.protocol = hostConfiguration.getProtocol(); - this.hostSet = hostConfiguration.isHostSet(); - - this.proxyHost = hostConfiguration.getProxyHost(); - this.proxyPort = hostConfiguration.getProxyPort(); - this.proxySet = hostConfiguration.isProxySet(); - this.localAddress = hostConfiguration.getLocalAddress(); + synchronized (hostConfiguration) { try { + if (hostConfiguration.host != null) { + this.host = (HttpHost) hostConfiguration.host.clone(); + } else { + this.host = null; + } + if (hostConfiguration.proxyHost != null) { + this.proxyHost = (ProxyHost) hostConfiguration.proxyHost.clone(); + } else { + this.proxyHost = null; + } + this.localAddress = hostConfiguration.getLocalAddress(); this.params = (HostParams)hostConfiguration.getParams().clone(); } catch (CloneNotSupportedException e) { - this.params = new HostParams(); + throw new IllegalArgumentException("Host configuration could not be cloned"); } - } - + } } /** @@ -144,37 +114,35 @@ public synchronized String toString() { boolean appendComma = false; - StringBuffer b = new StringBuffer(50); b.append("HostConfiguration["); - if (isHostSet()) { + if (this.host != null) { appendComma = true; - b.append("host=").append(host); - b.append(", protocol=").append(protocol); - b.append(", port=").append(port); - if (virtualHost != null) { - b.append(", virtualHost=").append(virtualHost); - } + b.append("host=").append(this.host); } - if (isProxySet()) { + if (this.proxyHost != null) { if (appendComma) { b.append(", "); } else { appendComma = true; } - b.append("proxyHost=").append(proxyHost); - b.append(", proxyPort=").append(proxyPort); + b.append("proxyHost=").append(this.proxyHost); } - if (localAddress != null) { + if (this.localAddress != null) { + if (appendComma) { + b.append(", "); + } else { + appendComma = true; + } + b.append("localAddress=").append(this.localAddress); if (appendComma) { b.append(", "); } else { appendComma = true; } - b.append("localAddress=").append(localAddress); + b.append("params=").append(this.params); } - b.append("]"); return b.toString(); } @@ -189,27 +157,19 @@ * configuration * * @see #proxyEquals(HttpConnection) - * @see #isHostSet() */ - public synchronized boolean hostEquals(HttpConnection connection) { - - if (hostSet) { - if (!this.host.equalsIgnoreCase(connection.getHost())) { + public synchronized boolean hostEquals(final HttpConnection connection) { + if (connection == null) { + throw new IllegalArgumentException("Connection may not be null"); + } + if (this.host != null) { + if (!this.host.getHostName().equalsIgnoreCase(connection.getHost())) { return false; } - if (this.virtualHost != null) { - if (!this.virtualHost.equalsIgnoreCase(connection.getVirtualHost())) { - return false; - } - } else { - if (connection.getVirtualHost() != null) { - return false; - } - } - if (this.port != connection.getPort()) { + if (this.host.getPort() != connection.getPort()) { return false; } - if (!this.protocol.equals(connection.getProtocol())) { + if (!this.host.getProtocol().equals(connection.getProtocol())) { return false; } if (this.localAddress != null) { @@ -225,7 +185,6 @@ } else { return false; } - } /** @@ -238,15 +197,16 @@ * * @see #hostEquals(HttpConnection) */ - public synchronized boolean proxyEquals(HttpConnection connection) { - - if (proxyHost == null) { - return connection.getProxyHost() == null; + public synchronized boolean proxyEquals(final HttpConnection connection) { + if (connection == null) { + throw new IllegalArgumentException("Connection may not be null"); + } + if (this.proxyHost != null) { + return + this.proxyHost.getHostName().equalsIgnoreCase(connection.getProxyHost()) + && this.proxyHost.getPort() == connection.getProxyPort(); } else { - return ( - proxyHost.equalsIgnoreCase(connection.getProxyHost()) - && proxyPort == connection.getProxyPort() - ); + return connection.getProxyHost() == null; } } @@ -255,18 +215,27 @@ * @return true if the host is set. */ public synchronized boolean isHostSet() { - return hostSet; + return this.host != null; } /** + * Sets the given host + * + * @param host the host + */ + public synchronized void setHost(final HttpHost host) { + this.host = host; + } + + /** * Sets the given host, port and protocol * * @param host the host(IP or DNS name) * @param port The port * @param protocol The protocol. */ - public synchronized void setHost(String host, int port, String protocol) { - setHost(host, null, port, Protocol.getProtocol(protocol)); + public synchronized void setHost(final String host, int port, final String protocol) { + this.host = new HttpHost(host, null, port, Protocol.getProtocol(protocol)); } /** @@ -277,22 +246,15 @@ * @param port the host port or -1 to use protocol default * @param protocol the protocol */ - public synchronized void setHost(String host, String virtualHost, int port, - Protocol protocol) { - + public synchronized void setHost(final String host, final String virtualHost, int port, + final Protocol protocol) { if (host == null) { throw new IllegalArgumentException("host must not be null"); } if (protocol == null) { throw new IllegalArgumentException("protocol must not be null"); } - - this.host = host; - this.virtualHost = virtualHost; - this.port = port == -1 ? protocol.getDefaultPort() : port; - this.protocol = protocol; - - this.hostSet = true; + this.host = new HttpHost(host, virtualHost, port, protocol); } /** @@ -302,7 +264,7 @@ * @param port The port * @param protocol the protocol */ - public synchronized void setHost(String host, int port, Protocol protocol) { + public synchronized void setHost(final String host, int port, final Protocol protocol) { setHost(host, null, port, protocol); } @@ -312,7 +274,7 @@ * @param host the host(IP or DNS name) * @param port The port */ - public synchronized void setHost(String host, int port) { + public synchronized void setHost(final String host, int port) { setHost(host, null, port, Protocol.getProtocol("http")); } @@ -321,7 +283,7 @@ * * @param host The host(IP or DNS name). */ - public synchronized void setHost(String host) { + public synchronized void setHost(final String host) { Protocol defaultProtocol = Protocol.getProtocol("http"); setHost(host, null, defaultProtocol.getDefaultPort(), defaultProtocol); } @@ -330,7 +292,7 @@ * Sets the protocol, host and port from the given URI. * @param uri the URI. */ - public synchronized void setHost(URI uri) { + public synchronized void setHost(final URI uri) { try { setHost(uri.getHost(), uri.getPort(), uri.getScheme()); } catch (URIException e) { @@ -344,20 +306,11 @@ * @return The host url. */ public synchronized String getHostURL() { - - if (!hostSet) { - throw new IllegalStateException("a default host must be set to " - + "create a host URL" - ); - } - - String url = protocol.getScheme() + "://" + host; - - if (port != -1 && port != protocol.getDefaultPort()) { - url += ":" + port; + if (this.host == null) { + throw new IllegalStateException("Host must be set to create a host URL"); + } else { + return this.host.toURI(); } - - return url; } /** @@ -368,7 +321,11 @@ * @see #isHostSet() */ public synchronized String getHost() { - return host; + if (this.host != null) { + return this.host.getHostName(); + } else { + return null; + } } /** @@ -377,7 +334,11 @@ * @return the virtual host name, or null if not set */ public synchronized String getVirtualHost() { - return virtualHost; + if (this.host != null) { + return this.host.getVirtualHostName(); + } else { + return null; + } } /** @@ -388,7 +349,11 @@ * @see #isHostSet() */ public synchronized int getPort() { - return port; + if (this.host != null) { + return this.host.getPort(); + } else { + return -1; + } } /** @@ -396,7 +361,11 @@ * @return The protocol. */ public synchronized Protocol getProtocol() { - return protocol; + if (this.host != null) { + return this.host.getProtocol(); + } else { + return null; + } } /** @@ -407,20 +376,25 @@ * @see #setProxy(String, int) */ public synchronized boolean isProxySet() { - return proxySet; + return this.proxyHost != null; } /** + * Sets the given proxy host + * + * @param host the proxy host + */ + public synchronized void setProxyHost(final ProxyHost proxyHost) { + this.proxyHost = proxyHost; + } + + /** * Set the proxy settings. * @param proxyHost The proxy host * @param proxyPort The proxy port */ - public synchronized void setProxy(String proxyHost, int proxyPort) { - - this.proxyHost = proxyHost; - this.proxyPort = proxyPort; - - this.proxySet = true; + public synchronized void setProxy(final String proxyHost, int proxyPort) { + this.proxyHost = new ProxyHost(proxyHost, proxyPort); } /** @@ -431,7 +405,11 @@ * @see #isProxySet() */ public synchronized String getProxyHost() { - return proxyHost; + if (this.proxyHost != null) { + return this.proxyHost.getHostName(); + } else { + return null; + } } /** @@ -442,7 +420,11 @@ * @see #isProxySet() */ public synchronized int getProxyPort() { - return proxyPort; + if (this.proxyHost != null) { + return this.proxyHost.getPort(); + } else { + return -1; + } } /** @@ -494,7 +476,7 @@ /** * @see java.lang.Object#equals(java.lang.Object) */ - public synchronized boolean equals(Object o) { + public synchronized boolean equals(final Object o) { if (o instanceof HostConfiguration) { @@ -503,52 +485,37 @@ return true; } - HostConfiguration config = (HostConfiguration) o; + HostConfiguration that = (HostConfiguration) o; - if (hostSet) { - if (!host.equalsIgnoreCase(config.getHost())) { + if (this.host != null) { + if (!this.host.equals(that.host)) { return false; } - if (virtualHost != null) { - if (!virtualHost.equalsIgnoreCase(config.getVirtualHost())) { - return false; - } - } else { - if (config.getVirtualHost() != null) { - return false; - } - } - if (port != config.getPort()) { + } else { + if (that.host != null) { return false; } - if (!protocol.equals(config.getProtocol())) { + } + if (this.proxyHost != null) { + if (!this.proxyHost.equals(that.proxyHost)) { return false; } - } else if (config.isHostSet()) { - return false; - } - if (proxyHost != null) { - if (!proxyHost.equalsIgnoreCase (config.getProxyHost()) - || proxyPort != config.getProxyPort()) { - // either proxyHost or proxyPort don't match + } else { + if (that.proxyHost != null) { return false; } - } else if (config.getProxyHost() != null) { - return false; - } + } if (localAddress != null) { - if (!localAddress.equals(config.getLocalAddress())) { + if (!localAddress.equals(that.getLocalAddress())) { return false; } } else { - if (config.getLocalAddress() != null) { + if (that.getLocalAddress() != null) { return false; } } - // everything matches return true; - } else { return false; } @@ -559,7 +526,6 @@ * @see java.lang.Object#hashCode() */ public int hashCode() { - if (host != null) { return host.hashCode(); } else if (proxyHost != null) { Index: java/org/apache/commons/httpclient/HttpClient.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpClient.java,v retrieving revision 1.96 diff -u -r1.96 HttpClient.java --- java/org/apache/commons/httpclient/HttpClient.java 14 Jun 2004 21:25:38 -0000 1.96 +++ java/org/apache/commons/httpclient/HttpClient.java 1 Oct 2004 15:24:10 -0000 @@ -321,14 +321,7 @@ LOG.trace("enter HttpClient.executeMethod(HttpMethod)"); // execute this method and use its host configuration, if it has one - return executeMethod( - method.getHostConfiguration() != null - ? method.getHostConfiguration() - : getHostConfiguration(), - method, - null - ); - + return executeMethod(null, method, null); } /** @@ -345,11 +338,11 @@ * cannot be recovered from. * @since 2.0 */ - public int executeMethod(HostConfiguration hostConfiguration, HttpMethod method) + public int executeMethod(final HostConfiguration hostConfiguration, final HttpMethod method) throws IOException, HttpException { LOG.trace("enter HttpClient.executeMethod(HostConfiguration,HttpMethod)"); - + return executeMethod(hostConfiguration, method, null); } @@ -374,7 +367,7 @@ * @since 2.0 */ public int executeMethod(HostConfiguration hostConfiguration, - HttpMethod method, HttpState state) + final HttpMethod method, final HttpState state) throws IOException, HttpException { LOG.trace("enter HttpClient.executeMethod(HostConfiguration,HttpMethod,HttpState)"); @@ -382,58 +375,20 @@ if (method == null) { throw new IllegalArgumentException("HttpMethod parameter may not be null"); } - - if (hostConfiguration == null) { - hostConfiguration = ( - method.getHostConfiguration() != null - ? method.getHostConfiguration() - : getHostConfiguration() - ); - } - - HostConfiguration defaultHostConfiguration = null; - synchronized (this) { - defaultHostConfiguration = getHostConfiguration(); - } - HostConfiguration methodConfiguration = new HostConfiguration(hostConfiguration); - if (hostConfiguration != defaultHostConfiguration) { - // we may need to apply some defaults - if (!methodConfiguration.isHostSet()) { - methodConfiguration.setHost( - defaultHostConfiguration.getHost(), - defaultHostConfiguration.getVirtualHost(), - defaultHostConfiguration.getPort(), - defaultHostConfiguration.getProtocol() - ); - } - if (!methodConfiguration.isProxySet() - && defaultHostConfiguration.isProxySet()) { - - methodConfiguration.setProxy( - defaultHostConfiguration.getProxyHost(), - defaultHostConfiguration.getProxyPort() - ); - } - if (methodConfiguration.getLocalAddress() == null - && defaultHostConfiguration.getLocalAddress() != null) { - - methodConfiguration.setLocalAddress(defaultHostConfiguration.getLocalAddress()); + HostConfiguration defaulthostconfig = getHostConfiguration(); + if (hostConfiguration == null || hostConfiguration == defaulthostconfig) { + // make a deep copy of the host derfauls + hostConfiguration = new HostConfiguration(defaulthostconfig); + if (method.getHost() != null) { + hostConfiguration.setHost(method.getHost()); } } - /* access all synchronized data in a single block, this will keeps us - * from accessing data asynchronously as well having to regain the lock - * for each item. - */ - HttpMethodDirector methodDirector = null; - synchronized (this) { - methodDirector = new HttpMethodDirector( + HttpMethodDirector methodDirector = new HttpMethodDirector( this.httpConnectionManager, - methodConfiguration, + hostConfiguration, this.params, (state == null ? getState() : state)); - defaultHostConfiguration = getHostConfiguration(); - } methodDirector.executeMethod(method); return method.getStatusCode(); } Index: java/org/apache/commons/httpclient/HttpMethod.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethod.java,v retrieving revision 1.41 diff -u -r1.41 HttpMethod.java --- java/org/apache/commons/httpclient/HttpMethod.java 5 Jul 2004 22:46:58 -0000 1.41 +++ java/org/apache/commons/httpclient/HttpMethod.java 1 Oct 2004 15:24:10 -0000 @@ -65,11 +65,20 @@ /** * Gets the host configuration for this method. The configuration specifies * the server, port, protocol, and proxy server via which this method will - * send its HTTP request. + * send its HTTP request. + * + * @deprecated no longer applicable * * @return the HostConfiguration or null if none is set */ HostConfiguration getHostConfiguration(); + + /** + * Gets the target host for this method. + * + * @return the {@link HttpHost} or null if none is set + */ + HttpHost getHost(); /** * Sets the path of the HTTP method. Index: java/org/apache/commons/httpclient/HttpMethodBase.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java,v retrieving revision 1.216 diff -u -r1.216 HttpMethodBase.java --- java/org/apache/commons/httpclient/HttpMethodBase.java 30 Sep 2004 18:15:22 -0000 1.216 +++ java/org/apache/commons/httpclient/HttpMethodBase.java 1 Oct 2004 15:24:10 -0000 @@ -153,8 +153,8 @@ * a recoverable exception. */ private int recoverableExceptionCount = 0; - /** the host configuration for this HTTP method, can be null */ - private HostConfiguration hostConfiguration; + /** the host for this HTTP method, can be null */ + private HttpHost httphost = null; /** * Handles method retries @@ -242,7 +242,7 @@ */ public URI getURI() throws URIException { - if (hostConfiguration == null) { + if (this.httphost == null) { // just use a relative URI, the host hasn't been set URI tmpUri = new URI(null, null, path, null, null); tmpUri.setEscapedQuery(queryString); @@ -250,15 +250,14 @@ } else { // we only want to include the port if it's not the default - int port = hostConfiguration.getPort(); - if (port == hostConfiguration.getProtocol().getDefaultPort()) { + int port = this.httphost.getPort(); + if (port == this.httphost.getProtocol().getDefaultPort()) { port = -1; } - URI tmpUri = new URI( - hostConfiguration.getProtocol().getScheme(), + this.httphost.getProtocol().getScheme(), null, - hostConfiguration.getHost(), + this.httphost.getHostName(), port, path, null // to set an escaped form @@ -282,16 +281,8 @@ public void setURI(URI uri) throws URIException { // only set the host if specified by the URI if (uri.isAbsoluteURI()) { - if (this.hostConfiguration == null) { - this.hostConfiguration = new HostConfiguration(); - } - this.hostConfiguration.setHost( - uri.getHost(), - uri.getPort(), - uri.getScheme() - ); + this.httphost = new HttpHost(uri); } - // set the path, defaulting to root setPath( uri.getPath() == null @@ -2286,22 +2277,39 @@ } } + public HttpHost getHost() { + return this.httphost; + } + /** * Returns the {@link HostConfiguration host configuration}. * * @return the host configuration + * + * @deprecated no longer applicable */ public HostConfiguration getHostConfiguration() { - return hostConfiguration; + HostConfiguration hostconfig = new HostConfiguration(); + hostconfig.setHost(this.httphost); + return hostconfig; } - /** * Sets the {@link HostConfiguration host configuration}. * * @param hostConfiguration The hostConfiguration to set + * + * @deprecated no longer applicable */ - public void setHostConfiguration(HostConfiguration hostConfiguration) { - this.hostConfiguration = hostConfiguration; + public void setHostConfiguration(final HostConfiguration hostconfig) { + if (hostconfig != null) { + this.httphost = new HttpHost( + hostconfig.getHost(), + hostconfig.getVirtualHost(), + hostconfig.getPort(), + hostconfig.getProtocol()); + } else { + this.httphost = null; + } } /** Index: test/org/apache/commons/httpclient/params/TestHttpParams.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/params/TestHttpParams.java,v retrieving revision 1.2 diff -u -r1.2 TestHttpParams.java --- test/org/apache/commons/httpclient/params/TestHttpParams.java 15 Sep 2004 20:45:48 -0000 1.2 +++ test/org/apache/commons/httpclient/params/TestHttpParams.java 1 Oct 2004 15:24:11 -0000 @@ -1,5 +1,5 @@ /* - * $Header: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/params/TestHttpParams.java,v 1.2 2004/09/15 20:45:48 olegk Exp $ + * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/params/TestHttpParams.java,v 1.2 2004/09/15 20:45:48 olegk Exp $ * $Revision: 1.2 $ * $Date: 2004/09/15 20:45:48 $ * ==================================================================== @@ -129,5 +129,31 @@ Header[] thatheader = httpget.getRequestHeaders("that-header"); assertEquals(2, thatheader.length); assertEquals("test", httpget.getRequestHeader("User-Agent").getValue()); + } + + public void testDefaults() throws IOException { + this.server.setHttpService(new SimpleService()); + + this.client.getParams().setParameter(HttpMethodParams.USER_AGENT, "test"); + HostConfiguration hostconfig = new HostConfiguration(); + hostconfig.setHost( + this.server.getLocalAddress(), + this.server.getLocalPort(), + Protocol.getProtocol("http")); + + GetMethod httpget = new GetMethod("/miss/"); + try { + this.client.executeMethod(hostconfig, httpget); + } finally { + httpget.releaseConnection(); + } + assertEquals(HttpStatus.SC_OK, httpget.getStatusCode()); + assertEquals("test", httpget.getRequestHeader("User-Agent").getValue()); + assertEquals("test", httpget.getParams(). + getParameter(HttpMethodParams.USER_AGENT)); + assertEquals("test", hostconfig.getParams(). + getParameter(HttpMethodParams.USER_AGENT)); + assertEquals("test", client.getParams(). + getParameter(HttpMethodParams.USER_AGENT)); } } Index: java/org/apache/commons/httpclient/HttpHost.java =================================================================== RCS file: java/org/apache/commons/httpclient/HttpHost.java diff -N java/org/apache/commons/httpclient/HttpHost.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ java/org/apache/commons/httpclient/HttpHost.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,254 @@ +/* + * $Header$ + * $Revision$ + * $Date$ + * + * ==================================================================== + * + * Copyright 2002-2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ + +package org.apache.commons.httpclient; + +import org.apache.commons.httpclient.protocol.Protocol; + +/** + * Holds all of the variables needed to describe an HTTP connection to a host. This includes + * remote host, port and protocol, and virtual host. + * + * @author Michael Becke + * @author Mike Bowler + * @author Oleg Kalnichevski + * @author Laura Werner + * + * @since 3.0 + */ +public class HttpHost implements Cloneable { + + /** The host to use. */ + private String hostname = null; + + /** The virtual host to use. */ + private String virtualHostname = null; + + /** The port to use. */ + private int port = -1; + + /** The protocol */ + private Protocol protocol = null; + + /** + * Constructor for HttpHost. + * + * @param hostname the hostname (IP or DNS name). Can be null. + * @param virtualHostname the virtual hostname. Can be null. + * @param port the port. Value -1 can be used to set default protocol port + * @param protocol the protocol. Value null can be used to set default protocol + */ + public HttpHost( + final String hostname, final String virtualHostname, int port, + final Protocol protocol) { + super(); + if (hostname == null) { + throw new IllegalArgumentException("Host name may not be null"); + } + if (protocol == null) { + throw new IllegalArgumentException("Protocol may not be null"); + } + this.hostname = hostname; + this.virtualHostname = virtualHostname; + this.protocol = protocol; + if (port >= 0) { + this.port = port; + } else { + this.port = this.protocol.getDefaultPort(); + } + } + + /** + * Constructor for HttpHost. + * + * @param hostname the hostname (IP or DNS name). Can be null. + * @param port the port. Value -1 can be used to set default protocol port + */ + public HttpHost(final String hostname, int port) { + this(hostname, null, port, Protocol.getProtocol("http")); + } + + /** + * Constructor for HttpHost. + * + * @param hostname the hostname (IP or DNS name). Can be null. + */ + public HttpHost(final String hostname) { + this(hostname, null, -1, Protocol.getProtocol("http")); + } + + /** + * URI constructor for HttpHost. + * + * @param uri the URI. + */ + public HttpHost(final URI uri) throws URIException { + this(uri.getHost(), null, uri.getPort(), Protocol.getProtocol(uri.getScheme())); + } + + /** + * Copy constructor for HttpHost + * + * @param httphost the HTTP host to copy details from + */ + public HttpHost (final HttpHost httphost) { + super(); + this.hostname = httphost.hostname; + this.virtualHostname = httphost.virtualHostname; + this.port = httphost.port; + this.protocol = httphost.protocol; + } + + /** + * @see java.lang.Object#clone() + */ + public Object clone() { + return new HttpHost(this); + } + + /** + * Returns the host name (IP or DNS name). + * + * @return the host name (IP or DNS name), or null if not set + */ + public String getHostName() { + return this.hostname; + } + + /** + * Returns the virtual host name. + * + * @return the virtual host name, or null if not set + */ + public synchronized String getVirtualHostName() { + return this.virtualHostname; + } + + /** + * Returns the port. + * + * @return the host port, or -1 if not set + */ + public synchronized int getPort() { + return this.port; + } + + /** + * Returns the protocol. + * @return The protocol. + */ + public synchronized Protocol getProtocol() { + return this.protocol; + } + + /** + * Return the host uri. + * + * @return The host uri. + */ + public String toURI() { + StringBuffer buffer = new StringBuffer(50); + if (this.protocol != null) { + buffer.append(this.protocol.getScheme()); + buffer.append("://"); + } + if (this.virtualHostname != null) { + buffer.append(this.virtualHostname); + } else { + buffer.append(this.hostname); + } + if (this.port != this.protocol.getDefaultPort()) { + buffer.append(':'); + buffer.append(this.port); + } + return buffer.toString(); + } + + /** + * @see java.lang.Object#toString() + */ + public String toString() { + StringBuffer buffer = new StringBuffer(50); + buffer.append(toURI()); + if (this.virtualHostname != null) { + buffer.append(" ["); + buffer.append(this.hostname); + buffer.append(']'); + } + return buffer.toString(); + } + + /** + * @see java.lang.Object#equals(java.lang.Object) + */ + public boolean equals(final Object o) { + + if (o instanceof HttpHost) { + // shortcut if we're comparing with ourselves + if (o == this) { + return true; + } + HttpHost that = (HttpHost) o; + if (!this.hostname.equalsIgnoreCase(that.hostname)) { + return false; + } + if (this.virtualHostname != null) { + if (!this.hostname.equalsIgnoreCase(that.virtualHostname)) { + return false; + } + } else { + if (this.virtualHostname != that.virtualHostname) { + return false; + } + } + if (this.port != that.port) { + return false; + } + if (!this.protocol.equals(that.protocol)) { + return false; + } + // everything matches + return true; + } else { + return false; + } + } + + /** + * @see java.lang.Object#hashCode() + */ + public int hashCode() { + return + this.hostname.hashCode() ^ + (this.virtualHostname != null ? this.hostname.hashCode() : 0) + + this.port + + this.protocol.hashCode(); + } + +} Index: java/org/apache/commons/httpclient/ProxyHost.java =================================================================== RCS file: java/org/apache/commons/httpclient/ProxyHost.java diff -N java/org/apache/commons/httpclient/ProxyHost.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ java/org/apache/commons/httpclient/ProxyHost.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,83 @@ +/* + * $Header$ + * $Revision$ + * $Date$ + * + * ==================================================================== + * + * Copyright 2002-2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ + +package org.apache.commons.httpclient; + +import org.apache.commons.httpclient.protocol.Protocol; + +/** + * Holds all of the variables needed to describe an HTTP connection to a proxy. Proxy hosts + * cannot have a virtual host name and always use plain HTTP connection when communicating with + * clients. + * + * @author Michael Becke + * @author Mike Bowler + * @author Oleg Kalnichevski + * @author Laura Werner + * + * @since 3.0 + */ +public class ProxyHost extends HttpHost { + + /** + * Copy constructor for HttpHost + * + * @param httphost the HTTP host to copy details from + */ + public ProxyHost (final ProxyHost httpproxy) { + super(httpproxy); + } + + /** + * Constructor for ProxyHost. + * + * @param hostname the hostname (IP or DNS name). Can be null. + * @param port the port. Value -1 can be used to set default protocol port + */ + public ProxyHost(final String hostname, int port) { + super(hostname, null, port, Protocol.getProtocol("http")); + } + + /** + * Constructor for HttpHost. + * + * @param hostname the hostname (IP or DNS name). Can be null. + */ + public ProxyHost(final String hostname) { + this(hostname, -1); + } + + /** + * @see java.lang.Object#clone() + */ + public Object clone() { + return new ProxyHost(this); + } + +}