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
33
34
35
36
37
38
39
40
41
42
43
44
45
46 package edu.internet2.middleware.grouperClientExt.org.apache.commons.httpclient;
47
48 import java.io.IOException;
49 import java.net.Socket;
50
51 import edu.internet2.middleware.grouperClientExt.org.apache.commons.httpclient.params.HttpClientParams;
52 import edu.internet2.middleware.grouperClientExt.org.apache.commons.httpclient.params.HttpConnectionManagerParams;
53 import edu.internet2.middleware.grouperClientExt.org.apache.commons.httpclient.params.HttpParams;
54
55
56
57
58
59
60
61
62
63
64
65
66
67 public class ProxyClient {
68
69
70
71
72
73
74 private HttpStateleware/grouperClientExt/org/apache/commons/httpclient/HttpState.html#HttpState">HttpState state = new HttpState();
75
76
77
78
79 private HttpClientParams params = null;
80
81
82
83
84
85 private HostConfigurationExt/org/apache/commons/httpclient/HostConfiguration.html#HostConfiguration">HostConfiguration hostConfiguration = new HostConfiguration();
86
87
88
89
90
91
92 public ProxyClient() {
93 this(new HttpClientParams());
94 }
95
96
97
98
99
100
101
102
103
104 public ProxyClient(HttpClientParams params) {
105 super();
106 if (params == null) {
107 throw new IllegalArgumentException("Params may not be null");
108 }
109 this.params = params;
110 }
111
112
113
114
115
116
117
118
119
120 public synchronized HttpState getState() {
121 return state;
122 }
123
124
125
126
127
128
129
130 public synchronized void setState(HttpState state) {
131 this.state = state;
132 }
133
134
135
136
137
138
139
140 public synchronized HostConfiguration getHostConfiguration() {
141 return hostConfiguration;
142 }
143
144
145
146
147
148
149
150 public synchronized void setHostConfiguration(HostConfiguration hostConfiguration) {
151 this.hostConfiguration = hostConfiguration;
152 }
153
154
155
156
157
158
159 public synchronized HttpClientParams getParams() {
160 return this.params;
161 }
162
163
164
165
166
167
168 public synchronized void setParams(final HttpClientParams params) {
169 if (params == null) {
170 throw new IllegalArgumentException("Parameters may not be null");
171 }
172 this.params = params;
173 }
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195 public ConnectResponse connect() throws IOException, HttpException {
196
197 HostConfiguration hostconf = getHostConfiguration();
198 if (hostconf.getProxyHost() == null) {
199 throw new IllegalStateException("proxy host must be configured");
200 }
201 if (hostconf.getHost() == null) {
202 throw new IllegalStateException("destination host must be configured");
203 }
204 if (hostconf.getProtocol().isSecure()) {
205 throw new IllegalStateException("secure protocol socket factory may not be used");
206 }
207
208 ConnectMethode/grouperClientExt/org/apache/commons/httpclient/ConnectMethod.html#ConnectMethod">ConnectMethod method = new ConnectMethod(getHostConfiguration());
209 method.getParams().setDefaults(getParams());
210
211 DummyConnectionManager connectionManager = new DummyConnectionManager();
212 connectionManager.setConnectionParams(getParams());
213
214 HttpMethodDirectorerClientExt/org/apache/commons/httpclient/HttpMethodDirector.html#HttpMethodDirector">HttpMethodDirector director = new HttpMethodDirector(
215 connectionManager,
216 hostconf,
217 getParams(),
218 getState()
219 );
220
221 director.executeMethod(method);
222
223 ConnectResponse response = new ConnectResponse();
224 response.setConnectMethod(method);
225
226
227 if (method.getStatusCode() == HttpStatus.SC_OK) {
228 response.setSocket(connectionManager.getConnection().getSocket());
229 } else {
230 connectionManager.getConnection().close();
231 }
232
233 return response;
234 }
235
236
237
238
239 public static class ConnectResponse {
240
241 private ConnectMethod connectMethod;
242
243 private Socket socket;
244
245 private ConnectResponse() {}
246
247
248
249
250
251
252
253 public ConnectMethod getConnectMethod() {
254 return connectMethod;
255 }
256
257
258
259 private void setConnectMethod(ConnectMethod connectMethod) {
260 this.connectMethod = connectMethod;
261 }
262
263
264
265
266
267
268
269 public Socket getSocket() {
270 return socket;
271 }
272
273
274
275 private void setSocket(Socket socket) {
276 this.socket = socket;
277 }
278 }
279
280
281
282
283 static class DummyConnectionManager implements HttpConnectionManager {
284
285 private HttpConnection httpConnection;
286
287 private HttpParams connectionParams;
288
289 public void closeIdleConnections(long idleTimeout) {
290 }
291
292 public HttpConnection getConnection() {
293 return httpConnection;
294 }
295
296 public void setConnectionParams(HttpParams httpParams) {
297 this.connectionParams = httpParams;
298 }
299
300 public HttpConnection getConnectionWithTimeout(
301 HostConfiguration hostConfiguration, long timeout) {
302
303 httpConnection = new HttpConnection(hostConfiguration);
304 httpConnection.setHttpConnectionManager(this);
305 httpConnection.getParams().setDefaults(connectionParams);
306 return httpConnection;
307 }
308
309
310
311
312 public HttpConnection getConnection(HostConfiguration hostConfiguration, long timeout)
313 throws HttpException {
314 return getConnectionWithTimeout(hostConfiguration, timeout);
315 }
316
317 public HttpConnection getConnection(HostConfiguration hostConfiguration) {
318 return getConnectionWithTimeout(hostConfiguration, -1);
319 }
320
321 public void releaseConnection(HttpConnection conn) {
322 }
323
324 public HttpConnectionManagerParams getParams() {
325 return null;
326 }
327
328 public void setParams(HttpConnectionManagerParams params) {
329 }
330 }
331 }