1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package org.apache.commons.httpclient.methods;
31
32 import java.io.IOException;
33
34 import org.apache.commons.httpclient.HttpConnection;
35 import org.apache.commons.httpclient.HttpException;
36 import org.apache.commons.httpclient.HttpMethodBase;
37 import org.apache.commons.httpclient.HttpState;
38 import org.apache.commons.httpclient.ProtocolException;
39 import org.apache.commons.httpclient.params.HttpMethodParams;
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.28 $
66 * @since 1.0
67 */
68 public class HeadMethod extends HttpMethodBase {
69
70
71 /*** Log object for this class. */
72 private static final Log LOG = LogFactory.getLog(HeadMethod.class);
73
74
75
76 /***
77 * No-arg constructor.
78 *
79 * @since 1.0
80 */
81 public HeadMethod() {
82 setFollowRedirects(true);
83 }
84
85 /***
86 * Constructor specifying a URI.
87 *
88 * @param uri either an absolute or relative URI
89 *
90 * @since 1.0
91 */
92 public HeadMethod(String uri) {
93 super(uri);
94 setFollowRedirects(true);
95 }
96
97
98
99 /***
100 * Returns <tt>"HEAD"</tt>.
101 *
102 * @return <tt>"HEAD"</tt>
103 *
104 * @since 2.0
105 */
106 public String getName() {
107 return "HEAD";
108 }
109
110 /***
111 * Recycles the HTTP method so that it can be used again.
112 * Note that all of the instance variables will be reset
113 * once this method has been called. This method will also
114 * release the connection being used by this HTTP method.
115 *
116 * @see #releaseConnection()
117 *
118 * @since 1.0
119 */
120 public void recycle() {
121 super.recycle();
122 setFollowRedirects(true);
123 }
124
125 /***
126 * Overrides {@link HttpMethodBase} method to <i>not</i> read a response
127 * body, despite the presence of a <tt>Content-Length</tt> or
128 * <tt>Transfer-Encoding</tt> header.
129 *
130 * @param state the {@link HttpState state} information associated with this method
131 * @param conn the {@link HttpConnection connection} used to execute
132 * this HTTP method
133 *
134 * @throws IOException if an I/O (transport) error occurs. Some transport exceptions
135 * can be recovered from.
136 * @throws HttpException if a protocol exception occurs. Usually protocol exceptions
137 * cannot be recovered from.
138 *
139 * @see #readResponse
140 * @see #processResponseBody
141 *
142 * @since 2.0
143 */
144 protected void readResponseBody(HttpState state, HttpConnection conn)
145 throws HttpException, IOException {
146 LOG.trace(
147 "enter HeadMethod.readResponseBody(HttpState, HttpConnection)");
148
149 int bodyCheckTimeout =
150 getParams().getIntParameter(HttpMethodParams.HEAD_BODY_CHECK_TIMEOUT, -1);
151
152 if (bodyCheckTimeout < 0) {
153 responseBodyConsumed();
154 } else {
155 if (LOG.isDebugEnabled()) {
156 LOG.debug("Check for non-compliant response body. Timeout in "
157 + bodyCheckTimeout + " ms");
158 }
159 boolean responseAvailable = false;
160 try {
161 responseAvailable = conn.isResponseAvailable(bodyCheckTimeout);
162 } catch (IOException e) {
163 LOG.debug("An IOException occurred while testing if a response was available,"
164 + " we will assume one is not.",
165 e);
166 responseAvailable = false;
167 }
168 if (responseAvailable) {
169 if (getParams().isParameterTrue(HttpMethodParams.REJECT_HEAD_BODY)) {
170 throw new ProtocolException(
171 "Body content may not be sent in response to HTTP HEAD request");
172 } else {
173 LOG.warn("Body content returned in response to HTTP HEAD");
174 }
175 super.readResponseBody(state, conn);
176 }
177 }
178
179 }
180
181 /***
182 * Returns non-compliant response body check timeout.
183 *
184 * @return The period of time in milliseconds to wait for a response
185 * body from a non-compliant server. <tt>-1</tt> returned when
186 * non-compliant response body check is disabled
187 *
188 * @deprecated Use {@link HttpMethodParams}
189 *
190 * @see #getParams()
191 * @see HttpMethodParams
192 * @see HttpMethodParams#HEAD_BODY_CHECK_TIMEOUT
193 */
194 public int getBodyCheckTimeout() {
195 return getParams().getIntParameter(HttpMethodParams.HEAD_BODY_CHECK_TIMEOUT, -1);
196 }
197
198 /***
199 * Sets non-compliant response body check timeout.
200 *
201 * @param timeout The period of time in milliseconds to wait for a response
202 * body from a non-compliant server. <tt>-1</tt> can be used to
203 * disable non-compliant response body check
204 *
205 * @deprecated Use {@link HttpMethodParams}
206 *
207 * @see #getParams()
208 * @see HttpMethodParams
209 * @see HttpMethodParams#HEAD_BODY_CHECK_TIMEOUT
210 */
211 public void setBodyCheckTimeout(int timeout) {
212 getParams().setIntParameter(HttpMethodParams.HEAD_BODY_CHECK_TIMEOUT, timeout);
213 }
214
215 }