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);
}
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);
}
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);
}
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);
}
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);
}
Aggregations