Return-Path: X-Original-To: apmail-commons-commits-archive@minotaur.apache.org Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id AB344D2CE for ; Sat, 14 Jul 2012 12:08:20 +0000 (UTC) Received: (qmail 37821 invoked by uid 500); 14 Jul 2012 12:08:19 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 37374 invoked by uid 500); 14 Jul 2012 12:08:14 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 37314 invoked by uid 99); 14 Jul 2012 12:08:12 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 14 Jul 2012 12:08:12 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED,T_FRT_STOCK2 X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 14 Jul 2012 12:08:08 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id C0A012388847 for ; Sat, 14 Jul 2012 12:07:47 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1361529 - in /commons/proper/net/trunk/src: changes/ main/java/org/apache/commons/net/ test/java/org/apache/commons/net/ Date: Sat, 14 Jul 2012 12:07:47 -0000 To: commits@commons.apache.org From: sebb@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20120714120747.C0A012388847@eris.apache.org> Author: sebb Date: Sat Jul 14 12:07:47 2012 New Revision: 1361529 URL: http://svn.apache.org/viewvc?rev=1361529&view=rev Log: NET-468 Request for native support for socks proxy routing with Commons net FTP Added: commons/proper/net/trunk/src/test/java/org/apache/commons/net/SocketClientFunctionalTest.java (with props) commons/proper/net/trunk/src/test/java/org/apache/commons/net/SocketClientTest.java (with props) Modified: commons/proper/net/trunk/src/changes/changes.xml commons/proper/net/trunk/src/main/java/org/apache/commons/net/DefaultSocketFactory.java commons/proper/net/trunk/src/main/java/org/apache/commons/net/SocketClient.java Modified: commons/proper/net/trunk/src/changes/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/net/trunk/src/changes/changes.xml?rev=1361529&r1=1361528&r2=1361529&view=diff ============================================================================== --- commons/proper/net/trunk/src/changes/changes.xml (original) +++ commons/proper/net/trunk/src/changes/changes.xml Sat Jul 14 12:07:47 2012 @@ -65,6 +65,9 @@ The type attribute can be add,u + + Request for native support for socks proxy routing with Commons net FTP. + FtpClient sends REST when calling listFiles. Clarified Javadoc. Modified: commons/proper/net/trunk/src/main/java/org/apache/commons/net/DefaultSocketFactory.java URL: http://svn.apache.org/viewvc/commons/proper/net/trunk/src/main/java/org/apache/commons/net/DefaultSocketFactory.java?rev=1361529&r1=1361528&r2=1361529&view=diff ============================================================================== --- commons/proper/net/trunk/src/main/java/org/apache/commons/net/DefaultSocketFactory.java (original) +++ commons/proper/net/trunk/src/main/java/org/apache/commons/net/DefaultSocketFactory.java Sat Jul 14 12:07:47 2012 @@ -19,6 +19,8 @@ package org.apache.commons.net; import java.io.IOException; import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.Proxy; import java.net.ServerSocket; import java.net.Socket; import java.net.UnknownHostException; @@ -40,6 +42,44 @@ import javax.net.SocketFactory; public class DefaultSocketFactory extends SocketFactory { + /** The proxy to use when creating new sockets. */ + private final Proxy connProxy; + + /** + * The default constructor. + */ + public DefaultSocketFactory() + { + this(null); + } + + /** + * A constructor for sockets with proxy support. + * + * @param proxy The Proxy to use when creating new Sockets. + * @since 3.2 + */ + public DefaultSocketFactory(Proxy proxy) + { + connProxy = proxy; + } + + /** + * Creates an unconnected Socket. + * + * @return A new unconnected Socket. + * @exception IOException If an I/O error occurs while creating the Socket. + * @since 3.2 + */ + @Override + public Socket createSocket() throws IOException + { + if (connProxy != null) + { + return new Socket(connProxy); + } + return new Socket(); + } /*** * Creates a Socket connected to the given host and port. @@ -54,6 +94,12 @@ public class DefaultSocketFactory extend public Socket createSocket(String host, int port) throws UnknownHostException, IOException { + if (connProxy != null) + { + Socket s = new Socket(connProxy); + s.connect(new InetSocketAddress(host, port)); + return s; + } return new Socket(host, port); } @@ -69,6 +115,12 @@ public class DefaultSocketFactory extend public Socket createSocket(InetAddress address, int port) throws IOException { + if (connProxy != null) + { + Socket s = new Socket(connProxy); + s.connect(new InetSocketAddress(address, port)); + return s; + } return new Socket(address, port); } @@ -89,6 +141,13 @@ public class DefaultSocketFactory extend InetAddress localAddr, int localPort) throws UnknownHostException, IOException { + if (connProxy != null) + { + Socket s = new Socket(connProxy); + s.bind(new InetSocketAddress(localAddr, localPort)); + s.connect(new InetSocketAddress(host, port)); + return s; + } return new Socket(host, port, localAddr, localPort); } @@ -108,6 +167,13 @@ public class DefaultSocketFactory extend InetAddress localAddr, int localPort) throws IOException { + if (connProxy != null) + { + Socket s = new Socket(connProxy); + s.bind(new InetSocketAddress(localAddr, localPort)); + s.connect(new InetSocketAddress(address, port)); + return s; + } return new Socket(address, port, localAddr, localPort); } Modified: commons/proper/net/trunk/src/main/java/org/apache/commons/net/SocketClient.java URL: http://svn.apache.org/viewvc/commons/proper/net/trunk/src/main/java/org/apache/commons/net/SocketClient.java?rev=1361529&r1=1361528&r2=1361529&view=diff ============================================================================== --- commons/proper/net/trunk/src/main/java/org/apache/commons/net/SocketClient.java (original) +++ commons/proper/net/trunk/src/main/java/org/apache/commons/net/SocketClient.java Sat Jul 14 12:07:47 2012 @@ -23,6 +23,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.net.InetAddress; import java.net.InetSocketAddress; +import java.net.Proxy; import java.net.Socket; import java.net.SocketException; @@ -103,6 +104,9 @@ public abstract class SocketClient /** Hint for SO_SNDBUF size */ private int sendBufferSize = -1; + /** The proxy to use when connecting. */ + private Proxy connProxy; + /** * Default constructor for SocketClient. Initializes * _socket_ to null, _timeout_ to 0, _defaultPort to 0, @@ -659,6 +663,7 @@ public abstract class SocketClient * connections. If the factory value is null, then a default * factory is used (only do this to reset the factory after having * previously altered it). + * Any proxy setting is discarded. *

* @param factory The new SocketFactory the SocketClient should use. */ @@ -669,6 +674,10 @@ public abstract class SocketClient } else { _socketFactory_ = factory; } + // re-setting the socket factory makes the proxy setting useless, + // so set the field to null so that getProxy() doesn't return a + // Proxy that we're actually not using. + connProxy = null; } /** @@ -781,6 +790,27 @@ public abstract class SocketClient return __commandSupport; } + /** + * Sets the proxy for use with all the connections. + * The proxy is used for connections established after the + * call to this method. + * + * @param proxy the new proxy for connections. + * @since 3.2 + */ + public void setProxy(Proxy proxy) { + setSocketFactory(new DefaultSocketFactory(proxy)); + connProxy = proxy; + } + + /** + * Gets the proxy for use with all the connections. + * @return the current proxy for connections. + */ + public Proxy getProxy() { + return connProxy; + } + /* * N.B. Fields cannot be pulled up into a super-class without breaking binary compatibility, * so the abstract method is needed to pass the instance to the methods which were moved here. Added: commons/proper/net/trunk/src/test/java/org/apache/commons/net/SocketClientFunctionalTest.java URL: http://svn.apache.org/viewvc/commons/proper/net/trunk/src/test/java/org/apache/commons/net/SocketClientFunctionalTest.java?rev=1361529&view=auto ============================================================================== --- commons/proper/net/trunk/src/test/java/org/apache/commons/net/SocketClientFunctionalTest.java (added) +++ commons/proper/net/trunk/src/test/java/org/apache/commons/net/SocketClientFunctionalTest.java Sat Jul 14 12:07:47 2012 @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ +package org.apache.commons.net; + +import junit.framework.TestCase; + +import java.net.InetSocketAddress; +import java.net.Proxy; + +import org.apache.commons.net.ftp.FTPClient; + +/** + * A simple functional test class for SocketClients. + */ +public class SocketClientFunctionalTest extends TestCase +{ + // any subclass will do, but it should be able to connect to the destination host + SocketClient sc = new FTPClient(); + private static final String PROXY_HOST = "127.0.0.1"; + private static final int PROXY_PORT = 9050; + private static final String DEST_HOST = "ftp.gnu.org"; + private static final int DEST_PORT = 21; + + /** + * The constructor for this test case. + * @param name passed to TestCase + */ + public SocketClientFunctionalTest(String name) + { + super(name); + } + + /** + * A simple test to verify that the Proxy settings work. + * @throws Exception in case of connection errors + */ + public void testProxySettings() throws Exception + { + // NOTE: HTTP Proxies seem to be invalid for raw sockets + Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(PROXY_HOST, PROXY_PORT)); + sc.setProxy(proxy); + sc.connect(DEST_HOST, DEST_PORT); + assertTrue(sc.isConnected()); + sc.disconnect(); + } +} + Propchange: commons/proper/net/trunk/src/test/java/org/apache/commons/net/SocketClientFunctionalTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/net/trunk/src/test/java/org/apache/commons/net/SocketClientFunctionalTest.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Added: commons/proper/net/trunk/src/test/java/org/apache/commons/net/SocketClientTest.java URL: http://svn.apache.org/viewvc/commons/proper/net/trunk/src/test/java/org/apache/commons/net/SocketClientTest.java?rev=1361529&view=auto ============================================================================== --- commons/proper/net/trunk/src/test/java/org/apache/commons/net/SocketClientTest.java (added) +++ commons/proper/net/trunk/src/test/java/org/apache/commons/net/SocketClientTest.java Sat Jul 14 12:07:47 2012 @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ +package org.apache.commons.net; + +import junit.framework.TestCase; + +import java.net.InetSocketAddress; +import java.net.Proxy; + +import org.apache.commons.net.ftp.FTPClient; + +/** + * A simple test class for SocketClient settings. + * + * @since 3.2 + */ +public class SocketClientTest extends TestCase +{ + private static final String PROXY_HOST = "127.0.0.1"; + private static final int PROXY_PORT = 1080; + + /** + * A simple test to verify that the Proxy is being set. + */ + public void testProxySettings() + { + SocketClient socketClient = new FTPClient(); + assertNull(socketClient.getProxy()); + Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(PROXY_HOST, PROXY_PORT)); + socketClient.setProxy(proxy); + assertEquals(proxy, socketClient.getProxy()); + assertFalse(socketClient.isConnected()); + } +} Propchange: commons/proper/net/trunk/src/test/java/org/apache/commons/net/SocketClientTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/net/trunk/src/test/java/org/apache/commons/net/SocketClientTest.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision