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  
28  package org.apache.http.impl.client;
29  
30  import org.apache.http.HttpVersion;
31  import org.apache.http.annotation.ThreadSafe;
32  import org.apache.http.client.protocol.RequestAddCookies;
33  import org.apache.http.client.protocol.RequestAuthCache;
34  import org.apache.http.client.protocol.RequestClientConnControl;
35  import org.apache.http.client.protocol.RequestDefaultHeaders;
36  import org.apache.http.client.protocol.RequestProxyAuthentication;
37  import org.apache.http.client.protocol.RequestTargetAuthentication;
38  import org.apache.http.client.protocol.ResponseProcessCookies;
39  import org.apache.http.conn.ClientConnectionManager;
40  import org.apache.http.params.HttpConnectionParams;
41  import org.apache.http.params.HttpParams;
42  import org.apache.http.params.HttpProtocolParams;
43  import org.apache.http.params.SyncBasicHttpParams;
44  import org.apache.http.protocol.BasicHttpProcessor;
45  import org.apache.http.protocol.HTTP;
46  import org.apache.http.protocol.RequestContent;
47  import org.apache.http.protocol.RequestExpectContinue;
48  import org.apache.http.protocol.RequestTargetHost;
49  import org.apache.http.protocol.RequestUserAgent;
50  import org.apache.http.util.VersionInfo;
51  
52  /**
53   * Default implementation of {@link org.apache.http.client.HttpClient} pre-configured
54   * for most common use scenarios.
55   * <p>
56   * Please see the Javadoc for {@link #createHttpProcessor()} for the details of the interceptors
57   * that are set up by default.
58   * <p>
59   * Additional interceptors can be added as follows, but
60   * take care not to add the same interceptor more than once.
61   * <pre>
62   * DefaultHttpClient httpclient = new DefaultHttpClient();
63   * httpclient.addRequestInterceptor(new RequestAcceptEncoding());
64   * httpclient.addResponseInterceptor(new ResponseContentEncoding());
65   * </pre>
66   * <p>
67   * This class sets up the following parameters if not explicitly set:
68   * <ul>
69   * <li>Version: HttpVersion.HTTP_1_1</li>
70   * <li>ContentCharset: HTTP.DEFAULT_CONTENT_CHARSET</li>
71   * <li>NoTcpDelay: true</li>
72   * <li>SocketBufferSize: 8192</li>
73   * <li>UserAgent: Apache-HttpClient/release (java 1.5)</li>
74   * </ul>
75   * <p>
76   * The following parameters can be used to customize the behavior of this
77   * class:
78   * <ul>
79   *  <li>{@link org.apache.http.params.CoreProtocolPNames#PROTOCOL_VERSION}</li>
80   *  <li>{@link org.apache.http.params.CoreProtocolPNames#STRICT_TRANSFER_ENCODING}</li>
81   *  <li>{@link org.apache.http.params.CoreProtocolPNames#HTTP_ELEMENT_CHARSET}</li>
82   *  <li>{@link org.apache.http.params.CoreProtocolPNames#USE_EXPECT_CONTINUE}</li>
83   *  <li>{@link org.apache.http.params.CoreProtocolPNames#WAIT_FOR_CONTINUE}</li>
84   *  <li>{@link org.apache.http.params.CoreProtocolPNames#USER_AGENT}</li>
85   *  <li>{@link org.apache.http.params.CoreConnectionPNames#TCP_NODELAY}</li>
86   *  <li>{@link org.apache.http.params.CoreConnectionPNames#SO_TIMEOUT}</li>
87   *  <li>{@link org.apache.http.params.CoreConnectionPNames#SO_LINGER}</li>
88   *  <li>{@link org.apache.http.params.CoreConnectionPNames#SO_REUSEADDR}</li>
89   *  <li>{@link org.apache.http.params.CoreConnectionPNames#SOCKET_BUFFER_SIZE}</li>
90   *  <li>{@link org.apache.http.params.CoreConnectionPNames#CONNECTION_TIMEOUT}</li>
91   *  <li>{@link org.apache.http.params.CoreConnectionPNames#MAX_LINE_LENGTH}</li>
92   *  <li>{@link org.apache.http.params.CoreConnectionPNames#MAX_HEADER_COUNT}</li>
93   *  <li>{@link org.apache.http.params.CoreConnectionPNames#STALE_CONNECTION_CHECK}</li>
94   *  <li>{@link org.apache.http.conn.params.ConnRoutePNames#FORCED_ROUTE}</li>
95   *  <li>{@link org.apache.http.conn.params.ConnRoutePNames#LOCAL_ADDRESS}</li>
96   *  <li>{@link org.apache.http.conn.params.ConnRoutePNames#DEFAULT_PROXY}</li>
97   *  <li>{@link org.apache.http.cookie.params.CookieSpecPNames#DATE_PATTERNS}</li>
98   *  <li>{@link org.apache.http.cookie.params.CookieSpecPNames#SINGLE_COOKIE_HEADER}</li>
99   *  <li>{@link org.apache.http.auth.params.AuthPNames#CREDENTIAL_CHARSET}</li>
100  *  <li>{@link org.apache.http.client.params.ClientPNames#COOKIE_POLICY}</li>
101  *  <li>{@link org.apache.http.client.params.ClientPNames#HANDLE_AUTHENTICATION}</li>
102  *  <li>{@link org.apache.http.client.params.ClientPNames#HANDLE_REDIRECTS}</li>
103  *  <li>{@link org.apache.http.client.params.ClientPNames#MAX_REDIRECTS}</li>
104  *  <li>{@link org.apache.http.client.params.ClientPNames#ALLOW_CIRCULAR_REDIRECTS}</li>
105  *  <li>{@link org.apache.http.client.params.ClientPNames#VIRTUAL_HOST}</li>
106  *  <li>{@link org.apache.http.client.params.ClientPNames#DEFAULT_HOST}</li>
107  *  <li>{@link org.apache.http.client.params.ClientPNames#DEFAULT_HEADERS}</li>
108  *  <li>{@link org.apache.http.client.params.ClientPNames#CONN_MANAGER_TIMEOUT}</li>
109  * </ul>
110  *
111  * @since 4.0
112  *
113  * @deprecated (4.3) use {@link HttpClientBuilder}.
114  */
115 @ThreadSafe
116 @Deprecated
117 public class DefaultHttpClient extends AbstractHttpClient {
118 
119     /**
120      * Creates a new HTTP client from parameters and a connection manager.
121      *
122      * @param params    the parameters
123      * @param conman    the connection manager
124      */
125     public DefaultHttpClient(
126             final ClientConnectionManager conman,
127             final HttpParams params) {
128         super(conman, params);
129     }
130 
131 
132     /**
133      * @since 4.1
134      */
135     public DefaultHttpClient(
136             final ClientConnectionManager conman) {
137         super(conman, null);
138     }
139 
140 
141     public DefaultHttpClient(final HttpParams params) {
142         super(null, params);
143     }
144 
145 
146     public DefaultHttpClient() {
147         super(null, null);
148     }
149 
150 
151     /**
152      * Creates the default set of HttpParams by invoking {@link DefaultHttpClient#setDefaultHttpParams(HttpParams)}
153      *
154      * @return a new instance of {@link SyncBasicHttpParams} with the defaults applied to it.
155      */
156     @Override
157     protected HttpParams createHttpParams() {
158         final HttpParams params = new SyncBasicHttpParams();
159         setDefaultHttpParams(params);
160         return params;
161     }
162 
163     /**
164      * Saves the default set of HttpParams in the provided parameter.
165      * These are:
166      * <ul>
167      * <li>{@link org.apache.http.params.CoreProtocolPNames#PROTOCOL_VERSION}:
168      *   1.1</li>
169      * <li>{@link org.apache.http.params.CoreProtocolPNames#HTTP_CONTENT_CHARSET}:
170      *   ISO-8859-1</li>
171      * <li>{@link org.apache.http.params.CoreConnectionPNames#TCP_NODELAY}:
172      *   true</li>
173      * <li>{@link org.apache.http.params.CoreConnectionPNames#SOCKET_BUFFER_SIZE}:
174      *   8192</li>
175      * <li>{@link org.apache.http.params.CoreProtocolPNames#USER_AGENT}:
176      *   Apache-HttpClient (Java 1.5)</li>
177      * </ul>
178      */
179     public static void setDefaultHttpParams(final HttpParams params) {
180         HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
181         HttpProtocolParams.setContentCharset(params, HTTP.DEF_CONTENT_CHARSET.name());
182         HttpConnectionParams.setTcpNoDelay(params, true);
183         HttpConnectionParams.setSocketBufferSize(params, 8192);
184         HttpProtocolParams.setUserAgent(params, VersionInfo.getUserAgent("Apache-HttpClient",
185                 "org.apache.http.client", DefaultHttpClient.class));
186     }
187 
188     /**
189     * Create the processor with the following interceptors:
190     * <ul>
191     * <li>{@link RequestDefaultHeaders}</li>
192     * <li>{@link RequestContent}</li>
193     * <li>{@link RequestTargetHost}</li>
194     * <li>{@link RequestClientConnControl}</li>
195     * <li>{@link RequestUserAgent}</li>
196     * <li>{@link RequestExpectContinue}</li>
197     * <li>{@link RequestAddCookies}</li>
198     * <li>{@link ResponseProcessCookies}</li>
199     * <li>{@link RequestAuthCache}</li>
200     * <li>{@link RequestTargetAuthentication}</li>
201     * <li>{@link RequestProxyAuthentication}</li>
202     * </ul>
203     * <p>
204     * @return the processor with the added interceptors.
205     */
206     @Override
207     protected BasicHttpProcessor createHttpProcessor() {
208         final BasicHttpProcessor httpproc = new BasicHttpProcessor();
209         httpproc.addInterceptor(new RequestDefaultHeaders());
210         // Required protocol interceptors
211         httpproc.addInterceptor(new RequestContent());
212         httpproc.addInterceptor(new RequestTargetHost());
213         // Recommended protocol interceptors
214         httpproc.addInterceptor(new RequestClientConnControl());
215         httpproc.addInterceptor(new RequestUserAgent());
216         httpproc.addInterceptor(new RequestExpectContinue());
217         // HTTP state management interceptors
218         httpproc.addInterceptor(new RequestAddCookies());
219         httpproc.addInterceptor(new ResponseProcessCookies());
220         // HTTP authentication interceptors
221         httpproc.addInterceptor(new RequestAuthCache());
222         httpproc.addInterceptor(new RequestTargetAuthentication());
223         httpproc.addInterceptor(new RequestProxyAuthentication());
224         return httpproc;
225     }
226 
227 }