1. /*
  2. * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/HeadMethod.java,v 1.29 2004/06/13 20:22:19 olegk Exp $
  3. * $Revision: 1.29 $
  4. * $Date: 2004/06/13 20:22:19 $
  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. package org.apache.commons.httpclient.methods;
  30. import java.io.IOException;
  31. import org.apache.commons.httpclient.HttpConnection;
  32. import org.apache.commons.httpclient.HttpException;
  33. import org.apache.commons.httpclient.HttpMethodBase;
  34. import org.apache.commons.httpclient.HttpState;
  35. import org.apache.commons.httpclient.ProtocolException;
  36. import org.apache.commons.httpclient.params.HttpMethodParams;
  37. import org.apache.commons.logging.Log;
  38. import org.apache.commons.logging.LogFactory;
  39. /**
  40. * Implements the HTTP HEAD method.
  41. * <p>
  42. * The HTTP HEAD method is defined in section 9.4 of
  43. * <a href="http://www.ietf.org/rfc/rfc2616.txt">RFC2616</a>:
  44. * <blockquote>
  45. * The HEAD method is identical to GET except that the server MUST NOT
  46. * return a message-body in the response. The metainformation contained
  47. * in the HTTP headers in response to a HEAD request SHOULD be identical
  48. * to the information sent in response to a GET request. This method can
  49. * be used for obtaining metainformation about the entity implied by the
  50. * request without transferring the entity-body itself. This method is
  51. * often used for testing hypertext links for validity, accessibility,
  52. * and recent modification.
  53. * </blockquote>
  54. * </p>
  55. *
  56. * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
  57. * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
  58. * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
  59. * @author <a href="mailto:oleg@ural.ru">oleg Kalnichevski</a>
  60. *
  61. * @version $Revision: 1.29 $
  62. * @since 1.0
  63. */
  64. public class HeadMethod extends HttpMethodBase {
  65. //~ Static variables/initializers
  66. /** Log object for this class. */
  67. private static final Log LOG = LogFactory.getLog(HeadMethod.class);
  68. //~ Constructors
  69. /**
  70. * No-arg constructor.
  71. *
  72. * @since 1.0
  73. */
  74. public HeadMethod() {
  75. setFollowRedirects(true);
  76. }
  77. /**
  78. * Constructor specifying a URI.
  79. *
  80. * @param uri either an absolute or relative URI
  81. *
  82. * @since 1.0
  83. */
  84. public HeadMethod(String uri) {
  85. super(uri);
  86. setFollowRedirects(true);
  87. }
  88. //~ Methods
  89. /**
  90. * Returns <tt>"HEAD"</tt>.
  91. *
  92. * @return <tt>"HEAD"</tt>
  93. *
  94. * @since 2.0
  95. */
  96. public String getName() {
  97. return "HEAD";
  98. }
  99. /**
  100. * Recycles the HTTP method so that it can be used again.
  101. * Note that all of the instance variables will be reset
  102. * once this method has been called. This method will also
  103. * release the connection being used by this HTTP method.
  104. *
  105. * @see #releaseConnection()
  106. *
  107. * @since 1.0
  108. *
  109. * @deprecated no longer supported and will be removed in the future
  110. * version of HttpClient
  111. */
  112. public void recycle() {
  113. super.recycle();
  114. setFollowRedirects(true);
  115. }
  116. /**
  117. * Overrides {@link HttpMethodBase} method to <i>not</i> read a response
  118. * body, despite the presence of a <tt>Content-Length</tt> or
  119. * <tt>Transfer-Encoding</tt> header.
  120. *
  121. * @param state the {@link HttpState state} information associated with this method
  122. * @param conn the {@link HttpConnection connection} used to execute
  123. * this HTTP method
  124. *
  125. * @throws IOException if an I/O (transport) error occurs. Some transport exceptions
  126. * can be recovered from.
  127. * @throws HttpException if a protocol exception occurs. Usually protocol exceptions
  128. * cannot be recovered from.
  129. *
  130. * @see #readResponse
  131. * @see #processResponseBody
  132. *
  133. * @since 2.0
  134. */
  135. protected void readResponseBody(HttpState state, HttpConnection conn)
  136. throws HttpException, IOException {
  137. LOG.trace(
  138. "enter HeadMethod.readResponseBody(HttpState, HttpConnection)");
  139. int bodyCheckTimeout =
  140. getParams().getIntParameter(HttpMethodParams.HEAD_BODY_CHECK_TIMEOUT, -1);
  141. if (bodyCheckTimeout < 0) {
  142. responseBodyConsumed();
  143. } else {
  144. if (LOG.isDebugEnabled()) {
  145. LOG.debug("Check for non-compliant response body. Timeout in "
  146. + bodyCheckTimeout + " ms");
  147. }
  148. boolean responseAvailable = false;
  149. try {
  150. responseAvailable = conn.isResponseAvailable(bodyCheckTimeout);
  151. } catch (IOException e) {
  152. LOG.debug("An IOException occurred while testing if a response was available,"
  153. + " we will assume one is not.",
  154. e);
  155. responseAvailable = false;
  156. }
  157. if (responseAvailable) {
  158. if (getParams().isParameterTrue(HttpMethodParams.REJECT_HEAD_BODY)) {
  159. throw new ProtocolException(
  160. "Body content may not be sent in response to HTTP HEAD request");
  161. } else {
  162. LOG.warn("Body content returned in response to HTTP HEAD");
  163. }
  164. super.readResponseBody(state, conn);
  165. }
  166. }
  167. }
  168. /**
  169. * Returns non-compliant response body check timeout.
  170. *
  171. * @return The period of time in milliseconds to wait for a response
  172. * body from a non-compliant server. <tt>-1</tt> returned when
  173. * non-compliant response body check is disabled
  174. *
  175. * @deprecated Use {@link HttpMethodParams}
  176. *
  177. * @see #getParams()
  178. * @see HttpMethodParams
  179. * @see HttpMethodParams#HEAD_BODY_CHECK_TIMEOUT
  180. */
  181. public int getBodyCheckTimeout() {
  182. return getParams().getIntParameter(HttpMethodParams.HEAD_BODY_CHECK_TIMEOUT, -1);
  183. }
  184. /**
  185. * Sets non-compliant response body check timeout.
  186. *
  187. * @param timeout The period of time in milliseconds to wait for a response
  188. * body from a non-compliant server. <tt>-1</tt> can be used to
  189. * disable non-compliant response body check
  190. *
  191. * @deprecated Use {@link HttpMethodParams}
  192. *
  193. * @see #getParams()
  194. * @see HttpMethodParams
  195. * @see HttpMethodParams#HEAD_BODY_CHECK_TIMEOUT
  196. */
  197. public void setBodyCheckTimeout(int timeout) {
  198. getParams().setIntParameter(HttpMethodParams.HEAD_BODY_CHECK_TIMEOUT, timeout);
  199. }
  200. }