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 32 package org.apache.http.impl.client; 33 34 import org.apache.http.ConnectionReuseStrategy; 35 import org.apache.http.HttpVersion; 36 import org.apache.http.auth.AuthSchemeRegistry; 37 import org.apache.http.client.AuthenticationHandler; 38 import org.apache.http.client.CookieStore; 39 import org.apache.http.client.CredentialsProvider; 40 import org.apache.http.client.HttpRequestRetryHandler; 41 import org.apache.http.client.RedirectHandler; 42 import org.apache.http.client.UserTokenHandler; 43 import org.apache.http.client.params.AuthPolicy; 44 import org.apache.http.client.params.ClientPNames; 45 import org.apache.http.client.params.CookiePolicy; 46 import org.apache.http.client.protocol.ClientContext; 47 import org.apache.http.client.protocol.RequestAddCookies; 48 import org.apache.http.client.protocol.RequestDefaultHeaders; 49 import org.apache.http.client.protocol.RequestProxyAuthentication; 50 import org.apache.http.client.protocol.RequestTargetAuthentication; 51 import org.apache.http.client.protocol.ResponseProcessCookies; 52 import org.apache.http.conn.ClientConnectionManager; 53 import org.apache.http.conn.ClientConnectionManagerFactory; 54 import org.apache.http.conn.ConnectionKeepAliveStrategy; 55 import org.apache.http.conn.routing.HttpRoutePlanner; 56 import org.apache.http.conn.scheme.PlainSocketFactory; 57 import org.apache.http.conn.scheme.Scheme; 58 import org.apache.http.conn.scheme.SchemeRegistry; 59 import org.apache.http.conn.ssl.SSLSocketFactory; 60 import org.apache.http.cookie.CookieSpecRegistry; 61 import org.apache.http.impl.DefaultConnectionReuseStrategy; 62 import org.apache.http.impl.auth.BasicSchemeFactory; 63 import org.apache.http.impl.auth.DigestSchemeFactory; 64 import org.apache.http.impl.conn.ProxySelectorRoutePlanner; 65 import org.apache.http.impl.conn.SingleClientConnManager; 66 import org.apache.http.impl.cookie.BestMatchSpecFactory; 67 import org.apache.http.impl.cookie.BrowserCompatSpecFactory; 68 import org.apache.http.impl.cookie.NetscapeDraftSpecFactory; 69 import org.apache.http.impl.cookie.RFC2109SpecFactory; 70 import org.apache.http.impl.cookie.RFC2965SpecFactory; 71 import org.apache.http.params.BasicHttpParams; 72 import org.apache.http.params.HttpParams; 73 import org.apache.http.params.HttpProtocolParams; 74 import org.apache.http.protocol.BasicHttpContext; 75 import org.apache.http.protocol.BasicHttpProcessor; 76 import org.apache.http.protocol.HTTP; 77 import org.apache.http.protocol.HttpContext; 78 import org.apache.http.protocol.HttpRequestExecutor; 79 import org.apache.http.protocol.RequestConnControl; 80 import org.apache.http.protocol.RequestContent; 81 import org.apache.http.protocol.RequestExpectContinue; 82 import org.apache.http.protocol.RequestTargetHost; 83 import org.apache.http.protocol.RequestUserAgent; 84 import 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 */ 101 public 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 135 /* 136 * Android note: Send each request body without first asking the server 137 * whether it will be accepted. Asking first slows down the common case 138 * and results in "417 expectation failed" errors when a HTTP/1.0 server 139 * is behind a proxy. http://b/2471595 140 */ 141 HttpProtocolParams.setUseExpectContinue(params, 142 false); // android-changed 143 144 // determine the release version from packaged version info 145 final VersionInfo vi = VersionInfo.loadVersionInfo 146 ("org.apache.http.client", getClass().getClassLoader()); 147 final String release = (vi != null) ? 148 vi.getRelease() : VersionInfo.UNAVAILABLE; 149 HttpProtocolParams.setUserAgent(params, 150 "Apache-HttpClient/" + release + " (java 1.4)"); 151 152 return params; 153 } 154 155 156 @Override 157 protected HttpRequestExecutor createRequestExecutor() { 158 return new HttpRequestExecutor(); 159 } 160 161 162 @Override 163 protected ClientConnectionManager createClientConnectionManager() { 164 SchemeRegistry registry = new SchemeRegistry(); 165 registry.register( 166 new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 167 registry.register( 168 new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); 169 170 ClientConnectionManager connManager = null; 171 HttpParams params = getParams(); 172 173 ClientConnectionManagerFactory factory = null; 174 175 // Try first getting the factory directly as an object. 176 factory = (ClientConnectionManagerFactory) params 177 .getParameter(ClientPNames.CONNECTION_MANAGER_FACTORY); 178 if (factory == null) { // then try getting its class name. 179 String className = (String) params.getParameter( 180 ClientPNames.CONNECTION_MANAGER_FACTORY_CLASS_NAME); 181 if (className != null) { 182 try { 183 Class<?> clazz = Class.forName(className); 184 factory = (ClientConnectionManagerFactory) clazz.newInstance(); 185 } catch (ClassNotFoundException ex) { 186 throw new IllegalStateException("Invalid class name: " + className); 187 } catch (IllegalAccessException ex) { 188 throw new IllegalAccessError(ex.getMessage()); 189 } catch (InstantiationException ex) { 190 throw new InstantiationError(ex.getMessage()); 191 } 192 } 193 } 194 195 if(factory != null) { 196 connManager = factory.newInstance(params, registry); 197 } else { 198 connManager = new SingleClientConnManager(getParams(), registry); 199 } 200 201 return connManager; 202 } 203 204 205 @Override 206 protected HttpContext createHttpContext() { 207 HttpContext context = new BasicHttpContext(); 208 context.setAttribute( 209 ClientContext.AUTHSCHEME_REGISTRY, 210 getAuthSchemes()); 211 context.setAttribute( 212 ClientContext.COOKIESPEC_REGISTRY, 213 getCookieSpecs()); 214 context.setAttribute( 215 ClientContext.COOKIE_STORE, 216 getCookieStore()); 217 context.setAttribute( 218 ClientContext.CREDS_PROVIDER, 219 getCredentialsProvider()); 220 return context; 221 } 222 223 224 @Override 225 protected ConnectionReuseStrategy createConnectionReuseStrategy() { 226 return new DefaultConnectionReuseStrategy(); 227 } 228 229 @Override 230 protected ConnectionKeepAliveStrategy createConnectionKeepAliveStrategy() { 231 return new DefaultConnectionKeepAliveStrategy(); 232 } 233 234 235 @Override 236 protected AuthSchemeRegistry createAuthSchemeRegistry() { 237 AuthSchemeRegistry registry = new AuthSchemeRegistry(); 238 registry.register( 239 AuthPolicy.BASIC, 240 new BasicSchemeFactory()); 241 registry.register( 242 AuthPolicy.DIGEST, 243 new DigestSchemeFactory()); 244 return registry; 245 } 246 247 248 @Override 249 protected CookieSpecRegistry createCookieSpecRegistry() { 250 CookieSpecRegistry registry = new CookieSpecRegistry(); 251 registry.register( 252 CookiePolicy.BEST_MATCH, 253 new BestMatchSpecFactory()); 254 registry.register( 255 CookiePolicy.BROWSER_COMPATIBILITY, 256 new BrowserCompatSpecFactory()); 257 registry.register( 258 CookiePolicy.NETSCAPE, 259 new NetscapeDraftSpecFactory()); 260 registry.register( 261 CookiePolicy.RFC_2109, 262 new RFC2109SpecFactory()); 263 registry.register( 264 CookiePolicy.RFC_2965, 265 new RFC2965SpecFactory()); 266 return registry; 267 } 268 269 270 @Override 271 protected BasicHttpProcessor createHttpProcessor() { 272 BasicHttpProcessor httpproc = new BasicHttpProcessor(); 273 httpproc.addInterceptor(new RequestDefaultHeaders()); 274 // Required protocol interceptors 275 httpproc.addInterceptor(new RequestContent()); 276 httpproc.addInterceptor(new RequestTargetHost()); 277 // Recommended protocol interceptors 278 httpproc.addInterceptor(new RequestConnControl()); 279 httpproc.addInterceptor(new RequestUserAgent()); 280 httpproc.addInterceptor(new RequestExpectContinue()); 281 // HTTP state management interceptors 282 httpproc.addInterceptor(new RequestAddCookies()); 283 httpproc.addInterceptor(new ResponseProcessCookies()); 284 // HTTP authentication interceptors 285 httpproc.addInterceptor(new RequestTargetAuthentication()); 286 httpproc.addInterceptor(new RequestProxyAuthentication()); 287 return httpproc; 288 } 289 290 291 @Override 292 protected HttpRequestRetryHandler createHttpRequestRetryHandler() { 293 return new DefaultHttpRequestRetryHandler(); 294 } 295 296 297 @Override 298 protected RedirectHandler createRedirectHandler() { 299 return new DefaultRedirectHandler(); 300 } 301 302 303 @Override 304 protected AuthenticationHandler createTargetAuthenticationHandler() { 305 return new DefaultTargetAuthenticationHandler(); 306 } 307 308 309 @Override 310 protected AuthenticationHandler createProxyAuthenticationHandler() { 311 return new DefaultProxyAuthenticationHandler(); 312 } 313 314 315 @Override 316 protected CookieStore createCookieStore() { 317 return new BasicCookieStore(); 318 } 319 320 321 @Override 322 protected CredentialsProvider createCredentialsProvider() { 323 return new BasicCredentialsProvider(); 324 } 325 326 327 @Override 328 protected HttpRoutePlanner createHttpRoutePlanner() { 329 // BEGIN android-changed 330 // Use the proxy specified by system properties 331 return new ProxySelectorRoutePlanner(getConnectionManager().getSchemeRegistry(), null); 332 // END android-changed 333 } 334 335 336 @Override 337 protected UserTokenHandler createUserTokenHandler() { 338 return new DefaultUserTokenHandler(); 339 } 340 341 } // class DefaultHttpClient 342