001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/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 $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030:
031: package org.apache.commons.httpclient.methods;
032:
033: import java.io.IOException;
034:
035: import org.apache.commons.httpclient.HttpConnection;
036: import org.apache.commons.httpclient.HttpException;
037: import org.apache.commons.httpclient.HttpMethodBase;
038: import org.apache.commons.httpclient.HttpState;
039: import org.apache.commons.httpclient.ProtocolException;
040: import org.apache.commons.httpclient.params.HttpMethodParams;
041: import org.apache.commons.logging.Log;
042: import org.apache.commons.logging.LogFactory;
043:
044: /**
045: * Implements the HTTP HEAD method.
046: * <p>
047: * The HTTP HEAD method is defined in section 9.4 of
048: * <a href="http://www.ietf.org/rfc/rfc2616.txt">RFC2616</a>:
049: * <blockquote>
050: * The HEAD method is identical to GET except that the server MUST NOT
051: * return a message-body in the response. The metainformation contained
052: * in the HTTP headers in response to a HEAD request SHOULD be identical
053: * to the information sent in response to a GET request. This method can
054: * be used for obtaining metainformation about the entity implied by the
055: * request without transferring the entity-body itself. This method is
056: * often used for testing hypertext links for validity, accessibility,
057: * and recent modification.
058: * </blockquote>
059: * </p>
060: *
061: * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
062: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
063: * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
064: * @author <a href="mailto:oleg@ural.ru">oleg Kalnichevski</a>
065: *
066: * @version $Revision: 480424 $
067: * @since 1.0
068: */
069: public class HeadMethod extends HttpMethodBase {
070: //~ Static variables/initializers
071:
072: /** Log object for this class. */
073: private static final Log LOG = LogFactory.getLog(HeadMethod.class);
074:
075: //~ Constructors
076:
077: /**
078: * No-arg constructor.
079: *
080: * @since 1.0
081: */
082: public HeadMethod() {
083: setFollowRedirects(true);
084: }
085:
086: /**
087: * Constructor specifying a URI.
088: *
089: * @param uri either an absolute or relative URI
090: *
091: * @since 1.0
092: */
093: public HeadMethod(String uri) {
094: super (uri);
095: setFollowRedirects(true);
096: }
097:
098: //~ Methods
099:
100: /**
101: * Returns <tt>"HEAD"</tt>.
102: *
103: * @return <tt>"HEAD"</tt>
104: *
105: * @since 2.0
106: */
107: public String getName() {
108: return "HEAD";
109: }
110:
111: /**
112: * Recycles the HTTP method so that it can be used again.
113: * Note that all of the instance variables will be reset
114: * once this method has been called. This method will also
115: * release the connection being used by this HTTP method.
116: *
117: * @see #releaseConnection()
118: *
119: * @since 1.0
120: *
121: * @deprecated no longer supported and will be removed in the future
122: * version of HttpClient
123: */
124: public void recycle() {
125: super .recycle();
126: setFollowRedirects(true);
127: }
128:
129: /**
130: * Overrides {@link HttpMethodBase} method to <i>not</i> read a response
131: * body, despite the presence of a <tt>Content-Length</tt> or
132: * <tt>Transfer-Encoding</tt> header.
133: *
134: * @param state the {@link HttpState state} information associated with this method
135: * @param conn the {@link HttpConnection connection} used to execute
136: * this HTTP method
137: *
138: * @throws IOException if an I/O (transport) error occurs. Some transport exceptions
139: * can be recovered from.
140: * @throws HttpException if a protocol exception occurs. Usually protocol exceptions
141: * cannot be recovered from.
142: *
143: * @see #readResponse
144: * @see #processResponseBody
145: *
146: * @since 2.0
147: */
148: protected void readResponseBody(HttpState state, HttpConnection conn)
149: throws HttpException, IOException {
150: LOG
151: .trace("enter HeadMethod.readResponseBody(HttpState, HttpConnection)");
152:
153: int bodyCheckTimeout = getParams().getIntParameter(
154: HttpMethodParams.HEAD_BODY_CHECK_TIMEOUT, -1);
155:
156: if (bodyCheckTimeout < 0) {
157: responseBodyConsumed();
158: } else {
159: if (LOG.isDebugEnabled()) {
160: LOG
161: .debug("Check for non-compliant response body. Timeout in "
162: + bodyCheckTimeout + " ms");
163: }
164: boolean responseAvailable = false;
165: try {
166: responseAvailable = conn
167: .isResponseAvailable(bodyCheckTimeout);
168: } catch (IOException e) {
169: LOG.debug(
170: "An IOException occurred while testing if a response was available,"
171: + " we will assume one is not.", e);
172: responseAvailable = false;
173: }
174: if (responseAvailable) {
175: if (getParams().isParameterTrue(
176: HttpMethodParams.REJECT_HEAD_BODY)) {
177: throw new ProtocolException(
178: "Body content may not be sent in response to HTTP HEAD request");
179: } else {
180: LOG
181: .warn("Body content returned in response to HTTP HEAD");
182: }
183: super .readResponseBody(state, conn);
184: }
185: }
186:
187: }
188:
189: /**
190: * Returns non-compliant response body check timeout.
191: *
192: * @return The period of time in milliseconds to wait for a response
193: * body from a non-compliant server. <tt>-1</tt> returned when
194: * non-compliant response body check is disabled
195: *
196: * @deprecated Use {@link HttpMethodParams}
197: *
198: * @see #getParams()
199: * @see HttpMethodParams
200: * @see HttpMethodParams#HEAD_BODY_CHECK_TIMEOUT
201: */
202: public int getBodyCheckTimeout() {
203: return getParams().getIntParameter(
204: HttpMethodParams.HEAD_BODY_CHECK_TIMEOUT, -1);
205: }
206:
207: /**
208: * Sets non-compliant response body check timeout.
209: *
210: * @param timeout The period of time in milliseconds to wait for a response
211: * body from a non-compliant server. <tt>-1</tt> can be used to
212: * disable non-compliant response body check
213: *
214: * @deprecated Use {@link HttpMethodParams}
215: *
216: * @see #getParams()
217: * @see HttpMethodParams
218: * @see HttpMethodParams#HEAD_BODY_CHECK_TIMEOUT
219: */
220: public void setBodyCheckTimeout(int timeout) {
221: getParams().setIntParameter(
222: HttpMethodParams.HEAD_BODY_CHECK_TIMEOUT, timeout);
223: }
224:
225: }
|