Search in sources :

Example 31 with FileTransferException

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

the class Test_GenericTransferClient method testChangeModeException.

/**
 * Test case
 * @throws Exception
 */
@Test(expected = FileTransferException.class)
public void testChangeModeException() throws Exception {
    // setup expectations
    ftpMock.setTransferMode(TransferMode.ASCII);
    expectLastCall().andThrow(new FileTransferException(SAMPLE_EXCEPTION_MESSAGE));
    replay(ftpMock);
    // execute operations
    mockedTestObject.setTransferMode(TransferMode.ASCII);
    // 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 32 with FileTransferException

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

the class Test_GenericTransferClient method testStartUploadAndPauseAltException.

@Test(expected = FileTransferException.class)
public void testStartUploadAndPauseAltException() throws Exception {
    // setup expectations
    File fileMock = createMockAndExpectNew(File.class, SAMPLE_LOCAL_DIRECTORY + SAMPLE_LOCAL_FILE);
    expect(fileMock.getName()).andReturn(SAMPLE_LOCAL_FILE);
    ftpMock.startUploadAndPause(SAMPLE_LOCAL_DIRECTORY + SAMPLE_LOCAL_FILE, SAMPLE_REMOTE_DIRECTORY, SAMPLE_LOCAL_FILE);
    expectLastCall().andThrow(new FileTransferException(SAMPLE_EXCEPTION_MESSAGE));
    replayAll();
    // execute operations
    mockedTestObject.startUploadAndPause(SAMPLE_LOCAL_DIRECTORY + SAMPLE_LOCAL_FILE, SAMPLE_REMOTE_DIRECTORY);
    // verify results
    verify(ftpMock);
}
Also used : FileTransferException(com.axway.ats.common.filetransfer.FileTransferException) File(java.io.File) BaseTest(com.axway.ats.action.BaseTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 33 with FileTransferException

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

the class Test_GenericTransferClient method testFileDownloadAltException.

/**
 * Test case
 * @throws Exception
 */
@Test(expected = FileTransferException.class)
public void testFileDownloadAltException() throws Exception {
    // setup expectations
    ftpMock.downloadFile(SAMPLE_LOCAL_DIRECTORY + SAMPLE_LOCAL_FILE, SAMPLE_REMOTE_DIRECTORY, SAMPLE_REMOTE_FILE);
    expectLastCall().andThrow(new FileTransferException(SAMPLE_EXCEPTION_MESSAGE));
    replay(ftpMock);
    // execute operations
    mockedTestObject.downloadFile(SAMPLE_LOCAL_FILE, 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 34 with FileTransferException

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

the class FileTransferHttpClient method uploadFile.

/**
 * Upload a file
 *
 * @param localFile path to the local file
 * @param remoteDir the remote directory
 * @param remoteFile the remote file name
 * @throws FileTransferException
 */
@PublicAtsApi
@Override
public void uploadFile(String localFile, String remoteDir, String remoteFile) throws FileTransferException {
    checkClientInitialized();
    final String uploadUrl = constructRemoteHostUrl(remoteDir, remoteFile);
    log.info("Uploading " + uploadUrl);
    HttpEntityEnclosingRequestBase httpMethod;
    if (this.uploadMethod.equals(UPLOAD_METHOD__PUT)) {
        httpMethod = new HttpPut(uploadUrl);
    } else {
        httpMethod = new HttpPost(uploadUrl);
    }
    FileEntity fileUploadEntity = new FileEntity(new File(localFile), this.contentType);
    if (!StringUtils.isNullOrEmpty(this.transferEncoding)) {
        if (this.transferEncoding.toLowerCase().contains(TRANSFER_ENCODING_MODE_CHUNKED)) {
            fileUploadEntity.setChunked(true);
            log.info("Transfer-Encoding set to '" + this.transferEncoding + "'");
        }
    }
    httpMethod.setEntity(fileUploadEntity);
    HttpResponse response = null;
    try {
        // add headers specified by the user
        addHeadersToHttpMethod(httpMethod);
        // upload the file
        response = httpClient.execute(httpMethod, httpContext);
        int responseCode = response.getStatusLine().getStatusCode();
        if (responseCode < 200 || responseCode > 206) {
            // 204 No Content - there was a file with same name on same location, we replaced it
            throw new FileTransferException("Uploading '" + uploadUrl + "' returned '" + response.getStatusLine() + "'");
        }
    } catch (ClientProtocolException e) {
        log.error("Unable to upload file!", e);
        throw new FileTransferException(e);
    } catch (IOException e) {
        log.error("Unable to upload file!", e);
        throw new FileTransferException(e);
    } finally {
        // the UPLOAD returns response body on error
        if (response != null && response.getEntity() != null) {
            HttpEntity responseEntity = response.getEntity();
            // the underlying stream has been closed
            try {
                EntityUtils.consume(responseEntity);
            } catch (IOException e) {
            // we tried our best to release the resources
            }
        }
    }
    log.info("Successfully uploaded '" + localFile + "' to '" + remoteDir + remoteFile + "', host " + this.host);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) FileTransferException(com.axway.ats.common.filetransfer.FileTransferException) HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase) FileEntity(org.apache.http.entity.FileEntity) HttpEntity(org.apache.http.HttpEntity) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) File(java.io.File) HttpPut(org.apache.http.client.methods.HttpPut) ClientProtocolException(org.apache.http.client.ClientProtocolException) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 35 with FileTransferException

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

the class FileTransferHttpClient method downloadFile.

/**
 * Download a file
 *
 * @param localFile path to the local file
 * @param remoteDir the remote directory
 * @param remoteFile the remote file name
 * @throws FileTransferException
 */
@PublicAtsApi
@Override
public void downloadFile(String localFile, String remoteDir, String remoteFile) throws FileTransferException {
    checkClientInitialized();
    final String downloadUrl = constructRemoteHostUrl(remoteDir, remoteFile);
    log.info("Downloading " + downloadUrl);
    HttpGet httpGetMethod = new HttpGet(downloadUrl);
    HttpEntity responseEntity = null;
    OutputStream outstream = null;
    boolean errorSavingFile = true;
    try {
        // add headers specified by the user
        addHeadersToHttpMethod(httpGetMethod);
        // download the file
        HttpResponse response = httpClient.execute(httpGetMethod, httpContext);
        if (200 != response.getStatusLine().getStatusCode()) {
            throw new FileTransferException("Downloading " + downloadUrl + " 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.host);
}
Also used : FileTransferException(com.axway.ats.common.filetransfer.FileTransferException) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) 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) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

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