mobile/android/thirdparty/ch/boye/httpclientandroidlib/conn/MultihomePlainSocketFactory.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/conn/MultihomePlainSocketFactory.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,193 @@
     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;
    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.util.ArrayList;
    1.39 +import java.util.Collections;
    1.40 +import java.util.List;
    1.41 +import java.util.Arrays;
    1.42 +
    1.43 +import ch.boye.httpclientandroidlib.annotation.Immutable;
    1.44 +
    1.45 +import ch.boye.httpclientandroidlib.conn.scheme.SchemeSocketFactory;
    1.46 +import ch.boye.httpclientandroidlib.conn.scheme.SocketFactory;
    1.47 +import ch.boye.httpclientandroidlib.params.HttpConnectionParams;
    1.48 +import ch.boye.httpclientandroidlib.params.HttpParams;
    1.49 +
    1.50 +/**
    1.51 + * Socket factory that implements a simple multi-home fail-over on connect failure,
    1.52 + * provided the same hostname resolves to multiple {@link InetAddress}es. Please note
    1.53 + * the {@link #connectSocket(Socket, String, int, InetAddress, int, HttpParams)}
    1.54 + * method cannot be reliably interrupted by closing the socket returned by the
    1.55 + * {@link #createSocket()} method.
    1.56 + *
    1.57 + * @since 4.0
    1.58 + *
    1.59 + * @deprecated Do not use. For multihome support socket factories must implement
    1.60 + * {@link SchemeSocketFactory} interface.
    1.61 + */
    1.62 +@Deprecated
    1.63 +@Immutable
    1.64 +public final class MultihomePlainSocketFactory implements SocketFactory {
    1.65 +
    1.66 +    /**
    1.67 +     * The factory singleton.
    1.68 +     */
    1.69 +    private static final
    1.70 +    MultihomePlainSocketFactory DEFAULT_FACTORY = new MultihomePlainSocketFactory();
    1.71 +
    1.72 +    /**
    1.73 +     * Gets the singleton instance of this class.
    1.74 +     * @return the one and only plain socket factory
    1.75 +     */
    1.76 +    public static MultihomePlainSocketFactory getSocketFactory() {
    1.77 +        return DEFAULT_FACTORY;
    1.78 +    }
    1.79 +
    1.80 +    /**
    1.81 +     * Restricted default constructor.
    1.82 +     */
    1.83 +    private MultihomePlainSocketFactory() {
    1.84 +        super();
    1.85 +    }
    1.86 +
    1.87 +
    1.88 +    // non-javadoc, see interface ch.boye.httpclientandroidlib.conn.SocketFactory
    1.89 +    public Socket createSocket() {
    1.90 +        return new Socket();
    1.91 +    }
    1.92 +
    1.93 +    /**
    1.94 +     * Attempts to connects the socket to any of the {@link InetAddress}es the
    1.95 +     * given host name resolves to. If connection to all addresses fail, the
    1.96 +     * last I/O exception is propagated to the caller.
    1.97 +     *
    1.98 +     * @param sock socket to connect to any of the given addresses
    1.99 +     * @param host Host name to connect to
   1.100 +     * @param port the port to connect to
   1.101 +     * @param localAddress local address
   1.102 +     * @param localPort local port
   1.103 +     * @param params HTTP parameters
   1.104 +     *
   1.105 +     * @throws  IOException if an error occurs during the connection
   1.106 +     * @throws  SocketTimeoutException if timeout expires before connecting
   1.107 +     */
   1.108 +    public Socket connectSocket(Socket sock, String host, int port,
   1.109 +                                InetAddress localAddress, int localPort,
   1.110 +                                HttpParams params)
   1.111 +        throws IOException {
   1.112 +
   1.113 +        if (host == null) {
   1.114 +            throw new IllegalArgumentException("Target host may not be null.");
   1.115 +        }
   1.116 +        if (params == null) {
   1.117 +            throw new IllegalArgumentException("Parameters may not be null.");
   1.118 +        }
   1.119 +
   1.120 +        if (sock == null)
   1.121 +            sock = createSocket();
   1.122 +
   1.123 +        if ((localAddress != null) || (localPort > 0)) {
   1.124 +
   1.125 +            // we need to bind explicitly
   1.126 +            if (localPort < 0)
   1.127 +                localPort = 0; // indicates "any"
   1.128 +
   1.129 +            InetSocketAddress isa =
   1.130 +                new InetSocketAddress(localAddress, localPort);
   1.131 +            sock.bind(isa);
   1.132 +        }
   1.133 +
   1.134 +        int timeout = HttpConnectionParams.getConnectionTimeout(params);
   1.135 +
   1.136 +        InetAddress[] inetadrs = InetAddress.getAllByName(host);
   1.137 +        List<InetAddress> addresses = new ArrayList<InetAddress>(inetadrs.length);
   1.138 +        addresses.addAll(Arrays.asList(inetadrs));
   1.139 +        Collections.shuffle(addresses);
   1.140 +
   1.141 +        IOException lastEx = null;
   1.142 +        for (InetAddress remoteAddress: addresses) {
   1.143 +            try {
   1.144 +                sock.connect(new InetSocketAddress(remoteAddress, port), timeout);
   1.145 +                break;
   1.146 +            } catch (SocketTimeoutException ex) {
   1.147 +                throw new ConnectTimeoutException("Connect to " + remoteAddress + " timed out");
   1.148 +            } catch (IOException ex) {
   1.149 +                // create new socket
   1.150 +                sock = new Socket();
   1.151 +                // keep the last exception and retry
   1.152 +                lastEx = ex;
   1.153 +            }
   1.154 +        }
   1.155 +        if (lastEx != null) {
   1.156 +            throw lastEx;
   1.157 +        }
   1.158 +        return sock;
   1.159 +    } // connectSocket
   1.160 +
   1.161 +
   1.162 +    /**
   1.163 +     * Checks whether a socket connection is secure.
   1.164 +     * This factory creates plain socket connections
   1.165 +     * which are not considered secure.
   1.166 +     *
   1.167 +     * @param sock      the connected socket
   1.168 +     *
   1.169 +     * @return  <code>false</code>
   1.170 +     *
   1.171 +     * @throws IllegalArgumentException if the argument is invalid
   1.172 +     */
   1.173 +    public final boolean isSecure(Socket sock)
   1.174 +        throws IllegalArgumentException {
   1.175 +
   1.176 +        if (sock == null) {
   1.177 +            throw new IllegalArgumentException("Socket may not be null.");
   1.178 +        }
   1.179 +        // This class check assumes that createSocket() calls the constructor
   1.180 +        // directly. If it was using javax.net.SocketFactory, we couldn't make
   1.181 +        // an assumption about the socket class here.
   1.182 +        if (sock.getClass() != Socket.class) {
   1.183 +            throw new IllegalArgumentException
   1.184 +                ("Socket not created by this factory.");
   1.185 +        }
   1.186 +        // This check is performed last since it calls a method implemented
   1.187 +        // by the argument object. getClass() is final in java.lang.Object.
   1.188 +        if (sock.isClosed()) {
   1.189 +            throw new IllegalArgumentException("Socket is closed.");
   1.190 +        }
   1.191 +
   1.192 +        return false;
   1.193 +
   1.194 +    } // isSecure
   1.195 +
   1.196 +}

mercurial