1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/conn/scheme/PlainSocketFactory.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,182 @@ 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.conn.scheme; 1.32 + 1.33 +import java.io.IOException; 1.34 +import java.net.InetAddress; 1.35 +import java.net.InetSocketAddress; 1.36 +import java.net.Socket; 1.37 +import java.net.SocketTimeoutException; 1.38 +import java.net.UnknownHostException; 1.39 + 1.40 +import ch.boye.httpclientandroidlib.annotation.Immutable; 1.41 + 1.42 +import ch.boye.httpclientandroidlib.conn.ConnectTimeoutException; 1.43 +import ch.boye.httpclientandroidlib.params.HttpConnectionParams; 1.44 +import ch.boye.httpclientandroidlib.params.HttpParams; 1.45 + 1.46 +/** 1.47 + * The default class for creating plain (unencrypted) sockets. 1.48 + * <p> 1.49 + * The following parameters can be used to customize the behavior of this 1.50 + * class: 1.51 + * <ul> 1.52 + * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#CONNECTION_TIMEOUT}</li> 1.53 + * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#SO_REUSEADDR}</li> 1.54 + * </ul> 1.55 + * 1.56 + * @since 4.0 1.57 + */ 1.58 +@SuppressWarnings("deprecation") 1.59 +@Immutable 1.60 +public class PlainSocketFactory implements SocketFactory, SchemeSocketFactory { 1.61 + 1.62 + private final HostNameResolver nameResolver; 1.63 + 1.64 + /** 1.65 + * Gets the default factory. 1.66 + * 1.67 + * @return the default factory 1.68 + */ 1.69 + public static PlainSocketFactory getSocketFactory() { 1.70 + return new PlainSocketFactory(); 1.71 + } 1.72 + 1.73 + @Deprecated 1.74 + public PlainSocketFactory(final HostNameResolver nameResolver) { 1.75 + super(); 1.76 + this.nameResolver = nameResolver; 1.77 + } 1.78 + 1.79 + public PlainSocketFactory() { 1.80 + super(); 1.81 + this.nameResolver = null; 1.82 + } 1.83 + 1.84 + /** 1.85 + * @param params Optional parameters. Parameters passed to this method will have no effect. 1.86 + * This method will create a unconnected instance of {@link Socket} class 1.87 + * using default constructor. 1.88 + * 1.89 + * @since 4.1 1.90 + */ 1.91 + public Socket createSocket(final HttpParams params) { 1.92 + return new Socket(); 1.93 + } 1.94 + 1.95 + public Socket createSocket() { 1.96 + return new Socket(); 1.97 + } 1.98 + 1.99 + /** 1.100 + * @since 4.1 1.101 + */ 1.102 + public Socket connectSocket( 1.103 + final Socket socket, 1.104 + final InetSocketAddress remoteAddress, 1.105 + final InetSocketAddress localAddress, 1.106 + final HttpParams params) throws IOException, ConnectTimeoutException { 1.107 + if (remoteAddress == null) { 1.108 + throw new IllegalArgumentException("Remote address may not be null"); 1.109 + } 1.110 + if (params == null) { 1.111 + throw new IllegalArgumentException("HTTP parameters may not be null"); 1.112 + } 1.113 + Socket sock = socket; 1.114 + if (sock == null) { 1.115 + sock = createSocket(); 1.116 + } 1.117 + if (localAddress != null) { 1.118 + sock.setReuseAddress(HttpConnectionParams.getSoReuseaddr(params)); 1.119 + sock.bind(localAddress); 1.120 + } 1.121 + int connTimeout = HttpConnectionParams.getConnectionTimeout(params); 1.122 + int soTimeout = HttpConnectionParams.getSoTimeout(params); 1.123 + 1.124 + try { 1.125 + sock.setSoTimeout(soTimeout); 1.126 + sock.connect(remoteAddress, connTimeout); 1.127 + } catch (SocketTimeoutException ex) { 1.128 + throw new ConnectTimeoutException("Connect to " + remoteAddress + " timed out"); 1.129 + } 1.130 + return sock; 1.131 + } 1.132 + 1.133 + /** 1.134 + * Checks whether a socket connection is secure. 1.135 + * This factory creates plain socket connections 1.136 + * which are not considered secure. 1.137 + * 1.138 + * @param sock the connected socket 1.139 + * 1.140 + * @return <code>false</code> 1.141 + * 1.142 + * @throws IllegalArgumentException if the argument is invalid 1.143 + */ 1.144 + public final boolean isSecure(Socket sock) 1.145 + throws IllegalArgumentException { 1.146 + 1.147 + if (sock == null) { 1.148 + throw new IllegalArgumentException("Socket may not be null."); 1.149 + } 1.150 + // This check is performed last since it calls a method implemented 1.151 + // by the argument object. getClass() is final in java.lang.Object. 1.152 + if (sock.isClosed()) { 1.153 + throw new IllegalArgumentException("Socket is closed."); 1.154 + } 1.155 + return false; 1.156 + } 1.157 + 1.158 + /** 1.159 + * @deprecated Use {@link #connectSocket(Socket, InetSocketAddress, InetSocketAddress, HttpParams)} 1.160 + */ 1.161 + @Deprecated 1.162 + public Socket connectSocket( 1.163 + final Socket socket, 1.164 + final String host, int port, 1.165 + final InetAddress localAddress, int localPort, 1.166 + final HttpParams params) throws IOException, UnknownHostException, ConnectTimeoutException { 1.167 + InetSocketAddress local = null; 1.168 + if (localAddress != null || localPort > 0) { 1.169 + // we need to bind explicitly 1.170 + if (localPort < 0) { 1.171 + localPort = 0; // indicates "any" 1.172 + } 1.173 + local = new InetSocketAddress(localAddress, localPort); 1.174 + } 1.175 + InetAddress remoteAddress; 1.176 + if (this.nameResolver != null) { 1.177 + remoteAddress = this.nameResolver.resolve(host); 1.178 + } else { 1.179 + remoteAddress = InetAddress.getByName(host); 1.180 + } 1.181 + InetSocketAddress remote = new InetSocketAddress(remoteAddress, port); 1.182 + return connectSocket(socket, remote, local, params); 1.183 + } 1.184 + 1.185 +}