use of org.apache.camel.component.file.GenericFileOperationFailedException in project camel by apache.
the class SftpOperations method buildDirectory.
public synchronized boolean buildDirectory(String directory, boolean absolute) throws GenericFileOperationFailedException {
// must normalize directory first
directory = endpoint.getConfiguration().normalizePath(directory);
LOG.trace("buildDirectory({},{})", directory, absolute);
// ignore absolute as all dirs are relative with FTP
boolean success = false;
String originalDirectory = getCurrentDirectory();
try {
// maybe the full directory already exists
try {
channel.cd(directory);
success = true;
} catch (SftpException e) {
// ignore, we could not change directory so try to create it instead
}
if (!success) {
LOG.debug("Trying to build remote directory: {}", directory);
try {
channel.mkdir(directory);
success = true;
} catch (SftpException e) {
// we are here if the server side doesn't create intermediate folders
// so create the folder one by one
success = buildDirectoryChunks(directory);
}
}
} catch (IOException e) {
throw new GenericFileOperationFailedException("Cannot build directory: " + directory, e);
} catch (SftpException e) {
throw new GenericFileOperationFailedException("Cannot build directory: " + directory, e);
} finally {
// change back to original directory
if (originalDirectory != null) {
changeCurrentDirectory(originalDirectory);
}
}
return success;
}
use of org.apache.camel.component.file.GenericFileOperationFailedException in project camel by apache.
the class SftpOperations method renameFile.
public synchronized boolean renameFile(String from, String to) throws GenericFileOperationFailedException {
LOG.debug("Renaming file: {} to: {}", from, to);
try {
reconnectIfNecessary();
channel.rename(from, to);
return true;
} catch (SftpException e) {
LOG.debug("Cannot rename file from: " + from + " to: " + to, e);
throw new GenericFileOperationFailedException("Cannot rename file from: " + from + " to: " + to, e);
}
}
use of org.apache.camel.component.file.GenericFileOperationFailedException in project camel by apache.
the class SftpOperations method retrieveFileToStreamInBody.
@SuppressWarnings("unchecked")
private boolean retrieveFileToStreamInBody(String name, Exchange exchange) throws GenericFileOperationFailedException {
OutputStream os = null;
String currentDir = null;
try {
GenericFile<ChannelSftp.LsEntry> target = (GenericFile<ChannelSftp.LsEntry>) exchange.getProperty(FileComponent.FILE_EXCHANGE_FILE);
ObjectHelper.notNull(target, "Exchange should have the " + FileComponent.FILE_EXCHANGE_FILE + " set");
String remoteName = name;
if (endpoint.getConfiguration().isStepwise()) {
// remember current directory
currentDir = getCurrentDirectory();
// change directory to path where the file is to be retrieved
// (must do this as some FTP servers cannot retrieve using absolute path)
String path = FileUtil.onlyPath(name);
if (path != null) {
changeCurrentDirectory(path);
}
// remote name is now only the file name as we just changed directory
remoteName = FileUtil.stripPath(name);
}
// use input stream which works with Apache SSHD used for testing
InputStream is = channel.get(remoteName);
if (endpoint.getConfiguration().isStreamDownload()) {
target.setBody(is);
exchange.getIn().setHeader(RemoteFileComponent.REMOTE_FILE_INPUT_STREAM, is);
} else {
os = new ByteArrayOutputStream();
target.setBody(os);
IOHelper.copyAndCloseInput(is, os);
}
return true;
} catch (IOException e) {
throw new GenericFileOperationFailedException("Cannot retrieve file: " + name, e);
} catch (SftpException e) {
throw new GenericFileOperationFailedException("Cannot retrieve file: " + name, e);
} finally {
IOHelper.close(os, "retrieve: " + name, LOG);
// change back to current directory if we changed directory
if (currentDir != null) {
changeCurrentDirectory(currentDir);
}
}
}
use of org.apache.camel.component.file.GenericFileOperationFailedException in project camel by apache.
the class FtpOperations method deleteFile.
public boolean deleteFile(String name) throws GenericFileOperationFailedException {
log.debug("Deleting file: {}", name);
boolean result;
String target = name;
String currentDir = null;
try {
if (endpoint.getConfiguration().isStepwise()) {
// remember current directory
currentDir = getCurrentDirectory();
target = FileUtil.stripPath(name);
try {
changeCurrentDirectory(FileUtil.onlyPath(name));
} catch (GenericFileOperationFailedException e) {
// we could not change directory, try to change back before
changeCurrentDirectory(currentDir);
throw e;
}
}
// delete the file
log.trace("Client deleteFile: {}", target);
result = client.deleteFile(target);
// change back to previous directory
if (currentDir != null) {
changeCurrentDirectory(currentDir);
}
} catch (IOException e) {
throw new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString(), e.getMessage(), e);
}
return result;
}
use of org.apache.camel.component.file.GenericFileOperationFailedException in project camel by apache.
the class FtpOperations method listFiles.
public List<FTPFile> listFiles(String path) throws GenericFileOperationFailedException {
log.trace("listFiles({})", path);
// use current directory if path not given
if (ObjectHelper.isEmpty(path)) {
path = ".";
}
try {
final List<FTPFile> list = new ArrayList<FTPFile>();
FTPFile[] files = client.listFiles(path);
// can return either null or an empty list depending on FTP servers
if (files != null) {
list.addAll(Arrays.asList(files));
}
return list;
} catch (IOException e) {
throw new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString(), e.getMessage(), e);
}
}
Aggregations