// reset state variables
session.resetState();
// call Ftplet.onUploadUniqueStart() method
Ftplet ftpletContainer = context.getFtpletContainer();
FtpletEnum ftpletRet;
try {
ftpletRet = ftpletContainer.onUploadUniqueStart(session.getFtpletSession(), request);
} catch(Exception e) {
LOG.debug("Ftplet container threw exception", e);
ftpletRet = FtpletEnum.RET_DISCONNECT;
}
if(ftpletRet == FtpletEnum.RET_SKIP) {
return;
}
else if(ftpletRet == FtpletEnum.RET_DISCONNECT) {
session.closeOnFlush().awaitUninterruptibly(10000);
return;
}
String pathName = request.getArgument();
// get filenames
FileObject file = null;
try {
String filePrefix;
if(pathName == null) {
filePrefix = "ftp.dat";
} else {
FileObject dir = session.getFileSystemView().getFileObject(pathName);
if(dir.isDirectory()) {
filePrefix = pathName + "/ftp.dat";
} else {
filePrefix = pathName;
}
}
file = session.getFileSystemView().getFileObject(filePrefix);
if(file != null) {
file = getUniqueFile(session, file);
}
}
catch(Exception ex) {
LOG.debug("Exception getting file object", ex);
}
if(file == null) {
session.write(FtpReplyUtil.translate(session, request, context, FtpReply.REPLY_550_REQUESTED_ACTION_NOT_TAKEN, "STOU", null));
return;
}
String fileName = file.getFullName();
// check permission
if(!file.hasWritePermission()) {
session.write(FtpReplyUtil.translate(session, request, context, FtpReply.REPLY_550_REQUESTED_ACTION_NOT_TAKEN, "STOU.permission", fileName));
return;
}
// get data connection
session.write(new DefaultFtpReply(FtpReply.REPLY_150_FILE_STATUS_OKAY, "FILE: " + fileName));
// get data from client
boolean failure = false;
OutputStream os = null;
DataConnection dataConnection;
try {
dataConnection = session.getDataConnection().openConnection();
} catch (Exception e) {
LOG.debug("Exception getting the input data stream", e);
session.write(FtpReplyUtil.translate(session, request, context, FtpReply.REPLY_425_CANT_OPEN_DATA_CONNECTION, "STOU", fileName));
return;
}
try {
// open streams
os = file.createOutputStream(0L);
// transfer data
long transSz = dataConnection.transferFromClient(os);
// log message
String userName = session.getUser().getName();
LOG.info("File upload : " + userName + " - " + fileName);
// notify the statistics component
ServerFtpStatistics ftpStat = (ServerFtpStatistics)context.getFtpStatistics();
if(ftpStat != null) {
ftpStat.setUpload(session, file, transSz);
}
}
catch(SocketException ex) {
LOG.debug("Socket exception during data transfer", ex);
failure = true;
session.write(FtpReplyUtil.translate(session, request, context, FtpReply.REPLY_426_CONNECTION_CLOSED_TRANSFER_ABORTED, "STOU", fileName));
}
catch(IOException ex) {
LOG.debug("IOException during data transfer", ex);
failure = true;
session.write(FtpReplyUtil.translate(session, request, context, FtpReply.REPLY_551_REQUESTED_ACTION_ABORTED_PAGE_TYPE_UNKNOWN, "STOU", fileName));
}
finally {
IoUtils.close(os);
}
// if data transfer ok - send transfer complete message
if(!failure) {
session.write(FtpReplyUtil.translate(session, request, context, FtpReply.REPLY_226_CLOSING_DATA_CONNECTION, "STOU", fileName));
// call Ftplet.onUploadUniqueEnd() method
try {
ftpletRet = ftpletContainer.onUploadUniqueEnd(session.getFtpletSession(), request);
} catch(Exception e) {
LOG.debug("Ftplet container threw exception", e);
ftpletRet = FtpletEnum.RET_DISCONNECT;
}
if(ftpletRet == FtpletEnum.RET_DISCONNECT) {