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) {
is = exchange.getIn().getMandatoryBody(InputStream.class);
}
final StopWatch watch = new StopWatch();
LOG.debug("About to store file: {} using stream: {}", targetName, is);
if (endpoint.getFileExist() == GenericFileExist.Append) {
LOG.trace("Client appendFile: {}", targetName);
channel.put(is, targetName, ChannelSftp.APPEND);
} else {
LOG.trace("Client storeFile: {}", targetName);
// override is default
channel.put(is, targetName);
}
watch.stop();
if (LOG.isDebugEnabled()) {
LOG.debug("Took {} ({} millis) to store file: {} and FTP client returned: true",
new Object[]{TimeUtils.printDuration(watch.taken()), watch.taken(), targetName});
}
// after storing file, we may set chmod on the file
String mode = endpoint.getConfiguration().getChmod();
if (ObjectHelper.isNotEmpty(mode)) {
// parse to int using 8bit mode
int permissions = Integer.parseInt(mode, 8);
LOG.trace("Setting chmod: {} on file: {}", mode, targetName);
channel.chmod(permissions, targetName);
}
return true;
} catch (SftpException e) {
throw new GenericFileOperationFailedException("Cannot store file: " + name, e);
} catch (InvalidPayloadException e) {
throw new GenericFileOperationFailedException("Cannot store file: " + name, e);
} finally {
IOHelper.close(is, "store: " + name, LOG);
}
}