KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > httpclient > methods > HeadMethod


1 /*
2  * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/HeadMethod.java,v 1.19.2.4 2004/06/13 20:24:49 olegk Exp $
3  * $Revision: 1.19.2.4 $
4  * $Date: 2004/06/13 20:24:49 $
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  * [Additional notices, if required by prior licensing conditions]
29  *
30  */

31
32 package org.apache.commons.httpclient.methods;
33
34 import java.io.IOException JavaDoc;
35
36 import org.apache.commons.httpclient.HttpConnection;
37 import org.apache.commons.httpclient.HttpException;
38 import org.apache.commons.httpclient.HttpMethodBase;
39 import org.apache.commons.httpclient.HttpState;
40 import org.apache.commons.logging.Log;
41 import org.apache.commons.logging.LogFactory;
42
43 /**
44  * Implements the HTTP HEAD method.
45  * <p>
46  * The HTTP HEAD method is defined in section 9.4 of
47  * <a HREF="http://www.ietf.org/rfc/rfc2616.txt">RFC2616</a>:
48  * <blockquote>
49  * The HEAD method is identical to GET except that the server MUST NOT
50  * return a message-body in the response. The metainformation contained
51  * in the HTTP headers in response to a HEAD request SHOULD be identical
52  * to the information sent in response to a GET request. This method can
53  * be used for obtaining metainformation about the entity implied by the
54  * request without transferring the entity-body itself. This method is
55  * often used for testing hypertext links for validity, accessibility,
56  * and recent modification.
57  * </blockquote>
58  * </p>
59  *
60  * @author <a HREF="mailto:remm@apache.org">Remy Maucherat</a>
61  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
62  * @author <a HREF="mailto:jsdever@apache.org">Jeff Dever</a>
63  * @author <a HREF="mailto:oleg@ural.ru">oleg Kalnichevski</a>
64  *
65  * @version $Revision: 1.19.2.4 $
66  * @since 1.0
67  */

68 public class HeadMethod extends HttpMethodBase {
69     //~ Static variables/initializers ··········································
70

71     /** Log object for this class. */
72     private static final Log LOG = LogFactory.getLog(HeadMethod.class);
73
74     private int bodyCheckTimeout = -1; /* Disabled per default */
75
76     //~ Constructors ···························································
77

78     /**
79      * No-arg constructor.
80      *
81      * @since 1.0
82      */

83     public HeadMethod() {
84         setFollowRedirects(true);
85     }
86
87     /**
88      * Constructor specifying a URI.
89      *
90      * @param uri either an absolute or relative URI
91      *
92      * @since 1.0
93      */

94     public HeadMethod(String JavaDoc uri) {
95         super(uri);
96         setFollowRedirects(true);
97     }
98
99     //~ Methods ································································
100

101     /**
102      * Returns <tt>"HEAD"</tt>.
103      *
104      * @return <tt>"HEAD"</tt>
105      *
106      * @since 2.0
107      */

108     public String JavaDoc getName() {
109         return "HEAD";
110     }
111
112     /**
113      * Recycles the HTTP method so that it can be used again.
114      * Note that all of the instance variables will be reset
115      * once this method has been called. This method will also
116      * release the connection being used by this HTTP method.
117      *
118      * @see #releaseConnection()
119      *
120      * @since 1.0
121      *
122      * @deprecated no longer supported and will be removed in the future
123      * version of HttpClient
124      */

125     public void recycle() {
126         super.recycle();
127         setFollowRedirects(true);
128     }
129
130     /**
131      * Overrides {@link HttpMethodBase} method to <i>not</i> read a response
132      * body, despite the presence of a <tt>Content-Length</tt> or
133      * <tt>Transfer-Encoding</tt> header.
134      *
135      * @param state the {@link HttpState state} information associated with this method
136      * @param conn the {@link HttpConnection connection} used to execute
137      * this HTTP method
138      *
139      * @throws IOException if an I/O (transport) error occurs
140      * @throws HttpException if a protocol exception occurs.
141      * @throws HttpRecoverableException if a recoverable transport error occurs.
142      * Usually this kind of exceptions can be recovered from by
143      * retrying the HTTP method
144      *
145      * @see #readResponse
146      * @see #processResponseBody
147      *
148      * @since 2.0
149      */

150     protected void readResponseBody(HttpState state, HttpConnection conn)
151     throws IOException JavaDoc {
152         LOG.trace(
153             "enter HeadMethod.readResponseBody(HttpState, HttpConnection)");
154         
155         if (this.bodyCheckTimeout < 0) {
156             responseBodyConsumed();
157         } else {
158             if (LOG.isDebugEnabled()) {
159                 LOG.debug("Check for non-compliant response body. Timeout in "
160                  + this.bodyCheckTimeout + " ms");
161             }
162             boolean responseAvailable = false;
163             try {
164                 responseAvailable = conn.isResponseAvailable(this.bodyCheckTimeout);
165             } catch (IOException JavaDoc e) {
166                 LOG.debug("An IOException occurred while testing if a response was available,"
167                     + " we will assume one is not.",
168                     e);
169                 responseAvailable = false;
170             }
171             if (responseAvailable) {
172                 if (isStrictMode()) {
173                     throw new HttpException(
174                         "Body content may not be sent in response to HTTP HEAD request");
175                 } else {
176                     LOG.warn("Body content returned in response to HTTP HEAD");
177                 }
178                 super.readResponseBody(state, conn);
179             }
180         }
181
182     }
183     
184     /**
185      * Return non-compliant response body check timeout.
186      *
187      * @return The period of time in milliseconds to wait for a response
188      * body from a non-compliant server. <tt>-1</tt> returned when
189      * non-compliant response body check is disabled
190      */

191     public int getBodyCheckTimeout() {
192         return this.bodyCheckTimeout;
193     }
194
195     /**
196      * Set non-compliant response body check timeout.
197      *
198      * @param timeout The period of time in milliseconds to wait for a response
199      * body from a non-compliant server. <tt>-1</tt> can be used to
200      * disable non-compliant response body check
201      */

202     public void setBodyCheckTimeout(int timeout) {
203         this.bodyCheckTimeout = timeout;
204     }
205
206 }
207
Popular Tags