    1   /* HttpRecorderGetMethod
    2    *
    3    * Created on Feb 24, 2004
    4    *
    5    * Copyright (C) 2003 Internet Archive.
    6    *
    7    * This file is part of the Heritrix web crawler (crawler.archive.org).
    8    *
    9    * Heritrix is free software; you can redistribute it and/or modify
    10   * it under the terms of the GNU Lesser Public License as published by
    11   * the Free Software Foundation; either version 2.1 of the License, or
    12   * any later version.
    13   *
    14   * Heritrix is distributed in the hope that it will be useful,
    15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    17   * GNU Lesser Public License for more details.
    18   *
    19   * You should have received a copy of the GNU Lesser Public License
    20   * along with Heritrix; if not, write to the Free Software
    21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    22   */
    23  package org.archive.httpclient;
    24
    25  import java.io.IOException;
    26  import java.util.logging.Logger;
    27
    28  import org.apache.commons.httpclient.HttpConnection;
    29  import org.apache.commons.httpclient.HttpException;
    30  import org.apache.commons.httpclient.HttpState;
    31  import org.apache.commons.httpclient.methods.GetMethod;
    32  import org.archive.util.HttpRecorder;
    33
    34
    35  /***
    36   * Override of GetMethod that marks the passed HttpRecorder w/ the transition
    37   * from HTTP head to body and that forces a close on the http connection.
    38   *
    39   * The actions done in this subclass used to be done by copying
    40   * org.apache.commons.HttpMethodBase, overlaying our version in place of the
    41   * one that came w/ httpclient.  Here is the patch of the difference between
    42   * shipped httpclient code and our mods:
    43   * <pre>
    44   *    -- -1338,6 +1346,12 --
    45   *
    46   *        public void releaseConnection() {
    47   *
    48   *   +        // HERITRIX always ants the streams closed.
    49   *   +        if (responseConnection != null)
    50   *   +        {
    51   *   +            responseConnection.close();
    52   *   +        }
    53   *   +
    54   *            if (responseStream != null) {
    55   *                try {
    56   *                    // FYI - this may indirectly invoke responseBodyConsumed.
    57   *   -- -1959,6 +1973,11 --
    58   *                        this.statusLine = null;
    59   *                    }
    60   *                }
    61   *   +            // HERITRIX mark transition from header to content.
    62   *   +            if (this.httpRecorder != null)
    63   *   +            {
    64   *   +                this.httpRecorder.markContentBegin();
    65   *   +            }
    66   *                readResponseBody(state, conn);
    67   *                processResponseBody(state, conn);
    68   *            } catch (IOException e) {
    69   * </pre>
    70   *
    71   * <p>We're not supposed to have access to the underlying connection object;
    72   * am only violating contract because see cases where httpclient is skipping
    73   * out w/o cleaning up after itself.
    74   *
    75   * @author stack
    76   * @version $Revision: 4646 $, $Date: 2006-09-22 17:23:04 +0000 (Fri, 22 Sep 2006) $
    77   */
    78  public class HttpRecorderGetMethod extends GetMethod {
    79
    80      protected static Logger logger =
    81          Logger.getLogger(HttpRecorderGetMethod.class.getName());
    82
    83      /***
    84       * Instance of http recorder method.
    85       */
    86      protected HttpRecorderMethod httpRecorderMethod = null;
    87
    88
    89  	public HttpRecorderGetMethod(String uri, HttpRecorder recorder) {
    90  		super(uri);
    91          this.httpRecorderMethod = new HttpRecorderMethod(recorder);
    92  	}
    93
    94  	protected void readResponseBody(HttpState state, HttpConnection connection)
    95  	throws IOException, HttpException {
    96          // We're about to read the body.  Mark transition in http recorder.
    97  		this.httpRecorderMethod.markContentBegin(connection);
    98  		super.readResponseBody(state, connection);
    99  	}
    100
    101     protected boolean shouldCloseConnection(HttpConnection conn) {
    102         // Always close connection after each request. As best I can tell, this
    103         // is superfluous -- we've set our client to be HTTP/1.0.  Doing this
    104         // out of paranoia.
    105         return true;
    106     }
    107
    108     public int execute(HttpState state, HttpConnection conn)
    109     throws HttpException, IOException {
    110         // Save off the connection so we can close it on our way out in case
    111         // httpclient fails to (We're not supposed to have access to the
    112         // underlying connection object; am only violating contract because
    113         // see cases where httpclient is skipping out w/o cleaning up
    114         // after itself).
    115         this.httpRecorderMethod.setConnection(conn);
    116         return super.execute(state, conn);
    117     }
    118
    119     protected void addProxyConnectionHeader(HttpState state, HttpConnection conn)
    120             throws IOException, HttpException {
    121         super.addProxyConnectionHeader(state, conn);
    122         this.httpRecorderMethod.handleAddProxyConnectionHeader(this);
    123     }
    124 }