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  
33  import javax.net.ssl.SSLContext;
34  
35  import org.apache.http.HttpHost;
36  import org.apache.http.auth.AUTH;
37  import org.apache.http.auth.AuthScope;
38  import org.apache.http.auth.Credentials;
39  import org.apache.http.auth.MalformedChallengeException;
40  import org.apache.http.auth.NTCredentials;
41  import org.apache.http.auth.UsernamePasswordCredentials;
42  import org.apache.http.client.AuthCache;
43  import org.apache.http.client.ClientProtocolException;
44  import org.apache.http.client.CookieStore;
45  import org.apache.http.client.CredentialsProvider;
46  import org.apache.http.client.HttpClient;
47  import org.apache.http.client.protocol.HttpClientContext;
48  import org.apache.http.config.Registry;
49  import org.apache.http.config.RegistryBuilder;
50  import org.apache.http.conn.socket.ConnectionSocketFactory;
51  import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
52  import org.apache.http.conn.socket.PlainConnectionSocketFactory;
53  import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
54  import org.apache.http.conn.ssl.SSLInitializationException;
55  import org.apache.http.impl.auth.BasicScheme;
56  import org.apache.http.impl.client.BasicAuthCache;
57  import org.apache.http.impl.client.BasicCredentialsProvider;
58  import org.apache.http.impl.client.HttpClientBuilder;
59  import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
60  import org.apache.http.message.BasicHeader;
61  
62  /**
63   * An Executor for fluent requests
64   * <p/>
65   * A {@link PoolingHttpClientConnectionManager} with maximum 100 connections per route and
66   * a total maximum of 200 connections is used internally.
67   */
68  public class Executor {
69  
70      final static PoolingHttpClientConnectionManager CONNMGR;
71      final static HttpClient CLIENT;
72  
73      static {
74          LayeredConnectionSocketFactory ssl = null;
75          try {
76              ssl = SSLConnectionSocketFactory.getSystemSocketFactory();
77          } catch (final SSLInitializationException ex) {
78              final SSLContext sslcontext;
79              try {
80                  sslcontext = SSLContext.getInstance(SSLConnectionSocketFactory.TLS);
81                  sslcontext.init(null, null, null);
82                  ssl = new SSLConnectionSocketFactory(sslcontext);
83              } catch (final SecurityException ignore) {
84              } catch (final KeyManagementException ignore) {
85              } catch (final NoSuchAlgorithmException ignore) {
86              }
87          }
88  
89          final Registry<ConnectionSocketFactory> sfr = RegistryBuilder.<ConnectionSocketFactory>create()
90              .register("http", PlainConnectionSocketFactory.getSocketFactory())
91              .register("https", ssl != null ? ssl : SSLConnectionSocketFactory.getSocketFactory())
92              .build();
93  
94          CONNMGR = new PoolingHttpClientConnectionManager(sfr);
95          CONNMGR.setDefaultMaxPerRoute(100);
96          CONNMGR.setMaxTotal(200);
97          CLIENT = HttpClientBuilder.create().setConnectionManager(CONNMGR).build();
98      }
99  
100     public static Executor newInstance() {
101         return new Executor(CLIENT);
102     }
103 
104     public static Executor newInstance(final HttpClient httpclient) {
105         return new Executor(httpclient != null ? httpclient : CLIENT);
106     }
107 
108     private final HttpClient httpclient;
109     private final AuthCache authCache;
110     private final CredentialsProvider credentialsProvider;
111 
112     private volatile CookieStore cookieStore;
113 
114     Executor(final HttpClient httpclient) {
115         super();
116         this.httpclient = httpclient;
117         this.credentialsProvider = new BasicCredentialsProvider();
118         this.authCache = new BasicAuthCache();
119     }
120 
121     public Executor auth(final AuthScope authScope, final Credentials creds) {
122         this.credentialsProvider.setCredentials(authScope, creds);
123         return this;
124     }
125 
126     public Executor auth(final HttpHost host, final Credentials creds) {
127         final AuthScope authScope = host != null ?
128                 new AuthScope(host.getHostName(), host.getPort()) : AuthScope.ANY;
129         return auth(authScope, creds);
130     }
131 
132     public Executor authPreemptive(final HttpHost host) {
133         final BasicScheme basicScheme = new BasicScheme();
134         try {
135             basicScheme.processChallenge(new BasicHeader(AUTH.WWW_AUTH, "BASIC "));
136         } catch (final MalformedChallengeException ignore) {
137         }
138         this.authCache.put(host, basicScheme);
139         return this;
140     }
141 
142     public Executor authPreemptiveProxy(final HttpHost host) {
143         final BasicScheme basicScheme = new BasicScheme();
144         try {
145             basicScheme.processChallenge(new BasicHeader(AUTH.PROXY_AUTH, "BASIC "));
146         } catch (final MalformedChallengeException ignore) {
147         }
148         this.authCache.put(host, basicScheme);
149         return this;
150     }
151 
152     public Executor auth(final Credentials cred) {
153         return auth(AuthScope.ANY, cred);
154     }
155 
156     public Executor auth(final String username, final String password) {
157         return auth(new UsernamePasswordCredentials(username, password));
158     }
159 
160     public Executor auth(final String username, final String password,
161             final String workstation, final String domain) {
162         return auth(new NTCredentials(username, password, workstation, domain));
163     }
164 
165     public Executor auth(final HttpHost host,
166             final String username, final String password) {
167         return auth(host, new UsernamePasswordCredentials(username, password));
168     }
169 
170     public Executor auth(final HttpHost host,
171             final String username, final String password,
172             final String workstation, final String domain) {
173         return auth(host, new NTCredentials(username, password, workstation, domain));
174     }
175 
176     public Executor clearAuth() {
177         this.credentialsProvider.clear();
178         return this;
179     }
180 
181     public Executor cookieStore(final CookieStore cookieStore) {
182         this.cookieStore = cookieStore;
183         return this;
184     }
185 
186     public Executor clearCookies() {
187         this.cookieStore.clear();
188         return this;
189     }
190 
191     /**
192      * Executes the request. Please Note that response content must be processed
193      * or discarded using {@link Response#discardContent()}, otherwise the
194      * connection used for the request might not be released to the pool.
195      *
196      * @see Response#handleResponse(org.apache.http.client.ResponseHandler)
197      * @see Response#discardContent()
198      */
199     public Response execute(
200             final Request request) throws ClientProtocolException, IOException {
201         final HttpClientContext localContext = HttpClientContext.create();
202         localContext.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credentialsProvider);
203         localContext.setAttribute(HttpClientContext.AUTH_CACHE, this.authCache);
204         localContext.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore);
205         final InternalHttpRequest httprequest = request.prepareRequest();
206         return new Response(this.httpclient.execute(httprequest, localContext));
207     }
208 
209     /**
210      * @deprecated (4.3) do not use.
211      */
212     @Deprecated
213     public static void registerScheme(final org.apache.http.conn.scheme.Scheme scheme) {
214     }
215 
216     /**
217      * @deprecated (4.3) do not use.
218      */
219     @Deprecated
220     public static void unregisterScheme(final String name) {
221     }
222 
223 }