You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2018/11/16 12:47:46 UTC
[camel] branch master updated: CAMEL-12631 - Fixed CS
This is an automated email from the ASF dual-hosted git repository.
acosentino pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/master by this push:
new 4856ecd CAMEL-12631 - Fixed CS
4856ecd is described below
commit 4856ecda0a293e34f836d6e1542b5b2b3a8c5657
Author: Andrea Cosentino <an...@gmail.com>
AuthorDate: Fri Nov 16 13:47:02 2018 +0100
CAMEL-12631 - Fixed CS
---
.../component/file/remote/SftpOperations.java | 114 ++++++++++++---------
1 file changed, 65 insertions(+), 49 deletions(-)
diff --git a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
index 23abe1e..834595a 100644
--- a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
+++ b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
@@ -47,6 +47,7 @@ import com.jcraft.jsch.SftpException;
import com.jcraft.jsch.SocketFactory;
import com.jcraft.jsch.UIKeyboardInteractive;
import com.jcraft.jsch.UserInfo;
+
import org.apache.camel.Exchange;
import org.apache.camel.InvalidPayloadException;
import org.apache.camel.LoggingLevel;
@@ -70,7 +71,8 @@ import static org.apache.camel.util.ObjectHelper.isNotEmpty;
/**
* SFTP remote file operations
* <p/>
- * The JSCH session and channel are not thread-safe so we need to synchronize access to using this operation.
+ * The JSCH session and channel are not thread-safe so we need to synchronize
+ * access to using this operation.
*/
public class SftpOperations implements RemoteFileOperations<SftpRemoteFile> {
private static final Logger LOG = LoggerFactory.getLogger(SftpOperations.class);
@@ -88,13 +90,14 @@ public class SftpOperations implements RemoteFileOperations<SftpRemoteFile> {
}
/**
- * Extended user info which supports interactive keyboard mode, by entering the password.
+ * Extended user info which supports interactive keyboard mode, by entering
+ * the password.
*/
public interface ExtendedUserInfo extends UserInfo, UIKeyboardInteractive {
}
public void setEndpoint(GenericFileEndpoint<SftpRemoteFile> endpoint) {
- this.endpoint = (SftpEndpoint) endpoint;
+ this.endpoint = (SftpEndpoint)endpoint;
}
public synchronized boolean connect(RemoteFileConfiguration configuration) throws GenericFileOperationFailedException {
@@ -126,7 +129,7 @@ public class SftpOperations implements RemoteFileOperations<SftpRemoteFile> {
}
LOG.trace("Channel isn't connected, trying to recreate and connect.");
- channel = (ChannelSftp) session.openChannel("sftp");
+ channel = (ChannelSftp)session.openChannel("sftp");
if (endpoint.getConfiguration().getConnectTimeout() > 0) {
LOG.trace("Connecting use connectTimeout: {} ...", endpoint.getConfiguration().getConnectTimeout());
@@ -191,7 +194,7 @@ public class SftpOperations implements RemoteFileOperations<SftpRemoteFile> {
final JSch jsch = new JSch();
JSch.setLogger(new JSchLogger(endpoint.getConfiguration().getJschLoggingLevel()));
- SftpConfiguration sftpConfig = (SftpConfiguration) configuration;
+ SftpConfiguration sftpConfig = (SftpConfiguration)configuration;
if (isNotEmpty(sftpConfig.getCiphers())) {
LOG.debug("Using ciphers: {}", sftpConfig.getCiphers());
@@ -333,7 +336,8 @@ public class SftpOperations implements RemoteFileOperations<SftpRemoteFile> {
public boolean promptYesNo(String s) {
LOG.warn("Server asks for confirmation (yes|no): {}. Camel will answer no.", s);
- // Return 'false' indicating modification of the hosts file is disabled.
+ // Return 'false' indicating modification of the hosts file is
+ // disabled.
return false;
}
@@ -341,13 +345,12 @@ public class SftpOperations implements RemoteFileOperations<SftpRemoteFile> {
LOG.trace("Message received from Server: {}", s);
}
- public String[] promptKeyboardInteractive(String destination, String name,
- String instruction, String[] prompt, boolean[] echo) {
+ public String[] promptKeyboardInteractive(String destination, String name, String instruction, String[] prompt, boolean[] echo) {
// must return an empty array if password is null
if (configuration.getPassword() == null) {
return new String[0];
} else {
- return new String[]{configuration.getPassword()};
+ return new String[] {configuration.getPassword()};
}
}
@@ -356,10 +359,10 @@ public class SftpOperations implements RemoteFileOperations<SftpRemoteFile> {
// set the SO_TIMEOUT for the time after the connect phase
if (sftpConfig.getServerAliveInterval() == 0) {
if (configuration.getSoTimeout() > 0) {
- session.setTimeout(configuration.getSoTimeout());
+ session.setTimeout(configuration.getSoTimeout());
}
} else {
- LOG.debug("The Server Alive Internal is already set, the socket timeout won't be considered to avoid overidding the provided Server alive interval value");
+ LOG.debug("The Server Alive Internal is already set, the socket timeout won't be considered to avoid overidding the provided Server alive interval value");
}
// set proxy if configured
@@ -486,7 +489,7 @@ public class SftpOperations implements RemoteFileOperations<SftpRemoteFile> {
LOG.debug("Renaming file: {} to: {}", from, to);
try {
reconnectIfNecessary();
- //make use of the '/' separator because JSch expects this
+ // make use of the '/' separator because JSch expects this
// as the file separator even on Windows
to = FileUtil.compactPath(to, '/');
channel.rename(from, to);
@@ -511,7 +514,8 @@ public class SftpOperations implements RemoteFileOperations<SftpRemoteFile> {
channel.ls(directory);
success = true;
} catch (SftpException e) {
- // ignore, we could not change directory so try to create it instead
+ // ignore, we could not change directory so try to create it
+ // instead
}
if (!success) {
@@ -521,7 +525,8 @@ public class SftpOperations implements RemoteFileOperations<SftpRemoteFile> {
channel.mkdir(directory);
success = true;
} catch (SftpException e) {
- // we are here if the server side doesn't create intermediate folders
+ // we are here if the server side doesn't create
+ // intermediate folders
// so create the folder one by one
success = buildDirectoryChunks(directory);
}
@@ -582,8 +587,10 @@ public class SftpOperations implements RemoteFileOperations<SftpRemoteFile> {
return;
}
- // must compact path so SFTP server can traverse correctly, make use of the '/'
- // separator because JSch expects this as the file separator even on Windows
+ // must compact path so SFTP server can traverse correctly, make use of
+ // the '/'
+ // separator because JSch expects this as the file separator even on
+ // Windows
String before = path;
char separatorChar = '/';
path = FileUtil.compactPath(path, separatorChar);
@@ -597,8 +604,9 @@ public class SftpOperations implements RemoteFileOperations<SftpRemoteFile> {
return;
}
if (getCurrentDirectory().startsWith(path)) {
- // extract the path segment relative to the target path and make sure it keeps the preceding '/' for the regex op
- String p = getCurrentDirectory().substring(path.length() - (path.endsWith("/") ? 1 : 0));
+ // extract the path segment relative to the target path and make
+ // sure it keeps the preceding '/' for the regex op
+ String p = getCurrentDirectory().substring(path.length() - (path.endsWith("/") ? 1 : 0));
if (p.length() == 0) {
return;
}
@@ -606,7 +614,8 @@ public class SftpOperations implements RemoteFileOperations<SftpRemoteFile> {
path = UP_DIR_PATTERN.matcher(p).replaceAll("/..").substring(1);
}
- // if it starts with the root path then a little special handling for that
+ // if it starts with the root path then a little special handling for
+ // that
if (FileUtil.hasLeadingSeparator(path)) {
// change to root path
if (!FileUtil.isWindows()) {
@@ -677,7 +686,7 @@ public class SftpOperations implements RemoteFileOperations<SftpRemoteFile> {
// can return either null or an empty list depending on FTP servers
if (files != null) {
for (Object file : files) {
- list.add(new SftpRemoteFileJCraft((ChannelSftp.LsEntry) file));
+ list.add(new SftpRemoteFileJCraft((ChannelSftp.LsEntry)file));
}
}
return list;
@@ -689,7 +698,8 @@ public class SftpOperations implements RemoteFileOperations<SftpRemoteFile> {
public synchronized boolean retrieveFile(String name, Exchange exchange, long size) throws GenericFileOperationFailedException {
LOG.trace("retrieveFile({})", name);
if (ObjectHelper.isNotEmpty(endpoint.getLocalWorkDirectory())) {
- // local work directory is configured so we should store file content as files in this local directory
+ // local work directory is configured so we should store file
+ // content as files in this local directory
return retrieveFileToFileInLocalWorkDirectory(name, exchange);
} else {
// store file content directory as stream on the body
@@ -713,8 +723,7 @@ public class SftpOperations implements RemoteFileOperations<SftpRemoteFile> {
private boolean retrieveFileToStreamInBody(String name, Exchange exchange) throws GenericFileOperationFailedException {
String currentDir = null;
try {
- GenericFile<ChannelSftp.LsEntry> target =
- (GenericFile<ChannelSftp.LsEntry>) exchange.getProperty(FileComponent.FILE_EXCHANGE_FILE);
+ 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;
@@ -723,12 +732,14 @@ public class SftpOperations implements RemoteFileOperations<SftpRemoteFile> {
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)
+ // (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
+ // remote name is now only the file name as we just changed
+ // directory
remoteName = FileUtil.stripPath(name);
}
@@ -764,8 +775,7 @@ public class SftpOperations implements RemoteFileOperations<SftpRemoteFile> {
File temp;
File local = new File(endpoint.getLocalWorkDirectory());
OutputStream os;
- GenericFile<ChannelSftp.LsEntry> file =
- (GenericFile<ChannelSftp.LsEntry>) exchange.getProperty(FileComponent.FILE_EXCHANGE_FILE);
+ GenericFile<ChannelSftp.LsEntry> file = (GenericFile<ChannelSftp.LsEntry>)exchange.getProperty(FileComponent.FILE_EXCHANGE_FILE);
ObjectHelper.notNull(file, "Exchange should have the " + FileComponent.FILE_EXCHANGE_FILE + " set");
try {
// use relative filename in local work directory
@@ -794,7 +804,8 @@ public class SftpOperations implements RemoteFileOperations<SftpRemoteFile> {
throw new GenericFileOperationFailedException("Cannot create new local work file: " + temp);
}
- // store content as a file in the local work directory in the temp handle
+ // 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
@@ -813,12 +824,14 @@ public class SftpOperations implements RemoteFileOperations<SftpRemoteFile> {
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)
+ // (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
+ // remote name is now only the file name as we just changed
+ // directory
remoteName = FileUtil.stripPath(name);
}
@@ -826,7 +839,8 @@ public class SftpOperations implements RemoteFileOperations<SftpRemoteFile> {
} catch (SftpException 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
+ // 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);
@@ -845,7 +859,8 @@ public class SftpOperations implements RemoteFileOperations<SftpRemoteFile> {
LOG.debug("Retrieve file to local work file result: true");
- // operation went okay so rename temp to local after we have retrieved the data
+ // operation went okay so rename temp to local after we have retrieved
+ // the data
LOG.trace("Renaming local in progress file from: {} to: {}", temp, local);
try {
if (!FileUtil.renameFile(temp, local, false)) {
@@ -871,13 +886,15 @@ public class SftpOperations implements RemoteFileOperations<SftpRemoteFile> {
try {
if (path != null && endpoint.getConfiguration().isStepwise()) {
- // must remember current dir so we stay in that directory after the write
+ // must remember current dir so we stay in that directory after
+ // the write
currentDir = getCurrentDirectory();
// change to path of name
changeCurrentDirectory(path);
- // the target name should be without path, as we have changed directory
+ // the target name should be without path, as we have changed
+ // directory
targetName = FileUtil.stripPath(name);
}
@@ -897,9 +914,7 @@ public class SftpOperations implements RemoteFileOperations<SftpRemoteFile> {
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) {
+ 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
@@ -918,7 +933,7 @@ public class SftpOperations implements RemoteFileOperations<SftpRemoteFile> {
// 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[]{});
+ is = new ByteArrayInputStream(new byte[] {});
} else {
throw new GenericFileOperationFailedException("Cannot write null body to file: " + name);
}
@@ -949,8 +964,7 @@ public class SftpOperations implements RemoteFileOperations<SftpRemoteFile> {
}
if (LOG.isDebugEnabled()) {
long time = watch.taken();
- LOG.debug("Took {} ({} millis) to store file: {} and FTP client returned: true",
- new Object[]{TimeUtils.printDuration(time), time, targetName});
+ LOG.debug("Took {} ({} millis) to store file: {} and FTP client returned: true", new Object[] {TimeUtils.printDuration(time), time, targetName});
}
// after storing file, we may set chmod on the file
@@ -974,7 +988,7 @@ public class SftpOperations implements RemoteFileOperations<SftpRemoteFile> {
IOHelper.close(is, "store: " + name, LOG);
}
}
-
+
public synchronized boolean existsFile(String name) throws GenericFileOperationFailedException {
LOG.trace("existsFile({})", name);
if (endpoint.isFastExistsCheck()) {
@@ -996,7 +1010,7 @@ public class SftpOperations implements RemoteFileOperations<SftpRemoteFile> {
return false;
}
for (Object file : files) {
- ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry) file;
+ ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry)file;
String existing = entry.getFilename();
LOG.trace("Existing file: {}, target file: {}", existing, name);
existing = FileUtil.stripPath(existing);
@@ -1006,7 +1020,8 @@ public class SftpOperations implements RemoteFileOperations<SftpRemoteFile> {
}
return false;
} catch (SftpException e) {
- // or an exception can be thrown with id 2 which means file does not exists
+ // or an exception can be thrown with id 2 which means file does not
+ // exists
if (ChannelSftp.SSH_FX_NO_SUCH_FILE == e.id) {
return false;
}
@@ -1025,7 +1040,8 @@ public class SftpOperations implements RemoteFileOperations<SftpRemoteFile> {
}
return files.size() >= 1;
} catch (SftpException e) {
- // or an exception can be thrown with id 2 which means file does not exists
+ // or an exception can be thrown with id 2 which means file does not
+ // exists
if (ChannelSftp.SSH_FX_NO_SUCH_FILE == e.id) {
return false;
}
@@ -1054,10 +1070,9 @@ public class SftpOperations implements RemoteFileOperations<SftpRemoteFile> {
}
/*
- * adapted from com.jcraft.jsch.Util.createSocket(String, int, int)
- *
- * added possibility to specify the address of the local network interface, against the
- * connection should bind
+ * adapted from com.jcraft.jsch.Util.createSocket(String, int, int) added
+ * possibility to specify the address of the local network interface,
+ * against the connection should bind
*/
static Socket createSocketUtil(final String host, final int port, final String bindAddress, final int timeout) {
Socket socket = null;
@@ -1086,7 +1101,8 @@ public class SftpOperations implements RemoteFileOperations<SftpRemoteFile> {
if (sockp[0] != null && sockp[0].isConnected()) {
try {
sockp[0].close();
- } catch (Exception eee) { }
+ } catch (Exception eee) {
+ }
}
sockp[0] = null;
}