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