Index: src/java/org/apache/commons/httpclient/HttpConnection.java =================================================================== retrieving revision 1.85 diff -u -r1.85 HttpConnection.java --- src/java/org/apache/commons/httpclient/HttpConnection.java 22 Feb 2004 18:08:45 -0000 1.85 +++ src/java/org/apache/commons/httpclient/HttpConnection.java 5 Apr 2004 04:37:37 -0000 @@ -199,6 +199,15 @@ } // ------------------------------------------ Attribute Setters and Getters + + /** + * Returns the connection socket. + * + * @return the socket. + */ + protected Socket getSocket() { + return this.socket; + } /** * Returns the host. Index: src/examples/ProxyTunnelDemo.java =================================================================== RCS file: src/examples/ProxyTunnelDemo.java diff -N src/examples/ProxyTunnelDemo.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/examples/ProxyTunnelDemo.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,76 @@ +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.net.Socket; + +import org.apache.commons.httpclient.ProxyClient; +import org.apache.commons.httpclient.UsernamePasswordCredentials; +import org.apache.commons.httpclient.auth.HttpAuthRealm; + +/* + * $Header$ + * $Revision$ + * $Date$ + * ==================================================================== + * + * Copyright 2002-2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + * [Additional notices, if required by prior licensing conditions] + * + */ + +/** + * @author Oleg Kalnichevski + */ +public class ProxyTunnelDemo { + + public static void main(String[] args) throws Exception { + + ProxyClient proxyclient = new ProxyClient(); + proxyclient.getHostConfiguration().setHost("www.yahoo.com"); + proxyclient.getHostConfiguration().setProxy("localhost", 8888); + proxyclient.getState().setProxyCredentials( + new HttpAuthRealm("localhost", 8888, "squid"), + new UsernamePasswordCredentials("squid", "squid")); + Socket socket = proxyclient.connect(); + if (socket != null) { + try { + Writer out = new OutputStreamWriter( + socket.getOutputStream(), "ISO-8859-1"); + out.write("GET http://www.yahoo.com/ HTTP/1.1\r\n"); + out.write("Host: www.yahoo.com\r\n"); + out.write("Agent: whatever\r\n"); + out.write("\r\n"); + out.flush(); + BufferedReader in = new BufferedReader( + new InputStreamReader(socket.getInputStream(), "ISO-8859-1")); + String line = null; + while ((line = in.readLine()) != null) { + System.out.println(line); + } + } finally { + socket.close(); + } + } + } + +} Index: src/java/org/apache/commons/httpclient/ProxyClient.java =================================================================== RCS file: src/java/org/apache/commons/httpclient/ProxyClient.java diff -N src/java/org/apache/commons/httpclient/ProxyClient.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/java/org/apache/commons/httpclient/ProxyClient.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,225 @@ +/* + * $Header$ + * $Revision$ + * $Date$ + * + * ==================================================================== + * + * Copyright 1999-2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + * [Additional notices, if required by prior licensing conditions] + * + */ + +package org.apache.commons.httpclient; + +import java.io.IOException; +import java.net.Socket; + +import org.apache.commons.httpclient.params.HttpClientParams; +import org.apache.commons.httpclient.params.HttpConnectionManagerParams; +import org.apache.commons.httpclient.params.HttpParams; + + +/** + * @author Oleg Kalnichevski + * + * @version $Revision$ + */ +public class ProxyClient { + + // ----------------------------------------------------- Instance Variables + + /** + * The {@link HttpState HTTP state} associated with this ProxyClient. + */ + private HttpState state = new HttpState(); + + /** + * The {@link HttpClientParams collection of parameters} associated with this ProxyClient. + */ + private HttpClientParams params = null; + + /** + * The {@link HostConfiguration host configuration} associated with + * the ProxyClient + */ + private HostConfiguration hostConfiguration = new HostConfiguration(); + + /** + * Creates an instance of ProxyClient using default {@link HttpClientParams parameter set}. + * + * @see HttpClientParams + */ + public ProxyClient() { + this(new HttpClientParams()); + } + + /** + * Creates an instance of ProxyClient using the given + * {@link HttpClientParams parameter set}. + * + * @param params The {@link HttpClientParams parameters} to use. + * + * @see HttpClientParams + * + * @since 2.1 + */ + public ProxyClient(HttpClientParams params) { + super(); + if (params == null) { + throw new IllegalArgumentException("Params may not be null"); + } + this.params = params; + } + + // ------------------------------------------------------------- Properties + + /** + * Returns {@link HttpState HTTP state} associated with the ProxyClient. + * + * @see #setState(HttpState) + * @return the shared client state + */ + public synchronized HttpState getState() { + return state; + } + + /** + * Assigns {@link HttpState HTTP state} for the ProxyClient. + * + * @see #getState() + * @param state the new {@link HttpState HTTP state} for the client + */ + public synchronized void setState(HttpState state) { + this.state = state; + } + + /** + * Returns the {@link HostConfiguration host configuration} associated with the + * ProxyClient. + * + * @return {@link HostConfiguration host configuration} + * + * @since 2.0 + */ + public synchronized HostConfiguration getHostConfiguration() { + return hostConfiguration; + } + + /** + * Assigns the {@link HostConfiguration host configuration} to use with the + * ProxyClient. + * + * @param hostConfiguration The {@link HostConfiguration host configuration} to set + * + * @since 2.0 + */ + public synchronized void setHostConfiguration(HostConfiguration hostConfiguration) { + this.hostConfiguration = hostConfiguration; + } + + /** + * Returns {@link HttpClientParams HTTP protocol parameters} associated with this ProxyClient. + * + * @since 2.1 + * + * @see HttpClientParams + */ + public synchronized HttpClientParams getParams() { + return this.params; + } + + /** + * Assigns {@link HttpClientParams HTTP protocol parameters} for this ProxyClient. + * + * @since 2.1 + * + * @see HttpClientParams + */ + public synchronized void setParams(final HttpClientParams params) { + if (params == null) { + throw new IllegalArgumentException("Parameters may not be null"); + } + this.params = params; + } + + public synchronized Socket connect() throws IOException, HttpException { + ConnectMethod method = new ConnectMethod(); + method.getParams().setDefaults(this.params); + + DummyConnectionManager connectionManager = new DummyConnectionManager(); + connectionManager.setConnectionParams(this.params); + + HttpMethodDirector director = new HttpMethodDirector( + connectionManager, + getHostConfiguration(), + this.params, + (state == null ? getState() : state)); + + director.executeMethod(method); + + return connectionManager.getConnection().getSocket(); + } + + class DummyConnectionManager implements HttpConnectionManager { + + private HttpConnection httpConnection; + + private HttpParams connectionParams; + + public HttpConnection getConnection() { + return httpConnection; + } + + public void setConnectionParams(HttpParams httpParams) { + this.connectionParams = httpParams; + } + + public HttpConnection getConnectionWithTimeout( + HostConfiguration hostConfiguration, long timeout) { + + httpConnection = new HttpConnection(hostConfiguration); + httpConnection.setHttpConnectionManager(this); + httpConnection.getParams().setDefaults(connectionParams); + return httpConnection; + } + + public HttpConnection getConnection(HostConfiguration hostConfiguration, long timeout) + throws HttpException { + return getConnectionWithTimeout(hostConfiguration, timeout); + } + + public HttpConnection getConnection(HostConfiguration hostConfiguration) { + return getConnectionWithTimeout(hostConfiguration, -1); + } + + public void releaseConnection(HttpConnection conn) { + } + + public HttpConnectionManagerParams getParams() { + return null; + } + + public void setParams(HttpConnectionManagerParams params) { + } + } +}