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  package org.apache.http.impl.pool;
28  
29  import java.io.IOException;
30  import java.net.InetSocketAddress;
31  import java.net.Socket;
32  
33  import javax.net.ssl.SSLSocketFactory;
34  
35  import org.apache.http.HttpClientConnection;
36  import org.apache.http.HttpHost;
37  import org.apache.http.annotation.Immutable;
38  import org.apache.http.impl.DefaultHttpClientConnection;
39  import org.apache.http.params.HttpConnectionParams;
40  import org.apache.http.params.HttpParams;
41  import org.apache.http.pool.ConnFactory;
42  
43  /**
44   * A very basic {@link ConnFactory} implementation that creates
45   * {@link HttpClientConnection} instances given a {@link HttpHost} instance.
46   * <p/>
47   * The following parameters can be used to customize the behavior of this
48   * class:
49   * <ul>
50   *  <li>{@link org.apache.http.params.CoreProtocolPNames#HTTP_ELEMENT_CHARSET}</li>
51   *  <li>{@link org.apache.http.params.CoreConnectionPNames#TCP_NODELAY}</li>
52   *  <li>{@link org.apache.http.params.CoreConnectionPNames#SO_TIMEOUT}</li>
53   *  <li>{@link org.apache.http.params.CoreConnectionPNames#SO_LINGER}</li>
54   *  <li>{@link org.apache.http.params.CoreConnectionPNames#SOCKET_BUFFER_SIZE}</li>
55   *  <li>{@link org.apache.http.params.CoreConnectionPNames#MAX_LINE_LENGTH}</li>
56   * </ul>
57   *
58   * @see HttpHost
59   * @since 4.2
60   */
61  @Immutable
62  public class BasicConnFactory implements ConnFactory<HttpHost, HttpClientConnection> {
63  
64      private final SSLSocketFactory sslfactory;
65      private final HttpParams params;
66  
67      public BasicConnFactory(final SSLSocketFactory sslfactory, final HttpParams params) {
68          super();
69          if (params == null) {
70              throw new IllegalArgumentException("HTTP params may not be null");
71          }
72          this.sslfactory = sslfactory;
73          this.params = params;
74      }
75  
76      public BasicConnFactory(final HttpParams params) {
77          this(null, params);
78      }
79  
80      protected HttpClientConnection create(final Socket socket, final HttpParams params) throws IOException {
81          DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
82          conn.bind(socket, params);
83          return conn;
84      }
85  
86      public HttpClientConnection create(final HttpHost host) throws IOException {
87          String scheme = host.getSchemeName();
88          Socket socket = null;
89          if ("http".equalsIgnoreCase(scheme)) {
90              socket = new Socket();
91          } if ("https".equalsIgnoreCase(scheme)) {
92              if (this.sslfactory != null) {
93                  socket = this.sslfactory.createSocket();
94              }
95          }
96          if (socket == null) {
97              throw new IOException(scheme + " scheme is not supported");
98          }
99          int connectTimeout = HttpConnectionParams.getConnectionTimeout(this.params);
100         int soTimeout = HttpConnectionParams.getSoTimeout(this.params);
101 
102         socket.setSoTimeout(soTimeout);
103         socket.connect(new InetSocketAddress(host.getHostName(), host.getPort()), connectTimeout);
104         return create(socket, this.params);
105     }
106 
107 }