Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/http/params/HttpProtocolParams.java


1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/tags/4.0-alpha2/src/java/org/apache/http/params/HttpProtocolParams.java $
3    * $Revision: 376961 $
4    * $Date: 2006-02-11 11:32:50 +0100 (Sat, 11 Feb 2006) $
5    *
6    * ====================================================================
7    *
8    *  Copyright 1999-2006 The Apache Software Foundation
9    *
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
27   *
28   */
29  
30  package org.apache.http.params;
31  
32  import org.apache.http.HttpVersion;
33  import org.apache.http.protocol.HTTP;
34  
35  /**
36   * This class implements an adaptor around the {@link HttpParams} interface
37   * to simplify manipulation of the HTTP protocol specific parameters.
38   * 
39   * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
40   * 
41   * @version $Revision: 376961 $
42   * 
43   * @since 4.0
44   */
45  public final class HttpProtocolParams {
46  
47      /**
48       * Defines the {@link HttpVersion HTTP protocol version} used per default.
49       * <p>
50       * This parameter expects a value of type {@link HttpVersion}.
51       * </p>
52       */
53      public static final String PROTOCOL_VERSION = "http.protocol.version"; 
54  
55      /**
56       * Defines the charset to be used when encoding credentials.
57       * If not defined then the 
58       * {@link #HTTP_ELEMENT_CHARSET} should be used.
59       * <p>
60       * This parameter expects a value of type {@link String}.
61       * </p>
62       */
63      public static final String CREDENTIAL_CHARSET = "http.protocol.credential-charset"; 
64      
65      /**
66       * Defines the charset to be used for encoding HTTP protocol elements.
67       * <p>
68       * This parameter expects a value of type {@link String}.
69       * </p>
70       */
71      public static final String HTTP_ELEMENT_CHARSET = "http.protocol.element-charset"; 
72      
73      /**
74       * Defines the charset to be used per default for encoding content body.
75       * <p>
76       * This parameter expects a value of type {@link String}.
77       * </p>
78       */
79      public static final String HTTP_CONTENT_CHARSET = "http.protocol.content-charset"; 
80      
81      /**
82       * Defines the content of the <tt>User-Agent</tt> header.
83       * <p>
84       * This parameter expects a value of type {@link String}.
85       * </p>
86       */
87      public static final String USER_AGENT = "http.useragent"; 
88  
89      /**
90       * Defines the content of the <tt>Server</tt> header.
91       * <p>
92       * This parameter expects a value of type {@link String}.
93       * </p>
94       */
95      public static final String ORIGIN_SERVER = "http.origin-server"; 
96  
97      /**
98       * Defines the maximum number of ignorable lines before we expect
99       * a HTTP response's status code.
100      * <p>
101      * With HTTP/1.1 persistent connections, the problem arises that
102      * broken scripts could return a wrong Content-Length
103      * (there are more bytes sent than specified).<br />
104      * Unfortunately, in some cases, this is not possible after the bad response,
105      * but only before the next one. <br />
106      * So, HttpClient must be able to skip those surplus lines this way.
107      * </p>
108      * <p>
109      * Set this to 0 to disallow any garbage/empty lines before the status line.<br />
110      * To specify no limit, use {@link java.lang.Integer#MAX_VALUE} (default in lenient mode).
111      * </p>
112      *  
113      * This parameter expects a value of type {@link Integer}.
114      */
115     public static final String STATUS_LINE_GARBAGE_LIMIT = "http.protocol.status-line-garbage-limit";
116 
117     /**
118      * The key used to look up the date patterns used for parsing. The String patterns are stored
119      * in a {@link java.util.Collection} and must be compatible with 
120      * {@link java.text.SimpleDateFormat}.
121      * <p>
122      * This parameter expects a value of type {@link java.util.Collection}.
123      * </p>
124      */
125     public static final String DATE_PATTERNS = "http.dateparser.patterns";
126 
127     /**
128      * Defines the virtual host name.
129      * <p>
130      * This parameter expects a value of type {@link java.lang.String}. 
131      * </p>
132      */
133     public static final String VIRTUAL_HOST = "http.virtual-host"; 
134 
135     /**
136      * Defines whether responses with an invalid <tt>Transfer-Encoding</tt> header should be 
137      * rejected.
138      * <p>
139      * This parameter expects a value of type {@link Boolean}.
140      * </p>
141      */
142     public static final String STRICT_TRANSFER_ENCODING = "http.protocol.strict-transfer-encoding"; 
143 
144     /**
145      * <p>
146      * Activates 'Expect: 100-Continue' handshake for the 
147      * entity enclosing methods. The purpose of the 'Expect: 100-Continue'
148      * handshake to allow a client that is sending a request message with 
149      * a request body to determine if the origin server is willing to 
150      * accept the request (based on the request headers) before the client
151      * sends the request body.
152      * </p>
153      * 
154      * <p>
155      * The use of the 'Expect: 100-continue' handshake can result in 
156      * noticable peformance improvement for entity enclosing requests
157      * (such as POST and PUT) that require the target server's 
158      * authentication.
159      * </p>
160      * 
161      * <p>
162      * 'Expect: 100-continue' handshake should be used with 
163      * caution, as it may cause problems with HTTP servers and 
164      * proxies that do not support HTTP/1.1 protocol.
165      * </p>
166      * 
167      * This parameter expects a value of type {@link Boolean}.
168      */
169     public static final String USE_EXPECT_CONTINUE = "http.protocol.expect-continue"; 
170 
171     /**
172      */
173     private HttpProtocolParams() {
174         super();
175     }
176 
177     /**
178      * Returns the charset to be used for writing HTTP headers.
179      * @return The charset
180      */
181     public static String getHttpElementCharset(final HttpParams params) {
182         if (params == null) {
183             throw new IllegalArgumentException("HTTP parameters may not be null");
184         }
185         String charset = (String) params.getParameter(HTTP_ELEMENT_CHARSET);
186         if (charset == null) {
187             charset = HTTP.DEFAULT_PROTOCOL_CHARSET;
188         }
189         return charset;
190     }
191     
192     /**
193      * Sets the charset to be used for writing HTTP headers.
194      * @param charset The charset
195      */
196     public static void setHttpElementCharset(final HttpParams params, final String charset) {
197         if (params == null) {
198             throw new IllegalArgumentException("HTTP parameters may not be null");
199         }
200         params.setParameter(HTTP_ELEMENT_CHARSET, charset);
201     }
202 
203     /**
204      * Returns the default charset to be used for writing content body, 
205      * when no charset explicitly specified.
206      * @return The charset
207      */
208     public static String getContentCharset(final HttpParams params) {
209         if (params == null) {
210             throw new IllegalArgumentException("HTTP parameters may not be null");
211         }
212         String charset = (String) params.getParameter(HTTP_CONTENT_CHARSET);
213         if (charset == null) {
214             charset = HTTP.DEFAULT_CONTENT_CHARSET;
215         }
216         return charset;
217     }
218     
219     /**
220      * Sets the default charset to be used for writing content body,
221      * when no charset explicitly specified.
222      * @param charset The charset
223      */
224     public static void setContentCharset(final HttpParams params, final String charset) {
225         if (params == null) {
226             throw new IllegalArgumentException("HTTP parameters may not be null");
227         }
228         params.setParameter(HTTP_CONTENT_CHARSET, charset);
229     }
230 
231     /**
232      * Returns the charset to be used for credentials.
233      * If not configured the {@link #HTTP_ELEMENT_CHARSET HTTP element charset}
234      * is used.
235      * @return The charset
236      */
237     public static String getCredentialCharset(final HttpParams params) {
238         if (params == null) {
239             throw new IllegalArgumentException("HTTP parameters may not be null");
240         }
241         String charset = (String) params.getParameter(CREDENTIAL_CHARSET);
242         if (charset == null) {
243             charset = getHttpElementCharset(params);
244         }
245         return charset;
246     }
247     
248     /**
249      * Sets the charset to be used for writing HTTP headers.
250      * @param charset The charset
251      */
252     public static void setCredentialCharset(final HttpParams params, final String charset) {
253         if (params == null) {
254             throw new IllegalArgumentException("HTTP parameters may not be null");
255         }
256         params.setParameter(CREDENTIAL_CHARSET, charset);
257     }
258     
259     /**
260      * Returns {@link HttpVersion HTTP protocol version} to be used per default. 
261      *
262      * @return {@link HttpVersion HTTP protocol version}
263      */
264     public static HttpVersion getVersion(final HttpParams params) { 
265         if (params == null) {
266             throw new IllegalArgumentException("HTTP parameters may not be null");
267         }
268         Object param = params.getParameter(PROTOCOL_VERSION);
269         if (param == null) {
270             return HttpVersion.HTTP_1_1;
271         }
272         return (HttpVersion)param;
273     }
274     
275     /**
276      * Assigns the {@link HttpVersion HTTP protocol version} to be used by the 
277      * HTTP methods that this collection of parameters applies to. 
278      *
279      * @param version the {@link HttpVersion HTTP protocol version}
280      */
281     public static void setVersion(final HttpParams params, final HttpVersion version) {
282         if (params == null) {
283             throw new IllegalArgumentException("HTTP parameters may not be null");
284         }
285         params.setParameter(PROTOCOL_VERSION, version);
286     }
287 
288     /**
289      * Sets the virtual host name.
290      * 
291      * @param hostname The host name
292      */
293     public static void setVirtualHost(final HttpParams params, final String hostname) {
294         if (params == null) {
295             throw new IllegalArgumentException("HTTP parameters may not be null");
296         }
297         params.setParameter(VIRTUAL_HOST, hostname);
298     }
299 
300     /**
301      * Returns the virtual host name.
302      * 
303      * @return The virtual host name
304      */
305     public static String getVirtualHost(final HttpParams params) {
306         if (params == null) {
307             throw new IllegalArgumentException("HTTP parameters may not be null");
308         }
309         return (String) params.getParameter(VIRTUAL_HOST);
310     }
311     
312     public static String getUserAgent(final HttpParams params) { 
313         if (params == null) {
314             throw new IllegalArgumentException("HTTP parameters may not be null");
315         }
316         return (String) params.getParameter(USER_AGENT);
317     }
318     
319     public static void setUserAgent(final HttpParams params, final String useragent) {
320         if (params == null) {
321             throw new IllegalArgumentException("HTTP parameters may not be null");
322         }
323         params.setParameter(USER_AGENT, useragent);
324     }
325 
326     public static boolean useExpectContinue(final HttpParams params) {
327         if (params == null) {
328             throw new IllegalArgumentException("HTTP parameters may not be null");
329         }
330         return params.getBooleanParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
331     }
332     
333     public static void setUseExpectContinue(final HttpParams params, boolean b) {
334         if (params == null) {
335             throw new IllegalArgumentException("HTTP parameters may not be null");
336         }
337         params.setBooleanParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, b);
338     }
339 }