Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* |
michael@0 | 2 | * ==================================================================== |
michael@0 | 3 | * Licensed to the Apache Software Foundation (ASF) under one |
michael@0 | 4 | * or more contributor license agreements. See the NOTICE file |
michael@0 | 5 | * distributed with this work for additional information |
michael@0 | 6 | * regarding copyright ownership. The ASF licenses this file |
michael@0 | 7 | * to you under the Apache License, Version 2.0 (the |
michael@0 | 8 | * "License"); you may not use this file except in compliance |
michael@0 | 9 | * with the License. You may obtain a copy of the License at |
michael@0 | 10 | * |
michael@0 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 12 | * |
michael@0 | 13 | * Unless required by applicable law or agreed to in writing, |
michael@0 | 14 | * software distributed under the License is distributed on an |
michael@0 | 15 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
michael@0 | 16 | * KIND, either express or implied. See the License for the |
michael@0 | 17 | * specific language governing permissions and limitations |
michael@0 | 18 | * under the License. |
michael@0 | 19 | * ==================================================================== |
michael@0 | 20 | * |
michael@0 | 21 | * This software consists of voluntary contributions made by many |
michael@0 | 22 | * individuals on behalf of the Apache Software Foundation. For more |
michael@0 | 23 | * information on the Apache Software Foundation, please see |
michael@0 | 24 | * <http://www.apache.org/>. |
michael@0 | 25 | * |
michael@0 | 26 | */ |
michael@0 | 27 | |
michael@0 | 28 | package ch.boye.httpclientandroidlib.conn; |
michael@0 | 29 | |
michael@0 | 30 | import java.io.IOException; |
michael@0 | 31 | import java.net.InetAddress; |
michael@0 | 32 | import java.net.InetSocketAddress; |
michael@0 | 33 | import java.net.Socket; |
michael@0 | 34 | import java.net.SocketTimeoutException; |
michael@0 | 35 | import java.util.ArrayList; |
michael@0 | 36 | import java.util.Collections; |
michael@0 | 37 | import java.util.List; |
michael@0 | 38 | import java.util.Arrays; |
michael@0 | 39 | |
michael@0 | 40 | import ch.boye.httpclientandroidlib.annotation.Immutable; |
michael@0 | 41 | |
michael@0 | 42 | import ch.boye.httpclientandroidlib.conn.scheme.SchemeSocketFactory; |
michael@0 | 43 | import ch.boye.httpclientandroidlib.conn.scheme.SocketFactory; |
michael@0 | 44 | import ch.boye.httpclientandroidlib.params.HttpConnectionParams; |
michael@0 | 45 | import ch.boye.httpclientandroidlib.params.HttpParams; |
michael@0 | 46 | |
michael@0 | 47 | /** |
michael@0 | 48 | * Socket factory that implements a simple multi-home fail-over on connect failure, |
michael@0 | 49 | * provided the same hostname resolves to multiple {@link InetAddress}es. Please note |
michael@0 | 50 | * the {@link #connectSocket(Socket, String, int, InetAddress, int, HttpParams)} |
michael@0 | 51 | * method cannot be reliably interrupted by closing the socket returned by the |
michael@0 | 52 | * {@link #createSocket()} method. |
michael@0 | 53 | * |
michael@0 | 54 | * @since 4.0 |
michael@0 | 55 | * |
michael@0 | 56 | * @deprecated Do not use. For multihome support socket factories must implement |
michael@0 | 57 | * {@link SchemeSocketFactory} interface. |
michael@0 | 58 | */ |
michael@0 | 59 | @Deprecated |
michael@0 | 60 | @Immutable |
michael@0 | 61 | public final class MultihomePlainSocketFactory implements SocketFactory { |
michael@0 | 62 | |
michael@0 | 63 | /** |
michael@0 | 64 | * The factory singleton. |
michael@0 | 65 | */ |
michael@0 | 66 | private static final |
michael@0 | 67 | MultihomePlainSocketFactory DEFAULT_FACTORY = new MultihomePlainSocketFactory(); |
michael@0 | 68 | |
michael@0 | 69 | /** |
michael@0 | 70 | * Gets the singleton instance of this class. |
michael@0 | 71 | * @return the one and only plain socket factory |
michael@0 | 72 | */ |
michael@0 | 73 | public static MultihomePlainSocketFactory getSocketFactory() { |
michael@0 | 74 | return DEFAULT_FACTORY; |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | /** |
michael@0 | 78 | * Restricted default constructor. |
michael@0 | 79 | */ |
michael@0 | 80 | private MultihomePlainSocketFactory() { |
michael@0 | 81 | super(); |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | |
michael@0 | 85 | // non-javadoc, see interface ch.boye.httpclientandroidlib.conn.SocketFactory |
michael@0 | 86 | public Socket createSocket() { |
michael@0 | 87 | return new Socket(); |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | /** |
michael@0 | 91 | * Attempts to connects the socket to any of the {@link InetAddress}es the |
michael@0 | 92 | * given host name resolves to. If connection to all addresses fail, the |
michael@0 | 93 | * last I/O exception is propagated to the caller. |
michael@0 | 94 | * |
michael@0 | 95 | * @param sock socket to connect to any of the given addresses |
michael@0 | 96 | * @param host Host name to connect to |
michael@0 | 97 | * @param port the port to connect to |
michael@0 | 98 | * @param localAddress local address |
michael@0 | 99 | * @param localPort local port |
michael@0 | 100 | * @param params HTTP parameters |
michael@0 | 101 | * |
michael@0 | 102 | * @throws IOException if an error occurs during the connection |
michael@0 | 103 | * @throws SocketTimeoutException if timeout expires before connecting |
michael@0 | 104 | */ |
michael@0 | 105 | public Socket connectSocket(Socket sock, String host, int port, |
michael@0 | 106 | InetAddress localAddress, int localPort, |
michael@0 | 107 | HttpParams params) |
michael@0 | 108 | throws IOException { |
michael@0 | 109 | |
michael@0 | 110 | if (host == null) { |
michael@0 | 111 | throw new IllegalArgumentException("Target host may not be null."); |
michael@0 | 112 | } |
michael@0 | 113 | if (params == null) { |
michael@0 | 114 | throw new IllegalArgumentException("Parameters may not be null."); |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | if (sock == null) |
michael@0 | 118 | sock = createSocket(); |
michael@0 | 119 | |
michael@0 | 120 | if ((localAddress != null) || (localPort > 0)) { |
michael@0 | 121 | |
michael@0 | 122 | // we need to bind explicitly |
michael@0 | 123 | if (localPort < 0) |
michael@0 | 124 | localPort = 0; // indicates "any" |
michael@0 | 125 | |
michael@0 | 126 | InetSocketAddress isa = |
michael@0 | 127 | new InetSocketAddress(localAddress, localPort); |
michael@0 | 128 | sock.bind(isa); |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | int timeout = HttpConnectionParams.getConnectionTimeout(params); |
michael@0 | 132 | |
michael@0 | 133 | InetAddress[] inetadrs = InetAddress.getAllByName(host); |
michael@0 | 134 | List<InetAddress> addresses = new ArrayList<InetAddress>(inetadrs.length); |
michael@0 | 135 | addresses.addAll(Arrays.asList(inetadrs)); |
michael@0 | 136 | Collections.shuffle(addresses); |
michael@0 | 137 | |
michael@0 | 138 | IOException lastEx = null; |
michael@0 | 139 | for (InetAddress remoteAddress: addresses) { |
michael@0 | 140 | try { |
michael@0 | 141 | sock.connect(new InetSocketAddress(remoteAddress, port), timeout); |
michael@0 | 142 | break; |
michael@0 | 143 | } catch (SocketTimeoutException ex) { |
michael@0 | 144 | throw new ConnectTimeoutException("Connect to " + remoteAddress + " timed out"); |
michael@0 | 145 | } catch (IOException ex) { |
michael@0 | 146 | // create new socket |
michael@0 | 147 | sock = new Socket(); |
michael@0 | 148 | // keep the last exception and retry |
michael@0 | 149 | lastEx = ex; |
michael@0 | 150 | } |
michael@0 | 151 | } |
michael@0 | 152 | if (lastEx != null) { |
michael@0 | 153 | throw lastEx; |
michael@0 | 154 | } |
michael@0 | 155 | return sock; |
michael@0 | 156 | } // connectSocket |
michael@0 | 157 | |
michael@0 | 158 | |
michael@0 | 159 | /** |
michael@0 | 160 | * Checks whether a socket connection is secure. |
michael@0 | 161 | * This factory creates plain socket connections |
michael@0 | 162 | * which are not considered secure. |
michael@0 | 163 | * |
michael@0 | 164 | * @param sock the connected socket |
michael@0 | 165 | * |
michael@0 | 166 | * @return <code>false</code> |
michael@0 | 167 | * |
michael@0 | 168 | * @throws IllegalArgumentException if the argument is invalid |
michael@0 | 169 | */ |
michael@0 | 170 | public final boolean isSecure(Socket sock) |
michael@0 | 171 | throws IllegalArgumentException { |
michael@0 | 172 | |
michael@0 | 173 | if (sock == null) { |
michael@0 | 174 | throw new IllegalArgumentException("Socket may not be null."); |
michael@0 | 175 | } |
michael@0 | 176 | // This class check assumes that createSocket() calls the constructor |
michael@0 | 177 | // directly. If it was using javax.net.SocketFactory, we couldn't make |
michael@0 | 178 | // an assumption about the socket class here. |
michael@0 | 179 | if (sock.getClass() != Socket.class) { |
michael@0 | 180 | throw new IllegalArgumentException |
michael@0 | 181 | ("Socket not created by this factory."); |
michael@0 | 182 | } |
michael@0 | 183 | // This check is performed last since it calls a method implemented |
michael@0 | 184 | // by the argument object. getClass() is final in java.lang.Object. |
michael@0 | 185 | if (sock.isClosed()) { |
michael@0 | 186 | throw new IllegalArgumentException("Socket is closed."); |
michael@0 | 187 | } |
michael@0 | 188 | |
michael@0 | 189 | return false; |
michael@0 | 190 | |
michael@0 | 191 | } // isSecure |
michael@0 | 192 | |
michael@0 | 193 | } |