michael@0: /*
michael@0: * ====================================================================
michael@0: * Licensed to the Apache Software Foundation (ASF) under one
michael@0: * or more contributor license agreements. See the NOTICE file
michael@0: * distributed with this work for additional information
michael@0: * regarding copyright ownership. The ASF licenses this file
michael@0: * to you under the Apache License, Version 2.0 (the
michael@0: * "License"); you may not use this file except in compliance
michael@0: * with the License. You may obtain a copy of the License at
michael@0: *
michael@0: * http://www.apache.org/licenses/LICENSE-2.0
michael@0: *
michael@0: * Unless required by applicable law or agreed to in writing,
michael@0: * software distributed under the License is distributed on an
michael@0: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
michael@0: * KIND, either express or implied. See the License for the
michael@0: * specific language governing permissions and limitations
michael@0: * under the License.
michael@0: * ====================================================================
michael@0: *
michael@0: * This software consists of voluntary contributions made by many
michael@0: * individuals on behalf of the Apache Software Foundation. For more
michael@0: * information on the Apache Software Foundation, please see
michael@0: * .
michael@0: *
michael@0: */
michael@0:
michael@0: package ch.boye.httpclientandroidlib.impl.client;
michael@0:
michael@0: import ch.boye.httpclientandroidlib.HttpVersion;
michael@0: import ch.boye.httpclientandroidlib.annotation.ThreadSafe;
michael@0: import ch.boye.httpclientandroidlib.client.HttpClient;
michael@0: import ch.boye.httpclientandroidlib.client.protocol.RequestAddCookies;
michael@0: import ch.boye.httpclientandroidlib.client.protocol.RequestAuthCache;
michael@0: import ch.boye.httpclientandroidlib.client.protocol.RequestClientConnControl;
michael@0: import ch.boye.httpclientandroidlib.client.protocol.RequestDefaultHeaders;
michael@0: import ch.boye.httpclientandroidlib.client.protocol.RequestProxyAuthentication;
michael@0: import ch.boye.httpclientandroidlib.client.protocol.RequestTargetAuthentication;
michael@0: import ch.boye.httpclientandroidlib.client.protocol.ResponseAuthCache;
michael@0: import ch.boye.httpclientandroidlib.client.protocol.ResponseProcessCookies;
michael@0: import ch.boye.httpclientandroidlib.conn.ClientConnectionManager;
michael@0: import ch.boye.httpclientandroidlib.params.CoreConnectionPNames;
michael@0: import ch.boye.httpclientandroidlib.params.CoreProtocolPNames;
michael@0: import ch.boye.httpclientandroidlib.params.HttpConnectionParams;
michael@0: import ch.boye.httpclientandroidlib.params.HttpParams;
michael@0: import ch.boye.httpclientandroidlib.params.HttpProtocolParams;
michael@0: import ch.boye.httpclientandroidlib.params.SyncBasicHttpParams;
michael@0: import ch.boye.httpclientandroidlib.protocol.BasicHttpProcessor;
michael@0: import ch.boye.httpclientandroidlib.protocol.HTTP;
michael@0: import ch.boye.httpclientandroidlib.protocol.RequestContent;
michael@0: import ch.boye.httpclientandroidlib.protocol.RequestExpectContinue;
michael@0: import ch.boye.httpclientandroidlib.protocol.RequestTargetHost;
michael@0: import ch.boye.httpclientandroidlib.protocol.RequestUserAgent;
michael@0: import ch.boye.httpclientandroidlib.util.VersionInfo;
michael@0:
michael@0: /**
michael@0: * Default implementation of {@link HttpClient} pre-configured for most common use scenarios.
michael@0: *
michael@0: * This class creates the following chain of protocol interceptors per default:
michael@0: *
michael@0: * - {@link RequestDefaultHeaders}
michael@0: * - {@link RequestContent}
michael@0: * - {@link RequestTargetHost}
michael@0: * - {@link RequestClientConnControl}
michael@0: * - {@link RequestUserAgent}
michael@0: * - {@link RequestExpectContinue}
michael@0: * - {@link RequestAddCookies}
michael@0: * - {@link ResponseProcessCookies}
michael@0: * - {@link RequestTargetAuthentication}
michael@0: * - {@link RequestProxyAuthentication}
michael@0: *
michael@0: *
michael@0: * This class sets up the following parameters if not explicitly set:
michael@0: *
michael@0: * - Version: HttpVersion.HTTP_1_1
michael@0: * - ContentCharset: HTTP.DEFAULT_CONTENT_CHARSET
michael@0: * - NoTcpDelay: true
michael@0: * - SocketBufferSize: 8192
michael@0: * - UserAgent: Apache-HttpClient/release (java 1.5)
michael@0: *
michael@0: *
michael@0: * The following parameters can be used to customize the behavior of this
michael@0: * class:
michael@0: *
michael@0: * - {@link ch.boye.httpclientandroidlib.params.CoreProtocolPNames#PROTOCOL_VERSION}
michael@0: * - {@link ch.boye.httpclientandroidlib.params.CoreProtocolPNames#STRICT_TRANSFER_ENCODING}
michael@0: * - {@link ch.boye.httpclientandroidlib.params.CoreProtocolPNames#HTTP_ELEMENT_CHARSET}
michael@0: * - {@link ch.boye.httpclientandroidlib.params.CoreProtocolPNames#USE_EXPECT_CONTINUE}
michael@0: * - {@link ch.boye.httpclientandroidlib.params.CoreProtocolPNames#WAIT_FOR_CONTINUE}
michael@0: * - {@link ch.boye.httpclientandroidlib.params.CoreProtocolPNames#USER_AGENT}
michael@0: * - {@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#TCP_NODELAY}
michael@0: * - {@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#SO_TIMEOUT}
michael@0: * - {@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#SO_LINGER}
michael@0: * - {@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#SO_REUSEADDR}
michael@0: * - {@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#SOCKET_BUFFER_SIZE}
michael@0: * - {@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#CONNECTION_TIMEOUT}
michael@0: * - {@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#MAX_LINE_LENGTH}
michael@0: * - {@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#MAX_HEADER_COUNT}
michael@0: * - {@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#STALE_CONNECTION_CHECK}
michael@0: * - {@link ch.boye.httpclientandroidlib.conn.params.ConnRoutePNames#FORCED_ROUTE}
michael@0: * - {@link ch.boye.httpclientandroidlib.conn.params.ConnRoutePNames#LOCAL_ADDRESS}
michael@0: * - {@link ch.boye.httpclientandroidlib.conn.params.ConnRoutePNames#DEFAULT_PROXY}
michael@0: * - {@link ch.boye.httpclientandroidlib.cookie.params.CookieSpecPNames#DATE_PATTERNS}
michael@0: * - {@link ch.boye.httpclientandroidlib.cookie.params.CookieSpecPNames#SINGLE_COOKIE_HEADER}
michael@0: * - {@link ch.boye.httpclientandroidlib.auth.params.AuthPNames#CREDENTIAL_CHARSET}
michael@0: * - {@link ch.boye.httpclientandroidlib.client.params.ClientPNames#COOKIE_POLICY}
michael@0: * - {@link ch.boye.httpclientandroidlib.client.params.ClientPNames#HANDLE_AUTHENTICATION}
michael@0: * - {@link ch.boye.httpclientandroidlib.client.params.ClientPNames#HANDLE_REDIRECTS}
michael@0: * - {@link ch.boye.httpclientandroidlib.client.params.ClientPNames#MAX_REDIRECTS}
michael@0: * - {@link ch.boye.httpclientandroidlib.client.params.ClientPNames#ALLOW_CIRCULAR_REDIRECTS}
michael@0: * - {@link ch.boye.httpclientandroidlib.client.params.ClientPNames#VIRTUAL_HOST}
michael@0: * - {@link ch.boye.httpclientandroidlib.client.params.ClientPNames#DEFAULT_HOST}
michael@0: * - {@link ch.boye.httpclientandroidlib.client.params.ClientPNames#DEFAULT_HEADERS}
michael@0: * - {@link ch.boye.httpclientandroidlib.client.params.ClientPNames#CONNECTION_MANAGER_FACTORY_CLASS_NAME}
michael@0: *
michael@0: *
michael@0: * @since 4.0
michael@0: */
michael@0: @ThreadSafe
michael@0: public class DefaultHttpClient extends AbstractHttpClient {
michael@0:
michael@0: /**
michael@0: * Creates a new HTTP client from parameters and a connection manager.
michael@0: *
michael@0: * @param params the parameters
michael@0: * @param conman the connection manager
michael@0: */
michael@0: public DefaultHttpClient(
michael@0: final ClientConnectionManager conman,
michael@0: final HttpParams params) {
michael@0: super(conman, params);
michael@0: }
michael@0:
michael@0:
michael@0: /**
michael@0: * @since 4.1
michael@0: */
michael@0: public DefaultHttpClient(
michael@0: final ClientConnectionManager conman) {
michael@0: super(conman, null);
michael@0: }
michael@0:
michael@0:
michael@0: public DefaultHttpClient(final HttpParams params) {
michael@0: super(null, params);
michael@0: }
michael@0:
michael@0:
michael@0: public DefaultHttpClient() {
michael@0: super(null, null);
michael@0: }
michael@0:
michael@0:
michael@0: /**
michael@0: * Creates the default set of HttpParams by invoking {@link DefaultHttpClient#setDefaultHttpParams(HttpParams)}
michael@0: *
michael@0: * @return a new instance of {@link SyncBasicHttpParams} with the defaults applied to it.
michael@0: */
michael@0: @Override
michael@0: protected HttpParams createHttpParams() {
michael@0: HttpParams params = new SyncBasicHttpParams();
michael@0: setDefaultHttpParams(params);
michael@0: return params;
michael@0: }
michael@0:
michael@0: /**
michael@0: * Saves the default set of HttpParams in the provided parameter.
michael@0: * These are:
michael@0: *
michael@0: * - {@link CoreProtocolPNames#PROTOCOL_VERSION}: 1.1
michael@0: * - {@link CoreProtocolPNames#HTTP_CONTENT_CHARSET}: ISO-8859-1
michael@0: * - {@link CoreConnectionPNames#TCP_NODELAY}: true
michael@0: * - {@link CoreConnectionPNames#SOCKET_BUFFER_SIZE}: 8192
michael@0: * - {@link CoreProtocolPNames#USER_AGENT}: Apache-HttpClient/ (java 1.5)
michael@0: *
michael@0: */
michael@0: public static void setDefaultHttpParams(HttpParams params) {
michael@0: HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
michael@0: HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
michael@0: HttpConnectionParams.setTcpNoDelay(params, true);
michael@0: HttpConnectionParams.setSocketBufferSize(params, 8192);
michael@0:
michael@0: // determine the release version from packaged version info
michael@0: final VersionInfo vi = VersionInfo.loadVersionInfo
michael@0: ("ch.boye.httpclientandroidlib.client", DefaultHttpClient.class.getClassLoader());
michael@0: final String release = (vi != null) ?
michael@0: vi.getRelease() : VersionInfo.UNAVAILABLE;
michael@0: HttpProtocolParams.setUserAgent(params,
michael@0: "Apache-HttpClient/" + release + " (java 1.5)");
michael@0: }
michael@0:
michael@0:
michael@0: @Override
michael@0: protected BasicHttpProcessor createHttpProcessor() {
michael@0: BasicHttpProcessor httpproc = new BasicHttpProcessor();
michael@0: httpproc.addInterceptor(new RequestDefaultHeaders());
michael@0: // Required protocol interceptors
michael@0: httpproc.addInterceptor(new RequestContent());
michael@0: httpproc.addInterceptor(new RequestTargetHost());
michael@0: // Recommended protocol interceptors
michael@0: httpproc.addInterceptor(new RequestClientConnControl());
michael@0: httpproc.addInterceptor(new RequestUserAgent());
michael@0: httpproc.addInterceptor(new RequestExpectContinue());
michael@0: // HTTP state management interceptors
michael@0: httpproc.addInterceptor(new RequestAddCookies());
michael@0: httpproc.addInterceptor(new ResponseProcessCookies());
michael@0: // HTTP authentication interceptors
michael@0: httpproc.addInterceptor(new RequestAuthCache());
michael@0: httpproc.addInterceptor(new ResponseAuthCache());
michael@0: httpproc.addInterceptor(new RequestTargetAuthentication());
michael@0: httpproc.addInterceptor(new RequestProxyAuthentication());
michael@0: return httpproc;
michael@0: }
michael@0:
michael@0: }