1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package org.apache.commons.httpclient;
31
32 import java.io.IOException;
33 import java.io.InputStream;
34
35 import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
36
37 /***
38 * A connection manager that provides access to a single HttpConnection. This
39 * manager makes no attempt to provide exclusive access to the contained
40 * HttpConnection.
41 *
42 * @author <a href="mailto:becke@u.washington.edu">Michael Becke</a>
43 * @author Eric Johnson
44 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
45 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
46 * @author Laura Werner
47 *
48 * @since 2.0
49 */
50 public class SimpleHttpConnectionManager implements HttpConnectionManager {
51
52 /***
53 * Since the same connection is about to be reused, make sure the
54 * previous request was completely processed, and if not
55 * consume it now.
56 * @param conn The connection
57 */
58 static void finishLastResponse(HttpConnection conn) {
59 InputStream lastResponse = conn.getLastResponseInputStream();
60 if (lastResponse != null) {
61 conn.setLastResponseInputStream(null);
62 try {
63 lastResponse.close();
64 } catch (IOException ioe) {
65
66 conn.close();
67 }
68 }
69 }
70
71 /*** The http connection */
72 private HttpConnection httpConnection;
73
74 /***
75 * Collection of parameters associated with this connection manager.
76 */
77 private HttpConnectionManagerParams params = new HttpConnectionManagerParams();
78
79 /***
80 * The time the connection was made idle.
81 */
82 private long idleStartTime = Long.MAX_VALUE;
83
84 public SimpleHttpConnectionManager() {
85 }
86
87 /***
88 * @see HttpConnectionManager#getConnection(HostConfiguration)
89 */
90 public HttpConnection getConnection(HostConfiguration hostConfiguration) {
91 return getConnection(hostConfiguration, 0);
92 }
93
94 /***
95 * Gets the staleCheckingEnabled value to be set on HttpConnections that are created.
96 *
97 * @return <code>true</code> if stale checking will be enabled on HttpConections
98 *
99 * @see HttpConnection#isStaleCheckingEnabled()
100 *
101 * @deprecated Use {@link HttpConnectionManagerParams#isStaleCheckingEnabled()},
102 * {@link HttpConnectionManager#getParams()}.
103 */
104 public boolean isConnectionStaleCheckingEnabled() {
105 return this.params.isStaleCheckingEnabled();
106 }
107
108 /***
109 * Sets the staleCheckingEnabled value to be set on HttpConnections that are created.
110 *
111 * @param connectionStaleCheckingEnabled <code>true</code> if stale checking will be enabled
112 * on HttpConections
113 *
114 * @see HttpConnection#setStaleCheckingEnabled(boolean)
115 *
116 * @deprecated Use {@link HttpConnectionManagerParams#setStaleCheckingEnabled(boolean)},
117 * {@link HttpConnectionManager#getParams()}.
118 */
119 public void setConnectionStaleCheckingEnabled(boolean connectionStaleCheckingEnabled) {
120 this.params.setStaleCheckingEnabled(connectionStaleCheckingEnabled);
121 }
122
123 /***
124 * @see HttpConnectionManager#getConnectionWithTimeout(HostConfiguration, long)
125 *
126 * @since 3.0
127 */
128 public HttpConnection getConnectionWithTimeout(
129 HostConfiguration hostConfiguration, long timeout) {
130
131 if (httpConnection == null) {
132 httpConnection = new HttpConnection(hostConfiguration);
133 httpConnection.setHttpConnectionManager(this);
134 httpConnection.getParams().setDefaults(this.params);
135 } else {
136
137
138
139 if (!hostConfiguration.hostEquals(httpConnection)
140 || !hostConfiguration.proxyEquals(httpConnection)) {
141
142 if (httpConnection.isOpen()) {
143 httpConnection.close();
144 }
145
146 httpConnection.setHost(hostConfiguration.getHost());
147 httpConnection.setVirtualHost(hostConfiguration.getVirtualHost());
148 httpConnection.setPort(hostConfiguration.getPort());
149 httpConnection.setProtocol(hostConfiguration.getProtocol());
150 httpConnection.setLocalAddress(hostConfiguration.getLocalAddress());
151
152 httpConnection.setProxyHost(hostConfiguration.getProxyHost());
153 httpConnection.setProxyPort(hostConfiguration.getProxyPort());
154 } else {
155 finishLastResponse(httpConnection);
156 }
157 }
158
159
160 idleStartTime = Long.MAX_VALUE;
161
162 return httpConnection;
163 }
164
165 /***
166 * @see HttpConnectionManager#getConnection(HostConfiguration, long)
167 *
168 * @deprecated Use #getConnectionWithTimeout(HostConfiguration, long)
169 */
170 public HttpConnection getConnection(
171 HostConfiguration hostConfiguration, long timeout) {
172 return getConnectionWithTimeout(hostConfiguration, timeout);
173 }
174
175 /***
176 * @see HttpConnectionManager#releaseConnection(org.apache.commons.httpclient.HttpConnection)
177 */
178 public void releaseConnection(HttpConnection conn) {
179 if (conn != httpConnection) {
180 throw new IllegalStateException("Unexpected release of an unknown connection.");
181 }
182
183 finishLastResponse(httpConnection);
184
185
186 idleStartTime = System.currentTimeMillis();
187 }
188
189 /***
190 * Returns {@link HttpConnectionManagerParams parameters} associated
191 * with this connection manager.
192 *
193 * @since 2.1
194 *
195 * @see HttpConnectionManagerParams
196 */
197 public HttpConnectionManagerParams getParams() {
198 return this.params;
199 }
200
201 /***
202 * Assigns {@link HttpConnectionManagerParams parameters} for this
203 * connection manager.
204 *
205 * @since 2.1
206 *
207 * @see HttpConnectionManagerParams
208 */
209 public void setParams(final HttpConnectionManagerParams params) {
210 if (params == null) {
211 throw new IllegalArgumentException("Parameters may not be null");
212 }
213 this.params = params;
214 }
215
216 /***
217 * @since 3.0
218 */
219 public void closeIdleConnections(long idleTimeout) {
220 long maxIdleTime = System.currentTimeMillis() - idleTimeout;
221 if (idleStartTime <= maxIdleTime) {
222 httpConnection.close();
223 }
224 }
225 }