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.SocketFactory;
34  import javax.net.ssl.SSLSocketFactory;
35  
36  import org.apache.http.HttpClientConnection;
37  import org.apache.http.HttpConnectionFactory;
38  import org.apache.http.HttpHost;
39  import org.apache.http.annotation.ThreadingBehavior;
40  import org.apache.http.annotation.Contract;
41  import org.apache.http.config.ConnectionConfig;
42  import org.apache.http.config.SocketConfig;
43  import org.apache.http.impl.DefaultBHttpClientConnection;
44  import org.apache.http.impl.DefaultBHttpClientConnectionFactory;
45  import org.apache.http.params.CoreConnectionPNames;
46  import org.apache.http.params.HttpParamConfig;
47  import org.apache.http.params.HttpParams;
48  import org.apache.http.pool.ConnFactory;
49  import org.apache.http.util.Args;
50  
51  /**
52   * A very basic {@link ConnFactory} implementation that creates
53   * {@link HttpClientConnection} instances given a {@link HttpHost} instance.
54   *
55   * @see HttpHost
56   * @since 4.2
57   */
58  @SuppressWarnings("deprecation")
59  @Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL)
60  public class BasicConnFactory implements ConnFactory<HttpHost, HttpClientConnection> {
61  
62      private final SocketFactory plainfactory;
63      private final SSLSocketFactory sslfactory;
64      private final int connectTimeout;
65      private final SocketConfig sconfig;
66      private final HttpConnectionFactory<? extends HttpClientConnection> connFactory;
67  
68      /**
69       * @deprecated (4.3) use
70       *   {@link BasicConnFactory#BasicConnFactory(SocketFactory, SSLSocketFactory, int,
71       *     SocketConfig, ConnectionConfig)}.
72       */
73      @Deprecated
74      public BasicConnFactory(final SSLSocketFactory sslfactory, final HttpParams params) {
75          super();
76          Args.notNull(params, "HTTP params");
77          this.plainfactory = null;
78          this.sslfactory = sslfactory;
79          this.connectTimeout = params.getIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 0);
80          this.sconfig = HttpParamConfig.getSocketConfig(params);
81          this.connFactory = new DefaultBHttpClientConnectionFactory(
82                  HttpParamConfig.getConnectionConfig(params));
83      }
84  
85      /**
86       * @deprecated (4.3) use
87       *   {@link BasicConnFactory#BasicConnFactory(int, SocketConfig, ConnectionConfig)}.
88       */
89      @Deprecated
90      public BasicConnFactory(final HttpParams params) {
91          this(null, params);
92      }
93  
94      /**
95       * @since 4.3
96       */
97      public BasicConnFactory(
98              final SocketFactory plainfactory,
99              final SSLSocketFactory sslfactory,
100             final int connectTimeout,
101             final SocketConfig sconfig,
102             final ConnectionConfig cconfig) {
103         super();
104         this.plainfactory = plainfactory;
105         this.sslfactory = sslfactory;
106         this.connectTimeout = connectTimeout;
107         this.sconfig = sconfig != null ? sconfig : SocketConfig.DEFAULT;
108         this.connFactory = new DefaultBHttpClientConnectionFactory(
109                 cconfig != null ? cconfig : ConnectionConfig.DEFAULT);
110     }
111 
112     /**
113      * @since 4.3
114      */
115     public BasicConnFactory(
116             final int connectTimeout, final SocketConfig sconfig, final ConnectionConfig cconfig) {
117         this(null, null, connectTimeout, sconfig, cconfig);
118     }
119 
120     /**
121      * @since 4.3
122      */
123     public BasicConnFactory(final SocketConfig sconfig, final ConnectionConfig cconfig) {
124         this(null, null, 0, sconfig, cconfig);
125     }
126 
127     /**
128      * @since 4.3
129      */
130     public BasicConnFactory() {
131         this(null, null, 0, SocketConfig.DEFAULT, ConnectionConfig.DEFAULT);
132     }
133 
134     /**
135      * @deprecated (4.3) no longer used.
136      */
137     @Deprecated
138     protected HttpClientConnection create(final Socket socket, final HttpParams params) throws IOException {
139         final int bufsize = params.getIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024);
140         final DefaultBHttpClientConnection conn = new DefaultBHttpClientConnection(bufsize);
141         conn.bind(socket);
142         return conn;
143     }
144 
145     @Override
146     public HttpClientConnection create(final HttpHost host) throws IOException {
147         final String scheme = host.getSchemeName();
148         Socket socket = null;
149         if ("http".equalsIgnoreCase(scheme)) {
150             socket = this.plainfactory != null ? this.plainfactory.createSocket() :
151                     new Socket();
152         } if ("https".equalsIgnoreCase(scheme)) {
153             socket = (this.sslfactory != null ? this.sslfactory :
154                     SSLSocketFactory.getDefault()).createSocket();
155         }
156         if (socket == null) {
157             throw new IOException(scheme + " scheme is not supported");
158         }
159         final String hostname = host.getHostName();
160         int port = host.getPort();
161         if (port == -1) {
162             if (host.getSchemeName().equalsIgnoreCase("http")) {
163                 port = 80;
164             } else if (host.getSchemeName().equalsIgnoreCase("https")) {
165                 port = 443;
166             }
167         }
168         socket.setSoTimeout(this.sconfig.getSoTimeout());
169         if (this.sconfig.getSndBufSize() > 0) {
170             socket.setSendBufferSize(this.sconfig.getSndBufSize());
171         }
172         if (this.sconfig.getRcvBufSize() > 0) {
173             socket.setReceiveBufferSize(this.sconfig.getRcvBufSize());
174         }
175         socket.setTcpNoDelay(this.sconfig.isTcpNoDelay());
176         final int linger = this.sconfig.getSoLinger();
177         if (linger >= 0) {
178             socket.setSoLinger(true, linger);
179         }
180         socket.setKeepAlive(this.sconfig.isSoKeepAlive());
181         socket.connect(new InetSocketAddress(hostname, port), this.connectTimeout);
182         return this.connFactory.createConnection(socket);
183     }
184 
185 }