1   /* HttpRecorderMethod
2    *
3    * Created on August 22, 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.util.logging.Logger;
26  
27  import org.apache.commons.httpclient.Header;
28  import org.apache.commons.httpclient.HttpConnection;
29  import org.apache.commons.httpclient.HttpMethod;
30  import org.archive.util.HttpRecorder;
31  
32  
33  /***
34   * This class encapsulates the specializations supplied by the
35   * overrides {@link HttpRecorderGetMethod} and {@link HttpRecorderPostMethod}.
36   * 
37   * It keeps instance of HttpRecorder and HttpConnection.
38   *
39   * @author stack
40   * @version $Revision: 3351 $, $Date: 2005-04-07 21:44:47 +0000 (Thu, 07 Apr 2005) $
41   */
42  public class HttpRecorderMethod {
43      protected static Logger logger =
44          Logger.getLogger(HttpRecorderMethod.class.getName());
45      
46      /***
47       * Instance of http recorder we're using recording this http get.
48       */
49      private HttpRecorder httpRecorder = null;
50  
51      /***
52       * Save around so can force close.
53       *
54       * See [ 922080 ] IllegalArgumentException (size is wrong).
55       * https://sourceforge.net/tracker/?func=detail&aid=922080&group_id=73833&atid=539099
56       */
57      private HttpConnection connection = null;
58      
59  
60  	public HttpRecorderMethod(HttpRecorder recorder) {
61          this.httpRecorder = recorder;
62  	}
63  
64  	public void markContentBegin(HttpConnection c) {
65          if (c != this.connection) {
66              // We're checking that we're not being asked to work on
67              // a connection that is other than the one we started
68              // this method#execute with.
69              throw new IllegalArgumentException("Connections differ: " +
70                  this.connection + " " + c + " " +
71                  Thread.currentThread().getName());
72          }
73  		this.httpRecorder.markContentBegin();
74  	}
75      
76      /***
77       * @return Returns the connection.
78       */
79      public HttpConnection getConnection() {
80          return this.connection;
81      }
82      
83      /***
84       * @param connection The connection to set.
85       */
86      public void setConnection(HttpConnection connection) {
87          this.connection = connection;
88      }
89      /***
90       * @return Returns the httpRecorder.
91       */
92      public HttpRecorder getHttpRecorder() {
93          return httpRecorder;
94      }
95  
96      /***
97       * If a 'Proxy-Connection' header has been added to the request,
98       * it'll be of a 'keep-alive' type.  Until we support 'keep-alives',
99       * override the Proxy-Connection setting and instead pass a 'close'
100      * (Otherwise every request has to timeout before we notice
101      * end-of-document).
102      * @param method Method to find proxy-connection header in.
103      */
104     public void handleAddProxyConnectionHeader(HttpMethod method) {
105         Header h = method.getRequestHeader("Proxy-Connection");
106         if (h != null) {
107             h.setValue("close");
108             method.setRequestHeader(h);
109         }
110     }
111 }