Search in sources :

Example 11 with FileTransferException

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);
}
Also used : FileTransferException(com.axway.ats.common.filetransfer.FileTransferException) BaseTest(com.axway.ats.action.BaseTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 12 with FileTransferException

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();
}
Also used : TransferListener(com.axway.ats.core.filetransfer.model.TransferListener) FileTransferException(com.axway.ats.common.filetransfer.FileTransferException) Logger(org.apache.logging.log4j.Logger) File(java.io.File)

Example 13 with FileTransferException

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());
}
Also used : FileTransferException(com.axway.ats.common.filetransfer.FileTransferException) FileOutputStream(java.io.FileOutputStream) File(java.io.File) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) FileTransferException(com.axway.ats.common.filetransfer.FileTransferException)

Example 14 with FileTransferException

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);
    }
}
Also used : FtpListener(com.axway.ats.core.filetransfer.model.ftp.FtpListener) FileTransferException(com.axway.ats.common.filetransfer.FileTransferException) FtpResponseListener(com.axway.ats.core.filetransfer.model.ftp.FtpResponseListener) SSLContext(javax.net.ssl.SSLContext) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) FileTransferException(com.axway.ats.common.filetransfer.FileTransferException)

Example 15 with FileTransferException

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());
}
Also used : FileTransferException(com.axway.ats.common.filetransfer.FileTransferException) File(java.io.File) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) FileTransferException(com.axway.ats.common.filetransfer.FileTransferException)

Aggregations

FileTransferException (com.axway.ats.common.filetransfer.FileTransferException)36 IOException (java.io.IOException)16 File (java.io.File)11 BaseTest (com.axway.ats.action.BaseTest)7 Test (org.junit.Test)7 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)7 FileOutputStream (java.io.FileOutputStream)6 HttpEntity (org.apache.http.HttpEntity)5 HttpResponse (org.apache.http.HttpResponse)5 ClientProtocolException (org.apache.http.client.ClientProtocolException)5 CertificateException (java.security.cert.CertificateException)4 PublicAtsApi (com.axway.ats.common.PublicAtsApi)3 IFileTransferClient (com.axway.ats.core.filetransfer.model.IFileTransferClient)3 SftpException (com.jcraft.jsch.SftpException)3 FileInputStream (java.io.FileInputStream)3 FileNotFoundException (java.io.FileNotFoundException)3 OutputStream (java.io.OutputStream)3 HttpGet (org.apache.http.client.methods.HttpGet)3 HttpPost (org.apache.http.client.methods.HttpPost)3 SshCipher (com.axway.ats.common.filetransfer.SshCipher)2