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