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 import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
38
39 /***
40 */
41 public class NoHostHttpConnectionManager implements HttpConnectionManager {
42
43 private HttpConnection connection;
44
45 private boolean connectionReleased = false;
46
47 private HttpConnectionManagerParams params = new HttpConnectionManagerParams();
48
49 public NoHostHttpConnectionManager() {
50 super();
51 }
52
53 /***
54 * This method currently does nothing.
55 */
56 public void closeIdleConnections(long idleTimeout) {
57 }
58
59 /***
60 * @return
61 */
62 public boolean isConnectionReleased() {
63 return connectionReleased;
64 }
65
66 /***
67 * @param connection
68 */
69 public void setConnection(HttpConnection connection) {
70 this.connection = connection;
71 connection.setHttpConnectionManager(this);
72 connection.getParams().setDefaults(this.params);
73 }
74
75 public HttpConnection getConnection(HostConfiguration hostConfiguration) {
76
77
78
79 if (!hostConfiguration.hostEquals(connection)
80 || !hostConfiguration.proxyEquals(connection)) {
81
82 if (connection.isOpen()) {
83 connection.close();
84 }
85
86 connection.setHost(hostConfiguration.getHost());
87 connection.setPort(hostConfiguration.getPort());
88 connection.setProtocol(hostConfiguration.getProtocol());
89 connection.setLocalAddress(hostConfiguration.getLocalAddress());
90
91 connection.setProxyHost(hostConfiguration.getProxyHost());
92 connection.setProxyPort(hostConfiguration.getProxyPort());
93 } else {
94 finishLastResponse(connection);
95 }
96
97 connectionReleased = false;
98 return connection;
99 }
100
101 /***
102 * @deprecated
103 */
104 public HttpConnection getConnection(HostConfiguration hostConfiguration, long timeout)
105 throws HttpException {
106 return getConnection(hostConfiguration);
107 }
108
109 public HttpConnection getConnectionWithTimeout(
110 HostConfiguration hostConfiguration,
111 long timeout)
112 throws ConnectionPoolTimeoutException {
113 return getConnection(hostConfiguration);
114 }
115
116 public void releaseConnection(HttpConnection conn) {
117 if (conn != connection) {
118 throw new IllegalStateException("Unexpected close on a different connection.");
119 }
120
121 connectionReleased = true;
122 finishLastResponse(connection);
123 }
124
125 /***
126 * Since the same connection is about to be reused, make sure the
127 * previous request was completely processed, and if not
128 * consume it now.
129 * @param conn The connection
130 */
131 static void finishLastResponse(HttpConnection conn) {
132 InputStream lastResponse = conn.getLastResponseInputStream();
133 if (lastResponse != null) {
134 conn.setLastResponseInputStream(null);
135 try {
136 lastResponse.close();
137 } catch (IOException ioe) {
138
139 conn.close();
140 }
141 }
142 }
143
144 public HttpConnectionManagerParams getParams() {
145 return this.params;
146 }
147
148 public void setParams(final HttpConnectionManagerParams params) {
149 if (params == null) {
150 throw new IllegalArgumentException("Parameters may not be null");
151 }
152 this.params = params;
153 }
154
155 }