You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2021/02/05 02:18:55 UTC

[commons-vfs] branch master updated: Add support for FTP ControlKeepAliveReplyTimeout and ControlKeepAliveTimeout.

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 17f676f  Add support for FTP ControlKeepAliveReplyTimeout and ControlKeepAliveTimeout.
17f676f is described below

commit 17f676fe635cdcb45769a126480fda0255cb0a6f
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Thu Feb 4 21:18:50 2021 -0500

    Add support for FTP ControlKeepAliveReplyTimeout and
    ControlKeepAliveTimeout.
---
 .../commons/vfs2/FileSystemConfigBuilder.java      | 33 ++++++++++-
 .../vfs2/provider/ftp/FtpClientFactory.java        | 31 +++++++---
 .../provider/ftp/FtpFileSystemConfigBuilder.java   | 67 ++++++++++++++++++----
 .../test/FtpFileSystemConfigBuilderTestCase.java   | 48 ++++++++++++++++
 .../provider/ftp/test/FtpProviderTestCase.java     |  3 +
 src/changes/changes.xml                            |  3 +
 6 files changed, 164 insertions(+), 21 deletions(-)

diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/FileSystemConfigBuilder.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/FileSystemConfigBuilder.java
index 8451106..143ae55 100644
--- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/FileSystemConfigBuilder.java
+++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/FileSystemConfigBuilder.java
@@ -16,6 +16,7 @@
  */
 package org.apache.commons.vfs2;
 
+import java.time.Duration;
 import java.util.Objects;
 import java.util.function.Function;
 
@@ -249,6 +250,37 @@ public abstract class FileSystemConfigBuilder {
     }
 
     /**
+     * Gets a named option as a Duration.
+     *
+     * @param fileSystemOptions file system options to query, may be null.
+     * @param name the option name
+     * @return the option in {@code opts} or system properties, otherwise null
+     * @see #getLong(FileSystemOptions, String, Long)
+     * @throws NumberFormatException if option value is not a valid long.
+     *
+     * @since 2.8.0
+     */
+    protected Duration getDuration(final FileSystemOptions fileSystemOptions, final String name) {
+        return getDuration(fileSystemOptions, name, null);
+    }
+
+    /**
+     * Gets a named option as a Long.
+     *
+     * @param fileSystemOptions file system options to query, may be null.
+     * @param name the option name
+     * @param defaultValue value to return if option is not present
+     * @return the option in {@code opts} or system properties, otherwise {@code defaultValue}
+     * @throws NumberFormatException if option value is not a valid long.
+     *
+     * @since 2.8.0
+     */
+    protected Duration getDuration(final FileSystemOptions fileSystemOptions, final String name,
+        final Duration defaultValue) {
+        return getParam(fileSystemOptions, name, defaultValue, Duration::parse);
+    }
+
+    /**
      *Gets a named option as a Double.
      *
      * @param <E> enumeration type
@@ -282,7 +314,6 @@ public abstract class FileSystemConfigBuilder {
      */
     protected <E extends Enum<E>> E getEnum(final Class<E> enumClass, final FileSystemOptions fileSystemOptions,
         final String name, final E defaultValue) {
-        @SuppressWarnings("unchecked")
         E value = getParam(fileSystemOptions, name);
         if (value == null) {
             final String str = getProperty(name);
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 ea7e738..9ac10dc 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
@@ -21,6 +21,7 @@ import java.io.PrintWriter;
 import java.io.StringWriter;
 import java.io.Writer;
 import java.net.Proxy;
+import java.time.Duration;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -140,9 +141,9 @@ public final class FtpClientFactory {
 
                 try {
                     // Set connect timeout
-                    final Integer connectTimeout = builder.getConnectTimeout(fileSystemOptions);
-                    if (connectTimeout != null) {
-                        client.setDefaultTimeout(connectTimeout.intValue());
+                    final Integer connectTimeoutMillis = builder.getConnectTimeout(fileSystemOptions);
+                    if (connectTimeoutMillis != null) {
+                        client.setDefaultTimeout(connectTimeoutMillis.intValue());
                     }
 
                     final String controlEncoding = builder.getControlEncoding(fileSystemOptions);
@@ -169,9 +170,9 @@ public final class FtpClientFactory {
 
                     // Login
                     if (!client.login(UserAuthenticatorUtils.toString(username),
-                            UserAuthenticatorUtils.toString(password))) {
+                        UserAuthenticatorUtils.toString(password))) {
                         throw new FileSystemException("vfs.provider.ftp/login.error", hostname,
-                                UserAuthenticatorUtils.toString(username));
+                            UserAuthenticatorUtils.toString(username));
                     }
 
                     FtpFileType fileType = builder.getFileType(fileSystemOptions);
@@ -184,9 +185,9 @@ public final class FtpClientFactory {
                     }
 
                     // Set dataTimeout value
-                    final Integer dataTimeout = builder.getDataTimeout(fileSystemOptions);
-                    if (dataTimeout != null) {
-                        client.setDataTimeout(dataTimeout.intValue());
+                    final Integer dataTimeoutMillis = builder.getDataTimeout(fileSystemOptions);
+                    if (dataTimeoutMillis != null) {
+                        client.setDataTimeout(dataTimeoutMillis.intValue());
                     }
 
                     final Integer socketTimeout = builder.getSoTimeout(fileSystemOptions);
@@ -194,11 +195,23 @@ public final class FtpClientFactory {
                         client.setSoTimeout(socketTimeout.intValue());
                     }
 
+                    final Duration controlKeepAliveTimeout = builder.getControlKeepAliveTimeout(fileSystemOptions);
+                    if (controlKeepAliveTimeout != null) {
+                        // yes, in seconds.
+                        client.setControlKeepAliveTimeout(controlKeepAliveTimeout.toMillis() / 1000);
+                    }
+
+                    final Duration controlKeepAliveReplyTimeout = builder
+                        .getControlKeepAliveReplyTimeout(fileSystemOptions);
+                    if (controlKeepAliveReplyTimeout != null) {
+                        client.setControlKeepAliveReplyTimeout((int) controlKeepAliveReplyTimeout.toMillis());
+                    }
+
                     final Boolean userDirIsRoot = builder.getUserDirIsRoot(fileSystemOptions);
                     if (workingDirectory != null && (userDirIsRoot == null || !userDirIsRoot.booleanValue())) {
                         if (!client.changeWorkingDirectory(workingDirectory)) {
                             throw new FileSystemException("vfs.provider.ftp/change-work-directory.error",
-                                    workingDirectory);
+                                workingDirectory);
                         }
                     }
 
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 fe19586..110531f 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
@@ -17,6 +17,7 @@
 package org.apache.commons.vfs2.provider.ftp;
 
 import java.net.Proxy;
+import java.time.Duration;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
@@ -37,8 +38,8 @@ public class FtpFileSystemConfigBuilder extends FileSystemConfigBuilder {
     private static final FtpFileSystemConfigBuilder BUILDER = new FtpFileSystemConfigBuilder();
 
     private static final String AUTODETECT_UTF8 = _PREFIX + ".AUTODETECT_UTF8";
-    private static final String CONNECT_TIMEOUT = _PREFIX + ".CONNECT_TIMEOUT";
-    private static final String DATA_TIMEOUT = _PREFIX + ".DATA_TIMEOUT";
+    private static final String CONNECT_TIMEOUT_MILLIS = _PREFIX + ".CONNECT_TIMEOUT";
+    private static final String DATA_TIMEOUT_MILLIS = _PREFIX + ".DATA_TIMEOUT";
     private static final String DEFAULT_DATE_FORMAT = _PREFIX + ".DEFAULT_DATE_FORMAT";
     private static final String ENCODING = _PREFIX + ".ENCODING";
     private static final String FACTORY_KEY = FTPFileEntryParserFactory.class.getName() + ".KEY";
@@ -51,6 +52,8 @@ public class FtpFileSystemConfigBuilder extends FileSystemConfigBuilder {
     private static final String SERVER_TIME_ZONE_ID = _PREFIX + ".SERVER_TIME_ZONE_ID";
     private static final String SHORT_MONTH_NAMES = _PREFIX + ".SHORT_MONTH_NAMES";
     private static final String SO_TIMEOUT = _PREFIX + ".SO_TIMEOUT";
+    private static final String CONTROL_KEEP_ALIVE_TIMEOUT = _PREFIX + ".CONTROL_KEEP_ALIVE_TIMEOUT";
+    private static final String CONTROL_KEEP_ALIVE_REPLY_TIMEOUT = _PREFIX + ".CONTROL_KEEP_ALIVE_REPLY_TIMEOUT";
     private static final String USER_DIR_IS_ROOT = _PREFIX + ".USER_DIR_IS_ROOT";
     private static final String TRANSFER_ABORTED_OK_REPLY_CODES = _PREFIX + ".TRANSFER_ABORTED_OK_REPLY_CODES";
 
@@ -106,7 +109,7 @@ public class FtpFileSystemConfigBuilder extends FileSystemConfigBuilder {
      * @since 2.1
      */
     public Integer getConnectTimeout(final FileSystemOptions opts) {
-        return getInteger(opts, CONNECT_TIMEOUT);
+        return getInteger(opts, CONNECT_TIMEOUT_MILLIS);
     }
 
     /**
@@ -124,7 +127,7 @@ public class FtpFileSystemConfigBuilder extends FileSystemConfigBuilder {
      * @see #setDataTimeout
      */
     public Integer getDataTimeout(final FileSystemOptions opts) {
-        return getInteger(opts, DATA_TIMEOUT);
+        return getInteger(opts, DATA_TIMEOUT_MILLIS);
     }
 
     /**
@@ -153,7 +156,7 @@ public class FtpFileSystemConfigBuilder extends FileSystemConfigBuilder {
      * @return An FTPFileEntryParserFactory.
      */
     public FTPFileEntryParserFactory getEntryParserFactory(final FileSystemOptions opts) {
-        return (FTPFileEntryParserFactory) getParam(opts, FTPFileEntryParserFactory.class.getName());
+        return getParam(opts, FTPFileEntryParserFactory.class.getName());
     }
 
     /**
@@ -184,7 +187,7 @@ public class FtpFileSystemConfigBuilder extends FileSystemConfigBuilder {
      * @since 2.1
      */
     public Proxy getProxy(final FileSystemOptions opts) {
-        return (Proxy) this.getParam(opts, PROXY);
+        return getParam(opts, PROXY);
     }
 
     /**
@@ -235,7 +238,7 @@ public class FtpFileSystemConfigBuilder extends FileSystemConfigBuilder {
      * @return An array of short month names.
      */
     public String[] getShortMonthNames(final FileSystemOptions opts) {
-        return (String[]) getParam(opts, SHORT_MONTH_NAMES);
+        return getParam(opts, SHORT_MONTH_NAMES);
     }
 
     /**
@@ -249,6 +252,24 @@ public class FtpFileSystemConfigBuilder extends FileSystemConfigBuilder {
     }
 
     /**
+     * @param opts The FileSystem options
+     * @return The controlKeepAliveTimeout value.
+     * @since 2.8.0
+     */
+    public Duration getControlKeepAliveTimeout(FileSystemOptions opts) {
+        return getDuration(opts, CONTROL_KEEP_ALIVE_TIMEOUT);
+    }
+
+    /**
+     * @param opts The FileSystem options
+     * @return The controlKeepAliveReplyTimeout value.
+     * @since 2.8.0
+     */
+    public Duration getControlKeepAliveReplyTimeout(FileSystemOptions opts) {
+        return getDuration(opts, CONTROL_KEEP_ALIVE_REPLY_TIMEOUT);
+    }
+    
+    /**
      * Returns {@link Boolean#TRUE} if VFS should treat the user directory as the root directory. Defaults to
      * {@code Boolean.TRUE} if the method {@link #setUserDirIsRoot(FileSystemOptions, boolean)} has not been
      * invoked.
@@ -267,9 +288,8 @@ public class FtpFileSystemConfigBuilder extends FileSystemConfigBuilder {
      * closing a stream.
      * @since 2.4
      */
-    @SuppressWarnings("unchecked")
     public List<Integer> getTransferAbortedOkReplyCodes(final FileSystemOptions opts) {
-        return (List<Integer>) getParam(opts, TRANSFER_ABORTED_OK_REPLY_CODES);
+        return getParam(opts, TRANSFER_ABORTED_OK_REPLY_CODES);
     }
 
     /**
@@ -294,7 +314,7 @@ public class FtpFileSystemConfigBuilder extends FileSystemConfigBuilder {
      * @since 2.1
      */
     public void setConnectTimeout(final FileSystemOptions opts, final Integer connectTimeout) {
-        setParam(opts, CONNECT_TIMEOUT, connectTimeout);
+        setParam(opts, CONNECT_TIMEOUT_MILLIS, connectTimeout);
     }
 
     /**
@@ -318,7 +338,7 @@ public class FtpFileSystemConfigBuilder extends FileSystemConfigBuilder {
      * @param dataTimeout The timeout value.
      */
     public void setDataTimeout(final FileSystemOptions opts, final Integer dataTimeout) {
-        setParam(opts, DATA_TIMEOUT, dataTimeout);
+        setParam(opts, DATA_TIMEOUT_MILLIS, dataTimeout);
     }
 
     /**
@@ -461,6 +481,31 @@ public class FtpFileSystemConfigBuilder extends FileSystemConfigBuilder {
     public void setSoTimeout(final FileSystemOptions opts, final Integer soTimeout) {
         setParam(opts, SO_TIMEOUT, soTimeout);
     }
+    
+    /**
+     * Sets the control keep alive timeout for the FTP client.
+     * <p>
+     * Set the {@code controlKeepAliveTimeout} to ensure the socket be alive after download huge file.
+     * </p>
+     *
+     * @param opts The FileSystem options.
+     * @param controlKeepAliveTimeout The timeout duration.
+     * @since 2.8.0
+     */
+    public void setControlKeepAliveTimeout(FileSystemOptions opts, final Duration controlKeepAliveTimeout) {
+        setParam(opts, CONTROL_KEEP_ALIVE_TIMEOUT, controlKeepAliveTimeout);
+    }
+
+    /**
+     * Sets the control keep alive reply timeout for the FTP client.
+     *
+     * @param opts The FileSystem options.
+     * @param controlKeepAliveReplyTimeout timeout duration.
+     * @since 2.8.0
+     */
+    public void setControlKeepAliveReplyTimeout(FileSystemOptions opts, final Duration controlKeepAliveReplyTimeout) {
+        setParam(opts, CONTROL_KEEP_ALIVE_REPLY_TIMEOUT, controlKeepAliveReplyTimeout);
+    }
 
     /**
      * Use user directory as root (do not change to fs root).
diff --git a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/ftp/test/FtpFileSystemConfigBuilderTestCase.java b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/ftp/test/FtpFileSystemConfigBuilderTestCase.java
new file mode 100644
index 0000000..dc1cca6
--- /dev/null
+++ b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/ftp/test/FtpFileSystemConfigBuilderTestCase.java
@@ -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.vfs2.provider.ftp.test;
+
+import static org.junit.Assert.assertEquals;
+
+import java.time.Duration;
+
+import org.apache.commons.vfs2.FileSystemOptions;
+import org.apache.commons.vfs2.provider.ftp.FtpFileSystemConfigBuilder;
+import org.junit.Test;
+
+/**
+ * Tests {@link FtpFileSystemConfigBuilder}.
+ */
+public class FtpFileSystemConfigBuilderTestCase {
+
+    @Test
+    public void testControlKeepAliveReplyTimeout() {
+        final FtpFileSystemConfigBuilder instance = FtpFileSystemConfigBuilder.getInstance();
+        FileSystemOptions options = new FileSystemOptions();
+        instance.setControlKeepAliveReplyTimeout(options, Duration.ofSeconds(10));
+        assertEquals(Duration.ofSeconds(10), instance.getControlKeepAliveReplyTimeout(options));
+    }
+
+    @Test
+    public void testControlKeepAliveTimeout() {
+        final FtpFileSystemConfigBuilder instance = FtpFileSystemConfigBuilder.getInstance();
+        FileSystemOptions options = new FileSystemOptions();
+        instance.setControlKeepAliveTimeout(options, Duration.ofSeconds(10));
+        assertEquals(Duration.ofSeconds(10), instance.getControlKeepAliveTimeout(options));
+    }
+}
diff --git a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/ftp/test/FtpProviderTestCase.java b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/ftp/test/FtpProviderTestCase.java
index a17f922..caf4f59 100644
--- a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/ftp/test/FtpProviderTestCase.java
+++ b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/ftp/test/FtpProviderTestCase.java
@@ -18,6 +18,7 @@ package org.apache.commons.vfs2.provider.ftp.test;
 
 import java.io.IOException;
 import java.net.URL;
+import java.time.Duration;
 
 import org.apache.commons.vfs2.FileObject;
 import org.apache.commons.vfs2.FileSystemManager;
@@ -173,6 +174,8 @@ public class FtpProviderTestCase extends AbstractProviderTestConfig {
         builder.setFileType(opts, FtpFileType.BINARY);
         builder.setConnectTimeout(opts, 10000);
         builder.setControlEncoding(opts, "UTF-8");
+        builder.setControlKeepAliveReplyTimeout(opts, Duration.ofSeconds(35));
+        builder.setControlKeepAliveTimeout(opts, Duration.ofSeconds(30));
         return manager.resolveFile(uri, opts);
     }
 
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index ec1c36d..9461bea 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -76,6 +76,9 @@ The <action> type attribute can be add,update,fix,remove.
       <action dev="ggregory" due-to="Gary Gregory" type="update">
         Add and use VFS.close().
       </action>
+      <action dev="ggregory" due-to="Gary Gregory, xiaqingyun" type="update">
+        Add support for FTP ControlKeepAliveReplyTimeout and ControlKeepAliveTimeout.
+      </action>
       <!-- UPDATES -->
       <action dev="ggregory" due-to="PeterAlfredLee" type="update">
         Modify some code use for-each loop and stream API #142.