1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.opensaml.ws.soap.client.http;
18
19 import net.jcip.annotations.NotThreadSafe;
20
21 import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
22 import org.apache.commons.httpclient.HostConfiguration;
23 import org.apache.commons.httpclient.HttpClient;
24 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
25 import org.apache.commons.httpclient.UsernamePasswordCredentials;
26 import org.apache.commons.httpclient.auth.AuthScope;
27 import org.apache.commons.httpclient.params.HttpClientParams;
28 import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
29 import org.apache.commons.httpclient.protocol.Protocol;
30 import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
31 import org.opensaml.xml.util.DatatypeHelper;
32
33
34
35
36
37
38
39
40 @NotThreadSafe
41 public class HttpClientBuilder {
42
43
44 private String proxyHost;
45
46
47 private int proxyPort;
48
49
50 private String proxyUsername;
51
52
53 private String proxyPassword;
54
55
56 private boolean preemptiveAuthentication;
57
58
59 private String contentCharSet;
60
61
62 private int connectionTimeout;
63
64
65 private int sendBufferSize;
66
67
68 private int receiveBufferSize;
69
70
71 private boolean tcpNoDelay;
72
73
74 private int maxConnectionsPerHost;
75
76
77 private int maxTotalConnectons;
78
79
80 private int connectionRetryAttempts;
81
82
83 private SecureProtocolSocketFactory httpsProtocolSocketFactory;
84
85
86 public HttpClientBuilder() {
87 resetDefaults();
88 }
89
90
91 public void resetDefaults() {
92 proxyPort = -1;
93 preemptiveAuthentication = false;
94 contentCharSet = "UTF-8";
95 connectionTimeout = 5000;
96 sendBufferSize = 4096;
97 receiveBufferSize = 16384;
98 tcpNoDelay = true;
99 maxConnectionsPerHost = 5;
100 maxTotalConnectons = 20;
101 connectionRetryAttempts = 0;
102 }
103
104
105
106
107
108
109
110 public HttpClient buildClient() {
111 if (httpsProtocolSocketFactory != null) {
112 Protocol.registerProtocol("https", new Protocol("https", httpsProtocolSocketFactory, 443));
113 }
114
115 HttpClientParams clientParams = new HttpClientParams();
116 clientParams.setAuthenticationPreemptive(isPreemptiveAuthentication());
117 clientParams.setContentCharset(getContentCharSet());
118 clientParams.setParameter(HttpClientParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(
119 connectionRetryAttempts, false));
120
121 HttpConnectionManagerParams connMgrParams = new HttpConnectionManagerParams();
122 connMgrParams.setConnectionTimeout(getConnectionTimeout());
123 connMgrParams.setDefaultMaxConnectionsPerHost(getMaxConnectionsPerHost());
124 connMgrParams.setMaxTotalConnections(getMaxTotalConnections());
125 connMgrParams.setReceiveBufferSize(getReceiveBufferSize());
126 connMgrParams.setSendBufferSize(getSendBufferSize());
127 connMgrParams.setTcpNoDelay(isTcpNoDelay());
128
129 MultiThreadedHttpConnectionManager connMgr = new MultiThreadedHttpConnectionManager();
130 connMgr.setParams(connMgrParams);
131
132 HttpClient httpClient = new HttpClient(clientParams, connMgr);
133
134 if (proxyHost != null) {
135 HostConfiguration hostConfig = new HostConfiguration();
136 hostConfig.setProxy(proxyHost, proxyPort);
137 httpClient.setHostConfiguration(hostConfig);
138
139 if (proxyUsername != null) {
140 AuthScope proxyAuthScope = new AuthScope(proxyHost, proxyPort);
141 UsernamePasswordCredentials proxyCredentials = new UsernamePasswordCredentials(proxyUsername,
142 proxyPassword);
143 httpClient.getState().setProxyCredentials(proxyAuthScope, proxyCredentials);
144 }
145 }
146
147 return httpClient;
148 }
149
150
151
152
153
154
155 public String getProxyHost() {
156 return proxyHost;
157 }
158
159
160
161
162
163
164 public void setProxyHost(String host) {
165 proxyHost = DatatypeHelper.safeTrimOrNullString(host);
166 }
167
168
169
170
171
172
173 public int getProxyPort() {
174 return proxyPort;
175 }
176
177
178
179
180
181
182 public void setProxyPort(int port) {
183 proxyPort = port;
184 }
185
186
187
188
189
190
191 public String getProxyUsername() {
192 return proxyUsername;
193 }
194
195
196
197
198
199
200 public void setProxyUsername(String username) {
201 proxyUsername = DatatypeHelper.safeTrimOrNullString(username);
202 }
203
204
205
206
207
208
209 public String getProxyPassword() {
210 return proxyPassword;
211 }
212
213
214
215
216
217
218 public void setProxyPassword(String password) {
219 proxyPassword = DatatypeHelper.safeTrimOrNullString(password);
220 }
221
222
223
224
225
226
227 public boolean isPreemptiveAuthentication() {
228 return preemptiveAuthentication;
229 }
230
231
232
233
234
235
236 public void setPreemptiveAuthentication(boolean preemptive) {
237 preemptiveAuthentication = preemptive;
238 }
239
240
241
242
243
244
245 public String getContentCharSet() {
246 return contentCharSet;
247 }
248
249
250
251
252
253
254 public void setContentCharSet(String charSet) {
255 contentCharSet = charSet;
256 }
257
258
259
260
261
262
263
264 public int getConnectionTimeout() {
265 return connectionTimeout;
266 }
267
268
269
270
271
272
273
274 public void setConnectionTimeout(int timeout) {
275 connectionTimeout = timeout;
276 }
277
278
279
280
281
282
283 public int getSendBufferSize() {
284 return sendBufferSize;
285 }
286
287
288
289
290
291
292 public void setSendBufferSize(int size) {
293 sendBufferSize = size;
294 }
295
296
297
298
299
300
301 public int getReceiveBufferSize() {
302 return receiveBufferSize;
303 }
304
305
306
307
308
309
310 public void setReceiveBufferSize(int size) {
311 receiveBufferSize = size;
312 }
313
314
315
316
317
318
319 public boolean isTcpNoDelay() {
320 return tcpNoDelay;
321 }
322
323
324
325
326
327
328 public void setTcpNoDelay(boolean noDelay) {
329 tcpNoDelay = noDelay;
330 }
331
332
333
334
335
336
337
338 public int getMaxConnectionsPerHost() {
339 return maxConnectionsPerHost;
340 }
341
342
343
344
345
346
347
348 public void setMaxConnectionsPerHost(int max) {
349 maxConnectionsPerHost = max;
350 }
351
352
353
354
355
356
357 public int getMaxTotalConnections() {
358 return maxTotalConnectons;
359 }
360
361
362
363
364
365
366 public void setMaxTotalConnections(int max) {
367 if (max < 1) {
368 throw new IllegalArgumentException("Maximum total number of connections must be greater than zero.");
369 }
370 maxTotalConnectons = max;
371 }
372
373
374
375
376
377
378 public int getConnectionRetryAttempts() {
379 return connectionRetryAttempts;
380 }
381
382
383
384
385
386
387 public void setConnectionRetryAttempts(int attempts) {
388 connectionRetryAttempts = attempts;
389 }
390
391
392
393
394
395
396 public SecureProtocolSocketFactory getHttpsProtocolSocketFactory() {
397 return httpsProtocolSocketFactory;
398 }
399
400
401
402
403
404
405 public void setHttpsProtocolSocketFactory(SecureProtocolSocketFactory factory) {
406 httpsProtocolSocketFactory = factory;
407 }
408 }