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