Search in sources :

Example 16 with FileTransferException

use of com.axway.ats.common.filetransfer.FileTransferException in project ats-framework by Axway.

the class FtpClient method executeCommand.

/**
 * Currently not supporting commands requiring opening of data connection
 * @param command the command to run
 * @return String representing the return code
 * @throws FileTransferException
 */
@Override
public String executeCommand(String command) throws FileTransferException {
    log.info("Run '" + command + "'");
    String returnCode = "";
    try {
        returnCode = String.valueOf(this.ftpConnection.sendCommand(command));
    } catch (IOException e) {
        log.error("Error running command: '" + command + "'", e);
        throw new FileTransferException(e);
    }
    log.info("Return code is '" + returnCode + "'");
    return returnCode;
}
Also used : FileTransferException(com.axway.ats.common.filetransfer.FileTransferException) IOException(java.io.IOException)

Example 17 with FileTransferException

use of com.axway.ats.common.filetransfer.FileTransferException in project ats-framework by Axway.

the class FtpClient 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.ftpConnection.storeFile(remoteFileAbsPath, fis)) {
            throw new FileTransferException("Unable to store " + localFile + " to " + this.ftpConnection.getPassiveHost() + " as a " + (remoteDir.endsWith("/") ? remoteDir : remoteDir + "/") + remoteFile);
        }
    } catch (Exception e) {
        log.error("Unable to upload file!", e);
        throw new FileTransferException(e);
    } finally {
        // close the file input stream
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
                log.error("Unable to close the file stream after successful upload!", e);
            }
        }
    }
    if (remoteDir != null && !remoteDir.endsWith("/")) {
        remoteDir += "/";
    }
    log.info("Successfully uploaded '" + localFile + "' to '" + remoteDir + remoteFile + "', host " + ftpConnection.getPassiveHost());
}
Also used : FileTransferException(com.axway.ats.common.filetransfer.FileTransferException) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileTransferException(com.axway.ats.common.filetransfer.FileTransferException)

Example 18 with FileTransferException

use of com.axway.ats.common.filetransfer.FileTransferException in project ats-framework by Axway.

the class FtpClient 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.ftpConnection.retrieveFile(remoteFileAbsPath, fos)) {
            throw new FileTransferException("Unable to retrieve " + (remoteDir.endsWith("/") ? remoteDir : remoteDir + "/") + remoteFile + " from " + this.ftpConnection.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
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                log.error("Unable to close the file stream after successful download!", e);
            }
        }
    }
    if (remoteDir != null && !remoteDir.endsWith("/")) {
        remoteDir += "/";
    }
    log.info("Successfully downloaded '" + localFile + "' from '" + remoteDir + remoteFile + "', host " + ftpConnection.getPassiveHost());
}
Also used : FileTransferException(com.axway.ats.common.filetransfer.FileTransferException) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException) FileTransferException(com.axway.ats.common.filetransfer.FileTransferException)

Example 19 with FileTransferException

use of com.axway.ats.common.filetransfer.FileTransferException in project ats-framework by Axway.

the class SftpClient method performDownloadFile.

@Override
protected void performDownloadFile(String localFile, String remoteDir, String remoteFile) throws FileTransferException {
    checkConnected("file download");
    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
        File file = new File(localFile);
        fos = new FileOutputStream(localFile);
        if (isDebugMode() && debugProgressMonitor != null) {
            debugProgressMonitor.setTransferMetadata(localFile, remoteFileAbsPath, file.length());
            this.channel.get(remoteFileAbsPath, fos, debugProgressMonitor);
        } else {
            this.channel.get(remoteFileAbsPath, fos);
        }
    } catch (SftpException e) {
        log.error("Unable to download " + localFile, e);
        throw new FileTransferException(e);
    } catch (FileNotFoundException e) {
        log.error("Unable to create " + 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 " + this.hostname);
}
Also used : FileTransferException(com.axway.ats.common.filetransfer.FileTransferException) FileOutputStream(java.io.FileOutputStream) SftpException(com.jcraft.jsch.SftpException) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File)

Example 20 with FileTransferException

use of com.axway.ats.common.filetransfer.FileTransferException in project ats-framework by Axway.

the class HttpsClient method connect.

/**
     * Connect to a remote host using basic authentication.
     *
     * @param hostname the host to connect to
     * @param userName the user name
     * @param password the password for the provided user name
     * @throws FileTransferException
     */
@Override
public void connect(String hostname, String userName, String password) throws FileTransferException {
    super.connect(hostname, userName, password);
    // trust everybody
    try {
        SSLContext sslContext = SslUtils.getTrustAllSSLContext();
        SSLConnectionSocketFactory ssf = new SSLConnectionSocketFactory(sslContext, encryptionProtocols, cipherSuites, new NoopHostnameVerifier());
        Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("https", ssf).build();
        HttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
        this.httpBuilder.setConnectionManager(connectionManager).setSchemePortResolver(new DefaultSchemePortResolver());
        this.httpClient = this.httpBuilder.build();
    } catch (Exception e) {
        throw new FileTransferException("Error setting trust manager", e);
    }
}
Also used : FileTransferException(com.axway.ats.common.filetransfer.FileTransferException) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) DefaultSchemePortResolver(org.apache.http.impl.conn.DefaultSchemePortResolver) SSLContext(javax.net.ssl.SSLContext) HttpClientConnectionManager(org.apache.http.conn.HttpClientConnectionManager) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) CertificateException(java.security.cert.CertificateException) GeneralSecurityException(java.security.GeneralSecurityException) FileTransferException(com.axway.ats.common.filetransfer.FileTransferException) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

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