mobile/android/thirdparty/ch/boye/httpclientandroidlib/HttpHost.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /*
michael@0 2 * ====================================================================
michael@0 3 * Licensed to the Apache Software Foundation (ASF) under one
michael@0 4 * or more contributor license agreements. See the NOTICE file
michael@0 5 * distributed with this work for additional information
michael@0 6 * regarding copyright ownership. The ASF licenses this file
michael@0 7 * to you under the Apache License, Version 2.0 (the
michael@0 8 * "License"); you may not use this file except in compliance
michael@0 9 * with the License. You may obtain a copy of the License at
michael@0 10 *
michael@0 11 * http://www.apache.org/licenses/LICENSE-2.0
michael@0 12 *
michael@0 13 * Unless required by applicable law or agreed to in writing,
michael@0 14 * software distributed under the License is distributed on an
michael@0 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
michael@0 16 * KIND, either express or implied. See the License for the
michael@0 17 * specific language governing permissions and limitations
michael@0 18 * under the License.
michael@0 19 * ====================================================================
michael@0 20 *
michael@0 21 * This software consists of voluntary contributions made by many
michael@0 22 * individuals on behalf of the Apache Software Foundation. For more
michael@0 23 * information on the Apache Software Foundation, please see
michael@0 24 * <http://www.apache.org/>.
michael@0 25 *
michael@0 26 */
michael@0 27
michael@0 28 package ch.boye.httpclientandroidlib;
michael@0 29
michael@0 30 import java.io.Serializable;
michael@0 31 import java.util.Locale;
michael@0 32
michael@0 33 import ch.boye.httpclientandroidlib.util.CharArrayBuffer;
michael@0 34 import ch.boye.httpclientandroidlib.util.LangUtils;
michael@0 35
michael@0 36 /**
michael@0 37 * Holds all of the variables needed to describe an HTTP connection to a host.
michael@0 38 * This includes remote host name, port and scheme.
michael@0 39 *
michael@0 40 *
michael@0 41 * @since 4.0
michael@0 42 */
michael@0 43 //@Immutable
michael@0 44 public final class HttpHost implements Cloneable, Serializable {
michael@0 45
michael@0 46 private static final long serialVersionUID = -7529410654042457626L;
michael@0 47
michael@0 48 /** The default scheme is "http". */
michael@0 49 public static final String DEFAULT_SCHEME_NAME = "http";
michael@0 50
michael@0 51 /** The host to use. */
michael@0 52 protected final String hostname;
michael@0 53
michael@0 54 /** The lowercase host, for {@link #equals} and {@link #hashCode}. */
michael@0 55 protected final String lcHostname;
michael@0 56
michael@0 57
michael@0 58 /** The port to use. */
michael@0 59 protected final int port;
michael@0 60
michael@0 61 /** The scheme (lowercased) */
michael@0 62 protected final String schemeName;
michael@0 63
michael@0 64
michael@0 65 /**
michael@0 66 * Creates a new {@link HttpHost HttpHost}, specifying all values.
michael@0 67 * Constructor for HttpHost.
michael@0 68 *
michael@0 69 * @param hostname the hostname (IP or DNS name)
michael@0 70 * @param port the port number.
michael@0 71 * <code>-1</code> indicates the scheme default port.
michael@0 72 * @param scheme the name of the scheme.
michael@0 73 * <code>null</code> indicates the
michael@0 74 * {@link #DEFAULT_SCHEME_NAME default scheme}
michael@0 75 */
michael@0 76 public HttpHost(final String hostname, int port, final String scheme) {
michael@0 77 super();
michael@0 78 if (hostname == null) {
michael@0 79 throw new IllegalArgumentException("Host name may not be null");
michael@0 80 }
michael@0 81 this.hostname = hostname;
michael@0 82 this.lcHostname = hostname.toLowerCase(Locale.ENGLISH);
michael@0 83 if (scheme != null) {
michael@0 84 this.schemeName = scheme.toLowerCase(Locale.ENGLISH);
michael@0 85 } else {
michael@0 86 this.schemeName = DEFAULT_SCHEME_NAME;
michael@0 87 }
michael@0 88 this.port = port;
michael@0 89 }
michael@0 90
michael@0 91 /**
michael@0 92 * Creates a new {@link HttpHost HttpHost}, with default scheme.
michael@0 93 *
michael@0 94 * @param hostname the hostname (IP or DNS name)
michael@0 95 * @param port the port number.
michael@0 96 * <code>-1</code> indicates the scheme default port.
michael@0 97 */
michael@0 98 public HttpHost(final String hostname, int port) {
michael@0 99 this(hostname, port, null);
michael@0 100 }
michael@0 101
michael@0 102 /**
michael@0 103 * Creates a new {@link HttpHost HttpHost}, with default scheme and port.
michael@0 104 *
michael@0 105 * @param hostname the hostname (IP or DNS name)
michael@0 106 */
michael@0 107 public HttpHost(final String hostname) {
michael@0 108 this(hostname, -1, null);
michael@0 109 }
michael@0 110
michael@0 111 /**
michael@0 112 * Copy constructor for {@link HttpHost HttpHost}.
michael@0 113 *
michael@0 114 * @param httphost the HTTP host to copy details from
michael@0 115 */
michael@0 116 public HttpHost (final HttpHost httphost) {
michael@0 117 this(httphost.hostname, httphost.port, httphost.schemeName);
michael@0 118 }
michael@0 119
michael@0 120 /**
michael@0 121 * Returns the host name.
michael@0 122 *
michael@0 123 * @return the host name (IP or DNS name)
michael@0 124 */
michael@0 125 public String getHostName() {
michael@0 126 return this.hostname;
michael@0 127 }
michael@0 128
michael@0 129 /**
michael@0 130 * Returns the port.
michael@0 131 *
michael@0 132 * @return the host port, or <code>-1</code> if not set
michael@0 133 */
michael@0 134 public int getPort() {
michael@0 135 return this.port;
michael@0 136 }
michael@0 137
michael@0 138 /**
michael@0 139 * Returns the scheme name.
michael@0 140 *
michael@0 141 * @return the scheme name
michael@0 142 */
michael@0 143 public String getSchemeName() {
michael@0 144 return this.schemeName;
michael@0 145 }
michael@0 146
michael@0 147 /**
michael@0 148 * Return the host URI, as a string.
michael@0 149 *
michael@0 150 * @return the host URI
michael@0 151 */
michael@0 152 public String toURI() {
michael@0 153 CharArrayBuffer buffer = new CharArrayBuffer(32);
michael@0 154 buffer.append(this.schemeName);
michael@0 155 buffer.append("://");
michael@0 156 buffer.append(this.hostname);
michael@0 157 if (this.port != -1) {
michael@0 158 buffer.append(':');
michael@0 159 buffer.append(Integer.toString(this.port));
michael@0 160 }
michael@0 161 return buffer.toString();
michael@0 162 }
michael@0 163
michael@0 164
michael@0 165 /**
michael@0 166 * Obtains the host string, without scheme prefix.
michael@0 167 *
michael@0 168 * @return the host string, for example <code>localhost:8080</code>
michael@0 169 */
michael@0 170 public String toHostString() {
michael@0 171 if (this.port != -1) {
michael@0 172 //the highest port number is 65535, which is length 6 with the addition of the colon
michael@0 173 CharArrayBuffer buffer = new CharArrayBuffer(this.hostname.length() + 6);
michael@0 174 buffer.append(this.hostname);
michael@0 175 buffer.append(":");
michael@0 176 buffer.append(Integer.toString(this.port));
michael@0 177 return buffer.toString();
michael@0 178 } else {
michael@0 179 return this.hostname;
michael@0 180 }
michael@0 181 }
michael@0 182
michael@0 183
michael@0 184 public String toString() {
michael@0 185 return toURI();
michael@0 186 }
michael@0 187
michael@0 188
michael@0 189 public boolean equals(final Object obj) {
michael@0 190 if (this == obj) return true;
michael@0 191 if (obj instanceof HttpHost) {
michael@0 192 HttpHost that = (HttpHost) obj;
michael@0 193 return this.lcHostname.equals(that.lcHostname)
michael@0 194 && this.port == that.port
michael@0 195 && this.schemeName.equals(that.schemeName);
michael@0 196 } else {
michael@0 197 return false;
michael@0 198 }
michael@0 199 }
michael@0 200
michael@0 201 /**
michael@0 202 * @see java.lang.Object#hashCode()
michael@0 203 */
michael@0 204 public int hashCode() {
michael@0 205 int hash = LangUtils.HASH_SEED;
michael@0 206 hash = LangUtils.hashCode(hash, this.lcHostname);
michael@0 207 hash = LangUtils.hashCode(hash, this.port);
michael@0 208 hash = LangUtils.hashCode(hash, this.schemeName);
michael@0 209 return hash;
michael@0 210 }
michael@0 211
michael@0 212 public Object clone() throws CloneNotSupportedException {
michael@0 213 return super.clone();
michael@0 214 }
michael@0 215
michael@0 216 }

mercurial