Search in sources :

Example 21 with FileTransferException

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

the class HttpClient method performDownloadFile.

@Override
protected void performDownloadFile(String localFile, String remoteDir, String remoteFile) throws FileTransferException {
    checkClientInitialized();
    final String getUrl = constructDownloadUrl(remoteDir, remoteFile);
    log.info("Downloading " + getUrl);
    HttpGet httpGetMethod = new HttpGet(getUrl);
    HttpEntity responseEntity = null;
    OutputStream outstream = null;
    boolean errorSavingFile = true;
    try {
        // add headers specified by the user
        addRequestHeaders(httpGetMethod);
        // download the file
        HttpResponse response = httpClient.execute(httpGetMethod, httpContext);
        if (200 != response.getStatusLine().getStatusCode()) {
            throw new FileTransferException("Downloading " + getUrl + " returned " + response.getStatusLine());
        }
        // save the file
        responseEntity = response.getEntity();
        if (responseEntity != null) {
            outstream = new FileOutputStream(localFile);
            responseEntity.writeTo(outstream);
            outstream.flush();
            errorSavingFile = false;
        } else {
            throw new FileTransferException("No file present in the response");
        }
    } catch (ClientProtocolException e) {
        log.error("Unable to download file!", e);
        throw new FileTransferException(e);
    } catch (IOException e) {
        log.error("Unable to download file!", e);
        throw new FileTransferException(e);
    } finally {
        if (responseEntity != null) {
            IoUtils.closeStream(outstream);
            if (errorSavingFile) {
                try {
                    // We were not able to properly stream the entity content
                    // to the local file system. The next line consumes the
                    // entity content closes the underlying stream.
                    EntityUtils.consume(responseEntity);
                } catch (IOException e) {
                // we tried our best to release the resources
                }
            }
        }
    }
    log.info("Successfully downloaded '" + localFile + "' from '" + remoteDir + remoteFile + "' at " + this.hostname);
}
Also used : FileTransferException(com.axway.ats.common.filetransfer.FileTransferException) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 22 with FileTransferException

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

the class ClientFactory method loadCustomClient.

/**
     * Instantiates the custom file transfer client
     * 
     * @param customFileTransferClient the class name of the custom client
     * @return
     * @throws FileTransferException
     */
private IFileTransferClient loadCustomClient(String customFileTransferClient) throws FileTransferException {
    try {
        // load the custom client class
        Class<?> customFileTransferClientClass = this.getClass().getClassLoader().loadClass(customFileTransferClient);
        // make an instance of the custom client
        IFileTransferClient client = (IFileTransferClient) customFileTransferClientClass.newInstance();
        log.info("Loaded custom file transfer client: " + customFileTransferClient);
        return client;
    } catch (ClassNotFoundException e) {
        throw new FileTransferException("Custom file transfer client not found in the classpath: " + customFileTransferClient);
    } catch (ClassCastException e) {
        throw new FileTransferException("Wrong type of the custom file transfer client: " + customFileTransferClient);
    } catch (InstantiationException e) {
        throw new FileTransferException("Unable to create an instance of the custom file transfer client: " + customFileTransferClient);
    } catch (IllegalAccessException e) {
        throw new FileTransferException("Unable to create an instance of the custom file transfer client: " + customFileTransferClient);
    }
}
Also used : IFileTransferClient(com.axway.ats.core.filetransfer.model.IFileTransferClient) FileTransferException(com.axway.ats.common.filetransfer.FileTransferException)

Example 23 with FileTransferException

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

the class ClientFactory method getClient.

/**
     * Returns a product specific {@link IFileTransferClient} that handles transfers via the specified {@link TransferProtocol}.
     * 
     * @param protocol the {@link TransferProtocol} to use
     * @param port a custom port to use when connecting
     * @param customFileTransferClient the class name of the custom client
     * @return the {@link IFileTransferClient} that handles transfers via the specified {@link TransferProtocol}
     * @throws FileTransferException 
     * @see {@link TransferProtocol}
     */
public IFileTransferClient getClient(TransferProtocol protocol, int port, String customFileTransferClient) throws FileTransferException {
    switch(protocol) {
        case FTP:
            return new FtpClient(port);
        case FTPS:
            return new FtpsClient(port);
        case SFTP:
            return new SftpClient(port);
        case HTTP:
            return new HttpClient(port);
        case HTTPS:
            return new HttpsClient(port);
        case HTTP_CUSTOM:
        case HTTPS_CUSTOM:
        case PESIT_CUSTOM:
        case CUSTOM:
            // load the custom client
            IFileTransferClient client = loadCustomClient(customFileTransferClient);
            client.setCustomPort(port);
            return client;
        default:
            throw new FileTransferException("No implementation for the " + protocol + " protocol is currently available");
    }
}
Also used : IFileTransferClient(com.axway.ats.core.filetransfer.model.IFileTransferClient) FileTransferException(com.axway.ats.common.filetransfer.FileTransferException)

Example 24 with FileTransferException

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

the class AbstractFileTransferClient method resumePausedTransfer.

public synchronized void resumePausedTransfer() throws FileTransferException {
    checkPausedTransferRunning(true);
    final Logger log = LogManager.getLogger(AbstractFileTransferClient.class);
    while (!canResume) {
        try {
            log.debug("Waiting for the transfer to start...");
            // Wait to be notified when the transfer is started and will be paused.
            this.wait();
        } catch (InterruptedException e) {
            throw new FileTransferException("Interrupted while waiting for a transfer to start", e);
        }
    }
    // for the next resume
    canResume = false;
    // Notify the thread that is performing the transfer to continue.
    this.notifyAll();
    try {
        log.debug("Waiting for the transfer to finish...");
        // Wait to be notified that the transfer is done.
        this.wait();
    } catch (InterruptedException e) {
        throw new FileTransferException("Interrupted while waiting for a transfer to finish", e);
    } finally {
        // the paused transfer has finished
        this.isTransferStartedAndPaused = false;
    }
}
Also used : FileTransferException(com.axway.ats.common.filetransfer.FileTransferException) Logger(org.apache.logging.log4j.Logger)

Example 25 with FileTransferException

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

the class FtpClient method disconnect.

/**
 * Disconnect from the remote host
 *
 * @throws FileTransferException
 */
@Override
public void disconnect() throws FileTransferException {
    if (this.ftpConnection != null && this.ftpConnection.isConnected()) {
        try {
            this.ftpConnection.disconnect();
            this.ftpConnection = null;
        } catch (IOException e) {
            throw new FileTransferException(e);
        }
    }
}
Also used : FileTransferException(com.axway.ats.common.filetransfer.FileTransferException) IOException(java.io.IOException)

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