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.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   * An Executor for fluent requests.
65   * <p>
66   * A {@link PoolingHttpClientConnectionManager} with maximum 100 connections per route and
67   * a total maximum of 200 connections is used internally.
68   * </p>
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      * @since 4.5
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      * @since 4.4
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      * @since 4.4
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      * @since 4.4
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      * @deprecated (4.5) Use {@link #use(CookieStore)}.
220      */
221     @Deprecated
222     public Executor cookieStore(final CookieStore cookieStore) {
223         this.cookieStore = cookieStore;
224         return this;
225     }
226 
227     /**
228      * @since 4.5
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      * Executes the request. Please Note that response content must be processed
244      * or discarded using {@link Response#discardContent()}, otherwise the
245      * connection used for the request might not be released to the pool.
246      *
247      * @see Response#handleResponse(org.apache.http.client.ResponseHandler)
248      * @see Response#discardContent()
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      * @deprecated (4.3) do not use.
267      */
268     @Deprecated
269     public static void registerScheme(final org.apache.http.conn.scheme.Scheme scheme) {
270     }
271 
272     /**
273      * @deprecated (4.3) do not use.
274      */
275     @Deprecated
276     public static void unregisterScheme(final String name) {
277     }
278 
279     /**
280      * Closes all idle persistent connections used by the internal pool.
281      * @since 4.4
282      */
283     public static void closeIdleConnections() {
284         CONNMGR.closeIdleConnections(0, TimeUnit.MICROSECONDS);
285     }
286 
287 }