mobile/android/thirdparty/ch/boye/httpclientandroidlib/conn/scheme/PlainSocketFactory.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.

     1 /*
     2  * ====================================================================
     3  * Licensed to the Apache Software Foundation (ASF) under one
     4  * or more contributor license agreements.  See the NOTICE file
     5  * distributed with this work for additional information
     6  * regarding copyright ownership.  The ASF licenses this file
     7  * to you under the Apache License, Version 2.0 (the
     8  * "License"); you may not use this file except in compliance
     9  * with the License.  You may obtain a copy of the License at
    10  *
    11  *   http://www.apache.org/licenses/LICENSE-2.0
    12  *
    13  * Unless required by applicable law or agreed to in writing,
    14  * software distributed under the License is distributed on an
    15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    16  * KIND, either express or implied.  See the License for the
    17  * specific language governing permissions and limitations
    18  * under the License.
    19  * ====================================================================
    20  *
    21  * This software consists of voluntary contributions made by many
    22  * individuals on behalf of the Apache Software Foundation.  For more
    23  * information on the Apache Software Foundation, please see
    24  * <http://www.apache.org/>.
    25  *
    26  */
    28 package ch.boye.httpclientandroidlib.conn.scheme;
    30 import java.io.IOException;
    31 import java.net.InetAddress;
    32 import java.net.InetSocketAddress;
    33 import java.net.Socket;
    34 import java.net.SocketTimeoutException;
    35 import java.net.UnknownHostException;
    37 import ch.boye.httpclientandroidlib.annotation.Immutable;
    39 import ch.boye.httpclientandroidlib.conn.ConnectTimeoutException;
    40 import ch.boye.httpclientandroidlib.params.HttpConnectionParams;
    41 import ch.boye.httpclientandroidlib.params.HttpParams;
    43 /**
    44  * The default class for creating plain (unencrypted) sockets.
    45  * <p>
    46  * The following parameters can be used to customize the behavior of this
    47  * class:
    48  * <ul>
    49  *  <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#CONNECTION_TIMEOUT}</li>
    50  *  <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#SO_REUSEADDR}</li>
    51  * </ul>
    52  *
    53  * @since 4.0
    54  */
    55 @SuppressWarnings("deprecation")
    56 @Immutable
    57 public class PlainSocketFactory implements SocketFactory, SchemeSocketFactory {
    59     private final HostNameResolver nameResolver;
    61     /**
    62      * Gets the default factory.
    63      *
    64      * @return the default factory
    65      */
    66     public static PlainSocketFactory getSocketFactory() {
    67         return new PlainSocketFactory();
    68     }
    70     @Deprecated
    71     public PlainSocketFactory(final HostNameResolver nameResolver) {
    72         super();
    73         this.nameResolver = nameResolver;
    74     }
    76     public PlainSocketFactory() {
    77         super();
    78         this.nameResolver = null;
    79     }
    81     /**
    82      * @param params Optional parameters. Parameters passed to this method will have no effect.
    83      *               This method will create a unconnected instance of {@link Socket} class
    84      *               using default constructor.
    85      *
    86      * @since 4.1
    87      */
    88     public Socket createSocket(final HttpParams params) {
    89         return new Socket();
    90     }
    92     public Socket createSocket() {
    93         return new Socket();
    94     }
    96     /**
    97      * @since 4.1
    98      */
    99     public Socket connectSocket(
   100             final Socket socket,
   101             final InetSocketAddress remoteAddress,
   102             final InetSocketAddress localAddress,
   103             final HttpParams params) throws IOException, ConnectTimeoutException {
   104         if (remoteAddress == null) {
   105             throw new IllegalArgumentException("Remote address may not be null");
   106         }
   107         if (params == null) {
   108             throw new IllegalArgumentException("HTTP parameters may not be null");
   109         }
   110         Socket sock = socket;
   111         if (sock == null) {
   112             sock = createSocket();
   113         }
   114         if (localAddress != null) {
   115             sock.setReuseAddress(HttpConnectionParams.getSoReuseaddr(params));
   116             sock.bind(localAddress);
   117         }
   118         int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
   119         int soTimeout = HttpConnectionParams.getSoTimeout(params);
   121         try {
   122             sock.setSoTimeout(soTimeout);
   123             sock.connect(remoteAddress, connTimeout);
   124         } catch (SocketTimeoutException ex) {
   125             throw new ConnectTimeoutException("Connect to " + remoteAddress + " timed out");
   126         }
   127         return sock;
   128     }
   130     /**
   131      * Checks whether a socket connection is secure.
   132      * This factory creates plain socket connections
   133      * which are not considered secure.
   134      *
   135      * @param sock      the connected socket
   136      *
   137      * @return  <code>false</code>
   138      *
   139      * @throws IllegalArgumentException if the argument is invalid
   140      */
   141     public final boolean isSecure(Socket sock)
   142         throws IllegalArgumentException {
   144         if (sock == null) {
   145             throw new IllegalArgumentException("Socket may not be null.");
   146         }
   147         // This check is performed last since it calls a method implemented
   148         // by the argument object. getClass() is final in java.lang.Object.
   149         if (sock.isClosed()) {
   150             throw new IllegalArgumentException("Socket is closed.");
   151         }
   152         return false;
   153     }
   155     /**
   156      * @deprecated Use {@link #connectSocket(Socket, InetSocketAddress, InetSocketAddress, HttpParams)}
   157      */
   158     @Deprecated
   159     public Socket connectSocket(
   160             final Socket socket,
   161             final String host, int port,
   162             final InetAddress localAddress, int localPort,
   163             final HttpParams params) throws IOException, UnknownHostException, ConnectTimeoutException {
   164         InetSocketAddress local = null;
   165         if (localAddress != null || localPort > 0) {
   166             // we need to bind explicitly
   167             if (localPort < 0) {
   168                 localPort = 0; // indicates "any"
   169             }
   170             local = new InetSocketAddress(localAddress, localPort);
   171         }
   172         InetAddress remoteAddress;
   173         if (this.nameResolver != null) {
   174             remoteAddress = this.nameResolver.resolve(host);
   175         } else {
   176             remoteAddress = InetAddress.getByName(host);
   177         }
   178         InetSocketAddress remote = new InetSocketAddress(remoteAddress, port);
   179         return connectSocket(socket, remote, local, params);
   180     }
   182 }

mercurial