1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/conn/scheme/Scheme.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,260 @@ 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 +package ch.boye.httpclientandroidlib.conn.scheme; 1.31 + 1.32 +import java.util.Locale; 1.33 + 1.34 +import ch.boye.httpclientandroidlib.annotation.Immutable; 1.35 + 1.36 +import ch.boye.httpclientandroidlib.util.LangUtils; 1.37 + 1.38 +/** 1.39 + * Encapsulates specifics of a protocol scheme such as "http" or "https". Schemes are identified 1.40 + * by lowercase names. Supported schemes are typically collected in a {@link SchemeRegistry 1.41 + * SchemeRegistry}. 1.42 + * <p> 1.43 + * For example, to configure support for "https://" URLs, you could write code like the following: 1.44 + * <pre> 1.45 + * Scheme https = new Scheme("https", 443, new MySecureSocketFactory()); 1.46 + * SchemeRegistry.DEFAULT.register(https); 1.47 + * </pre> 1.48 + * 1.49 + * @since 4.0 1.50 + */ 1.51 +@Immutable 1.52 +public final class Scheme { 1.53 + 1.54 + /** The name of this scheme, in lowercase. (e.g. http, https) */ 1.55 + private final String name; 1.56 + 1.57 + /** The socket factory for this scheme */ 1.58 + private final SchemeSocketFactory socketFactory; 1.59 + 1.60 + /** The default port for this scheme */ 1.61 + private final int defaultPort; 1.62 + 1.63 + /** Indicates whether this scheme allows for layered connections */ 1.64 + private final boolean layered; 1.65 + 1.66 + /** A string representation, for {@link #toString toString}. */ 1.67 + private String stringRep; 1.68 + /* 1.69 + * This is used to cache the result of the toString() method 1.70 + * Since the method always generates the same value, there's no 1.71 + * need to synchronize, and it does not affect immutability. 1.72 + */ 1.73 + 1.74 + /** 1.75 + * Creates a new scheme. 1.76 + * Whether the created scheme allows for layered connections 1.77 + * depends on the class of <code>factory</code>. 1.78 + * 1.79 + * @param name the scheme name, for example "http". 1.80 + * The name will be converted to lowercase. 1.81 + * @param port the default port for this scheme 1.82 + * @param factory the factory for creating sockets for communication 1.83 + * with this scheme 1.84 + * 1.85 + * @since 4.1 1.86 + */ 1.87 + public Scheme(final String name, final int port, final SchemeSocketFactory factory) { 1.88 + if (name == null) { 1.89 + throw new IllegalArgumentException("Scheme name may not be null"); 1.90 + } 1.91 + if ((port <= 0) || (port > 0xffff)) { 1.92 + throw new IllegalArgumentException("Port is invalid: " + port); 1.93 + } 1.94 + if (factory == null) { 1.95 + throw new IllegalArgumentException("Socket factory may not be null"); 1.96 + } 1.97 + this.name = name.toLowerCase(Locale.ENGLISH); 1.98 + this.socketFactory = factory; 1.99 + this.defaultPort = port; 1.100 + this.layered = factory instanceof LayeredSchemeSocketFactory; 1.101 + } 1.102 + 1.103 + /** 1.104 + * Creates a new scheme. 1.105 + * Whether the created scheme allows for layered connections 1.106 + * depends on the class of <code>factory</code>. 1.107 + * 1.108 + * @param name the scheme name, for example "http". 1.109 + * The name will be converted to lowercase. 1.110 + * @param factory the factory for creating sockets for communication 1.111 + * with this scheme 1.112 + * @param port the default port for this scheme 1.113 + * 1.114 + * @deprecated Use {@link #Scheme(String, int, SchemeSocketFactory)} 1.115 + */ 1.116 + @Deprecated 1.117 + public Scheme(final String name, 1.118 + final SocketFactory factory, 1.119 + final int port) { 1.120 + 1.121 + if (name == null) { 1.122 + throw new IllegalArgumentException 1.123 + ("Scheme name may not be null"); 1.124 + } 1.125 + if (factory == null) { 1.126 + throw new IllegalArgumentException 1.127 + ("Socket factory may not be null"); 1.128 + } 1.129 + if ((port <= 0) || (port > 0xffff)) { 1.130 + throw new IllegalArgumentException 1.131 + ("Port is invalid: " + port); 1.132 + } 1.133 + 1.134 + this.name = name.toLowerCase(Locale.ENGLISH); 1.135 + if (factory instanceof LayeredSocketFactory) { 1.136 + this.socketFactory = new LayeredSchemeSocketFactoryAdaptor( 1.137 + (LayeredSocketFactory) factory); 1.138 + this.layered = true; 1.139 + } else { 1.140 + this.socketFactory = new SchemeSocketFactoryAdaptor(factory); 1.141 + this.layered = false; 1.142 + } 1.143 + this.defaultPort = port; 1.144 + } 1.145 + 1.146 + /** 1.147 + * Obtains the default port. 1.148 + * 1.149 + * @return the default port for this scheme 1.150 + */ 1.151 + public final int getDefaultPort() { 1.152 + return defaultPort; 1.153 + } 1.154 + 1.155 + 1.156 + /** 1.157 + * Obtains the socket factory. 1.158 + * If this scheme is {@link #isLayered layered}, the factory implements 1.159 + * {@link LayeredSocketFactory LayeredSocketFactory}. 1.160 + * 1.161 + * @return the socket factory for this scheme 1.162 + * 1.163 + * @deprecated Use {@link #getSchemeSocketFactory()} 1.164 + */ 1.165 + @Deprecated 1.166 + public final SocketFactory getSocketFactory() { 1.167 + if (this.socketFactory instanceof SchemeSocketFactoryAdaptor) { 1.168 + return ((SchemeSocketFactoryAdaptor) this.socketFactory).getFactory(); 1.169 + } else { 1.170 + if (this.layered) { 1.171 + return new LayeredSocketFactoryAdaptor( 1.172 + (LayeredSchemeSocketFactory) this.socketFactory); 1.173 + } else { 1.174 + return new SocketFactoryAdaptor(this.socketFactory); 1.175 + } 1.176 + } 1.177 + } 1.178 + 1.179 + /** 1.180 + * Obtains the socket factory. 1.181 + * If this scheme is {@link #isLayered layered}, the factory implements 1.182 + * {@link LayeredSocketFactory LayeredSchemeSocketFactory}. 1.183 + * 1.184 + * @return the socket factory for this scheme 1.185 + * 1.186 + * @since 4.1 1.187 + */ 1.188 + public final SchemeSocketFactory getSchemeSocketFactory() { 1.189 + return this.socketFactory; 1.190 + } 1.191 + 1.192 + /** 1.193 + * Obtains the scheme name. 1.194 + * 1.195 + * @return the name of this scheme, in lowercase 1.196 + */ 1.197 + public final String getName() { 1.198 + return name; 1.199 + } 1.200 + 1.201 + /** 1.202 + * Indicates whether this scheme allows for layered connections. 1.203 + * 1.204 + * @return <code>true</code> if layered connections are possible, 1.205 + * <code>false</code> otherwise 1.206 + */ 1.207 + public final boolean isLayered() { 1.208 + return layered; 1.209 + } 1.210 + 1.211 + /** 1.212 + * Resolves the correct port for this scheme. 1.213 + * Returns the given port if it is valid, the default port otherwise. 1.214 + * 1.215 + * @param port the port to be resolved, 1.216 + * a negative number to obtain the default port 1.217 + * 1.218 + * @return the given port or the defaultPort 1.219 + */ 1.220 + public final int resolvePort(int port) { 1.221 + return port <= 0 ? defaultPort : port; 1.222 + } 1.223 + 1.224 + /** 1.225 + * Return a string representation of this object. 1.226 + * 1.227 + * @return a human-readable string description of this scheme 1.228 + */ 1.229 + @Override 1.230 + public final String toString() { 1.231 + if (stringRep == null) { 1.232 + StringBuilder buffer = new StringBuilder(); 1.233 + buffer.append(this.name); 1.234 + buffer.append(':'); 1.235 + buffer.append(Integer.toString(this.defaultPort)); 1.236 + stringRep = buffer.toString(); 1.237 + } 1.238 + return stringRep; 1.239 + } 1.240 + 1.241 + @Override 1.242 + public final boolean equals(Object obj) { 1.243 + if (this == obj) return true; 1.244 + if (obj instanceof Scheme) { 1.245 + Scheme that = (Scheme) obj; 1.246 + return this.name.equals(that.name) 1.247 + && this.defaultPort == that.defaultPort 1.248 + && this.layered == that.layered; 1.249 + } else { 1.250 + return false; 1.251 + } 1.252 + } 1.253 + 1.254 + @Override 1.255 + public int hashCode() { 1.256 + int hash = LangUtils.HASH_SEED; 1.257 + hash = LangUtils.hashCode(hash, this.defaultPort); 1.258 + hash = LangUtils.hashCode(hash, this.name); 1.259 + hash = LangUtils.hashCode(hash, this.layered); 1.260 + return hash; 1.261 + } 1.262 + 1.263 +}