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: 155418 $
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 * @deprecated no longer supported and will be removed in the future
121 * version of HttpClient
122 */
123 public void recycle() {
124 super.recycle();
125 setFollowRedirects(true);
126 }
127
128 /***
129 * Overrides {@link HttpMethodBase} method to <i>not</i> read a response
130 * body, despite the presence of a <tt>Content-Length</tt> or
131 * <tt>Transfer-Encoding</tt> header.
132 *
133 * @param state the {@link HttpState state} information associated with this method
134 * @param conn the {@link HttpConnection connection} used to execute
135 * this HTTP method
136 *
137 * @throws IOException if an I/O (transport) error occurs. Some transport exceptions
138 * can be recovered from.
139 * @throws HttpException if a protocol exception occurs. Usually protocol exceptions
140 * cannot be recovered from.
141 *
142 * @see #readResponse
143 * @see #processResponseBody
144 *
145 * @since 2.0
146 */
147 protected void readResponseBody(HttpState state, HttpConnection conn)
148 throws HttpException, IOException {
149 LOG.trace(
150 "enter HeadMethod.readResponseBody(HttpState, HttpConnection)");
151
152 int bodyCheckTimeout =
153 getParams().getIntParameter(HttpMethodParams.HEAD_BODY_CHECK_TIMEOUT, -1);
154
155 if (bodyCheckTimeout < 0) {
156 responseBodyConsumed();
157 } else {
158 if (LOG.isDebugEnabled()) {
159 LOG.debug("Check for non-compliant response body. Timeout in "
160 + bodyCheckTimeout + " ms");
161 }
162 boolean responseAvailable = false;
163 try {
164 responseAvailable = conn.isResponseAvailable(bodyCheckTimeout);
165 } catch (IOException 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 (getParams().isParameterTrue(HttpMethodParams.REJECT_HEAD_BODY)) {
173 throw new ProtocolException(
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 * Returns 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 * @deprecated Use {@link HttpMethodParams}
192 *
193 * @see #getParams()
194 * @see HttpMethodParams
195 * @see HttpMethodParams#HEAD_BODY_CHECK_TIMEOUT
196 */
197 public int getBodyCheckTimeout() {
198 return getParams().getIntParameter(HttpMethodParams.HEAD_BODY_CHECK_TIMEOUT, -1);
199 }
200
201 /***
202 * Sets non-compliant response body check timeout.
203 *
204 * @param timeout The period of time in milliseconds to wait for a response
205 * body from a non-compliant server. <tt>-1</tt> can be used to
206 * disable non-compliant response body check
207 *
208 * @deprecated Use {@link HttpMethodParams}
209 *
210 * @see #getParams()
211 * @see HttpMethodParams
212 * @see HttpMethodParams#HEAD_BODY_CHECK_TIMEOUT
213 */
214 public void setBodyCheckTimeout(int timeout) {
215 getParams().setIntParameter(HttpMethodParams.HEAD_BODY_CHECK_TIMEOUT, timeout);
216 }
217
218 }