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 protected 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.setPort(hostConfiguration.getPort());
148 httpConnection.setProtocol(hostConfiguration.getProtocol());
149 httpConnection.setLocalAddress(hostConfiguration.getLocalAddress());
150
151 httpConnection.setProxyHost(hostConfiguration.getProxyHost());
152 httpConnection.setProxyPort(hostConfiguration.getProxyPort());
153 } else {
154 finishLastResponse(httpConnection);
155 }
156 }
157
158
159 idleStartTime = Long.MAX_VALUE;
160
161 return httpConnection;
162 }
163
164 /***
165 * @see HttpConnectionManager#getConnection(HostConfiguration, long)
166 *
167 * @deprecated Use #getConnectionWithTimeout(HostConfiguration, long)
168 */
169 public HttpConnection getConnection(
170 HostConfiguration hostConfiguration, long timeout) {
171 return getConnectionWithTimeout(hostConfiguration, timeout);
172 }
173
174 /***
175 * @see HttpConnectionManager#releaseConnection(org.apache.commons.httpclient.HttpConnection)
176 */
177 public void releaseConnection(HttpConnection conn) {
178 if (conn != httpConnection) {
179 throw new IllegalStateException("Unexpected release of an unknown connection.");
180 }
181
182 finishLastResponse(httpConnection);
183
184
185 idleStartTime = System.currentTimeMillis();
186 }
187
188 /***
189 * Returns {@link HttpConnectionManagerParams parameters} associated
190 * with this connection manager.
191 *
192 * @since 2.1
193 *
194 * @see HttpConnectionManagerParams
195 */
196 public HttpConnectionManagerParams getParams() {
197 return this.params;
198 }
199
200 /***
201 * Assigns {@link HttpConnectionManagerParams parameters} for this
202 * connection manager.
203 *
204 * @since 2.1
205 *
206 * @see HttpConnectionManagerParams
207 */
208 public void setParams(final HttpConnectionManagerParams params) {
209 if (params == null) {
210 throw new IllegalArgumentException("Parameters may not be null");
211 }
212 this.params = params;
213 }
214
215 /***
216 * @since 3.0
217 */
218 public void closeIdleConnections(long idleTimeout) {
219 long maxIdleTime = System.currentTimeMillis() - idleTimeout;
220 if (idleStartTime <= maxIdleTime) {
221 httpConnection.close();
222 }
223 }
224 }