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
31
32 package org.apache.commons.httpclient;
33
34 import java.io.IOException;
35 import java.io.InputStream;
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 /*** The http connection */
53 private HttpConnection httpConnection;
54
55 /*** The value to set when calling setStaleCheckingEnabled() on connections */
56 private boolean connectionStaleCheckingEnabled = true;
57
58 /***
59 * Constructor for SimpleHttpConnectionManager.
60 */
61 public SimpleHttpConnectionManager() {
62 super();
63 }
64
65 /***
66 * @see HttpConnectionManager#getConnection(HostConfiguration)
67 */
68 public HttpConnection getConnection(HostConfiguration hostConfiguration) {
69 return getConnection(hostConfiguration, 0);
70 }
71
72 /***
73 * Gets the staleCheckingEnabled value to be set on HttpConnections that are created.
74 *
75 * @return <code>true</code> if stale checking will be enabled on HttpConections
76 *
77 * @see HttpConnection#isStaleCheckingEnabled()
78 */
79 public boolean isConnectionStaleCheckingEnabled() {
80 return connectionStaleCheckingEnabled;
81 }
82
83 /***
84 * Sets the staleCheckingEnabled value to be set on HttpConnections that are created.
85 *
86 * @param connectionStaleCheckingEnabled <code>true</code> if stale checking will be enabled
87 * on HttpConections
88 *
89 * @see HttpConnection#setStaleCheckingEnabled(boolean)
90 */
91 public void setConnectionStaleCheckingEnabled(boolean connectionStaleCheckingEnabled) {
92 this.connectionStaleCheckingEnabled = connectionStaleCheckingEnabled;
93 }
94
95 /***
96 * @see HttpConnectionManager#getConnection(HostConfiguration, long)
97 */
98 public HttpConnection getConnection(
99 HostConfiguration hostConfiguration, long timeout) {
100
101 if (httpConnection == null) {
102 httpConnection = new HttpConnection(hostConfiguration);
103 httpConnection.setStaleCheckingEnabled(connectionStaleCheckingEnabled);
104 } else {
105
106
107
108 if (!hostConfiguration.hostEquals(httpConnection)
109 || !hostConfiguration.proxyEquals(httpConnection)) {
110
111 if (httpConnection.isOpen()) {
112 httpConnection.close();
113 }
114
115 httpConnection.setStaleCheckingEnabled(connectionStaleCheckingEnabled);
116
117 httpConnection.setHost(hostConfiguration.getHost());
118 httpConnection.setVirtualHost(hostConfiguration.getVirtualHost());
119 httpConnection.setPort(hostConfiguration.getPort());
120 httpConnection.setProtocol(hostConfiguration.getProtocol());
121 httpConnection.setLocalAddress(hostConfiguration.getLocalAddress());
122
123 httpConnection.setProxyHost(hostConfiguration.getProxyHost());
124 httpConnection.setProxyPort(hostConfiguration.getProxyPort());
125 } else {
126 finishLastResponse(httpConnection);
127 }
128 }
129
130 return httpConnection;
131 }
132
133 /***
134 * @see HttpConnectionManager#releaseConnection(org.apache.commons.httpclient.HttpConnection)
135 */
136 public void releaseConnection(HttpConnection conn) {
137 if (conn != httpConnection) {
138 throw new IllegalStateException("Unexpected close on a different connection.");
139 }
140
141 finishLastResponse(httpConnection);
142 }
143
144 /***
145 * Since the same connection is about to be reused, make sure the
146 * previous request was completely processed, and if not
147 * consume it now.
148 * @param conn The connection
149 */
150 static void finishLastResponse(HttpConnection conn) {
151 InputStream lastResponse = conn.getLastResponseInputStream();
152 if (lastResponse != null) {
153 conn.setLastResponseInputStream(null);
154 try {
155 lastResponse.close();
156 } catch (IOException ioe) {
157
158 conn.close();
159 }
160 }
161 }
162 }