michael@0: /*
michael@0: * ====================================================================
michael@0: * Licensed to the Apache Software Foundation (ASF) under one
michael@0: * or more contributor license agreements. See the NOTICE file
michael@0: * distributed with this work for additional information
michael@0: * regarding copyright ownership. The ASF licenses this file
michael@0: * to you under the Apache License, Version 2.0 (the
michael@0: * "License"); you may not use this file except in compliance
michael@0: * with the License. You may obtain a copy of the License at
michael@0: *
michael@0: * http://www.apache.org/licenses/LICENSE-2.0
michael@0: *
michael@0: * Unless required by applicable law or agreed to in writing,
michael@0: * software distributed under the License is distributed on an
michael@0: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
michael@0: * KIND, either express or implied. See the License for the
michael@0: * specific language governing permissions and limitations
michael@0: * under the License.
michael@0: * ====================================================================
michael@0: *
michael@0: * This software consists of voluntary contributions made by many
michael@0: * individuals on behalf of the Apache Software Foundation. For more
michael@0: * information on the Apache Software Foundation, please see
michael@0: * .
michael@0: *
michael@0: */
michael@0:
michael@0: package ch.boye.httpclientandroidlib;
michael@0:
michael@0: import java.io.Serializable;
michael@0: import java.util.Locale;
michael@0:
michael@0: import ch.boye.httpclientandroidlib.util.CharArrayBuffer;
michael@0: import ch.boye.httpclientandroidlib.util.LangUtils;
michael@0:
michael@0: /**
michael@0: * Holds all of the variables needed to describe an HTTP connection to a host.
michael@0: * This includes remote host name, port and scheme.
michael@0: *
michael@0: *
michael@0: * @since 4.0
michael@0: */
michael@0: //@Immutable
michael@0: public final class HttpHost implements Cloneable, Serializable {
michael@0:
michael@0: private static final long serialVersionUID = -7529410654042457626L;
michael@0:
michael@0: /** The default scheme is "http". */
michael@0: public static final String DEFAULT_SCHEME_NAME = "http";
michael@0:
michael@0: /** The host to use. */
michael@0: protected final String hostname;
michael@0:
michael@0: /** The lowercase host, for {@link #equals} and {@link #hashCode}. */
michael@0: protected final String lcHostname;
michael@0:
michael@0:
michael@0: /** The port to use. */
michael@0: protected final int port;
michael@0:
michael@0: /** The scheme (lowercased) */
michael@0: protected final String schemeName;
michael@0:
michael@0:
michael@0: /**
michael@0: * Creates a new {@link HttpHost HttpHost}, specifying all values.
michael@0: * Constructor for HttpHost.
michael@0: *
michael@0: * @param hostname the hostname (IP or DNS name)
michael@0: * @param port the port number.
michael@0: * -1
indicates the scheme default port.
michael@0: * @param scheme the name of the scheme.
michael@0: * null
indicates the
michael@0: * {@link #DEFAULT_SCHEME_NAME default scheme}
michael@0: */
michael@0: public HttpHost(final String hostname, int port, final String scheme) {
michael@0: super();
michael@0: if (hostname == null) {
michael@0: throw new IllegalArgumentException("Host name may not be null");
michael@0: }
michael@0: this.hostname = hostname;
michael@0: this.lcHostname = hostname.toLowerCase(Locale.ENGLISH);
michael@0: if (scheme != null) {
michael@0: this.schemeName = scheme.toLowerCase(Locale.ENGLISH);
michael@0: } else {
michael@0: this.schemeName = DEFAULT_SCHEME_NAME;
michael@0: }
michael@0: this.port = port;
michael@0: }
michael@0:
michael@0: /**
michael@0: * Creates a new {@link HttpHost HttpHost}, with default scheme.
michael@0: *
michael@0: * @param hostname the hostname (IP or DNS name)
michael@0: * @param port the port number.
michael@0: * -1
indicates the scheme default port.
michael@0: */
michael@0: public HttpHost(final String hostname, int port) {
michael@0: this(hostname, port, null);
michael@0: }
michael@0:
michael@0: /**
michael@0: * Creates a new {@link HttpHost HttpHost}, with default scheme and port.
michael@0: *
michael@0: * @param hostname the hostname (IP or DNS name)
michael@0: */
michael@0: public HttpHost(final String hostname) {
michael@0: this(hostname, -1, null);
michael@0: }
michael@0:
michael@0: /**
michael@0: * Copy constructor for {@link HttpHost HttpHost}.
michael@0: *
michael@0: * @param httphost the HTTP host to copy details from
michael@0: */
michael@0: public HttpHost (final HttpHost httphost) {
michael@0: this(httphost.hostname, httphost.port, httphost.schemeName);
michael@0: }
michael@0:
michael@0: /**
michael@0: * Returns the host name.
michael@0: *
michael@0: * @return the host name (IP or DNS name)
michael@0: */
michael@0: public String getHostName() {
michael@0: return this.hostname;
michael@0: }
michael@0:
michael@0: /**
michael@0: * Returns the port.
michael@0: *
michael@0: * @return the host port, or -1
if not set
michael@0: */
michael@0: public int getPort() {
michael@0: return this.port;
michael@0: }
michael@0:
michael@0: /**
michael@0: * Returns the scheme name.
michael@0: *
michael@0: * @return the scheme name
michael@0: */
michael@0: public String getSchemeName() {
michael@0: return this.schemeName;
michael@0: }
michael@0:
michael@0: /**
michael@0: * Return the host URI, as a string.
michael@0: *
michael@0: * @return the host URI
michael@0: */
michael@0: public String toURI() {
michael@0: CharArrayBuffer buffer = new CharArrayBuffer(32);
michael@0: buffer.append(this.schemeName);
michael@0: buffer.append("://");
michael@0: buffer.append(this.hostname);
michael@0: if (this.port != -1) {
michael@0: buffer.append(':');
michael@0: buffer.append(Integer.toString(this.port));
michael@0: }
michael@0: return buffer.toString();
michael@0: }
michael@0:
michael@0:
michael@0: /**
michael@0: * Obtains the host string, without scheme prefix.
michael@0: *
michael@0: * @return the host string, for example localhost:8080
michael@0: */
michael@0: public String toHostString() {
michael@0: if (this.port != -1) {
michael@0: //the highest port number is 65535, which is length 6 with the addition of the colon
michael@0: CharArrayBuffer buffer = new CharArrayBuffer(this.hostname.length() + 6);
michael@0: buffer.append(this.hostname);
michael@0: buffer.append(":");
michael@0: buffer.append(Integer.toString(this.port));
michael@0: return buffer.toString();
michael@0: } else {
michael@0: return this.hostname;
michael@0: }
michael@0: }
michael@0:
michael@0:
michael@0: public String toString() {
michael@0: return toURI();
michael@0: }
michael@0:
michael@0:
michael@0: public boolean equals(final Object obj) {
michael@0: if (this == obj) return true;
michael@0: if (obj instanceof HttpHost) {
michael@0: HttpHost that = (HttpHost) obj;
michael@0: return this.lcHostname.equals(that.lcHostname)
michael@0: && this.port == that.port
michael@0: && this.schemeName.equals(that.schemeName);
michael@0: } else {
michael@0: return false;
michael@0: }
michael@0: }
michael@0:
michael@0: /**
michael@0: * @see java.lang.Object#hashCode()
michael@0: */
michael@0: public int hashCode() {
michael@0: int hash = LangUtils.HASH_SEED;
michael@0: hash = LangUtils.hashCode(hash, this.lcHostname);
michael@0: hash = LangUtils.hashCode(hash, this.port);
michael@0: hash = LangUtils.hashCode(hash, this.schemeName);
michael@0: return hash;
michael@0: }
michael@0:
michael@0: public Object clone() throws CloneNotSupportedException {
michael@0: return super.clone();
michael@0: }
michael@0:
michael@0: }