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