use of org.apache.camel.component.file.GenericFileOperationFailedException in project camel by apache.
the class FtpOperations method existsFile.
public boolean existsFile(String name) throws GenericFileOperationFailedException {
log.trace("existsFile({})", name);
if (endpoint.isFastExistsCheck()) {
return fastExistsFile(name);
}
// check whether a file already exists
String directory = FileUtil.onlyPath(name);
String onlyName = FileUtil.stripPath(name);
try {
String[] names;
if (directory != null) {
names = client.listNames(directory);
} else {
names = client.listNames();
}
// can return either null or an empty list depending on FTP servers
if (names == null) {
return false;
}
for (String existing : names) {
log.trace("Existing file: {}, target file: {}", existing, name);
existing = FileUtil.stripPath(existing);
if (existing != null && existing.equals(onlyName)) {
return true;
}
}
return false;
} catch (IOException e) {
throw new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString(), e.getMessage(), e);
}
}
use of org.apache.camel.component.file.GenericFileOperationFailedException in project camel by apache.
the class FtpOperations method retrieveFileToFileInLocalWorkDirectory.
@SuppressWarnings("unchecked")
private boolean retrieveFileToFileInLocalWorkDirectory(String name, Exchange exchange) throws GenericFileOperationFailedException {
File temp;
File local = new File(FileUtil.normalizePath(endpoint.getLocalWorkDirectory()));
OutputStream os;
try {
// use relative filename in local work directory
GenericFile<FTPFile> target = (GenericFile<FTPFile>) exchange.getProperty(FileComponent.FILE_EXCHANGE_FILE);
ObjectHelper.notNull(target, "Exchange should have the " + FileComponent.FILE_EXCHANGE_FILE + " set");
String relativeName = target.getRelativeFilePath();
temp = new File(local, relativeName + ".inprogress");
local = new File(local, relativeName);
// create directory to local work file
local.mkdirs();
// delete any existing files
if (temp.exists()) {
if (!FileUtil.deleteFile(temp)) {
throw new GenericFileOperationFailedException("Cannot delete existing local work file: " + temp);
}
}
if (local.exists()) {
if (!FileUtil.deleteFile(local)) {
throw new GenericFileOperationFailedException("Cannot delete existing local work file: " + local);
}
}
// create new temp local work file
if (!temp.createNewFile()) {
throw new GenericFileOperationFailedException("Cannot create new local work file: " + temp);
}
// store content as a file in the local work directory in the temp handle
os = new FileOutputStream(temp);
// set header with the path to the local work file
exchange.getIn().setHeader(Exchange.FILE_LOCAL_WORK_PATH, local.getPath());
} catch (Exception e) {
throw new GenericFileOperationFailedException("Cannot create new local work file: " + local);
}
boolean result;
try {
GenericFile<FTPFile> target = (GenericFile<FTPFile>) exchange.getProperty(FileComponent.FILE_EXCHANGE_FILE);
// store the java.io.File handle as the body
target.setBody(local);
String remoteName = name;
String currentDir = null;
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);
}
log.trace("Client retrieveFile: {}", remoteName);
result = client.retrieveFile(remoteName, os);
// store client reply information after the operation
exchange.getIn().setHeader(FtpConstants.FTP_REPLY_CODE, client.getReplyCode());
exchange.getIn().setHeader(FtpConstants.FTP_REPLY_STRING, client.getReplyString());
// change back to current directory
if (endpoint.getConfiguration().isStepwise()) {
changeCurrentDirectory(currentDir);
}
} catch (IOException e) {
log.trace("Error occurred during retrieving file: {} to local directory. Deleting local work file: {}", name, temp);
// failed to retrieve the file so we need to close streams and delete in progress file
// must close stream before deleting file
IOHelper.close(os, "retrieve: " + name, log);
boolean deleted = FileUtil.deleteFile(temp);
if (!deleted) {
log.warn("Error occurred during retrieving file: " + name + " to local directory. Cannot delete local work file: " + temp);
}
throw new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString(), e.getMessage(), e);
} finally {
// need to close the stream before rename it
IOHelper.close(os, "retrieve: " + name, log);
}
log.debug("Retrieve file to local work file result: {}", result);
if (result) {
log.trace("Renaming local in progress file from: {} to: {}", temp, local);
// operation went okay so rename temp to local after we have retrieved the data
try {
if (!FileUtil.renameFile(temp, local, false)) {
throw new GenericFileOperationFailedException("Cannot rename local work file from: " + temp + " to: " + local);
}
} catch (IOException e) {
throw new GenericFileOperationFailedException("Cannot rename local work file from: " + temp + " to: " + local, e);
}
}
return result;
}
use of org.apache.camel.component.file.GenericFileOperationFailedException in project camel by apache.
the class FtpOperations method disconnect.
public void disconnect() throws GenericFileOperationFailedException {
// logout before disconnecting
try {
log.trace("Client logout");
client.logout();
} catch (IOException e) {
throw new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString(), e.getMessage(), e);
} finally {
try {
log.trace("Client disconnect");
client.disconnect();
} catch (IOException e) {
throw new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString(), e.getMessage(), e);
}
}
}
use of org.apache.camel.component.file.GenericFileOperationFailedException in project camel by apache.
the class FtpsOperations method connect.
@Override
public boolean connect(RemoteFileConfiguration configuration) throws GenericFileOperationFailedException {
boolean answer = super.connect(configuration);
FtpsConfiguration config = (FtpsConfiguration) configuration;
if (answer) {
try {
String execProt = config.getExecProt();
Long execPbsz = config.getExecPbsz();
// use default values for prop and pbsz, unless told to not do so
if (!config.isDisableSecureDataChannelDefaults()) {
if (ObjectHelper.isEmpty(execProt)) {
execProt = "P";
}
if (ObjectHelper.isEmpty(execPbsz)) {
execPbsz = 0L;
}
}
if (execPbsz != null) {
log.debug("FTPClient initializing with execPbsz={}", execPbsz);
getFtpClient().execPBSZ(execPbsz);
}
if (execProt != null) {
log.debug("FTPClient initializing with execProt={}", execProt);
getFtpClient().execPROT(execProt);
}
} catch (SSLException e) {
throw new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString(), e.getMessage(), e);
} catch (IOException e) {
throw new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString(), e.getMessage(), e);
}
}
return answer;
}
use of org.apache.camel.component.file.GenericFileOperationFailedException in project camel by apache.
the class SftpOperations method connect.
public synchronized boolean connect(RemoteFileConfiguration configuration) throws GenericFileOperationFailedException {
if (isConnected()) {
// already connected
return true;
}
boolean connected = false;
int attempt = 0;
while (!connected) {
try {
if (LOG.isTraceEnabled() && attempt > 0) {
LOG.trace("Reconnect attempt #{} connecting to + {}", attempt, configuration.remoteServerInformation());
}
if (channel == null || !channel.isConnected()) {
if (session == null || !session.isConnected()) {
LOG.trace("Session isn't connected, trying to recreate and connect.");
session = createSession(configuration);
if (endpoint.getConfiguration().getConnectTimeout() > 0) {
LOG.trace("Connecting use connectTimeout: " + endpoint.getConfiguration().getConnectTimeout() + " ...");
session.connect(endpoint.getConfiguration().getConnectTimeout());
} else {
LOG.trace("Connecting ...");
session.connect();
}
}
LOG.trace("Channel isn't connected, trying to recreate and connect.");
channel = (ChannelSftp) session.openChannel("sftp");
if (endpoint.getConfiguration().getConnectTimeout() > 0) {
LOG.trace("Connecting use connectTimeout: " + endpoint.getConfiguration().getConnectTimeout() + " ...");
channel.connect(endpoint.getConfiguration().getConnectTimeout());
} else {
LOG.trace("Connecting ...");
channel.connect();
}
LOG.debug("Connected to " + configuration.remoteServerInformation());
}
// yes we could connect
connected = true;
} catch (Exception e) {
// check if we are interrupted so we can break out
if (Thread.currentThread().isInterrupted()) {
throw new GenericFileOperationFailedException("Interrupted during connecting", new InterruptedException("Interrupted during connecting"));
}
GenericFileOperationFailedException failed = new GenericFileOperationFailedException("Cannot connect to " + configuration.remoteServerInformation(), e);
LOG.trace("Cannot connect due: {}", failed.getMessage());
attempt++;
if (attempt > endpoint.getMaximumReconnectAttempts()) {
throw failed;
}
if (endpoint.getReconnectDelay() > 0) {
try {
Thread.sleep(endpoint.getReconnectDelay());
} catch (InterruptedException ie) {
// we could potentially also be interrupted during sleep
Thread.currentThread().interrupt();
throw new GenericFileOperationFailedException("Interrupted during sleeping", ie);
}
}
}
}
configureBulkRequests();
return true;
}
Aggregations