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