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 */
27
28 package org.apache.http.conn;
29
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.util.ArrayList;
36 import java.util.Arrays;
37 import java.util.Collections;
38 import java.util.List;
39
40 import org.apache.http.annotation.Immutable;
41 import org.apache.http.conn.scheme.SchemeSocketFactory;
42 import org.apache.http.conn.scheme.SocketFactory;
43 import org.apache.http.params.HttpConnectionParams;
44 import org.apache.http.params.HttpParams;
45 import org.apache.http.util.Args;
46 import org.apache.http.util.Asserts;
47
48 /**
49 * Socket factory that implements a simple multi-home fail-over on connect failure,
50 * provided the same hostname resolves to multiple {@link InetAddress}es. Please note
51 * the {@link #connectSocket(Socket, String, int, InetAddress, int, HttpParams)}
52 * method cannot be reliably interrupted by closing the socket returned by the
53 * {@link #createSocket()} method.
54 *
55 * @since 4.0
56 *
57 * @deprecated (4.1) Do not use. For multihome support socket factories must implement
58 * {@link SchemeSocketFactory} interface.
59 */
60 @Deprecated
61 @Immutable
62 public final class MultihomePlainSocketFactory implements SocketFactory {
63
64 /**
65 * The factory singleton.
66 */
67 private static final
68 MultihomePlainSocketFactory DEFAULT_FACTORY = new MultihomePlainSocketFactory();
69
70 /**
71 * Gets the singleton instance of this class.
72 * @return the one and only plain socket factory
73 */
74 public static MultihomePlainSocketFactory getSocketFactory() {
75 return DEFAULT_FACTORY;
76 }
77
78 /**
79 * Restricted default constructor.
80 */
81 private MultihomePlainSocketFactory() {
82 super();
83 }
84
85
86 // non-javadoc, see interface org.apache.http.conn.SocketFactory
87 public Socket createSocket() {
88 return new Socket();
89 }
90
91 /**
92 * Attempts to connects the socket to any of the {@link InetAddress}es the
93 * given host name resolves to. If connection to all addresses fail, the
94 * last I/O exception is propagated to the caller.
95 *
96 * @param sock socket to connect to any of the given addresses
97 * @param host Host name to connect to
98 * @param port the port to connect to
99 * @param localAddress local address
100 * @param localPort local port
101 * @param params HTTP parameters
102 *
103 * @throws IOException if an error occurs during the connection
104 * @throws SocketTimeoutException if timeout expires before connecting
105 */
106 public Socket connectSocket(Socket sock, final String host, final int port,
107 final InetAddress localAddress, int localPort,
108 final HttpParams params)
109 throws IOException {
110 Args.notNull(host, "Target host");
111 Args.notNull(params, "HTTP parameters");
112
113 if (sock == null) {
114 sock = createSocket();
115 }
116
117 if ((localAddress != null) || (localPort > 0)) {
118
119 // we need to bind explicitly
120 if (localPort < 0)
121 {
122 localPort = 0; // indicates "any"
123 }
124
125 final InetSocketAddress isa =
126 new InetSocketAddress(localAddress, localPort);
127 sock.bind(isa);
128 }
129
130 final int timeout = HttpConnectionParams.getConnectionTimeout(params);
131
132 final InetAddress[] inetadrs = InetAddress.getAllByName(host);
133 final List<InetAddress> addresses = new ArrayList<InetAddress>(inetadrs.length);
134 addresses.addAll(Arrays.asList(inetadrs));
135 Collections.shuffle(addresses);
136
137 IOException lastEx = null;
138 for (final InetAddress remoteAddress: addresses) {
139 try {
140 sock.connect(new InetSocketAddress(remoteAddress, port), timeout);
141 break;
142 } catch (final SocketTimeoutException ex) {
143 throw new ConnectTimeoutException("Connect to " + remoteAddress + " timed out");
144 } catch (final IOException ex) {
145 // create new socket
146 sock = new Socket();
147 // keep the last exception and retry
148 lastEx = ex;
149 }
150 }
151 if (lastEx != null) {
152 throw lastEx;
153 }
154 return sock;
155 } // connectSocket
156
157
158 /**
159 * Checks whether a socket connection is secure.
160 * This factory creates plain socket connections
161 * which are not considered secure.
162 *
163 * @param sock the connected socket
164 *
165 * @return <code>false</code>
166 *
167 * @throws IllegalArgumentException if the argument is invalid
168 */
169 public final boolean isSecure(final Socket sock)
170 throws IllegalArgumentException {
171
172 Args.notNull(sock, "Socket");
173 // This check is performed last since it calls a method implemented
174 // by the argument object. getClass() is final in java.lang.Object.
175 Asserts.check(!sock.isClosed(), "Socket is closed");
176 return false;
177
178 } // isSecure
179
180 }