1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/HttpHost.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,216 @@ 1.4 +/* 1.5 + * ==================================================================== 1.6 + * Licensed to the Apache Software Foundation (ASF) under one 1.7 + * or more contributor license agreements. See the NOTICE file 1.8 + * distributed with this work for additional information 1.9 + * regarding copyright ownership. The ASF licenses this file 1.10 + * to you under the Apache License, Version 2.0 (the 1.11 + * "License"); you may not use this file except in compliance 1.12 + * with the License. You may obtain a copy of the License at 1.13 + * 1.14 + * http://www.apache.org/licenses/LICENSE-2.0 1.15 + * 1.16 + * Unless required by applicable law or agreed to in writing, 1.17 + * software distributed under the License is distributed on an 1.18 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1.19 + * KIND, either express or implied. See the License for the 1.20 + * specific language governing permissions and limitations 1.21 + * under the License. 1.22 + * ==================================================================== 1.23 + * 1.24 + * This software consists of voluntary contributions made by many 1.25 + * individuals on behalf of the Apache Software Foundation. For more 1.26 + * information on the Apache Software Foundation, please see 1.27 + * <http://www.apache.org/>. 1.28 + * 1.29 + */ 1.30 + 1.31 +package ch.boye.httpclientandroidlib; 1.32 + 1.33 +import java.io.Serializable; 1.34 +import java.util.Locale; 1.35 + 1.36 +import ch.boye.httpclientandroidlib.util.CharArrayBuffer; 1.37 +import ch.boye.httpclientandroidlib.util.LangUtils; 1.38 + 1.39 +/** 1.40 + * Holds all of the variables needed to describe an HTTP connection to a host. 1.41 + * This includes remote host name, port and scheme. 1.42 + * 1.43 + * 1.44 + * @since 4.0 1.45 + */ 1.46 +//@Immutable 1.47 +public final class HttpHost implements Cloneable, Serializable { 1.48 + 1.49 + private static final long serialVersionUID = -7529410654042457626L; 1.50 + 1.51 + /** The default scheme is "http". */ 1.52 + public static final String DEFAULT_SCHEME_NAME = "http"; 1.53 + 1.54 + /** The host to use. */ 1.55 + protected final String hostname; 1.56 + 1.57 + /** The lowercase host, for {@link #equals} and {@link #hashCode}. */ 1.58 + protected final String lcHostname; 1.59 + 1.60 + 1.61 + /** The port to use. */ 1.62 + protected final int port; 1.63 + 1.64 + /** The scheme (lowercased) */ 1.65 + protected final String schemeName; 1.66 + 1.67 + 1.68 + /** 1.69 + * Creates a new {@link HttpHost HttpHost}, specifying all values. 1.70 + * Constructor for HttpHost. 1.71 + * 1.72 + * @param hostname the hostname (IP or DNS name) 1.73 + * @param port the port number. 1.74 + * <code>-1</code> indicates the scheme default port. 1.75 + * @param scheme the name of the scheme. 1.76 + * <code>null</code> indicates the 1.77 + * {@link #DEFAULT_SCHEME_NAME default scheme} 1.78 + */ 1.79 + public HttpHost(final String hostname, int port, final String scheme) { 1.80 + super(); 1.81 + if (hostname == null) { 1.82 + throw new IllegalArgumentException("Host name may not be null"); 1.83 + } 1.84 + this.hostname = hostname; 1.85 + this.lcHostname = hostname.toLowerCase(Locale.ENGLISH); 1.86 + if (scheme != null) { 1.87 + this.schemeName = scheme.toLowerCase(Locale.ENGLISH); 1.88 + } else { 1.89 + this.schemeName = DEFAULT_SCHEME_NAME; 1.90 + } 1.91 + this.port = port; 1.92 + } 1.93 + 1.94 + /** 1.95 + * Creates a new {@link HttpHost HttpHost}, with default scheme. 1.96 + * 1.97 + * @param hostname the hostname (IP or DNS name) 1.98 + * @param port the port number. 1.99 + * <code>-1</code> indicates the scheme default port. 1.100 + */ 1.101 + public HttpHost(final String hostname, int port) { 1.102 + this(hostname, port, null); 1.103 + } 1.104 + 1.105 + /** 1.106 + * Creates a new {@link HttpHost HttpHost}, with default scheme and port. 1.107 + * 1.108 + * @param hostname the hostname (IP or DNS name) 1.109 + */ 1.110 + public HttpHost(final String hostname) { 1.111 + this(hostname, -1, null); 1.112 + } 1.113 + 1.114 + /** 1.115 + * Copy constructor for {@link HttpHost HttpHost}. 1.116 + * 1.117 + * @param httphost the HTTP host to copy details from 1.118 + */ 1.119 + public HttpHost (final HttpHost httphost) { 1.120 + this(httphost.hostname, httphost.port, httphost.schemeName); 1.121 + } 1.122 + 1.123 + /** 1.124 + * Returns the host name. 1.125 + * 1.126 + * @return the host name (IP or DNS name) 1.127 + */ 1.128 + public String getHostName() { 1.129 + return this.hostname; 1.130 + } 1.131 + 1.132 + /** 1.133 + * Returns the port. 1.134 + * 1.135 + * @return the host port, or <code>-1</code> if not set 1.136 + */ 1.137 + public int getPort() { 1.138 + return this.port; 1.139 + } 1.140 + 1.141 + /** 1.142 + * Returns the scheme name. 1.143 + * 1.144 + * @return the scheme name 1.145 + */ 1.146 + public String getSchemeName() { 1.147 + return this.schemeName; 1.148 + } 1.149 + 1.150 + /** 1.151 + * Return the host URI, as a string. 1.152 + * 1.153 + * @return the host URI 1.154 + */ 1.155 + public String toURI() { 1.156 + CharArrayBuffer buffer = new CharArrayBuffer(32); 1.157 + buffer.append(this.schemeName); 1.158 + buffer.append("://"); 1.159 + buffer.append(this.hostname); 1.160 + if (this.port != -1) { 1.161 + buffer.append(':'); 1.162 + buffer.append(Integer.toString(this.port)); 1.163 + } 1.164 + return buffer.toString(); 1.165 + } 1.166 + 1.167 + 1.168 + /** 1.169 + * Obtains the host string, without scheme prefix. 1.170 + * 1.171 + * @return the host string, for example <code>localhost:8080</code> 1.172 + */ 1.173 + public String toHostString() { 1.174 + if (this.port != -1) { 1.175 + //the highest port number is 65535, which is length 6 with the addition of the colon 1.176 + CharArrayBuffer buffer = new CharArrayBuffer(this.hostname.length() + 6); 1.177 + buffer.append(this.hostname); 1.178 + buffer.append(":"); 1.179 + buffer.append(Integer.toString(this.port)); 1.180 + return buffer.toString(); 1.181 + } else { 1.182 + return this.hostname; 1.183 + } 1.184 + } 1.185 + 1.186 + 1.187 + public String toString() { 1.188 + return toURI(); 1.189 + } 1.190 + 1.191 + 1.192 + public boolean equals(final Object obj) { 1.193 + if (this == obj) return true; 1.194 + if (obj instanceof HttpHost) { 1.195 + HttpHost that = (HttpHost) obj; 1.196 + return this.lcHostname.equals(that.lcHostname) 1.197 + && this.port == that.port 1.198 + && this.schemeName.equals(that.schemeName); 1.199 + } else { 1.200 + return false; 1.201 + } 1.202 + } 1.203 + 1.204 + /** 1.205 + * @see java.lang.Object#hashCode() 1.206 + */ 1.207 + public int hashCode() { 1.208 + int hash = LangUtils.HASH_SEED; 1.209 + hash = LangUtils.hashCode(hash, this.lcHostname); 1.210 + hash = LangUtils.hashCode(hash, this.port); 1.211 + hash = LangUtils.hashCode(hash, this.schemeName); 1.212 + return hash; 1.213 + } 1.214 + 1.215 + public Object clone() throws CloneNotSupportedException { 1.216 + return super.clone(); 1.217 + } 1.218 + 1.219 +}