use of org.apache.camel.component.file.GenericFileOperationFailedException in project camel by apache.
the class SftpOperations method deleteFile.
public synchronized boolean deleteFile(String name) throws GenericFileOperationFailedException {
LOG.debug("Deleting file: {}", name);
try {
reconnectIfNecessary();
channel.rm(name);
return true;
} catch (SftpException e) {
LOG.debug("Cannot delete file: " + name, e);
throw new GenericFileOperationFailedException("Cannot delete file: " + name, e);
}
}
use of org.apache.camel.component.file.GenericFileOperationFailedException in project camel by apache.
the class FtpOperations method doChangeDirectory.
private void doChangeDirectory(String path) {
if (path == null || ".".equals(path) || ObjectHelper.isEmpty(path)) {
return;
}
log.trace("Changing directory: {}", path);
boolean success;
try {
if ("..".equals(path)) {
changeToParentDirectory();
success = true;
} else {
success = client.changeWorkingDirectory(path);
}
} catch (IOException e) {
throw new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString(), e.getMessage(), e);
}
if (!success) {
throw new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString(), "Cannot change directory to: " + path);
}
}
use of org.apache.camel.component.file.GenericFileOperationFailedException in project camel by apache.
the class FtpOperations method doStoreFile.
private boolean doStoreFile(String name, String targetName, Exchange exchange) throws GenericFileOperationFailedException {
log.trace("doStoreFile({})", targetName);
// if an existing file already exists what should we do?
if (endpoint.getFileExist() == GenericFileExist.Ignore || endpoint.getFileExist() == GenericFileExist.Fail || endpoint.getFileExist() == GenericFileExist.Move) {
boolean existFile = existsFile(targetName);
if (existFile && endpoint.getFileExist() == GenericFileExist.Ignore) {
// ignore but indicate that the file was written
log.trace("An existing file already exists: {}. Ignore and do not override it.", name);
return true;
} else if (existFile && endpoint.getFileExist() == GenericFileExist.Fail) {
throw new GenericFileOperationFailedException("File already exist: " + name + ". Cannot write new file.");
} else if (existFile && endpoint.getFileExist() == GenericFileExist.Move) {
// move any existing file first
doMoveExistingFile(name, targetName);
}
}
InputStream is = null;
if (exchange.getIn().getBody() == null) {
// Do an explicit test for a null body and decide what to do
if (endpoint.isAllowNullBody()) {
log.trace("Writing empty file.");
is = new ByteArrayInputStream(new byte[] {});
} else {
throw new GenericFileOperationFailedException("Cannot write null body to file: " + name);
}
}
try {
if (is == null) {
String charset = endpoint.getCharset();
if (charset != null) {
// charset configured so we must convert to the desired
// charset so we can write with encoding
is = new ByteArrayInputStream(exchange.getIn().getMandatoryBody(String.class).getBytes(charset));
log.trace("Using InputStream {} with charset {}.", is, charset);
} else {
is = exchange.getIn().getMandatoryBody(InputStream.class);
}
}
final StopWatch watch = new StopWatch();
boolean answer;
log.debug("About to store file: {} using stream: {}", targetName, is);
if (endpoint.getFileExist() == GenericFileExist.Append) {
log.trace("Client appendFile: {}", targetName);
answer = client.appendFile(targetName, is);
} else {
log.trace("Client storeFile: {}", targetName);
answer = client.storeFile(targetName, is);
}
watch.stop();
if (log.isDebugEnabled()) {
log.debug("Took {} ({} millis) to store file: {} and FTP client returned: {}", new Object[] { TimeUtils.printDuration(watch.taken()), watch.taken(), targetName, answer });
}
// 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());
// after storing file, we may set chmod on the file
String chmod = ((FtpConfiguration) endpoint.getConfiguration()).getChmod();
if (ObjectHelper.isNotEmpty(chmod)) {
log.debug("Setting chmod: {} on file: {}", chmod, targetName);
String command = "chmod " + chmod + " " + targetName;
log.trace("Client sendSiteCommand: {}", command);
boolean success = client.sendSiteCommand(command);
log.trace("Client sendSiteCommand successful: {}", success);
}
return answer;
} catch (IOException e) {
throw new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString(), e.getMessage(), e);
} catch (InvalidPayloadException e) {
throw new GenericFileOperationFailedException("Cannot store file: " + name, e);
} finally {
IOHelper.close(is, "store: " + name, log);
}
}
use of org.apache.camel.component.file.GenericFileOperationFailedException in project camel by apache.
the class FtpLoginNoRetryTest method testBadLogin.
@Test
public void testBadLogin() throws Exception {
try {
uploadFile("dummy", "cantremeber");
fail("Should have thrown a GenericFileOperationFailedException");
} catch (GenericFileOperationFailedException e) {
// expected
assertEquals(530, e.getCode());
}
// assert file NOT created
File file = new File(FTP_ROOT_DIR + "login/report.txt");
assertFalse("The file should NOT exists", file.exists());
}
use of org.apache.camel.component.file.GenericFileOperationFailedException in project camel by apache.
the class GenericFileDeleteProcessStrategy method commit.
@Override
public void commit(GenericFileOperations<T> operations, GenericFileEndpoint<T> endpoint, Exchange exchange, GenericFile<T> file) throws Exception {
// special for file lock strategy as we must release that lock first before we can delete the file
boolean releaseEager = exclusiveReadLockStrategy instanceof FileLockExclusiveReadLockStrategy;
if (releaseEager) {
exclusiveReadLockStrategy.releaseExclusiveReadLockOnCommit(operations, file, exchange);
}
try {
deleteLocalWorkFile(exchange);
operations.releaseRetreivedFileResources(exchange);
int retries = 3;
boolean deleted = false;
while (retries > 0 && !deleted) {
retries--;
if (operations.deleteFile(file.getAbsoluteFilePath())) {
// file is deleted
deleted = true;
break;
}
// some OS can report false when deleting but the file is still deleted
// use exists to check instead
boolean exits = operations.existsFile(file.getAbsoluteFilePath());
if (!exits) {
deleted = true;
} else {
log.trace("File was not deleted at this attempt will try again in 1 sec.: {}", file);
// sleep a bit and try again
Thread.sleep(1000);
}
}
if (!deleted) {
throw new GenericFileOperationFailedException("Cannot delete file: " + file);
}
} finally {
// must release lock last
if (!releaseEager && exclusiveReadLockStrategy != null) {
exclusiveReadLockStrategy.releaseExclusiveReadLockOnCommit(operations, file, exchange);
}
}
}
Aggregations