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
commit 4e35c2bc97fffd189d434f1903955460ee56851a Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Mon Feb 8 12:44:31 2021 -0500 Sort members. --- .../vfs2/provider/ftp/FtpClientFactory.java | 148 ++++++++++---------- .../vfs2/provider/ftp/FtpFileNameParser.java | 8 +- .../commons/vfs2/provider/ftp/FtpFileProvider.java | 8 +- .../commons/vfs2/provider/ftp/FtpFileSystem.java | 56 ++++---- .../provider/ftp/FtpFileSystemConfigBuilder.java | 150 ++++++++++----------- .../vfs2/provider/ftp/FtpRandomAccessContent.java | 64 ++++----- 6 files changed, 217 insertions(+), 217 deletions(-) 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 9ac10dc..4576e60 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 @@ -39,45 +39,6 @@ import org.apache.commons.vfs2.util.UserAuthenticatorUtils; */ public final class FtpClientFactory { - private FtpClientFactory() { - } - - /** - * Creates a new connection to the server. - * - * @param hostname The host name of the server. - * @param port The port to connect to. - * @param username The name of the user for authentication. - * @param password The user's password. - * @param workingDirectory The base directory. - * @param fileSystemOptions The FileSystemOptions. - * @return An FTPClient. - * @throws FileSystemException if an error occurs while connecting. - */ - public static FTPClient createConnection(final String hostname, final int port, final char[] username, - final char[] password, final String workingDirectory, final FileSystemOptions fileSystemOptions) - throws FileSystemException { - final FtpConnectionFactory factory = new FtpConnectionFactory(FtpFileSystemConfigBuilder.getInstance()); - return factory.createConnection(hostname, port, username, password, workingDirectory, fileSystemOptions); - } - - /** Connection Factory, used to configure the FTPClient. */ - public static final class FtpConnectionFactory extends ConnectionFactory<FTPClient, FtpFileSystemConfigBuilder> { - private FtpConnectionFactory(final FtpFileSystemConfigBuilder builder) { - super(builder); - } - - @Override - protected FTPClient createClient(final FileSystemOptions fileSystemOptions) { - return new FTPClient(); - } - - @Override - protected void setupOpenConnection(final FTPClient client, final FileSystemOptions fileSystemOptions) { - // nothing to do for FTP - } - } - /** * Abstract Factory, used to configure different FTPClients. * @@ -97,6 +58,45 @@ public final class FtpClientFactory { this.builder = builder; } + private void configureClient(final FileSystemOptions fileSystemOptions, final C client) { + final String key = builder.getEntryParser(fileSystemOptions); + if (key != null) { + final FTPClientConfig config = new FTPClientConfig(key); + + final String serverLanguageCode = builder.getServerLanguageCode(fileSystemOptions); + if (serverLanguageCode != null) { + config.setServerLanguageCode(serverLanguageCode); + } + final String defaultDateFormat = builder.getDefaultDateFormat(fileSystemOptions); + if (defaultDateFormat != null) { + config.setDefaultDateFormatStr(defaultDateFormat); + } + final String recentDateFormat = builder.getRecentDateFormat(fileSystemOptions); + if (recentDateFormat != null) { + config.setRecentDateFormatStr(recentDateFormat); + } + final String serverTimeZoneId = builder.getServerTimeZoneId(fileSystemOptions); + if (serverTimeZoneId != null) { + config.setServerTimeZoneId(serverTimeZoneId); + } + final String[] shortMonthNames = builder.getShortMonthNames(fileSystemOptions); + if (shortMonthNames != null) { + final StringBuilder shortMonthNamesStr = new StringBuilder(BUFSZ); + for (final String shortMonthName : shortMonthNames) { + if (shortMonthNamesStr.length() > 0) { + shortMonthNamesStr.append("|"); + } + shortMonthNamesStr.append(shortMonthName); + } + config.setShortMonthNames(shortMonthNamesStr.toString()); + } + + client.configure(config); + } + } + + protected abstract C createClient(FileSystemOptions fileSystemOptions) throws FileSystemException; + public C createConnection(final String hostname, final int port, char[] username, char[] password, final String workingDirectory, final FileSystemOptions fileSystemOptions) throws FileSystemException { // Determine the username and password to use @@ -234,45 +234,45 @@ public final class FtpClientFactory { } } - protected abstract C createClient(FileSystemOptions fileSystemOptions) throws FileSystemException; - protected abstract void setupOpenConnection(C client, FileSystemOptions fileSystemOptions) throws IOException; + } - private void configureClient(final FileSystemOptions fileSystemOptions, final C client) { - final String key = builder.getEntryParser(fileSystemOptions); - if (key != null) { - final FTPClientConfig config = new FTPClientConfig(key); + /** Connection Factory, used to configure the FTPClient. */ + public static final class FtpConnectionFactory extends ConnectionFactory<FTPClient, FtpFileSystemConfigBuilder> { + private FtpConnectionFactory(final FtpFileSystemConfigBuilder builder) { + super(builder); + } - final String serverLanguageCode = builder.getServerLanguageCode(fileSystemOptions); - if (serverLanguageCode != null) { - config.setServerLanguageCode(serverLanguageCode); - } - final String defaultDateFormat = builder.getDefaultDateFormat(fileSystemOptions); - if (defaultDateFormat != null) { - config.setDefaultDateFormatStr(defaultDateFormat); - } - final String recentDateFormat = builder.getRecentDateFormat(fileSystemOptions); - if (recentDateFormat != null) { - config.setRecentDateFormatStr(recentDateFormat); - } - final String serverTimeZoneId = builder.getServerTimeZoneId(fileSystemOptions); - if (serverTimeZoneId != null) { - config.setServerTimeZoneId(serverTimeZoneId); - } - final String[] shortMonthNames = builder.getShortMonthNames(fileSystemOptions); - if (shortMonthNames != null) { - final StringBuilder shortMonthNamesStr = new StringBuilder(BUFSZ); - for (final String shortMonthName : shortMonthNames) { - if (shortMonthNamesStr.length() > 0) { - shortMonthNamesStr.append("|"); - } - shortMonthNamesStr.append(shortMonthName); - } - config.setShortMonthNames(shortMonthNamesStr.toString()); - } + @Override + protected FTPClient createClient(final FileSystemOptions fileSystemOptions) { + return new FTPClient(); + } - client.configure(config); - } + @Override + protected void setupOpenConnection(final FTPClient client, final FileSystemOptions fileSystemOptions) { + // nothing to do for FTP } } + + /** + * Creates a new connection to the server. + * + * @param hostname The host name of the server. + * @param port The port to connect to. + * @param username The name of the user for authentication. + * @param password The user's password. + * @param workingDirectory The base directory. + * @param fileSystemOptions The FileSystemOptions. + * @return An FTPClient. + * @throws FileSystemException if an error occurs while connecting. + */ + public static FTPClient createConnection(final String hostname, final int port, final char[] username, + final char[] password, final String workingDirectory, final FileSystemOptions fileSystemOptions) + throws FileSystemException { + final FtpConnectionFactory factory = new FtpConnectionFactory(FtpFileSystemConfigBuilder.getInstance()); + return factory.createConnection(hostname, port, username, password, workingDirectory, fileSystemOptions); + } + + private FtpClientFactory() { + } } diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FtpFileNameParser.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FtpFileNameParser.java index c419050..4f03fdb 100644 --- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FtpFileNameParser.java +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FtpFileNameParser.java @@ -28,11 +28,11 @@ public class FtpFileNameParser extends HostFileNameParser { private static final int PORT = 21; - public FtpFileNameParser() { - super(PORT); - } - public static FileNameParser getInstance() { return INSTANCE; } + + public FtpFileNameParser() { + super(PORT); + } } diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FtpFileProvider.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FtpFileProvider.java index ebf8833..aba9deb 100644 --- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FtpFileProvider.java +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FtpFileProvider.java @@ -76,12 +76,12 @@ public class FtpFileProvider extends AbstractOriginatingFileProvider { } @Override - public FileSystemConfigBuilder getConfigBuilder() { - return FtpFileSystemConfigBuilder.getInstance(); + public Collection<Capability> getCapabilities() { + return capabilities; } @Override - public Collection<Capability> getCapabilities() { - return capabilities; + public FileSystemConfigBuilder getConfigBuilder() { + return FtpFileSystemConfigBuilder.getInstance(); } } diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FtpFileSystem.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FtpFileSystem.java index 0759d73..de76ecb 100644 --- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FtpFileSystem.java +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FtpFileSystem.java @@ -61,15 +61,6 @@ public class FtpFileSystem extends AbstractFileSystem { idleClient.set(ftpClient); } - @Override - protected void doCloseCommunicationLink() { - final FtpClient idle = idleClient.getAndSet(null); - // Clean up the connection - if (idle != null) { - closeConnection(idle); - } - } - /** * Adds the capabilities of this file system. */ @@ -96,6 +87,34 @@ public class FtpFileSystem extends AbstractFileSystem { } /** + * Creates a file object. + */ + @Override + protected FileObject createFile(final AbstractFileName name) throws FileSystemException { + return new FtpFileObject(name, this, getRootName()); + } + + /** + * Gets the wrapper to access this file system. + * + * @return new instance. + * @throws FileSystemException if any error occurs. + * @since 2.1 + */ + protected FTPClientWrapper createWrapper() throws FileSystemException { + return new FTPClientWrapper((GenericFileName) getRoot().getName(), getFileSystemOptions()); + } + + @Override + protected void doCloseCommunicationLink() { + final FtpClient idle = idleClient.getAndSet(null); + // Clean up the connection + if (idle != null) { + closeConnection(idle); + } + } + + /** * Creates an FTP client to use. * * @return An FTPCleint. @@ -112,17 +131,6 @@ public class FtpFileSystem extends AbstractFileSystem { } /** - * Gets the wrapper to access this file system. - * - * @return new instance. - * @throws FileSystemException if any error occurs. - * @since 2.1 - */ - protected FTPClientWrapper createWrapper() throws FileSystemException { - return new FTPClientWrapper((GenericFileName) getRoot().getName(), getFileSystemOptions()); - } - - /** * Returns an FTP client after use. * * @param client The FTPClient. @@ -134,12 +142,4 @@ public class FtpFileSystem extends AbstractFileSystem { closeConnection(client); } } - - /** - * Creates a file object. - */ - @Override - protected FileObject createFile(final AbstractFileName name) throws FileSystemException { - return new FtpFileObject(name, this, getRootName()); - } } 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 0ecf724..21aecdf 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 @@ -57,20 +57,6 @@ public class FtpFileSystemConfigBuilder extends FileSystemConfigBuilder { 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"; - private FtpFileSystemConfigBuilder() { - super("ftp."); - } - - /** - * Create new config builder with specified prefix string. - * - * @param prefix prefix string to use for parameters of this config builder. - * @since 2.1 - */ - protected FtpFileSystemConfigBuilder(final String prefix) { - super(prefix); - } - /** * Gets the singleton instance. * @@ -80,16 +66,25 @@ public class FtpFileSystemConfigBuilder extends FileSystemConfigBuilder { return BUILDER; } - @Override - protected Class<? extends FileSystem> getConfigClass() { - return FtpFileSystem.class; - } - public static List<Integer> getSaneTransferAbortedOkReplyCodes() { // See VFS-674, its accompanying PR and https://github.com/apache/commons-vfs/pull/51 as to why 426 and 550 are here return new ArrayList<>(Arrays.asList(FTPReply.TRANSFER_ABORTED, FTPReply.FILE_UNAVAILABLE)); } + private FtpFileSystemConfigBuilder() { + super("ftp."); + } + + /** + * Create new config builder with specified prefix string. + * + * @param prefix prefix string to use for parameters of this config builder. + * @since 2.1 + */ + protected FtpFileSystemConfigBuilder(final String prefix) { + super(prefix); + } + /** * Gets whether to try to autodetect the server encoding (only UTF8 is supported). * @@ -101,6 +96,11 @@ public class FtpFileSystemConfigBuilder extends FileSystemConfigBuilder { return getBoolean(opts, AUTODETECT_UTF8); } + @Override + protected Class<? extends FileSystem> getConfigClass() { + return FtpFileSystem.class; + } + /** * Gets the timeout in milliseconds to use for the socket connection. * @@ -122,6 +122,24 @@ public class FtpFileSystemConfigBuilder extends FileSystemConfigBuilder { } /** + * @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); + } + + /** + * @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 FileSystemOptions. * @return The timeout for opening the data channel in milliseconds. * @see #setDataTimeout @@ -252,21 +270,13 @@ 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 + * @param opts The FileSystem options. + * @return The list of reply codes (apart from 200) that are considered as OK when prematurely + * closing a stream. + * @since 2.4 */ - public Duration getControlKeepAliveReplyTimeout(FileSystemOptions opts) { - return getDuration(opts, CONTROL_KEEP_ALIVE_REPLY_TIMEOUT); + public List<Integer> getTransferAbortedOkReplyCodes(final FileSystemOptions opts) { + return getParam(opts, TRANSFER_ABORTED_OK_REPLY_CODES); } /** @@ -283,16 +293,6 @@ public class FtpFileSystemConfigBuilder extends FileSystemConfigBuilder { } /** - * @param opts The FileSystem options. - * @return The list of reply codes (apart from 200) that are considered as OK when prematurely - * closing a stream. - * @since 2.4 - */ - public List<Integer> getTransferAbortedOkReplyCodes(final FileSystemOptions opts) { - return getParam(opts, TRANSFER_ABORTED_OK_REPLY_CODES); - } - - /** * Sets whether to try to autodetect the server encoding (only UTF8 is supported). * * @param opts The FileSystemOptions. @@ -329,6 +329,31 @@ public class FtpFileSystemConfigBuilder extends FileSystemConfigBuilder { } /** + * 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); + } + + /** + * 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); + } + + /** * Set the data timeout for the ftp client. * <p> * If you set the {@code dataTimeout} to {@code null}, no dataTimeout will be set on the ftp client. @@ -483,28 +508,18 @@ public class FtpFileSystemConfigBuilder extends FileSystemConfigBuilder { } /** - * Sets the control keep alive timeout for the FTP client. + * Sets the list of reply codes that are considered as OK when prematurely closing a stream. * <p> - * Set the {@code controlKeepAliveTimeout} to ensure the socket be alive after download huge file. + * If you set the {@code replyCodes} to an empty list, all reply codes besides 200 will be + * considered as an error. * </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 + * @param replyCodes The reply codes. + * @since 2.4 */ - public void setControlKeepAliveReplyTimeout(FileSystemOptions opts, final Duration controlKeepAliveReplyTimeout) { - setParam(opts, CONTROL_KEEP_ALIVE_REPLY_TIMEOUT, controlKeepAliveReplyTimeout); + public void setTransferAbortedOkReplyCodes(final FileSystemOptions opts, final List<Integer> replyCodes) { + setParam(opts, TRANSFER_ABORTED_OK_REPLY_CODES, replyCodes); } /** @@ -516,19 +531,4 @@ public class FtpFileSystemConfigBuilder extends FileSystemConfigBuilder { public void setUserDirIsRoot(final FileSystemOptions opts, final boolean userDirIsRoot) { setParam(opts, USER_DIR_IS_ROOT, toBooleanObject(userDirIsRoot)); } - - /** - * Sets the list of reply codes that are considered as OK when prematurely closing a stream. - * <p> - * If you set the {@code replyCodes} to an empty list, all reply codes besides 200 will be - * considered as an error. - * </p> - * - * @param opts The FileSystem options. - * @param replyCodes The reply codes. - * @since 2.4 - */ - public void setTransferAbortedOkReplyCodes(final FileSystemOptions opts, final List<Integer> replyCodes) { - setParam(opts, TRANSFER_ABORTED_OK_REPLY_CODES, replyCodes); - } } diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FtpRandomAccessContent.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FtpRandomAccessContent.java index a7b257d..aa6698a 100644 --- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FtpRandomAccessContent.java +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FtpRandomAccessContent.java @@ -43,25 +43,16 @@ class FtpRandomAccessContent extends AbstractRandomAccessStreamContent { } @Override - public long getFilePointer() throws IOException { - return filePointer; - } - - @Override - public void seek(final long pos) throws IOException { - if (pos == filePointer) { - // no change - return; - } - - if (pos < 0) { - throw new FileSystemException("vfs.provider/random-access-invalid-position.error", Long.valueOf(pos)); - } + public void close() throws IOException { if (dis != null) { - close(); - } + mis.abort(); - filePointer = pos; + // this is to avoid recursive close + final DataInputStream oldDis = dis; + dis = null; + oldDis.close(); + mis = null; + } } @Override @@ -74,6 +65,11 @@ class FtpRandomAccessContent extends AbstractRandomAccessStreamContent { mis = fileObject.getInputStream(filePointer); dis = new DataInputStream(new FilterInputStream(mis) { @Override + public void close() throws IOException { + FtpRandomAccessContent.this.close(); + } + + @Override public int read() throws IOException { final int ret = super.read(); if (ret > -1) { @@ -99,31 +95,35 @@ class FtpRandomAccessContent extends AbstractRandomAccessStreamContent { } return ret; } - - @Override - public void close() throws IOException { - FtpRandomAccessContent.this.close(); - } }); return dis; } @Override - public void close() throws IOException { - if (dis != null) { - mis.abort(); - - // this is to avoid recursive close - final DataInputStream oldDis = dis; - dis = null; - oldDis.close(); - mis = null; - } + public long getFilePointer() throws IOException { + return filePointer; } @Override public long length() throws IOException { return fileObject.getContent().getSize(); } + + @Override + public void seek(final long pos) throws IOException { + if (pos == filePointer) { + // no change + return; + } + + if (pos < 0) { + throw new FileSystemException("vfs.provider/random-access-invalid-position.error", Long.valueOf(pos)); + } + if (dis != null) { + close(); + } + + filePointer = pos; + } }