1 30 31 package org.apache.commons.httpclient; 32 33 import java.io.IOException ; 34 import java.net.Socket ; 35 36 import org.apache.commons.httpclient.params.HttpClientParams; 37 import org.apache.commons.httpclient.params.HttpConnectionManagerParams; 38 import org.apache.commons.httpclient.params.HttpParams; 39 40 52 public class ProxyClient { 53 54 56 59 private HttpState state = new HttpState(); 60 61 64 private HttpClientParams params = null; 65 66 70 private HostConfiguration hostConfiguration = new HostConfiguration(); 71 72 77 public ProxyClient() { 78 this(new HttpClientParams()); 79 } 80 81 89 public ProxyClient(HttpClientParams params) { 90 super(); 91 if (params == null) { 92 throw new IllegalArgumentException ("Params may not be null"); 93 } 94 this.params = params; 95 } 96 97 99 105 public synchronized HttpState getState() { 106 return state; 107 } 108 109 115 public synchronized void setState(HttpState state) { 116 this.state = state; 117 } 118 119 125 public synchronized HostConfiguration getHostConfiguration() { 126 return hostConfiguration; 127 } 128 129 135 public synchronized void setHostConfiguration(HostConfiguration hostConfiguration) { 136 this.hostConfiguration = hostConfiguration; 137 } 138 139 144 public synchronized HttpClientParams getParams() { 145 return this.params; 146 } 147 148 153 public synchronized void setParams(final HttpClientParams params) { 154 if (params == null) { 155 throw new IllegalArgumentException ("Parameters may not be null"); 156 } 157 this.params = params; 158 } 159 160 180 public ConnectResponse connect() throws IOException , HttpException { 181 182 HostConfiguration hostconf = getHostConfiguration(); 183 if (hostconf.getProxyHost() == null) { 184 throw new IllegalStateException ("proxy host must be configured"); 185 } 186 if (hostconf.getHost() == null) { 187 throw new IllegalStateException ("destination host must be configured"); 188 } 189 if (hostconf.getProtocol().isSecure()) { 190 throw new IllegalStateException ("secure protocol socket factory may not be used"); 191 } 192 193 ConnectMethod method = new ConnectMethod(getHostConfiguration()); 194 method.getParams().setDefaults(getParams()); 195 196 DummyConnectionManager connectionManager = new DummyConnectionManager(); 197 connectionManager.setConnectionParams(getParams()); 198 199 HttpMethodDirector director = new HttpMethodDirector( 200 connectionManager, 201 hostconf, 202 getParams(), 203 getState() 204 ); 205 206 director.executeMethod(method); 207 208 ConnectResponse response = new ConnectResponse(); 209 response.setConnectMethod(method); 210 211 if (method.getStatusCode() == HttpStatus.SC_OK) { 213 response.setSocket(connectionManager.getConnection().getSocket()); 214 } else { 215 connectionManager.getConnection().close(); 216 } 217 218 return response; 219 } 220 221 224 public static class ConnectResponse { 225 226 private ConnectMethod connectMethod; 227 228 private Socket socket; 229 230 private ConnectResponse() {} 231 232 238 public ConnectMethod getConnectMethod() { 239 return connectMethod; 240 } 241 244 private void setConnectMethod(ConnectMethod connectMethod) { 245 this.connectMethod = connectMethod; 246 } 247 254 public Socket getSocket() { 255 return socket; 256 } 257 260 private void setSocket(Socket socket) { 261 this.socket = socket; 262 } 263 } 264 265 268 static class DummyConnectionManager implements HttpConnectionManager { 269 270 private HttpConnection httpConnection; 271 272 private HttpParams connectionParams; 273 274 public void closeIdleConnections(long idleTimeout) { 275 } 276 277 public HttpConnection getConnection() { 278 return httpConnection; 279 } 280 281 public void setConnectionParams(HttpParams httpParams) { 282 this.connectionParams = httpParams; 283 } 284 285 public HttpConnection getConnectionWithTimeout( 286 HostConfiguration hostConfiguration, long timeout) { 287 288 httpConnection = new HttpConnection(hostConfiguration); 289 httpConnection.setHttpConnectionManager(this); 290 httpConnection.getParams().setDefaults(connectionParams); 291 return httpConnection; 292 } 293 294 297 public HttpConnection getConnection(HostConfiguration hostConfiguration, long timeout) 298 throws HttpException { 299 return getConnectionWithTimeout(hostConfiguration, timeout); 300 } 301 302 public HttpConnection getConnection(HostConfiguration hostConfiguration) { 303 return getConnectionWithTimeout(hostConfiguration, -1); 304 } 305 306 public void releaseConnection(HttpConnection conn) { 307 } 308 309 public HttpConnectionManagerParams getParams() { 310 return null; 311 } 312 313 public void setParams(HttpConnectionManagerParams params) { 314 } 315 } 316 } 317 | Popular Tags |