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