Use java.time.Duration for timeouts.
- [HTTP4] Add
Http4FileSystemConfigBuilder.setConnectionTimeout(FileSystemOptions,
Duration) and deprecate Integer version.
- [HTTP4] Add
Http4FileSystemConfigBuilder.setSoTimeout(FileSystemOptions, Duration)
and deprecate Integer version.
- [HTTP4] Add
Http4FileSystemConfigBuilder.getConnectionTimeoutDuration(FileSystemOptions)
and deprecate Integer version.
- [HTTP4] Add
Http4FileSystemConfigBuilder.getSoTimeoutDuration(FileSystemOptions) and
deprecate Integer version.
diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/Http4FileProvider.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/Http4FileProvider.java
index 91e3a38..1a2c11d 100644
--- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/Http4FileProvider.java
+++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/Http4FileProvider.java
@@ -42,6 +42,7 @@
import org.apache.commons.vfs2.UserAuthenticator;
import org.apache.commons.vfs2.provider.AbstractOriginatingFileProvider;
import org.apache.commons.vfs2.provider.GenericFileName;
+import org.apache.commons.vfs2.util.DurationUtils;
import org.apache.commons.vfs2.util.UserAuthenticatorUtils;
import org.apache.http.ConnectionReuseStrategy;
import org.apache.http.Header;
@@ -112,16 +113,18 @@
}
private HttpClientConnectionManager createConnectionManager(final Http4FileSystemConfigBuilder builder,
- final FileSystemOptions fileSystemOptions) throws FileSystemException {
+ final FileSystemOptions fileSystemOptions) {
final PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
connManager.setMaxTotal(builder.getMaxTotalConnections(fileSystemOptions));
connManager.setDefaultMaxPerRoute(builder.getMaxConnectionsPerHost(fileSystemOptions));
+ // @formatter:off
final SocketConfig socketConfig =
SocketConfig
.custom()
- .setSoTimeout(builder.getSoTimeout(fileSystemOptions))
+ .setSoTimeout(DurationUtils.toMillisInt(builder.getSoTimeoutDuration(fileSystemOptions)))
.build();
+ // @formatter:on
connManager.setDefaultSocketConfig(socketConfig);
@@ -141,10 +144,10 @@
}
private RequestConfig createDefaultRequestConfig(final Http4FileSystemConfigBuilder builder,
- final FileSystemOptions fileSystemOptions) {
+ final FileSystemOptions fileSystemOptions) {
return RequestConfig.custom()
- .setConnectTimeout(builder.getConnectionTimeout(fileSystemOptions))
- .build();
+ .setConnectTimeout(DurationUtils.toMillisInt(builder.getConnectionTimeoutDuration(fileSystemOptions)))
+ .build();
}
private HostnameVerifier createHostnameVerifier(final Http4FileSystemConfigBuilder builder,
diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/Http4FileSystemConfigBuilder.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/Http4FileSystemConfigBuilder.java
index 9506a9d..f5a41bc 100644
--- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/Http4FileSystemConfigBuilder.java
+++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/Http4FileSystemConfigBuilder.java
@@ -17,6 +17,7 @@
package org.apache.commons.vfs2.provider.http4;
import java.security.KeyStore;
+import java.time.Duration;
import org.apache.commons.vfs2.FileSystem;
import org.apache.commons.vfs2.FileSystemConfigBuilder;
@@ -140,12 +141,12 @@
/**
* The default value for {@link #CONNECTION_TIMEOUT} configuration.
*/
- private static final int DEFAULT_CONNECTION_TIMEOUT = 0;
+ private static final Duration DEFAULT_CONNECTION_TIMEOUT = Duration.ZERO;
/**
* The default value for {@link #SO_TIMEOUT} configuration.
*/
- private static final int DEFAULT_SO_TIMEOUT = 0;
+ private static final Duration DEFAULT_SO_TIMEOUT = Duration.ZERO;
/**
* The default value for {@link #KEEP_ALIVE} configuration.
@@ -207,9 +208,23 @@
*
* @param opts The FileSystem options.
* @return The connection timeout.
+ * @deprecated Use {@link #getConnectionTimeoutDuration(FileSystemOptions)}.
*/
+ @Deprecated
public int getConnectionTimeout(final FileSystemOptions opts) {
- return getInteger(opts, CONNECTION_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT);
+ return getDurationInteger(opts, CONNECTION_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT);
+ }
+
+ /**
+ /**
+ * Gets the connection timeout.
+ *
+ * @param opts The FileSystem options.
+ * @return The connection timeout.
+ * @since 2.8.0
+ */
+ public Duration getConnectionTimeoutDuration(final FileSystemOptions opts) {
+ return getDuration(opts, CONNECTION_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT);
}
/**
@@ -331,9 +346,22 @@
*
* @param opts The FileSystemOptions.
* @return The socket timeout.
+ * @deprecated Use {@link #getSoTimeoutDuration(FileSystemOptions)}.
*/
+ @Deprecated
public int getSoTimeout(final FileSystemOptions opts) {
- return getInteger(opts, SO_TIMEOUT, DEFAULT_SO_TIMEOUT);
+ return getDurationInteger(opts, SO_TIMEOUT, DEFAULT_SO_TIMEOUT);
+ }
+
+ /**
+ * Gets the socket timeout.
+ *
+ * @param opts The FileSystemOptions.
+ * @return The socket timeout.
+ * @since 2.8.0
+ */
+ public Duration getSoTimeoutDuration(final FileSystemOptions opts) {
+ return getDuration(opts, SO_TIMEOUT, DEFAULT_SO_TIMEOUT);
}
/**
@@ -388,13 +416,26 @@
}
/**
- * The connection timeout.
+ * Sets the connection timeout.
*
* @param opts The FileSystem options.
* @param connectionTimeout The connection timeout.
+ * @since 2.8.0
*/
+ public void setConnectionTimeout(final FileSystemOptions opts, final Duration connectionTimeout) {
+ setParam(opts, CONNECTION_TIMEOUT, connectionTimeout);
+ }
+
+ /**
+ * Sets the connection timeout.
+ *
+ * @param opts The FileSystem options.
+ * @param connectionTimeout The connection timeout.
+ * @deprecated Use {@link #setConnectionTimeout(FileSystemOptions, Duration)}.
+ */
+ @Deprecated
public void setConnectionTimeout(final FileSystemOptions opts, final int connectionTimeout) {
- setParam(opts, CONNECTION_TIMEOUT, Integer.valueOf(connectionTimeout));
+ setConnectionTimeout(opts, Duration.ofMillis(connectionTimeout));
}
/**
@@ -548,13 +589,25 @@
}
/**
- * The socket timeout.
+ * Sets the socket timeout.
*
* @param opts The FileSystem options.
* @param soTimeout socket timeout.
*/
+ public void setSoTimeout(final FileSystemOptions opts, final Duration soTimeout) {
+ setParam(opts, SO_TIMEOUT, soTimeout);
+ }
+
+ /**
+ * Sets the socket timeout.
+ *
+ * @param opts The FileSystem options.
+ * @param soTimeout socket timeout.
+ * @deprecated Use {@link #setSoTimeout(FileSystemOptions, Duration)}.
+ */
+ @Deprecated
public void setSoTimeout(final FileSystemOptions opts, final int soTimeout) {
- setParam(opts, SO_TIMEOUT, Integer.valueOf(soTimeout));
+ setSoTimeout(opts, Duration.ofMillis(soTimeout));
}
/**
diff --git a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http4/Http4ProviderTestCase.java b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http4/Http4ProviderTestCase.java
index 89a7c86..4b35f31 100644
--- a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http4/Http4ProviderTestCase.java
+++ b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http4/Http4ProviderTestCase.java
@@ -17,6 +17,7 @@
package org.apache.commons.vfs2.provider.http4;
import java.io.File;
+import java.time.Duration;
import java.util.concurrent.TimeUnit;
import org.apache.commons.vfs2.AbstractProviderTestConfig;
@@ -143,24 +144,41 @@
}
/** Ensure VFS-453 options are present. */
+ @SuppressWarnings("deprecation")
public void testHttpTimeoutConfig() {
final FileSystemOptions opts = new FileSystemOptions();
final Http4FileSystemConfigBuilder builder = Http4FileSystemConfigBuilder.getInstance();
// ensure defaults are 0
assertEquals(0, builder.getConnectionTimeout(opts));
+ assertEquals(Duration.ZERO, builder.getConnectionTimeoutDuration(opts));
assertEquals(0, builder.getSoTimeout(opts));
+ assertEquals(Duration.ZERO, builder.getSoTimeoutDuration(opts));
assertEquals("Jakarta-Commons-VFS", builder.getUserAgent(opts));
+ // Set int timeouts
builder.setConnectionTimeout(opts, 60000);
builder.setSoTimeout(opts, 60000);
builder.setUserAgent(opts, "foo/bar");
// ensure changes are visible
- assertEquals(60000, builder.getConnectionTimeout(opts));
- assertEquals(60000, builder.getSoTimeout(opts));
+ assertEquals(60_000, builder.getConnectionTimeout(opts));
+ assertEquals(60_000, builder.getConnectionTimeoutDuration(opts).toMillis());
+ assertEquals(60_000, builder.getSoTimeout(opts));
+ assertEquals(60_000, builder.getSoTimeoutDuration(opts).toMillis());
assertEquals("foo/bar", builder.getUserAgent(opts));
+ // Set Duration timeouts
+ builder.setConnectionTimeout(opts, Duration.ofMinutes(1));
+ builder.setSoTimeout(opts, Duration.ofMinutes(1));
+ builder.setUserAgent(opts, "foo/bar");
+
+ // ensure changes are visible
+ assertEquals(60_000, builder.getConnectionTimeout(opts));
+ assertEquals(60_000, builder.getConnectionTimeoutDuration(opts).toMillis());
+ assertEquals(60_000, builder.getSoTimeout(opts));
+ assertEquals(60_000, builder.getSoTimeoutDuration(opts).toMillis());
+ assertEquals("foo/bar", builder.getUserAgent(opts));
}
private void testResloveFolderSlash(final String uri, final boolean followRedirect) throws FileSystemException {
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 422e420..eb41a47 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -130,6 +130,18 @@
<action dev="ggregory" due-to="Gary Gregory" type="add">
[SFTP] Add SftpFileSystemConfigBuilder.getSessionTimeout(FileSystemOptions) and deprecate Integer version.
</action>
+ <action dev="ggregory" due-to="Gary Gregory" type="add">
+ [HTTP4] Add Http4FileSystemConfigBuilder.setConnectionTimeout(FileSystemOptions, Duration) and deprecate Integer version.
+ </action>
+ <action dev="ggregory" due-to="Gary Gregory" type="add">
+ [HTTP4] Add Http4FileSystemConfigBuilder.setSoTimeout(FileSystemOptions, Duration) and deprecate Integer version.
+ </action>
+ <action dev="ggregory" due-to="Gary Gregory" type="add">
+ [HTTP4] Add Http4FileSystemConfigBuilder.getConnectionTimeoutDuration(FileSystemOptions) and deprecate Integer version.
+ </action>
+ <action dev="ggregory" due-to="Gary Gregory" type="add">
+ [HTTP4] Add Http4FileSystemConfigBuilder.getSoTimeoutDuration(FileSystemOptions) and deprecate Integer version.
+ </action>
<!-- UPDATES -->
<action dev="ggregory" due-to="PeterAlfredLee" type="update">
Modify some code use for-each loop and stream API #142.