DefaultHttpClient.java revision 417f3b92ba4549b2f22340e3107d869d2b9c5bb8
1/*
2 * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultHttpClient.java $
3 * $Revision: 677250 $
4 * $Date: 2008-07-16 04:45:47 -0700 (Wed, 16 Jul 2008) $
5 *
6 * ====================================================================
7 * Licensed to the Apache Software Foundation (ASF) under one
8 * or more contributor license agreements.  See the NOTICE file
9 * distributed with this work for additional information
10 * regarding copyright ownership.  The ASF licenses this file
11 * to you under the Apache License, Version 2.0 (the
12 * "License"); you may not use this file except in compliance
13 * with the License.  You may obtain a copy of the License at
14 *
15 *   http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing,
18 * software distributed under the License is distributed on an
19 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20 * KIND, either express or implied.  See the License for the
21 * specific language governing permissions and limitations
22 * under the License.
23 * ====================================================================
24 *
25 * This software consists of voluntary contributions made by many
26 * individuals on behalf of the Apache Software Foundation.  For more
27 * information on the Apache Software Foundation, please see
28 * <http://www.apache.org/>.
29 *
30 */
31
32package org.apache.http.impl.client;
33
34import org.apache.http.ConnectionReuseStrategy;
35import org.apache.http.HttpVersion;
36import org.apache.http.auth.AuthSchemeRegistry;
37import org.apache.http.client.AuthenticationHandler;
38import org.apache.http.client.CookieStore;
39import org.apache.http.client.CredentialsProvider;
40import org.apache.http.client.HttpRequestRetryHandler;
41import org.apache.http.client.RedirectHandler;
42import org.apache.http.client.UserTokenHandler;
43import org.apache.http.client.params.AuthPolicy;
44import org.apache.http.client.params.ClientPNames;
45import org.apache.http.client.params.CookiePolicy;
46import org.apache.http.client.protocol.ClientContext;
47import org.apache.http.client.protocol.RequestAddCookies;
48import org.apache.http.client.protocol.RequestDefaultHeaders;
49import org.apache.http.client.protocol.RequestProxyAuthentication;
50import org.apache.http.client.protocol.RequestTargetAuthentication;
51import org.apache.http.client.protocol.ResponseProcessCookies;
52import org.apache.http.conn.ClientConnectionManager;
53import org.apache.http.conn.ClientConnectionManagerFactory;
54import org.apache.http.conn.ConnectionKeepAliveStrategy;
55import org.apache.http.conn.routing.HttpRoutePlanner;
56import org.apache.http.conn.scheme.PlainSocketFactory;
57import org.apache.http.conn.scheme.Scheme;
58import org.apache.http.conn.scheme.SchemeRegistry;
59import org.apache.http.conn.ssl.SSLSocketFactory;
60import org.apache.http.cookie.CookieSpecRegistry;
61import org.apache.http.impl.DefaultConnectionReuseStrategy;
62import org.apache.http.impl.auth.BasicSchemeFactory;
63import org.apache.http.impl.auth.DigestSchemeFactory;
64import org.apache.http.impl.conn.DefaultHttpRoutePlanner;
65import org.apache.http.impl.conn.SingleClientConnManager;
66import org.apache.http.impl.cookie.BestMatchSpecFactory;
67import org.apache.http.impl.cookie.BrowserCompatSpecFactory;
68import org.apache.http.impl.cookie.NetscapeDraftSpecFactory;
69import org.apache.http.impl.cookie.RFC2109SpecFactory;
70import org.apache.http.impl.cookie.RFC2965SpecFactory;
71import org.apache.http.params.BasicHttpParams;
72import org.apache.http.params.HttpParams;
73import org.apache.http.params.HttpProtocolParams;
74import org.apache.http.protocol.BasicHttpContext;
75import org.apache.http.protocol.BasicHttpProcessor;
76import org.apache.http.protocol.HTTP;
77import org.apache.http.protocol.HttpContext;
78import org.apache.http.protocol.HttpRequestExecutor;
79import org.apache.http.protocol.RequestConnControl;
80import org.apache.http.protocol.RequestContent;
81import org.apache.http.protocol.RequestExpectContinue;
82import org.apache.http.protocol.RequestTargetHost;
83import org.apache.http.protocol.RequestUserAgent;
84import org.apache.http.util.VersionInfo;
85
86
87
88/**
89 * Default implementation of an HTTP client.
90 * <br/>
91 * This class replaces <code>HttpClient</code> in HttpClient 3.
92 *
93 * @author <a href="mailto:rolandw at apache.org">Roland Weber</a>
94 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
95 *
96 * <!-- empty lines to avoid svn diff problems -->
97 * @version   $Revision: 677250 $
98 *
99 * @since 4.0
100 */
101public class DefaultHttpClient extends AbstractHttpClient {
102
103
104    /**
105     * Creates a new HTTP client from parameters and a connection manager.
106     *
107     * @param params    the parameters
108     * @param conman    the connection manager
109     */
110    public DefaultHttpClient(
111            final ClientConnectionManager conman,
112            final HttpParams params) {
113        super(conman, params);
114    }
115
116
117    public DefaultHttpClient(final HttpParams params) {
118        super(null, params);
119    }
120
121
122    public DefaultHttpClient() {
123        super(null, null);
124    }
125
126
127    @Override
128    protected HttpParams createHttpParams() {
129        HttpParams params = new BasicHttpParams();
130        HttpProtocolParams.setVersion(params,
131                HttpVersion.HTTP_1_1);
132        HttpProtocolParams.setContentCharset(params,
133                HTTP.DEFAULT_CONTENT_CHARSET);
134        HttpProtocolParams.setUseExpectContinue(params,
135                true);
136
137        // determine the release version from packaged version info
138        final VersionInfo vi = VersionInfo.loadVersionInfo
139            ("org.apache.http.client", getClass().getClassLoader());
140        final String release = (vi != null) ?
141            vi.getRelease() : VersionInfo.UNAVAILABLE;
142        HttpProtocolParams.setUserAgent(params,
143                "Apache-HttpClient/" + release + " (java 1.4)");
144
145        return params;
146    }
147
148
149    @Override
150    protected HttpRequestExecutor createRequestExecutor() {
151        return new HttpRequestExecutor();
152    }
153
154
155    @Override
156    protected ClientConnectionManager createClientConnectionManager() {
157        SchemeRegistry registry = new SchemeRegistry();
158        registry.register(
159                new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
160        registry.register(
161                new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
162
163        ClientConnectionManager connManager = null;
164        HttpParams params = getParams();
165
166        ClientConnectionManagerFactory factory = null;
167
168        // Try first getting the factory directly as an object.
169        factory = (ClientConnectionManagerFactory) params
170                .getParameter(ClientPNames.CONNECTION_MANAGER_FACTORY);
171        if (factory == null) { // then try getting its class name.
172            String className = (String) params.getParameter(
173                    ClientPNames.CONNECTION_MANAGER_FACTORY_CLASS_NAME);
174            if (className != null) {
175                try {
176                    Class<?> clazz = Class.forName(className);
177                    factory = (ClientConnectionManagerFactory) clazz.newInstance();
178                } catch (ClassNotFoundException ex) {
179                    throw new IllegalStateException("Invalid class name: " + className);
180                } catch (IllegalAccessException ex) {
181                    throw new IllegalAccessError(ex.getMessage());
182                } catch (InstantiationException ex) {
183                    throw new InstantiationError(ex.getMessage());
184                }
185            }
186        }
187
188        if(factory != null) {
189            connManager = factory.newInstance(params, registry);
190        } else {
191            connManager = new SingleClientConnManager(getParams(), registry);
192        }
193
194        return connManager;
195    }
196
197
198    @Override
199    protected HttpContext createHttpContext() {
200        HttpContext context = new BasicHttpContext();
201        context.setAttribute(
202                ClientContext.AUTHSCHEME_REGISTRY,
203                getAuthSchemes());
204        context.setAttribute(
205                ClientContext.COOKIESPEC_REGISTRY,
206                getCookieSpecs());
207        context.setAttribute(
208                ClientContext.COOKIE_STORE,
209                getCookieStore());
210        context.setAttribute(
211                ClientContext.CREDS_PROVIDER,
212                getCredentialsProvider());
213        return context;
214    }
215
216
217    @Override
218    protected ConnectionReuseStrategy createConnectionReuseStrategy() {
219        return new DefaultConnectionReuseStrategy();
220    }
221
222    @Override
223    protected ConnectionKeepAliveStrategy createConnectionKeepAliveStrategy() {
224        return new DefaultConnectionKeepAliveStrategy();
225    }
226
227
228    @Override
229    protected AuthSchemeRegistry createAuthSchemeRegistry() {
230        AuthSchemeRegistry registry = new AuthSchemeRegistry();
231        registry.register(
232                AuthPolicy.BASIC,
233                new BasicSchemeFactory());
234        registry.register(
235                AuthPolicy.DIGEST,
236                new DigestSchemeFactory());
237        return registry;
238    }
239
240
241    @Override
242    protected CookieSpecRegistry createCookieSpecRegistry() {
243        CookieSpecRegistry registry = new CookieSpecRegistry();
244        registry.register(
245                CookiePolicy.BEST_MATCH,
246                new BestMatchSpecFactory());
247        registry.register(
248                CookiePolicy.BROWSER_COMPATIBILITY,
249                new BrowserCompatSpecFactory());
250        registry.register(
251                CookiePolicy.NETSCAPE,
252                new NetscapeDraftSpecFactory());
253        registry.register(
254                CookiePolicy.RFC_2109,
255                new RFC2109SpecFactory());
256        registry.register(
257                CookiePolicy.RFC_2965,
258                new RFC2965SpecFactory());
259        return registry;
260    }
261
262
263    @Override
264    protected BasicHttpProcessor createHttpProcessor() {
265        BasicHttpProcessor httpproc = new BasicHttpProcessor();
266        httpproc.addInterceptor(new RequestDefaultHeaders());
267        // Required protocol interceptors
268        httpproc.addInterceptor(new RequestContent());
269        httpproc.addInterceptor(new RequestTargetHost());
270        // Recommended protocol interceptors
271        httpproc.addInterceptor(new RequestConnControl());
272        httpproc.addInterceptor(new RequestUserAgent());
273        httpproc.addInterceptor(new RequestExpectContinue());
274        // HTTP state management interceptors
275        httpproc.addInterceptor(new RequestAddCookies());
276        httpproc.addInterceptor(new ResponseProcessCookies());
277        // HTTP authentication interceptors
278        httpproc.addInterceptor(new RequestTargetAuthentication());
279        httpproc.addInterceptor(new RequestProxyAuthentication());
280        return httpproc;
281    }
282
283
284    @Override
285    protected HttpRequestRetryHandler createHttpRequestRetryHandler() {
286        return new DefaultHttpRequestRetryHandler();
287    }
288
289
290    @Override
291    protected RedirectHandler createRedirectHandler() {
292        return new DefaultRedirectHandler();
293    }
294
295
296    @Override
297    protected AuthenticationHandler createTargetAuthenticationHandler() {
298        return new DefaultTargetAuthenticationHandler();
299    }
300
301
302    @Override
303    protected AuthenticationHandler createProxyAuthenticationHandler() {
304        return new DefaultProxyAuthenticationHandler();
305    }
306
307
308    @Override
309    protected CookieStore createCookieStore() {
310        return new BasicCookieStore();
311    }
312
313
314    @Override
315    protected CredentialsProvider createCredentialsProvider() {
316        return new BasicCredentialsProvider();
317    }
318
319
320    @Override
321    protected HttpRoutePlanner createHttpRoutePlanner() {
322        return new DefaultHttpRoutePlanner
323            (getConnectionManager().getSchemeRegistry());
324    }
325
326
327    @Override
328    protected UserTokenHandler createUserTokenHandler() {
329        return new DefaultUserTokenHandler();
330    }
331
332} // class DefaultHttpClient
333