View Javadoc
1 /* 2 * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/HeadMethod.java,v 1.19.2.2 2003/09/10 21:34:11 mbecke Exp $ 3 * $Revision: 1.19.2.2 $ 4 * $Date: 2003/09/10 21:34:11 $ 5 * 6 * ==================================================================== 7 * 8 * The Apache Software License, Version 1.1 9 * 10 * Copyright (c) 1999-2003 The Apache Software Foundation. All rights 11 * reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in 22 * the documentation and/or other materials provided with the 23 * distribution. 24 * 25 * 3. The end-user documentation included with the redistribution, if 26 * any, must include the following acknowlegement: 27 * "This product includes software developed by the 28 * Apache Software Foundation (http://www.apache.org/)." 29 * Alternately, this acknowlegement may appear in the software itself, 30 * if and wherever such third-party acknowlegements normally appear. 31 * 32 * 4. The names "The Jakarta Project", "Commons", and "Apache Software 33 * Foundation" must not be used to endorse or promote products derived 34 * from this software without prior written permission. For written 35 * permission, please contact apache@apache.org. 36 * 37 * 5. Products derived from this software may not be called "Apache" 38 * nor may "Apache" appear in their names without prior written 39 * permission of the Apache Group. 40 * 41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 52 * SUCH DAMAGE. 53 * ==================================================================== 54 * 55 * This software consists of voluntary contributions made by many 56 * individuals on behalf of the Apache Software Foundation. For more 57 * information on the Apache Software Foundation, please see 58 * <http://www.apache.org/>. 59 * 60 * [Additional notices, if required by prior licensing conditions] 61 * 62 */ 63 64 package org.apache.commons.httpclient.methods; 65 66 import java.io.IOException; 67 68 import org.apache.commons.httpclient.HttpConnection; 69 import org.apache.commons.httpclient.HttpException; 70 import org.apache.commons.httpclient.HttpMethodBase; 71 import org.apache.commons.httpclient.HttpState; 72 import org.apache.commons.logging.Log; 73 import org.apache.commons.logging.LogFactory; 74 75 /*** 76 * Implements the HTTP HEAD method. 77 * <p> 78 * The HTTP HEAD method is defined in section 9.4 of 79 * <a href="http://www.ietf.org/rfc/rfc2616.txt">RFC2616</a>: 80 * <blockquote> 81 * The HEAD method is identical to GET except that the server MUST NOT 82 * return a message-body in the response. The metainformation contained 83 * in the HTTP headers in response to a HEAD request SHOULD be identical 84 * to the information sent in response to a GET request. This method can 85 * be used for obtaining metainformation about the entity implied by the 86 * request without transferring the entity-body itself. This method is 87 * often used for testing hypertext links for validity, accessibility, 88 * and recent modification. 89 * </blockquote> 90 * </p> 91 * 92 * @author <a href="mailto:remm@apache.org">Remy Maucherat</a> 93 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a> 94 * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a> 95 * @author <a href="mailto:oleg@ural.ru">oleg Kalnichevski</a> 96 * 97 * @version $Revision: 1.19.2.2 $ 98 * @since 1.0 99 */ 100 public class HeadMethod extends HttpMethodBase { 101 //~ Static variables/initializers ������������������������������������������ 102 103 /*** Log object for this class. */ 104 private static final Log LOG = LogFactory.getLog(HeadMethod.class); 105 106 private int bodyCheckTimeout = -1; /* Disabled per default */ 107 108 //~ Constructors ����������������������������������������������������������� 109 110 /*** 111 * No-arg constructor. 112 * 113 * @since 1.0 114 */ 115 public HeadMethod() { 116 setFollowRedirects(true); 117 } 118 119 /*** 120 * Constructor specifying a URI. 121 * 122 * @param uri either an absolute or relative URI 123 * 124 * @since 1.0 125 */ 126 public HeadMethod(String uri) { 127 super(uri); 128 setFollowRedirects(true); 129 } 130 131 //~ Methods ���������������������������������������������������������������� 132 133 /*** 134 * Returns <tt>"HEAD"</tt>. 135 * 136 * @return <tt>"HEAD"</tt> 137 * 138 * @since 2.0 139 */ 140 public String getName() { 141 return "HEAD"; 142 } 143 144 /*** 145 * Recycles the HTTP method so that it can be used again. 146 * Note that all of the instance variables will be reset 147 * once this method has been called. This method will also 148 * release the connection being used by this HTTP method. 149 * 150 * @see #releaseConnection() 151 * 152 * @since 1.0 153 */ 154 public void recycle() { 155 super.recycle(); 156 setFollowRedirects(true); 157 } 158 159 /*** 160 * Overrides {@link HttpMethodBase} method to <i>not</i> read a response 161 * body, despite the presence of a <tt>Content-Length</tt> or 162 * <tt>Transfer-Encoding</tt> header. 163 * 164 * @param state the {@link HttpState state} information associated with this method 165 * @param conn the {@link HttpConnection connection} used to execute 166 * this HTTP method 167 * 168 * @throws IOException if an I/O (transport) error occurs 169 * @throws HttpException if a protocol exception occurs. 170 * @throws HttpRecoverableException if a recoverable transport error occurs. 171 * Usually this kind of exceptions can be recovered from by 172 * retrying the HTTP method 173 * 174 * @see #readResponse 175 * @see #processResponseBody 176 * 177 * @since 2.0 178 */ 179 protected void readResponseBody(HttpState state, HttpConnection conn) 180 throws IOException { 181 LOG.trace( 182 "enter HeadMethod.readResponseBody(HttpState, HttpConnection)"); 183 184 if (this.bodyCheckTimeout < 0) { 185 responseBodyConsumed(); 186 } else { 187 if (LOG.isDebugEnabled()) { 188 LOG.debug("Check for non-compliant response body. Timeout in " 189 + this.bodyCheckTimeout + " ms"); 190 } 191 boolean responseAvailable = false; 192 try { 193 responseAvailable = conn.isResponseAvailable(this.bodyCheckTimeout); 194 } catch (IOException e) { 195 LOG.debug("An IOException occurred while testing if a response was available," 196 + " we will assume one is not.", 197 e); 198 responseAvailable = false; 199 } 200 if (responseAvailable) { 201 if (isStrictMode()) { 202 throw new HttpException( 203 "Body content may not be sent in response to HTTP HEAD request"); 204 } else { 205 LOG.warn("Body content returned in response to HTTP HEAD"); 206 } 207 super.readResponseBody(state, conn); 208 } 209 } 210 211 } 212 213 /*** 214 * Return non-compliant response body check timeout. 215 * 216 * @return The period of time in milliseconds to wait for a response 217 * body from a non-compliant server. <tt>-1</tt> returned when 218 * non-compliant response body check is disabled 219 */ 220 public int getBodyCheckTimeout() { 221 return this.bodyCheckTimeout; 222 } 223 224 /*** 225 * Set non-compliant response body check timeout. 226 * 227 * @param timeout The period of time in milliseconds to wait for a response 228 * body from a non-compliant server. <tt>-1</tt> can be used to 229 * disable non-compliant response body check 230 */ 231 public void setBodyCheckTimeout(int timeout) { 232 this.bodyCheckTimeout = timeout; 233 } 234 235 }

This page was automatically generated by Maven