Skip to content

Commit 9d12984

Browse files
committed
Add org.apache.commons.vfs2.provider.ftp.FTPClientWrapper.sendOptions(String,
String)
1 parent 4b0786b commit 9d12984

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FTPClientWrapper.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.apache.commons.logging.Log;
2525
import org.apache.commons.logging.LogFactory;
2626
import org.apache.commons.net.ftp.FTPClient;
27+
import org.apache.commons.net.ftp.FTPConnectionClosedException;
2728
import org.apache.commons.net.ftp.FTPFile;
2829
import org.apache.commons.net.ftp.FTPReply;
2930
import org.apache.commons.vfs2.FileSystemException;
@@ -94,17 +95,14 @@ public boolean completePendingCommand() throws IOException {
9495
if (ftpClient != null) {
9596
return getFtpClient().completePendingCommand();
9697
}
97-
9898
return true;
9999
}
100100

101101
private FTPClient createClient() throws FileSystemException {
102102
final GenericFileName rootName = getRoot();
103-
104103
UserAuthenticationData authData = null;
105104
try {
106105
authData = UserAuthenticatorUtils.authenticate(fileSystemOptions, FtpFileProvider.AUTHENTICATOR_TYPES);
107-
108106
return createClient(rootName, authData);
109107
} finally {
110108
UserAuthenticatorUtils.cleanup(authData);
@@ -168,7 +166,6 @@ private FTPClient getFtpClient() throws FileSystemException {
168166
if (ftpClient == null) {
169167
ftpClient = createClient();
170168
}
171-
172169
return ftpClient;
173170
}
174171

@@ -328,6 +325,32 @@ public InputStream retrieveFileStream(final String relPath, final long restartOf
328325
}
329326
}
330327

328+
/**
329+
* A convenience method to send the FTP OPTS command to the server, receive the reply, and return the reply code.
330+
* <p>
331+
* FTP request Syntax:
332+
* </p>
333+
* <pre>{@code
334+
* opts = opts-cmd SP command-name
335+
* [ SP command-options ] CRLF
336+
* opts-cmd = "opts"
337+
* command-name = <any FTP command which allows option setting>
338+
* command-options = <format specified by individual FTP command>
339+
* }</pre>
340+
* @param commandName The OPTS command name.
341+
* @param commandOptions The OPTS command options.
342+
* @return The reply code received from the server.
343+
* @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
344+
* causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
345+
* independently as itself.
346+
* @throws IOException If an I/O error occurs while either sending the command or receiving the server reply.
347+
*/
348+
public int sendOptions(final String commandName, String commandOptions) throws IOException {
349+
// Commons Net 3.12.0
350+
// return getFtpClient().opts(commandName, commandOptions);
351+
return getFtpClient().sendCommand("OPTS", commandName + ' ' + commandOptions);
352+
}
353+
331354
@Override
332355
public void setBufferSize(final int bufferSize) throws FileSystemException {
333356
getFtpClient().setBufferSize(bufferSize);

commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/ftp/FtpProviderTestCase.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@
2323
import java.nio.charset.StandardCharsets;
2424
import java.time.Duration;
2525

26-
import junit.framework.Test;
27-
2826
import org.apache.commons.vfs2.AbstractProviderTestCase;
2927
import org.apache.commons.vfs2.AbstractProviderTestConfig;
3028
import org.apache.commons.vfs2.FileObject;
3129
import org.apache.commons.vfs2.FileSystemManager;
3230
import org.apache.commons.vfs2.FileSystemOptions;
3331
import org.apache.commons.vfs2.ProviderTestSuite;
32+
import org.apache.commons.vfs2.impl.DecoratedFileObject;
3433
import org.apache.commons.vfs2.impl.DefaultFileSystemManager;
3534
import org.apache.ftpserver.FtpServer;
3635
import org.apache.ftpserver.FtpServerFactory;
@@ -44,6 +43,8 @@
4443
import org.apache.ftpserver.usermanager.impl.BaseUser;
4544
import org.junit.jupiter.api.Assertions;
4645

46+
import junit.framework.Test;
47+
4748
/**
4849
* Tests for FTP file systems.
4950
*/
@@ -202,7 +203,16 @@ public FileObject getBaseTestFolder(final FileSystemManager manager) throws Exce
202203
final FileSystemOptions options = new FileSystemOptions();
203204
final FtpFileSystemConfigBuilder builder = FtpFileSystemConfigBuilder.getInstance();
204205
init(builder, options);
205-
return manager.resolveFile(uri, options);
206+
// OPTS UTF-8
207+
final FileObject remoteFolder = manager.resolveFile(uri, options);
208+
final FtpFileObject ftpFileObject = remoteFolder instanceof DecoratedFileObject
209+
? (FtpFileObject) ((DecoratedFileObject) remoteFolder).getDecoratedFileObject()
210+
: (FtpFileObject) remoteFolder;
211+
final FtpFileSystem ftpFileSystem = (FtpFileSystem) ftpFileObject.getFileSystem();
212+
final FTPClientWrapper client = (FTPClientWrapper) ftpFileSystem.getClient();
213+
// TODO Needs Apache Commons Net 3.12.0
214+
// client.sendOptions("UTF-8", "NLST");
215+
return remoteFolder;
206216
}
207217

208218
/**

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ The <action> type attribute can be add,update,fix,remove.
4848
<release version="2.10.1" date="YYYY-MM-DD" description="This is a feature and maintenance release. Java 8 or later is required.">
4949
<!-- FIX -->
5050
<!-- ADD -->
51+
<action dev="ggregory" type="update" due-to="Gary Gregory">Add org.apache.commons.vfs2.provider.ftp.FTPClientWrapper.sendOptions(String, String).</action>
5152
<!-- UPDATE -->
5253
<action dev="ggregory" type="update" due-to="Gary Gregory">Bump org.apache.ftpserver:ftpserver-core from 1.2.0 to 1.2.1.</action>
5354
<action dev="ggregory" type="update" due-to="Gary Gregory">Bump org.apache.mina:mina-core from 2.1.10 to 2.2.4.</action>

0 commit comments

Comments
 (0)