This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-vfs.git
The following commit(s) were added to refs/heads/master by this push: new 224b39a3 Add active port range configuration for FTP client factory (#318) 224b39a3 is described below commit 224b39a3862720fd96bb0a15138fc9ad4b2a0213 Author: Maksym Perevertov <maxgls....@gmail.com> AuthorDate: Mon Oct 17 16:44:07 2022 +0200 Add active port range configuration for FTP client factory (#318) * Add active port range configuration for FtpClient * Add test of ActivePortRange configuration * remove unused import in FtpClientFactory * remove FTPActivePortRange to use generic Range * Use Range instead of removed FtpActivePortRange * use Range instead of FtpActivePortRange in test * add since tag for active port range configuration --- .../vfs2/provider/ftp/FtpClientFactory.java | 6 ++++++ .../provider/ftp/FtpFileSystemConfigBuilder.java | 24 ++++++++++++++++++++++ .../ftp/FtpFileSystemConfigBuilderTest.java | 8 ++++++++ 3 files changed, 38 insertions(+) diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FtpClientFactory.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FtpClientFactory.java index 505dc9fc..c5bf5826 100644 --- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FtpClientFactory.java +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FtpClientFactory.java @@ -23,6 +23,7 @@ import java.io.Writer; import java.net.Proxy; import java.time.Duration; +import org.apache.commons.lang3.Range; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.time.DurationUtils; import org.apache.commons.logging.Log; @@ -240,6 +241,11 @@ public final class FtpClientFactory { client.enterLocalPassiveMode(); } + final Range<Integer> activePortRange = builder.getActivePortRange(fileSystemOptions); + if (activePortRange != null) { + client.setActivePortRange(activePortRange.getMinimum(), activePortRange.getMaximum()); + } + setupOpenConnection(client, fileSystemOptions); } catch (final IOException e) { if (client.isConnected()) { diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FtpFileSystemConfigBuilder.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FtpFileSystemConfigBuilder.java index 549072d2..01e5474c 100644 --- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FtpFileSystemConfigBuilder.java +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FtpFileSystemConfigBuilder.java @@ -22,6 +22,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import org.apache.commons.lang3.Range; import org.apache.commons.net.ftp.FTPReply; import org.apache.commons.net.ftp.parser.FTPFileEntryParserFactory; import org.apache.commons.vfs2.FileContent; @@ -46,6 +47,7 @@ public class FtpFileSystemConfigBuilder extends FileSystemConfigBuilder { private static final String FACTORY_KEY = FTPFileEntryParserFactory.class.getName() + ".KEY"; private static final String FILE_TYPE = PREFIX + ".FILE_TYPE"; private static final String PASSIVE_MODE = PREFIX + ".PASSIVE"; + private static final String ACTIVE_PORT_RANGE = PREFIX + ".ACTIVE_PORT_RANGE"; private static final String PROXY = PREFIX + ".PROXY"; private static final String RECENT_DATE_FORMAT = PREFIX + ".RECENT_DATE_FORMAT"; private static final String REMOTE_VERIFICATION = PREFIX + ".REMOTE_VERIFICATION"; @@ -244,6 +246,17 @@ public class FtpFileSystemConfigBuilder extends FileSystemConfigBuilder { return getBoolean(options, PASSIVE_MODE); } + /** + * Gets the active port range. + * + * @param options The FileSystemOptions. + * @return the Range of active ports + * @since 2.10.0 + */ + public Range<Integer> getActivePortRange(final FileSystemOptions options) { + return getParam(options, ACTIVE_PORT_RANGE); + } + /** * Gets the Proxy. * @@ -528,6 +541,17 @@ public class FtpFileSystemConfigBuilder extends FileSystemConfigBuilder { setParam(options, PASSIVE_MODE, toBooleanObject(passiveMode)); } + /** + * Sets the active port range. + * + * @param options The FileSystemOptions. + * @param portRange the Range of active ports + * @since 2.10.0 + */ + public void setActivePortRange(final FileSystemOptions options, final Range<Integer> portRange) { + setParam(options, ACTIVE_PORT_RANGE, portRange); + } + /** * Sets the Proxy. * <p> diff --git a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/ftp/FtpFileSystemConfigBuilderTest.java b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/ftp/FtpFileSystemConfigBuilderTest.java index ea017a0e..b87c63f1 100644 --- a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/ftp/FtpFileSystemConfigBuilderTest.java +++ b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/ftp/FtpFileSystemConfigBuilderTest.java @@ -20,6 +20,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import java.time.Duration; +import org.apache.commons.lang3.Range; import org.apache.commons.vfs2.FileSystemOptions; import org.junit.jupiter.api.Test; @@ -44,4 +45,11 @@ public class FtpFileSystemConfigBuilderTest { assertEquals(Duration.ofSeconds(10), instance.getControlKeepAliveTimeout(options)); } + @Test + public void testActivePortRange() { + final FtpFileSystemConfigBuilder instance = FtpFileSystemConfigBuilder.getInstance(); + final FileSystemOptions options = new FileSystemOptions(); + instance.setActivePortRange(options, Range.between(2121, 2125)); + assertEquals(Range.between(2121, 2125), instance.getActivePortRange(options)); + } }