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.HttpClient;
33  import org.apache.http.client.protocol.RequestAddCookies;
34  import org.apache.http.client.protocol.RequestAuthCache;
35  import org.apache.http.client.protocol.RequestClientConnControl;
36  import org.apache.http.client.protocol.RequestDefaultHeaders;
37  import org.apache.http.client.protocol.RequestProxyAuthentication;
38  import org.apache.http.client.protocol.RequestTargetAuthentication;
39  import org.apache.http.client.protocol.ResponseProcessCookies;
40  import org.apache.http.conn.ClientConnectionManager;
41  import org.apache.http.params.CoreConnectionPNames;
42  import org.apache.http.params.CoreProtocolPNames;
43  import org.apache.http.params.HttpConnectionParams;
44  import org.apache.http.params.HttpParams;
45  import org.apache.http.params.HttpProtocolParams;
46  import org.apache.http.params.SyncBasicHttpParams;
47  import org.apache.http.protocol.BasicHttpProcessor;
48  import org.apache.http.protocol.HTTP;
49  import org.apache.http.protocol.RequestContent;
50  import org.apache.http.protocol.RequestExpectContinue;
51  import org.apache.http.protocol.RequestTargetHost;
52  import org.apache.http.protocol.RequestUserAgent;
53  import org.apache.http.util.VersionInfo;
54  
55  /**
56   * Default implementation of {@link HttpClient} pre-configured for most common use scenarios.
57   * <p>
58   * Please see the Javadoc for {@link #createHttpProcessor()} for the details of the interceptors
59   * that are set up by default.
60   * <p>
61   * Additional interceptors can be added as follows, but
62   * take care not to add the same interceptor more than once.
63   * <pre>
64   * DefaultHttpClient httpclient = new DefaultHttpClient();
65   * httpclient.addRequestInterceptor(new RequestAcceptEncoding());
66   * httpclient.addResponseInterceptor(new ResponseContentEncoding());
67   * </pre>
68   * <p>
69   * This class sets up the following parameters if not explicitly set:
70   * <ul>
71   * <li>Version: HttpVersion.HTTP_1_1</li>
72   * <li>ContentCharset: HTTP.DEFAULT_CONTENT_CHARSET</li>
73   * <li>NoTcpDelay: true</li>
74   * <li>SocketBufferSize: 8192</li>
75   * <li>UserAgent: Apache-HttpClient/release (java 1.5)</li>
76   * </ul>
77   * <p>
78   * The following parameters can be used to customize the behavior of this
79   * class:
80   * <ul>
81   *  <li>{@link org.apache.http.params.CoreProtocolPNames#PROTOCOL_VERSION}</li>
82   *  <li>{@link org.apache.http.params.CoreProtocolPNames#STRICT_TRANSFER_ENCODING}</li>
83   *  <li>{@link org.apache.http.params.CoreProtocolPNames#HTTP_ELEMENT_CHARSET}</li>
84   *  <li>{@link org.apache.http.params.CoreProtocolPNames#USE_EXPECT_CONTINUE}</li>
85   *  <li>{@link org.apache.http.params.CoreProtocolPNames#WAIT_FOR_CONTINUE}</li>
86   *  <li>{@link org.apache.http.params.CoreProtocolPNames#USER_AGENT}</li>
87   *  <li>{@link org.apache.http.params.CoreConnectionPNames#TCP_NODELAY}</li>
88   *  <li>{@link org.apache.http.params.CoreConnectionPNames#SO_TIMEOUT}</li>
89   *  <li>{@link org.apache.http.params.CoreConnectionPNames#SO_LINGER}</li>
90   *  <li>{@link org.apache.http.params.CoreConnectionPNames#SO_REUSEADDR}</li>
91   *  <li>{@link org.apache.http.params.CoreConnectionPNames#SOCKET_BUFFER_SIZE}</li>
92   *  <li>{@link org.apache.http.params.CoreConnectionPNames#CONNECTION_TIMEOUT}</li>
93   *  <li>{@link org.apache.http.params.CoreConnectionPNames#MAX_LINE_LENGTH}</li>
94   *  <li>{@link org.apache.http.params.CoreConnectionPNames#MAX_HEADER_COUNT}</li>
95   *  <li>{@link org.apache.http.params.CoreConnectionPNames#STALE_CONNECTION_CHECK}</li>
96   *  <li>{@link org.apache.http.conn.params.ConnRoutePNames#FORCED_ROUTE}</li>
97   *  <li>{@link org.apache.http.conn.params.ConnRoutePNames#LOCAL_ADDRESS}</li>
98   *  <li>{@link org.apache.http.conn.params.ConnRoutePNames#DEFAULT_PROXY}</li>
99   *  <li>{@link org.apache.http.cookie.params.CookieSpecPNames#DATE_PATTERNS}</li>
100  *  <li>{@link org.apache.http.cookie.params.CookieSpecPNames#SINGLE_COOKIE_HEADER}</li>
101  *  <li>{@link org.apache.http.auth.params.AuthPNames#CREDENTIAL_CHARSET}</li>
102  *  <li>{@link org.apache.http.client.params.ClientPNames#COOKIE_POLICY}</li>
103  *  <li>{@link org.apache.http.client.params.ClientPNames#HANDLE_AUTHENTICATION}</li>
104  *  <li>{@link org.apache.http.client.params.ClientPNames#HANDLE_REDIRECTS}</li>
105  *  <li>{@link org.apache.http.client.params.ClientPNames#MAX_REDIRECTS}</li>
106  *  <li>{@link org.apache.http.client.params.ClientPNames#ALLOW_CIRCULAR_REDIRECTS}</li>
107  *  <li>{@link org.apache.http.client.params.ClientPNames#VIRTUAL_HOST}</li>
108  *  <li>{@link org.apache.http.client.params.ClientPNames#DEFAULT_HOST}</li>
109  *  <li>{@link org.apache.http.client.params.ClientPNames#DEFAULT_HEADERS}</li>
110  *  <li>{@link org.apache.http.client.params.ClientPNames#CONN_MANAGER_TIMEOUT}</li>
111  * </ul>
112  *
113  * @since 4.0
114  */
115 @ThreadSafe
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         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 CoreProtocolPNames#PROTOCOL_VERSION}: 1.1</li>
167      * <li>{@link CoreProtocolPNames#HTTP_CONTENT_CHARSET}: ISO-8859-1</li>
168      * <li>{@link CoreConnectionPNames#TCP_NODELAY}: true</li>
169      * <li>{@link CoreConnectionPNames#SOCKET_BUFFER_SIZE}: 8192</li>
170      * <li>{@link CoreProtocolPNames#USER_AGENT}: Apache-HttpClient/<release> (java 1.5)</li>
171      * </ul>
172      */
173     public static void setDefaultHttpParams(HttpParams params) {
174         HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
175         HttpProtocolParams.setContentCharset(params, HTTP.DEF_CONTENT_CHARSET.name());
176         HttpConnectionParams.setTcpNoDelay(params, true);
177         HttpConnectionParams.setSocketBufferSize(params, 8192);
178 
179         // determine the release version from packaged version info
180         final VersionInfo vi = VersionInfo.loadVersionInfo
181             ("org.apache.http.client", DefaultHttpClient.class.getClassLoader());
182         final String release = (vi != null) ?
183             vi.getRelease() : VersionInfo.UNAVAILABLE;
184         HttpProtocolParams.setUserAgent(params,
185                 "Apache-HttpClient/" + release + " (java 1.5)");
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         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 }