use of org.apache.ftpserver.ftplet.FtpFile in project ddf by codice.
the class FtpRequestHandler method onRenameStart.
@Override
public FtpletResult onRenameStart(FtpSession session, FtpRequest request) throws FtpException, IOException {
FtpFile fromFtpFile = session.getRenameFrom();
String toFilename = request.getArgument();
if (isDotFile(fromFtpFile.getName())) {
Optional<TemporaryFileBackedOutputStream> tfbosOpt = findTempFileInSession(session, fromFtpFile.getAbsolutePath());
if (!tfbosOpt.isPresent()) {
session.write(new DefaultFtpReply(FtpReply.REPLY_501_SYNTAX_ERROR_IN_PARAMETERS_OR_ARGUMENTS, "file not found: " + fromFtpFile.getAbsolutePath()));
return FtpletResult.SKIP;
}
try (TemporaryFileBackedOutputStream tfbos = tfbosOpt.get()) {
Subject shiroSubject = (Subject) session.getAttribute(SUBJECT);
if (shiroSubject == null) {
return FtpletResult.DISCONNECT;
}
CreateStorageRequest createRequest = getCreateStorageRequest(toFilename, tfbos);
storeObject(shiroSubject, toFilename, createRequest);
} finally {
removeTempFileFromSession(session, fromFtpFile.getAbsolutePath());
}
}
session.write(new DefaultFtpReply(FtpReply.REPLY_250_REQUESTED_FILE_ACTION_OKAY, "RNTO successful"));
return FtpletResult.SKIP;
}
use of org.apache.ftpserver.ftplet.FtpFile in project ddf by codice.
the class FtpRequestHandler method store.
private FtpletResult store(FtpSession session, FtpRequest request, boolean isStoreUnique) throws FtpException, IOException {
LOGGER.debug("Beginning FTP ingest of {}", request.getArgument());
Subject shiroSubject = (Subject) session.getAttribute(SUBJECT);
if (shiroSubject == null) {
return FtpletResult.DISCONNECT;
}
FtpFile ftpFile = null;
String fileName = request.getArgument();
try {
ftpFile = session.getFileSystemView().getFile(fileName);
} catch (FtpException e) {
LOGGER.debug("Failed to retrieve file from FTP session");
}
String requestTypeString = isStoreUnique ? STOU_REQUEST : STOR_REQUEST;
if (ftpFile == null) {
LOGGER.debug("Sending FTP status code 501 to client - syntax errors in request parameters");
session.write(new DefaultFtpReply(FtpReply.REPLY_501_SYNTAX_ERROR_IN_PARAMETERS_OR_ARGUMENTS, requestTypeString));
throw new FtpException("File to be transferred from client did not exist");
}
DataConnectionFactory connFactory = session.getDataConnection();
if (connFactory instanceof IODataConnectionFactory) {
InetAddress address = ((IODataConnectionFactory) connFactory).getInetAddress();
if (address == null) {
session.write(new DefaultFtpReply(FtpReply.REPLY_503_BAD_SEQUENCE_OF_COMMANDS, "PORT or PASV must be issued first"));
LOGGER.debug("Sending FTP status code 503 to client - PORT or PASV must be issued before STOR");
throw new FtpException("FTP client address was null");
}
}
if (!ftpFile.isWritable()) {
session.write(new DefaultFtpReply(FtpReply.REPLY_550_REQUESTED_ACTION_NOT_TAKEN, "Insufficient permissions"));
LOGGER.debug("Sending FTP status code 550 to client - insufficient permissions to write file.");
throw new FtpException("Insufficient permissions to write file");
}
session.write(new DefaultFtpReply(FtpReply.REPLY_150_FILE_STATUS_OKAY, requestTypeString + " " + fileName));
LOGGER.debug("Replying to client with code 150 - file status okay");
if (isDotFile(request.getArgument())) {
DataConnection dataConnection;
try {
dataConnection = connFactory.openConnection();
} catch (Exception e) {
throw new IOException("Error getting the output stream from FTP session", e);
}
dataConnection.transferFromClient(session, addTempFileToSession(session, ftpFile.getAbsolutePath(), new TemporaryFileBackedOutputStream()));
if (isStoreUnique) {
session.write(new DefaultFtpReply(FtpReply.REPLY_125_DATA_CONNECTION_ALREADY_OPEN, "Storing data with unique name: " + fileName));
}
session.write(new DefaultFtpReply(FtpReply.REPLY_226_CLOSING_DATA_CONNECTION, "Closing data connection"));
LOGGER.debug("Sending FTP status code 226 to client - closing data connection");
} else {
try (TemporaryFileBackedOutputStream outputStream = new TemporaryFileBackedOutputStream()) {
DataConnection dataConnection = connFactory.openConnection();
dataConnection.transferFromClient(session, outputStream);
CreateStorageRequest createRequest = getCreateStorageRequest(fileName, outputStream);
List<Metacard> storedMetacards = storeObject(shiroSubject, fileName, createRequest);
if (isStoreUnique && !storedMetacards.isEmpty()) {
String ids = storedMetacards.stream().map(Metacard::getId).collect(Collectors.joining(","));
session.write(new DefaultFtpReply(FtpReply.REPLY_125_DATA_CONNECTION_ALREADY_OPEN, "Storing data with unique name: " + ids));
}
session.write(new DefaultFtpReply(FtpReply.REPLY_226_CLOSING_DATA_CONNECTION, "Closing data connection"));
LOGGER.debug("Sending FTP status code 226 to client - closing data connection");
} catch (FtpException fe) {
throw new FtpException("Failure to create metacard for file " + fileName, fe);
} catch (Exception e) {
throw new IOException("Error getting the output stream from FTP session", e);
} finally {
session.getDataConnection().closeDataConnection();
}
}
return FtpletResult.SKIP;
}
use of org.apache.ftpserver.ftplet.FtpFile in project ddf by codice.
the class FtpRequestHandlerTest method testCreateStorageRequestFail.
@SuppressWarnings("unchecked")
@Test(expected = FtpException.class)
public void testCreateStorageRequestFail() throws Exception {
Subject subject = mock(Subject.class);
FtpFile ftpFile = mock(FtpFile.class);
when(session.getAttribute(SUBJECT)).thenReturn(subject);
when(request.getArgument()).thenReturn(FILE_NAME);
when(session.getFileSystemView().getFile(FILE_NAME)).thenReturn(ftpFile);
when(ftpFile.isWritable()).thenReturn(true);
when(ftpFile.getAbsolutePath()).thenReturn(FILE_NAME);
when(subject.execute(any(Callable.class))).thenAnswer(invocationOnMock -> ((Callable) invocationOnMock.getArguments()[0]).call());
when(catalogFramework.create((CreateStorageRequest) anyObject())).thenThrow(new IngestException());
ftplet.onUploadStart(session, request);
}
use of org.apache.ftpserver.ftplet.FtpFile in project ddf by codice.
the class FtpRequestHandlerTest method testOnRenameStart.
@Test
public void testOnRenameStart() throws FtpException, IOException {
FtpFile ftpFile = mock(FtpFile.class);
when(ftpFile.getName()).thenReturn("test.txt");
when(session.getRenameFrom()).thenReturn(ftpFile);
FtpletResult result = ftplet.onRenameStart(session, request);
assertEquals(FtpletResult.SKIP, result);
assertThat(getReplyCodes(), hasItem(250));
}
use of org.apache.ftpserver.ftplet.FtpFile in project ddf by codice.
the class FtpRequestHandlerTest method setupIngest.
@SuppressWarnings("unchecked")
private void setupIngest() throws FtpException, SourceUnavailableException, IngestException {
Subject subject = mock(Subject.class);
FtpFile ftpFile = mock(FtpFile.class);
CreateResponse createResponse = mock(CreateResponse.class);
Metacard metacard = mock(Metacard.class);
when(metacard.getId()).thenReturn(METACARD_ID);
when(createResponse.getCreatedMetacards()).thenReturn(Collections.singletonList(metacard));
when(session.getAttribute(SUBJECT)).thenReturn(subject);
when(request.getArgument()).thenReturn(FILE_NAME);
when(session.getFileSystemView().getFile(FILE_NAME)).thenReturn(ftpFile);
when(ftpFile.isWritable()).thenReturn(true);
when(ftpFile.getAbsolutePath()).thenReturn(FILE_NAME);
when(subject.execute(any(Callable.class))).thenAnswer(invocationOnMock -> ((Callable) invocationOnMock.getArguments()[0]).call());
when(catalogFramework.create(any(CreateStorageRequest.class))).thenReturn(createResponse);
}
Aggregations