1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
54
55
56
57
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
70
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
84
85
86 @Deprecated
87 public BasicConnFactory(final HttpParams params) {
88 this(null, params);
89 }
90
91
92
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
108
109 public BasicConnFactory(
110 final int connectTimeout, final SocketConfig sconfig, final ConnectionConfig cconfig) {
111 this(null, connectTimeout, sconfig, cconfig);
112 }
113
114
115
116
117 public BasicConnFactory(final SocketConfig sconfig, final ConnectionConfig cconfig) {
118 this(null, 0, sconfig, cconfig);
119 }
120
121
122
123
124 public BasicConnFactory() {
125 this(null, 0, SocketConfig.DEFAULT, ConnectionConfig.DEFAULT);
126 }
127
128
129
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 }