View Javadoc
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.impl.conn;
29  
30  import java.io.IOException;
31  import java.net.ConnectException;
32  import java.net.InetAddress;
33  import java.net.InetSocketAddress;
34  import java.net.Socket;
35  import java.net.UnknownHostException;
36  
37  import org.apache.commons.logging.Log;
38  import org.apache.commons.logging.LogFactory;
39  import org.apache.http.HttpHost;
40  import org.apache.http.annotation.Contract;
41  import org.apache.http.annotation.ThreadingBehavior;
42  import org.apache.http.client.protocol.ClientContext;
43  import org.apache.http.conn.ClientConnectionOperator;
44  import org.apache.http.conn.ConnectTimeoutException;
45  import org.apache.http.conn.DnsResolver;
46  import org.apache.http.conn.HttpInetSocketAddress;
47  import org.apache.http.conn.OperatedClientConnection;
48  import org.apache.http.conn.scheme.Scheme;
49  import org.apache.http.conn.scheme.SchemeLayeredSocketFactory;
50  import org.apache.http.conn.scheme.SchemeRegistry;
51  import org.apache.http.conn.scheme.SchemeSocketFactory;
52  import org.apache.http.params.HttpConnectionParams;
53  import org.apache.http.params.HttpParams;
54  import org.apache.http.protocol.HttpContext;
55  import org.apache.http.util.Args;
56  import org.apache.http.util.Asserts;
57  
58  /**
59   * Default implementation of a {@link ClientConnectionOperator}. It uses a {@link SchemeRegistry}
60   * to look up {@link SchemeSocketFactory} objects.
61   * <p>
62   * This connection operator is multihome network aware and will attempt to retry failed connects
63   * against all known IP addresses sequentially until the connect is successful or all known
64   * addresses fail to respond. Please note the same
65   * {@link org.apache.http.params.CoreConnectionPNames#CONNECTION_TIMEOUT} value will be used
66   * for each connection attempt, so in the worst case the total elapsed time before timeout
67   * can be {@code CONNECTION_TIMEOUT * n} where {@code n} is the number of IP addresses
68   * of the given host. One can disable multihome support by overriding
69   * the {@link #resolveHostname(String)} method and returning only one IP address for the given
70   * host name.
71   * <p>
72   * The following parameters can be used to customize the behavior of this
73   * class:
74   * <ul>
75   *  <li>{@link org.apache.http.params.CoreProtocolPNames#HTTP_ELEMENT_CHARSET}</li>
76   *  <li>{@link org.apache.http.params.CoreConnectionPNames#SO_TIMEOUT}</li>
77   *  <li>{@link org.apache.http.params.CoreConnectionPNames#SO_LINGER}</li>
78   *  <li>{@link org.apache.http.params.CoreConnectionPNames#SO_REUSEADDR}</li>
79   *  <li>{@link org.apache.http.params.CoreConnectionPNames#TCP_NODELAY}</li>
80   *  <li>{@link org.apache.http.params.CoreConnectionPNames#SOCKET_BUFFER_SIZE}</li>
81   *  <li>{@link org.apache.http.params.CoreConnectionPNames#CONNECTION_TIMEOUT}</li>
82   *  <li>{@link org.apache.http.params.CoreConnectionPNames#MAX_LINE_LENGTH}</li>
83   * </ul>
84   *
85   * @since 4.0
86   *
87   * @deprecated (4.3) use {@link PoolingHttpClientConnectionManager}.
88   */
89  @Deprecated
90  @Contract(threading = ThreadingBehavior.SAFE_CONDITIONAL)
91  public class DefaultClientConnectionOperator implements ClientConnectionOperator {
92  
93      private final Log log = LogFactory.getLog(getClass());
94  
95      /** The scheme registry for looking up socket factories. */
96      protected final SchemeRegistry schemeRegistry; // @Contract(threading = ThreadingBehavior.SAFE)
97  
98      /** the custom-configured DNS lookup mechanism. */
99      protected final DnsResolver dnsResolver;
100 
101     /**
102      * Creates a new client connection operator for the given scheme registry.
103      *
104      * @param schemes   the scheme registry
105      *
106      * @since 4.2
107      */
108     public DefaultClientConnectionOperator(final SchemeRegistry schemes) {
109         Args.notNull(schemes, "Scheme registry");
110         this.schemeRegistry = schemes;
111         this.dnsResolver = new SystemDefaultDnsResolver();
112     }
113 
114     /**
115     * Creates a new client connection operator for the given scheme registry
116     * and the given custom DNS lookup mechanism.
117     *
118     * @param schemes
119     *            the scheme registry
120     * @param dnsResolver
121     *            the custom DNS lookup mechanism
122     */
123     public DefaultClientConnectionOperator(final SchemeRegistry schemes,final DnsResolver dnsResolver) {
124         Args.notNull(schemes, "Scheme registry");
125 
126         Args.notNull(dnsResolver, "DNS resolver");
127 
128         this.schemeRegistry = schemes;
129         this.dnsResolver = dnsResolver;
130     }
131 
132     @Override
133     public OperatedClientConnection createConnection() {
134         return new DefaultClientConnection();
135     }
136 
137     private SchemeRegistry getSchemeRegistry(final HttpContext context) {
138         SchemeRegistry reg = (SchemeRegistry) context.getAttribute(
139                 ClientContext.SCHEME_REGISTRY);
140         if (reg == null) {
141             reg = this.schemeRegistry;
142         }
143         return reg;
144     }
145 
146     @Override
147     public void openConnection(
148             final OperatedClientConnection conn,
149             final HttpHost target,
150             final InetAddress local,
151             final HttpContext context,
152             final HttpParams params) throws IOException {
153         Args.notNull(conn, "Connection");
154         Args.notNull(target, "Target host");
155         Args.notNull(params, "HTTP parameters");
156         Asserts.check(!conn.isOpen(), "Connection must not be open");
157 
158         final SchemeRegistry registry = getSchemeRegistry(context);
159         final Scheme schm = registry.getScheme(target.getSchemeName());
160         final SchemeSocketFactory sf = schm.getSchemeSocketFactory();
161 
162         final InetAddress[] addresses = resolveHostname(target.getHostName());
163         final int port = schm.resolvePort(target.getPort());
164         for (int i = 0; i < addresses.length; i++) {
165             final InetAddress address = addresses[i];
166             final boolean last = i == addresses.length - 1;
167 
168             Socket sock = sf.createSocket(params);
169             conn.opening(sock, target);
170 
171             final InetSocketAddress remoteAddress = new HttpInetSocketAddress(target, address, port);
172             InetSocketAddress localAddress = null;
173             if (local != null) {
174                 localAddress = new InetSocketAddress(local, 0);
175             }
176             if (this.log.isDebugEnabled()) {
177                 this.log.debug("Connecting to " + remoteAddress);
178             }
179             try {
180                 final Socket connsock = sf.connectSocket(sock, remoteAddress, localAddress, params);
181                 if (sock != connsock) {
182                     sock = connsock;
183                     conn.opening(sock, target);
184                 }
185                 prepareSocket(sock, context, params);
186                 conn.openCompleted(sf.isSecure(sock), params);
187                 return;
188             } catch (final ConnectException ex) {
189                 if (last) {
190                     throw ex;
191                 }
192             } catch (final ConnectTimeoutException ex) {
193                 if (last) {
194                     throw ex;
195                 }
196             }
197             if (this.log.isDebugEnabled()) {
198                 this.log.debug("Connect to " + remoteAddress + " timed out. " +
199                         "Connection will be retried using another IP address");
200             }
201         }
202     }
203 
204     @Override
205     public void updateSecureConnection(
206             final OperatedClientConnection conn,
207             final HttpHost target,
208             final HttpContext context,
209             final HttpParams params) throws IOException {
210         Args.notNull(conn, "Connection");
211         Args.notNull(target, "Target host");
212         Args.notNull(params, "Parameters");
213         Asserts.check(conn.isOpen(), "Connection must be open");
214 
215         final SchemeRegistry registry = getSchemeRegistry(context);
216         final Scheme schm = registry.getScheme(target.getSchemeName());
217         Asserts.check(schm.getSchemeSocketFactory() instanceof SchemeLayeredSocketFactory,
218             "Socket factory must implement SchemeLayeredSocketFactory");
219         final SchemeLayeredSocketFactory lsf = (SchemeLayeredSocketFactory) schm.getSchemeSocketFactory();
220         final Socket sock = lsf.createLayeredSocket(
221                 conn.getSocket(), target.getHostName(), schm.resolvePort(target.getPort()), params);
222         prepareSocket(sock, context, params);
223         conn.update(sock, target, lsf.isSecure(sock), params);
224     }
225 
226     /**
227      * Performs standard initializations on a newly created socket.
228      *
229      * @param sock      the socket to prepare
230      * @param context   the context for the connection
231      * @param params    the parameters from which to prepare the socket
232      *
233      * @throws IOException      in case of an IO problem
234      */
235     protected void prepareSocket(
236             final Socket sock,
237             final HttpContext context,
238             final HttpParams params) throws IOException {
239         sock.setTcpNoDelay(HttpConnectionParams.getTcpNoDelay(params));
240         sock.setSoTimeout(HttpConnectionParams.getSoTimeout(params));
241 
242         final int linger = HttpConnectionParams.getLinger(params);
243         if (linger >= 0) {
244             sock.setSoLinger(linger > 0, linger);
245         }
246     }
247 
248     /**
249      * Resolves the given host name to an array of corresponding IP addresses, based on the
250      * configured name service on the provided DNS resolver. If one wasn't provided, the system
251      * configuration is used.
252      *
253      * @param host host name to resolve
254      * @return array of IP addresses
255      * @exception  UnknownHostException  if no IP address for the host could be determined.
256      *
257      * @see DnsResolver
258      * @see SystemDefaultDnsResolver
259      *
260      * @since 4.1
261      */
262     protected InetAddress[] resolveHostname(final String host) throws UnknownHostException {
263             return dnsResolver.resolve(host);
264     }
265 
266 }
267