use of com.axway.ats.common.filetransfer.FileTransferException in project ats-framework by Axway.
the class Test_GenericTransferClient method testFileDownloadException.
/**
* Test case
* @throws Exception
*/
@Test(expected = FileTransferException.class)
public void testFileDownloadException() throws Exception {
// setup expectations
ftpMock.downloadFile(SAMPLE_LOCAL_DIRECTORY + SAMPLE_REMOTE_FILE, SAMPLE_REMOTE_DIRECTORY, SAMPLE_REMOTE_FILE);
expectLastCall().andThrow(new FileTransferException(SAMPLE_EXCEPTION_MESSAGE));
replay(ftpMock);
// execute operations
mockedTestObject.downloadFile(SAMPLE_LOCAL_DIRECTORY, SAMPLE_REMOTE_DIRECTORY, SAMPLE_REMOTE_FILE);
// verify results
verify(ftpMock);
}
use of com.axway.ats.common.filetransfer.FileTransferException in project ats-framework by Axway.
the class AbstractFileTransferClient method startUploadAndPause.
public void startUploadAndPause(final String localFile, final String remoteDir, final String remoteFile) throws FileTransferException {
checkPausedTransferRunning(false);
// a paused transfer is started.
this.isTransferStartedAndPaused = true;
// get the executor thread.
final Thread currentThread = Thread.currentThread();
final Thread uploadThread = new Thread("Upload Thread") {
private final Logger log = LogManager.getLogger(this.getName());
@Override
public void run() {
synchronized (AbstractFileTransferClient.this) {
// Notify the executor thread that the upload is starting.
// The executor thread will stop waiting for the upload to start
// but will not do anything until it receives the object's monitor.
canResume = true;
AbstractFileTransferClient.this.notifyAll();
// the number of the progress event on which to wait
int progressEventNumber = 0;
if (new File(localFile).length() == 0) {
// if the file is empty no progress events will be fired
progressEventNumber = -1;
}
// Add a listener to notify the executor that the transfer is paused.
TransferListener listener = addListener(progressEventNumber);
try {
// Start the upload.
AbstractFileTransferClient.this.performUploadFile(localFile, remoteDir, remoteFile);
// Notify the executor thread that the upload has finished successfully.
AbstractFileTransferClient.this.notifyAll();
} catch (FileTransferException e) {
log.error("Upload failed.", e);
// Interrupt the executor thread so that an exception can be thrown
// while waiting for the upload to finish.
currentThread.interrupt();
} finally {
// Ensure the listener is removed to prevent deadlocks in further transfers.
removeListener(listener);
}
}
}
};
uploadThread.start();
}
use of com.axway.ats.common.filetransfer.FileTransferException in project ats-framework by Axway.
the class FtpsClient method performDownloadFile.
@Override
protected void performDownloadFile(String localFile, String remoteDir, String remoteFile) throws FileTransferException {
FileOutputStream fos = null;
try {
String remoteFileAbsPath = null;
remoteDir = remoteDir.replace("\\", "/");
remoteFile = remoteFile.replace("\\", "/");
if (remoteDir.endsWith("/") && remoteFile.endsWith("/")) {
remoteFileAbsPath = remoteDir.substring(0, remoteDir.length() - 2) + remoteFile;
} else if (!remoteDir.endsWith("/") && !remoteFile.endsWith("/")) {
remoteFileAbsPath = remoteDir + "/" + remoteFile;
} else {
remoteFileAbsPath = remoteDir + remoteFile;
}
// download the file
fos = new FileOutputStream(new File(localFile));
if (!this.ftpsConnection.retrieveFile(remoteFileAbsPath, fos)) {
throw new FileTransferException("Unable to retrieve " + remoteDir + "/" + remoteFile + " from " + this.ftpsConnection.getPassiveHost() + " as a" + localFile);
}
} catch (Exception e) {
log.error("Unable to download file " + localFile, e);
throw new FileTransferException(e);
} finally {
// close the file output stream
IoUtils.closeStream(fos, "Unable to close the file stream after successful download!");
}
if (remoteDir != null && !remoteDir.endsWith("/")) {
remoteDir += "/";
}
log.info("Successfully downloaded '" + localFile + "' from '" + remoteDir + remoteFile + "', host " + ftpsConnection.getPassiveHost());
}
use of com.axway.ats.common.filetransfer.FileTransferException in project ats-framework by Axway.
the class FtpsClient method performConnect.
private void performConnect(String hostname, String userName, String password) throws FileTransferException {
try {
// make new FTP object for every new connection
disconnect();
applyCustomProperties();
SSLContext sslContext = null;
try {
sslContext = createSSLContext();
} catch (Exception e) {
throw new Exception("Error while creating SSL context", e);
}
// this.ftpsConnection = new FTPSClient( this.protocol, this.implicit);
this.ftpsConnection = new org.apache.commons.net.ftp.FTPSClient(this.implicit, sslContext);
if (this.listener != null) {
this.listener.setResponses(new ArrayList<String>());
this.ftpsConnection.addProtocolCommandListener(((FtpResponseListener) listener));
}
/* if debug mode is true, we log messages from all levels */
if (isDebugMode()) {
this.ftpsConnection.addProtocolCommandListener(new FtpListener());
}
this.ftpsConnection.setConnectTimeout(this.timeout);
// connect to the host
this.ftpsConnection.connect(hostname, this.port);
// login to the host
if (!this.ftpsConnection.login(userName, password)) {
throw new Exception("Invalid username and/or password. ");
}
// set transfer mode
if (this.transferMode == TransferMode.ASCII) {
if (!this.ftpsConnection.setFileType(org.apache.commons.net.ftp.FTPSClient.ASCII_FILE_TYPE)) {
throw new Exception("Unable to set transfer mode to ASCII");
}
} else {
if (!this.ftpsConnection.setFileType(org.apache.commons.net.ftp.FTPSClient.BINARY_FILE_TYPE)) {
throw new Exception("Unable to set transfer mode to BINARY");
}
}
// initial fix - always use passive mode
// Currently not working: int replyCode = this.ftpsConnection.pasv();
this.ftpsConnection.enterLocalPassiveMode();
} catch (Exception e) {
String errMessage = "Unable to connect to " + hostname + " on port " + this.port + " using username " + userName + " and password " + password;
log.error(errMessage, e);
throw new FileTransferException(e);
}
}
use of com.axway.ats.common.filetransfer.FileTransferException in project ats-framework by Axway.
the class FtpsClient method performUploadFile.
@Override
protected void performUploadFile(String localFile, String remoteDir, String remoteFile) throws FileTransferException {
FileInputStream fis = null;
try {
String remoteFileAbsPath = null;
remoteDir = remoteDir.replace("\\", "/");
remoteFile = remoteFile.replace("\\", "/");
if (remoteDir.endsWith("/") && remoteFile.endsWith("/")) {
remoteFileAbsPath = remoteDir.substring(0, remoteDir.length() - 2) + remoteFile;
} else if (!remoteDir.endsWith("/") && !remoteFile.endsWith("/")) {
remoteFileAbsPath = remoteDir + "/" + remoteFile;
} else {
remoteFileAbsPath = remoteDir + remoteFile;
}
// upload the file
fis = new FileInputStream(new File(localFile));
if (!this.ftpsConnection.storeFile(remoteFileAbsPath, fis)) {
throw new FileTransferException("Unable to store " + localFile + " to " + this.ftpsConnection.getPassiveHost() + " as a " + (remoteDir.endsWith("/") ? remoteDir : remoteDir + "/") + remoteFile);
}
} catch (Exception e) {
log.error("Unable to upload file!", e);
throw new FileTransferException(e);
} finally {
IoUtils.closeStream(fis, "Unable to close the file stream after successful upload!");
}
if (remoteDir != null && !remoteDir.endsWith("/")) {
remoteDir += "/";
}
log.info("Successfully uploaded '" + localFile + "' to '" + remoteDir + remoteFile + "', host " + ftpsConnection.getPassiveHost());
}
Aggregations