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          CLIENT = HttpClientBuilder.create().setConnectionManager(CONNMGR).build();
100     }
101 
102     public static Executor newInstance() {
103         return new Executor(CLIENT);
104     }
105 
106     public static Executor newInstance(final HttpClient httpclient) {
107         return new Executor(httpclient != null ? httpclient : CLIENT);
108     }
109 
110     private final HttpClient httpclient;
111     private final AuthCache authCache;
112     private final CredentialsProvider credentialsProvider;
113 
114     private volatile CookieStore cookieStore;
115 
116     Executor(final HttpClient httpclient) {
117         super();
118         this.httpclient = httpclient;
119         this.credentialsProvider = new BasicCredentialsProvider();
120         this.authCache = new BasicAuthCache();
121     }
122 
123     public Executor auth(final AuthScope authScope, final Credentials creds) {
124         this.credentialsProvider.setCredentials(authScope, creds);
125         return this;
126     }
127 
128     public Executor auth(final HttpHost host, final Credentials creds) {
129         final AuthScope authScope = host != null ?
130                 new AuthScope(host.getHostName(), host.getPort()) : AuthScope.ANY;
131         return auth(authScope, creds);
132     }
133 
134     /**
135      * @since 4.4
136      */
137     public Executor auth(final String host, final Credentials creds) {
138         return auth(HttpHost.create(host), creds);
139     }
140 
141     public Executor authPreemptive(final HttpHost host) {
142         final BasicScheme basicScheme = new BasicScheme();
143         try {
144             basicScheme.processChallenge(new BasicHeader(AUTH.WWW_AUTH, "BASIC "));
145         } catch (final MalformedChallengeException ignore) {
146         }
147         this.authCache.put(host, basicScheme);
148         return this;
149     }
150 
151     /**
152      * @since 4.4
153      */
154     public Executor authPreemptive(final String host) {
155         return authPreemptive(HttpHost.create(host));
156     }
157 
158     public Executor authPreemptiveProxy(final HttpHost proxy) {
159         final BasicScheme basicScheme = new BasicScheme();
160         try {
161             basicScheme.processChallenge(new BasicHeader(AUTH.PROXY_AUTH, "BASIC "));
162         } catch (final MalformedChallengeException ignore) {
163         }
164         this.authCache.put(proxy, basicScheme);
165         return this;
166     }
167 
168     /**
169      * @since 4.4
170      */
171     public Executor authPreemptiveProxy(final String proxy) {
172         return authPreemptiveProxy(HttpHost.create(proxy));
173     }
174 
175     public Executor auth(final Credentials cred) {
176         return auth(AuthScope.ANY, cred);
177     }
178 
179     public Executor auth(final String username, final String password) {
180         return auth(new UsernamePasswordCredentials(username, password));
181     }
182 
183     public Executor auth(final String username, final String password,
184             final String workstation, final String domain) {
185         return auth(new NTCredentials(username, password, workstation, domain));
186     }
187 
188     public Executor auth(final HttpHost host,
189             final String username, final String password) {
190         return auth(host, new UsernamePasswordCredentials(username, password));
191     }
192 
193     public Executor auth(final HttpHost host,
194             final String username, final String password,
195             final String workstation, final String domain) {
196         return auth(host, new NTCredentials(username, password, workstation, domain));
197     }
198 
199     public Executor clearAuth() {
200         this.credentialsProvider.clear();
201         return this;
202     }
203 
204     public Executor cookieStore(final CookieStore cookieStore) {
205         this.cookieStore = cookieStore;
206         return this;
207     }
208 
209     public Executor clearCookies() {
210         this.cookieStore.clear();
211         return this;
212     }
213 
214     /**
215      * Executes the request. Please Note that response content must be processed
216      * or discarded using {@link Response#discardContent()}, otherwise the
217      * connection used for the request might not be released to the pool.
218      *
219      * @see Response#handleResponse(org.apache.http.client.ResponseHandler)
220      * @see Response#discardContent()
221      */
222     public Response execute(
223             final Request request) throws ClientProtocolException, IOException {
224         final HttpClientContext localContext = HttpClientContext.create();
225         localContext.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credentialsProvider);
226         localContext.setAttribute(HttpClientContext.AUTH_CACHE, this.authCache);
227         localContext.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore);
228         final InternalHttpRequest httprequest = request.prepareRequest();
229         return new Response(this.httpclient.execute(httprequest, localContext));
230     }
231 
232     /**
233      * @deprecated (4.3) do not use.
234      */
235     @Deprecated
236     public static void registerScheme(final org.apache.http.conn.scheme.Scheme scheme) {
237     }
238 
239     /**
240      * @deprecated (4.3) do not use.
241      */
242     @Deprecated
243     public static void unregisterScheme(final String name) {
244     }
245 
246     /**
247      * Closes all idle persistent connections used by the internal pool.
248      * @since 4.4
249      */
250     public static void closeIdleConnections() {
251         CONNMGR.closeIdleConnections(0, TimeUnit.MICROSECONDS);
252     }
253 
254 }