2

I set up an FTPS server using Apache MINA. By overriding the default ftplet I can detect when a client starts uploading a new file to the server. I want to redirect the transfer to an S3 database, instead of having the file written in disk. The ftplet documentation in the MINA project states (https://mina.apache.org/ftpserver-project/ftplet.html) that

We can get the data input stream from request

But I cannot find how to get that stream from the two arguments.

Furthermore, in the FAQ there is a code example where a download is obtained from a database, by overriding the onDownloadStart method (https://mina.apache.org/ftpserver-project/faq.html#how-can-i-send-binary-data-stored-in-a-database-when-the-ftp-server-gets-the-retr-command):

public FtpletEnum onDownloadStart(FtpSession session, FtpRequest request,
    FtpReplyOutput response) throws FtpException, IOException {
....

However, I am using the latest MINA version (mina-core 2.0.16, ftplet-api 1.1.1, ftpserver-core 1.1.1) and that method does not include the third argument. Has this changed in the latest versions??

Alberto Anguita
  • 103
  • 1
  • 11

1 Answers1

1

The onDownloadStart example you're referring to seems to be out of date. For starters, the FtpletEnum class used was part of an early version of ftplet-api. Newer versions don't have it anymore. At least I was not able to find it.

Despite that, it's still possible to get the uploaded file from the client. You can ask for a DataConnection from the session, when overriding DefaultFtplet's onUploadStart method.

OutputStream outputStream = new ByteArrayOutputStream();
DataConnectionFactory connectionFactory = session.getDataConnection();
try {
    DataConnection dataConnection = connectionFactory.openConnection();
    dataConnection.transferFromClient(session, outputStream);
    // now outputstream contains the uploaded file and you could
    // store it in S3 if you wish
} catch (Exception e) {
    e.printStackTrace();
} finally {
    connectionFactory.closeDataConnection();
}

Keep in mind that you might also have to notify the client with response codes if your onUploadStart method returns SKIP. From Ftplet docs

This method will be called before the file upload. The file name can be get from the request argument. We can get the data input stream from request. This will be called before the permission check. This is called during STOR command. If the method returns SKIP, it has to send responses before and after processing. For example, before opening the data input stream, the method has to notify the client with a response code 150. Similarly, after the data transfer, the method has to notify the client with a response code 226. In case of any error, the method should send different response codes like 450, 425, 426, 551.

Indrek Ots
  • 3,701
  • 1
  • 19
  • 24