1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
97 this.httpRecorderMethod.markContentBegin(connection);
98 super.readResponseBody(state, connection);
99 }
100
101 protected boolean shouldCloseConnection(HttpConnection conn) {
102
103
104
105 return true;
106 }
107
108 public int execute(HttpState state, HttpConnection conn)
109 throws HttpException, IOException {
110
111
112
113
114
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 }