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.client.fluent;
28
29 import java.io.IOException;
30 import java.security.KeyManagementException;
31 import java.security.NoSuchAlgorithmException;
32 import java.util.concurrent.TimeUnit;
33
34 import javax.net.ssl.SSLContext;
35
36 import org.apache.http.HttpHost;
37 import org.apache.http.auth.AUTH;
38 import org.apache.http.auth.AuthScope;
39 import org.apache.http.auth.Credentials;
40 import org.apache.http.auth.MalformedChallengeException;
41 import org.apache.http.auth.NTCredentials;
42 import org.apache.http.auth.UsernamePasswordCredentials;
43 import org.apache.http.client.AuthCache;
44 import org.apache.http.client.ClientProtocolException;
45 import org.apache.http.client.CookieStore;
46 import org.apache.http.client.CredentialsProvider;
47 import org.apache.http.client.HttpClient;
48 import org.apache.http.client.protocol.HttpClientContext;
49 import org.apache.http.config.Registry;
50 import org.apache.http.config.RegistryBuilder;
51 import org.apache.http.conn.socket.ConnectionSocketFactory;
52 import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
53 import org.apache.http.conn.socket.PlainConnectionSocketFactory;
54 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
55 import org.apache.http.conn.ssl.SSLInitializationException;
56 import org.apache.http.impl.auth.BasicScheme;
57 import org.apache.http.impl.client.BasicAuthCache;
58 import org.apache.http.impl.client.BasicCredentialsProvider;
59 import org.apache.http.impl.client.HttpClientBuilder;
60 import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
61 import org.apache.http.message.BasicHeader;
62
63
64
65
66
67
68
69
70 public class Executor {
71
72 final static PoolingHttpClientConnectionManager CONNMGR;
73 final static HttpClient CLIENT;
74
75 static {
76 LayeredConnectionSocketFactory ssl = null;
77 try {
78 ssl = SSLConnectionSocketFactory.getSystemSocketFactory();
79 } catch (final SSLInitializationException ex) {
80 final SSLContext sslcontext;
81 try {
82 sslcontext = SSLContext.getInstance(SSLConnectionSocketFactory.TLS);
83 sslcontext.init(null, null, null);
84 ssl = new SSLConnectionSocketFactory(sslcontext);
85 } catch (final SecurityException ignore) {
86 } catch (final KeyManagementException ignore) {
87 } catch (final NoSuchAlgorithmException ignore) {
88 }
89 }
90
91 final Registry<ConnectionSocketFactory> sfr = RegistryBuilder.<ConnectionSocketFactory>create()
92 .register("http", PlainConnectionSocketFactory.getSocketFactory())
93 .register("https", ssl != null ? ssl : SSLConnectionSocketFactory.getSocketFactory())
94 .build();
95
96 CONNMGR = new PoolingHttpClientConnectionManager(sfr);
97 CONNMGR.setDefaultMaxPerRoute(100);
98 CONNMGR.setMaxTotal(200);
99 CONNMGR.setValidateAfterInactivity(1000);
100 CLIENT = HttpClientBuilder.create()
101 .setConnectionManager(CONNMGR)
102 .build();
103 }
104
105 public static Executor newInstance() {
106 return new Executor(CLIENT);
107 }
108
109 public static Executor newInstance(final HttpClient httpclient) {
110 return new Executor(httpclient != null ? httpclient : CLIENT);
111 }
112
113 private final HttpClient httpclient;
114 private volatile AuthCache authCache;
115 private volatile CredentialsProvider credentialsProvider;
116 private volatile CookieStore cookieStore;
117
118 Executor(final HttpClient httpclient) {
119 super();
120 this.httpclient = httpclient;
121 this.authCache = new BasicAuthCache();
122 }
123
124
125
126
127 public Executor use(final CredentialsProvider credentialsProvider) {
128 this.credentialsProvider = credentialsProvider;
129 return this;
130 }
131
132 public Executor auth(final AuthScope authScope, final Credentials creds) {
133 if (this.credentialsProvider == null) {
134 this.credentialsProvider = new BasicCredentialsProvider();
135 }
136 this.credentialsProvider.setCredentials(authScope, creds);
137 return this;
138 }
139
140 public Executor auth(final HttpHost host, final Credentials creds) {
141 final AuthScope authScope = host != null ?
142 new AuthScope(host.getHostName(), host.getPort()) : AuthScope.ANY;
143 return auth(authScope, creds);
144 }
145
146
147
148
149 public Executor auth(final String host, final Credentials creds) {
150 return auth(HttpHost.create(host), creds);
151 }
152
153 public Executor authPreemptive(final HttpHost host) {
154 final BasicScheme basicScheme = new BasicScheme();
155 try {
156 basicScheme.processChallenge(new BasicHeader(AUTH.WWW_AUTH, "BASIC "));
157 } catch (final MalformedChallengeException ignore) {
158 }
159 this.authCache.put(host, basicScheme);
160 return this;
161 }
162
163
164
165
166 public Executor authPreemptive(final String host) {
167 return authPreemptive(HttpHost.create(host));
168 }
169
170 public Executor authPreemptiveProxy(final HttpHost proxy) {
171 final BasicScheme basicScheme = new BasicScheme();
172 try {
173 basicScheme.processChallenge(new BasicHeader(AUTH.PROXY_AUTH, "BASIC "));
174 } catch (final MalformedChallengeException ignore) {
175 }
176 this.authCache.put(proxy, basicScheme);
177 return this;
178 }
179
180
181
182
183 public Executor authPreemptiveProxy(final String proxy) {
184 return authPreemptiveProxy(HttpHost.create(proxy));
185 }
186
187 public Executor auth(final Credentials cred) {
188 return auth(AuthScope.ANY, cred);
189 }
190
191 public Executor auth(final String username, final String password) {
192 return auth(new UsernamePasswordCredentials(username, password));
193 }
194
195 public Executor auth(final String username, final String password,
196 final String workstation, final String domain) {
197 return auth(new NTCredentials(username, password, workstation, domain));
198 }
199
200 public Executor auth(final HttpHost host,
201 final String username, final String password) {
202 return auth(host, new UsernamePasswordCredentials(username, password));
203 }
204
205 public Executor auth(final HttpHost host,
206 final String username, final String password,
207 final String workstation, final String domain) {
208 return auth(host, new NTCredentials(username, password, workstation, domain));
209 }
210
211 public Executor clearAuth() {
212 if (this.credentialsProvider != null) {
213 this.credentialsProvider.clear();
214 }
215 return this;
216 }
217
218
219
220
221 @Deprecated
222 public Executor cookieStore(final CookieStore cookieStore) {
223 this.cookieStore = cookieStore;
224 return this;
225 }
226
227
228
229
230 public Executor use(final CookieStore cookieStore) {
231 this.cookieStore = cookieStore;
232 return this;
233 }
234
235 public Executor clearCookies() {
236 if (this.cookieStore != null) {
237 this.cookieStore.clear();
238 }
239 return this;
240 }
241
242
243
244
245
246
247
248
249
250 public Response execute(
251 final Request request) throws ClientProtocolException, IOException {
252 final HttpClientContext localContext = HttpClientContext.create();
253 if (this.credentialsProvider != null) {
254 localContext.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credentialsProvider);
255 }
256 if (this.authCache != null) {
257 localContext.setAttribute(HttpClientContext.AUTH_CACHE, this.authCache);
258 }
259 if (this.cookieStore != null) {
260 localContext.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore);
261 }
262 return new Response(request.internalExecute(this.httpclient, localContext));
263 }
264
265
266
267
268 @Deprecated
269 public static void registerScheme(final org.apache.http.conn.scheme.Scheme scheme) {
270 }
271
272
273
274
275 @Deprecated
276 public static void unregisterScheme(final String name) {
277 }
278
279
280
281
282
283 public static void closeIdleConnections() {
284 CONNMGR.closeIdleConnections(0, TimeUnit.MICROSECONDS);
285 }
286
287 }