View Javadoc

1   /*
2    * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/params/HttpMethodParams.java,v 1.17 2004/10/06 17:32:04 olegk Exp $
3    * $Revision: 354155 $
4    * $Date: 2005-12-05 15:18:10 -0500 (Mon, 05 Dec 2005) $
5    *
6    * ====================================================================
7    *
8    *  Copyright 1999-2004 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.commons.httpclient.params;
31  
32  import org.apache.commons.httpclient.HttpVersion;
33  import org.apache.commons.httpclient.cookie.CookiePolicy;
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  
37  /***
38   * This class represents a collection of HTTP protocol parameters applicable to 
39   * {@link org.apache.commons.httpclient.HttpMethod HTTP methods}. Protocol 
40   * parameters may be linked together to form a hierarchy. If a particular 
41   * parameter value has not been explicitly defined in the collection itself, 
42   * its value will be drawn from the parent collection of parameters.
43   * 
44   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
45   * @author Christian Kohlschuetter
46   * 
47   * @version $Revision: 354155 $
48   * 
49   * @since 3.0
50   */
51  public class HttpMethodParams extends DefaultHttpParams {
52  
53      /*** Log object for this class. */
54      private static final Log LOG = LogFactory.getLog(HttpMethodParams.class);
55  
56      /***
57       * Defines the content of the <tt>User-Agent</tt> header used by  
58       * {@link org.apache.commons.httpclient.HttpMethod HTTP methods}.
59       * <p>
60       * This parameter expects a value of type {@link String}.
61       * </p>
62       */
63      public static final String USER_AGENT = "http.useragent"; 
64  
65      /***
66       * Defines the {@link HttpVersion HTTP protocol version} used by  
67       * {@link org.apache.commons.httpclient.HttpMethod HTTP methods} per
68       * default.
69       * <p>
70       * This parameter expects a value of type {@link HttpVersion}.
71       * </p>
72       */
73      public static final String PROTOCOL_VERSION = "http.protocol.version"; 
74  
75      /***
76       * Defines whether {@link org.apache.commons.httpclient.HttpMethod HTTP methods} should
77       * reject ambiguous {@link org.apache.commons.httpclient.StatusLine HTTP status line}.
78       * <p>
79       * This parameter expects a value of type {@link Boolean}.
80       * </p>
81       */
82      public static final String UNAMBIGUOUS_STATUS_LINE = "http.protocol.unambiguous-statusline"; 
83  
84      /***
85       * Defines whether {@link org.apache.commons.httpclient.Cookie cookies} should be put on 
86       * a single {@link org.apache.commons.httpclient.Header response header}.
87       * <p>
88       * This parameter expects a value of type {@link Boolean}.
89       * </p>
90       */
91      public static final String SINGLE_COOKIE_HEADER = "http.protocol.single-cookie-header"; 
92  
93      /***
94       * Defines whether responses with an invalid <tt>Transfer-Encoding</tt> header should be 
95       * rejected.
96       * <p>
97       * This parameter expects a value of type {@link Boolean}.
98       * </p>
99       */
100     public static final String STRICT_TRANSFER_ENCODING = "http.protocol.strict-transfer-encoding"; 
101 
102     /***
103      * Defines whether the content body sent in response to 
104      * {@link org.apache.commons.httpclient.methods.HeadMethod} should be rejected.
105      * <p>
106      * This parameter expects a value of type {@link Boolean}.
107      * </p>
108      */
109     public static final String REJECT_HEAD_BODY = "http.protocol.reject-head-body"; 
110 
111     /***
112      * Sets period of time in milliseconds to wait for a content body sent in response to 
113      * {@link org.apache.commons.httpclient.methods.HeadMethod HEAD method} from a 
114      * non-compliant server. If the parameter is not set or set to <tt>-1</tt> non-compliant 
115      * response body check is disabled.
116      * <p>
117      * This parameter expects a value of type {@link Integer}.
118      * </p>
119      */
120     public static final String HEAD_BODY_CHECK_TIMEOUT = "http.protocol.head-body-timeout"; 
121 
122     /***
123      * <p>
124      * Activates 'Expect: 100-Continue' handshake for the 
125      * {@link org.apache.commons.httpclient.methods.ExpectContinueMethod 
126      * entity enclosing methods}. The purpose of the 'Expect: 100-Continue'
127      * handshake to allow a client that is sending a request message with 
128      * a request body to determine if the origin server is willing to 
129      * accept the request (based on the request headers) before the client
130      * sends the request body.
131      * </p>
132      * 
133      * <p>
134      * The use of the 'Expect: 100-continue' handshake can result in 
135      * noticable peformance improvement for entity enclosing requests
136      * (such as POST and PUT) that require the target server's 
137      * authentication.
138      * </p>
139      * 
140      * <p>
141      * 'Expect: 100-continue' handshake should be used with 
142      * caution, as it may cause problems with HTTP servers and 
143      * proxies that do not support HTTP/1.1 protocol.
144      * </p>
145      * 
146      * This parameter expects a value of type {@link Boolean}.
147      */
148     public static final String USE_EXPECT_CONTINUE = "http.protocol.expect-continue"; 
149 
150     /***
151      * Defines the charset to be used when encoding 
152      * {@link org.apache.commons.httpclient.Credentials}. If not defined then the 
153      * {@link #HTTP_ELEMENT_CHARSET} should be used.
154      * <p>
155      * This parameter expects a value of type {@link String}.
156      * </p>
157      */
158     public static final String CREDENTIAL_CHARSET = "http.protocol.credential-charset"; 
159     
160     /***
161      * Defines the charset to be used for encoding HTTP protocol elements.
162      * <p>
163      * This parameter expects a value of type {@link String}.
164      * </p>
165      */
166     public static final String HTTP_ELEMENT_CHARSET = "http.protocol.element-charset"; 
167     
168     /***
169      * Defines the charset to be used for encoding content body.
170      * <p>
171      * This parameter expects a value of type {@link String}.
172      * </p>
173      */
174     public static final String HTTP_CONTENT_CHARSET = "http.protocol.content-charset"; 
175     
176     /***
177      * Defines {@link CookiePolicy cookie policy} to be used for cookie management.
178      * <p>
179      * This parameter expects a value of type {@link String}.
180      * </p>
181      */
182     public static final String COOKIE_POLICY = "http.protocol.cookie-policy";
183     
184     /***
185      * Defines HttpClient's behavior when a response provides more bytes than
186      * expected (specified with Content-Length, for example).
187      * <p>
188      * Such surplus data makes the HTTP connection unreliable for keep-alive
189      * requests, as malicious response data (faked headers etc.) can lead to undesired
190      * results on the next request using that connection.
191      * </p>
192      * <p>
193      * If this parameter is set to <code>true</code>, any detection of extra
194      * input data will generate a warning in the log.
195      * </p>
196      * <p>
197      * This parameter expects a value of type {@link Boolean}.
198      * </p>
199      */
200     public static final String WARN_EXTRA_INPUT = "http.protocol.warn-extra-input";
201     
202     /***
203      * Defines the maximum number of ignorable lines before we expect
204      * a HTTP response's status code.
205      * <p>
206      * With HTTP/1.1 persistent connections, the problem arises that
207      * broken scripts could return a wrong Content-Length
208      * (there are more bytes sent than specified).<br />
209      * Unfortunately, in some cases, this is not possible after the bad response,
210      * but only before the next one. <br />
211      * So, HttpClient must be able to skip those surplus lines this way.
212      * </p>
213      * <p>
214      * Set this to 0 to disallow any garbage/empty lines before the status line.<br />
215      * To specify no limit, use {@link java.lang.Integer#MAX_VALUE} (default in lenient mode).
216      * </p>
217      *  
218      * This parameter expects a value of type {@link Integer}.
219      */
220     public static final String STATUS_LINE_GARBAGE_LIMIT = "http.protocol.status-line-garbage-limit";
221 
222     /***
223      * Sets the socket timeout (<tt>SO_TIMEOUT</tt>) in milliseconds to be used when executing the method. 
224      * A timeout value of zero is interpreted as an infinite timeout.
225      * <p>
226      * This parameter expects a value of type {@link Integer}.
227      * </p>
228      * @see java.net.SocketOptions#SO_TIMEOUT
229      */
230     public static final String SO_TIMEOUT = "http.socket.timeout"; 
231 
232     /***
233      * The key used to look up the date patterns used for parsing. The String patterns are stored
234      * in a {@link java.util.Collection} and must be compatible with 
235      * {@link java.text.SimpleDateFormat}.
236      * <p>
237      * This parameter expects a value of type {@link java.util.Collection}.
238      * </p>
239      */
240     public static final String DATE_PATTERNS = "http.dateparser.patterns";
241 
242     /***
243      * Sets the method retry handler parameter.
244      * <p>
245      * This parameter expects a value of type {@link org.apache.commons.httpclient.HttpMethodRetryHandler}.
246      * </p>
247      */ 
248     public static final String RETRY_HANDLER = "http.method.retry-handler";
249     
250     /***
251      * Sets the maximum buffered response size (in bytes) that triggers no warning. Buffered
252      * responses exceeding this size will trigger a warning in the log.
253      * <p>
254      * This parameter expects a value if type {@link Integer}.
255      * </p>
256      */
257     public static final String BUFFER_WARN_TRIGGER_LIMIT = "http.method.response.buffer.warnlimit";
258     
259     /***
260      * Defines the virtual host name.
261      * <p>
262      * This parameter expects a value of type {@link java.lang.String}. 
263      * </p>
264      */
265     public static final String VIRTUAL_HOST = "http.virtual-host"; 
266 
267     /***
268      * Sets the value to use as the multipart boundary.
269      * <p>
270      * This parameter expects a value if type {@link String}.
271      * </p>
272      * @see org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity
273      */
274     public static final String MULTIPART_BOUNDARY = "http.method.multipart.boundary";
275     
276     /***
277      * Creates a new collection of parameters with the collection returned
278      * by {@link #getDefaultParams()} as a parent. The collection will defer
279      * to its parent for a default value if a particular parameter is not 
280      * explicitly set in the collection itself.
281      * 
282      * @see #getDefaultParams()
283      */
284     public HttpMethodParams() {
285         super(getDefaultParams());
286     }
287 
288     /***
289      * Creates a new collection of parameters with the given parent. 
290      * The collection will defer to its parent for a default value 
291      * if a particular parameter is not explicitly set in the collection
292      * itself.
293      * 
294      * @param defaults the parent collection to defer to, if a parameter
295      * is not explictly set in the collection itself.
296      *
297      * @see #getDefaultParams()
298      */
299     public HttpMethodParams(HttpParams defaults) {
300         super(defaults);
301     }
302 
303     /***
304      * Returns the charset to be used for writing HTTP headers.
305      * @return The charset
306      */
307     public String getHttpElementCharset() {
308         String charset = (String) getParameter(HTTP_ELEMENT_CHARSET);
309         if (charset == null) {
310             LOG.warn("HTTP element charset not configured, using US-ASCII");
311             charset = "US-ASCII";
312         }
313         return charset;
314     }
315     
316     /***
317      * Sets the charset to be used for writing HTTP headers.
318      * @param charset The charset
319      */
320     public void setHttpElementCharset(String charset) {
321         setParameter(HTTP_ELEMENT_CHARSET, charset);
322     }
323 
324     /***
325      * Returns the default charset to be used for writing content body, 
326      * when no charset explicitly specified.
327      * @return The charset
328      */
329     public String getContentCharset() {
330         String charset = (String) getParameter(HTTP_CONTENT_CHARSET);
331         if (charset == null) {
332             LOG.warn("Default content charset not configured, using ISO-8859-1");
333             charset = "ISO-8859-1";
334         }
335         return charset;
336     }
337     
338     /***
339      * Sets the default charset to be used for writing content body,
340      * when no charset explicitly specified.
341      * @param charset The charset
342      */
343     public void setContentCharset(String charset) {
344         setParameter(HTTP_CONTENT_CHARSET, charset);
345     }
346 
347     /***
348      * Returns the charset to be used for {@link org.apache.commons.httpclient.Credentials}. If
349      * not configured the {@link #HTTP_ELEMENT_CHARSET HTTP element charset} is used.
350      * @return The charset
351      */
352     public String getCredentialCharset() {
353         String charset = (String) getParameter(CREDENTIAL_CHARSET);
354         if (charset == null) {
355             LOG.debug("Credential charset not configured, using HTTP element charset");
356             charset = getHttpElementCharset();
357         }
358         return charset;
359     }
360     
361     /***
362      * Sets the charset to be used for writing HTTP headers.
363      * @param charset The charset
364      */
365     public void setCredentialCharset(String charset) {
366         setParameter(CREDENTIAL_CHARSET, charset);
367     }
368     
369     /***
370      * Returns {@link HttpVersion HTTP protocol version} to be used by the 
371      * {@link org.apache.commons.httpclient.HttpMethod HTTP methods} that 
372      * this collection of parameters applies to. 
373      *
374      * @return {@link HttpVersion HTTP protocol version}
375      */
376     public HttpVersion getVersion() { 
377         Object param = getParameter(PROTOCOL_VERSION);
378         if (param == null) {
379             return HttpVersion.HTTP_1_1;
380         }
381         return (HttpVersion)param;
382     }
383     
384     /***
385      * Assigns the {@link HttpVersion HTTP protocol version} to be used by the 
386      * {@link org.apache.commons.httpclient.HttpMethod HTTP methods} that 
387      * this collection of parameters applies to. 
388      *
389      * @param version the {@link HttpVersion HTTP protocol version}
390      */
391     public void setVersion(HttpVersion version) {
392         setParameter(PROTOCOL_VERSION, version);
393     }
394 
395 
396     /***
397      * Returns {@link CookiePolicy cookie policy} to be used by the 
398      * {@link org.apache.commons.httpclient.HttpMethod HTTP methods} 
399      * this collection of parameters applies to. 
400      *
401      * @return {@link CookiePolicy cookie policy}
402      */
403     public String getCookiePolicy() { 
404         Object param = getParameter(COOKIE_POLICY);
405         if (param == null) {
406             return CookiePolicy.DEFAULT;
407         }
408         return (String)param;
409     }
410     
411     /***
412      * Assigns the {@link CookiePolicy cookie policy} to be used by the 
413      * {@link org.apache.commons.httpclient.HttpMethod HTTP methods} 
414      * this collection of parameters applies to. 
415      *
416      * @param policy the {@link CookiePolicy cookie policy}
417      */
418     public void setCookiePolicy(String policy) {
419         setParameter(COOKIE_POLICY, policy);
420     }
421 
422     /***
423      * Returns the default socket timeout (<tt>SO_TIMEOUT</tt>) in milliseconds which is the 
424      * timeout for waiting for data. A timeout value of zero is interpreted as an infinite 
425      * timeout.  
426      *
427      * @return timeout in milliseconds
428      */
429     public int getSoTimeout() {
430         return getIntParameter(SO_TIMEOUT, 0);
431     }
432 
433     /***
434      * Sets the default socket timeout (<tt>SO_TIMEOUT</tt>) in milliseconds which is the 
435      * timeout for waiting for data. A timeout value of zero is interpreted as an infinite 
436      * timeout.  
437      *
438      * @param timeout Timeout in milliseconds
439      */
440     public void setSoTimeout(int timeout) {
441         setIntParameter(SO_TIMEOUT, timeout);
442     }
443 
444     /***
445      * Sets the virtual host name.
446      * 
447      * @param hostname The host name
448      */
449     public void setVirtualHost(final String hostname) {
450         setParameter(VIRTUAL_HOST, hostname);
451     }
452 
453     /***
454      * Returns the virtual host name.
455      * 
456      * @return The virtual host name
457      */
458     public String getVirtualHost() {
459         return (String) getParameter(VIRTUAL_HOST);
460     }
461     
462     private static final String[] PROTOCOL_STRICTNESS_PARAMETERS = {
463         UNAMBIGUOUS_STATUS_LINE,
464         SINGLE_COOKIE_HEADER,
465         STRICT_TRANSFER_ENCODING,
466         REJECT_HEAD_BODY,
467         WARN_EXTRA_INPUT
468     };
469     
470     /***
471      * Makes the {@link org.apache.commons.httpclient.HttpMethod HTTP methods} 
472      * strictly follow the HTTP protocol specification (RFC 2616 and other relevant RFCs).
473      * It must be noted that popular HTTP agents have different degree of HTTP protocol 
474      * compliance and some HTTP serves are programmed to expect the behaviour that does not 
475      * strictly adhere to the HTTP specification.  
476      */
477     public void makeStrict() {
478         setParameters(PROTOCOL_STRICTNESS_PARAMETERS, Boolean.TRUE);
479         setIntParameter(STATUS_LINE_GARBAGE_LIMIT, 0);
480     }
481 
482     /***
483      * Makes the {@link org.apache.commons.httpclient.HttpMethod HTTP methods}
484      * attempt to mimic the exact behaviour of commonly used HTTP agents, 
485      * which many HTTP servers expect, even though such behaviour may violate   
486      * the HTTP protocol specification (RFC 2616 and other relevant RFCs).
487      */
488     public void makeLenient() {
489         setParameters(PROTOCOL_STRICTNESS_PARAMETERS, Boolean.FALSE);
490         setIntParameter(STATUS_LINE_GARBAGE_LIMIT, Integer.MAX_VALUE);
491     }
492 
493 }