INT-4046: Add FtpRemoteFileTemplate.ExistsMode

JIRA: https://jira.spring.io/browse/INT-4046

Since not all FTP servers provide proper `STAT` command implementation,
plus the `NLIST` doesn't work properly for directories cases, introduce the `FtpRemoteFileTemplate.ExistsMode`
to let:
* to perform `STAT` by default (previous) behavior;
* to switch to `NLIST` for `FtpRemoteFileTemplate` internal use;
* perform the full `NLIST` and `FTPClient.changeWorkingDirectory()` algorithm if needed.

* Improve (S)Ftp components to use proper `RemoteFileTemplate` for internal instantiation
* Introduce `FtpMessageHandler` to wrap `FtpRemoteFileTemplate` with the proper `NLIST` `ExistsMode`
* Cover `NLIST` switching from the `FtpOutboundChannelAdapterParser` and `FtpOutboundGatewayParser`
* Document the `FtpRemoteFileTemplate.ExistsMode`

* Fix typo in the recently introduced `RemoteFileOperations.getSession()` method name
* Add JavaDoc to `Session.exists()`
* Add `NLIST` support for the `FtpSession.exists()` to meet the API requirements

**Cherry-pick to 4.2.x and 4.1.x**

Addressing PR comments

Doc Polishing

Conflicts:
	spring-integration-file/src/main/java/org/springframework/integration/file/remote/AbstractRemoteFileStreamingMessageSource.java
	spring-integration-file/src/main/java/org/springframework/integration/file/remote/RemoteFileOperations.java
	spring-integration-file/src/main/java/org/springframework/integration/file/remote/RemoteFileTemplate.java
	spring-integration-ftp/src/main/java/org/springframework/integration/ftp/session/FtpSession.java
	spring-integration-ftp/src/test/java/org/springframework/integration/ftp/config/FtpOutboundChannelAdapterParserTests.java
	spring-integration-sftp/src/main/java/org/springframework/integration/sftp/gateway/SftpOutboundGateway.java
	spring-integration-sftp/src/main/java/org/springframework/integration/sftp/outbound/SftpMessageHandler.java
	spring-integration-sftp/src/main/java/org/springframework/integration/sftp/outbound/package-info.java
This commit is contained in:
Artem Bilan
2016-06-06 12:25:26 -04:00
parent f6d6fc9e4e
commit 80e1a56653
23 changed files with 327 additions and 71 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@ package org.springframework.integration.sftp.config;
import org.springframework.integration.file.config.RemoteFileOutboundChannelAdapterParser;
import org.springframework.integration.file.remote.RemoteFileOperations;
import org.springframework.integration.sftp.outbound.SftpMessageHandler;
import org.springframework.integration.sftp.session.SftpRemoteFileTemplate;
/**
@@ -28,6 +29,12 @@ import org.springframework.integration.sftp.session.SftpRemoteFileTemplate;
*/
public class SftpOutboundChannelAdapterParser extends RemoteFileOutboundChannelAdapterParser {
@Override
protected Class<?> handlerClass() {
return SftpMessageHandler.class;
}
@Override
protected Class<? extends RemoteFileOperations<?>> getTemplateClass() {
return SftpRemoteFileTemplate.class;

View File

@@ -27,6 +27,7 @@ import org.springframework.integration.file.remote.RemoteFileTemplate;
import org.springframework.integration.file.remote.gateway.AbstractRemoteFileOutboundGateway;
import org.springframework.integration.file.remote.session.SessionFactory;
import org.springframework.integration.sftp.session.SftpFileInfo;
import org.springframework.integration.sftp.session.SftpRemoteFileTemplate;
import com.jcraft.jsch.ChannelSftp.LsEntry;
@@ -47,7 +48,7 @@ public class SftpOutboundGateway extends AbstractRemoteFileOutboundGateway<LsEnt
*/
public SftpOutboundGateway(SessionFactory<LsEntry> sessionFactory,
MessageSessionCallback<LsEntry, ?> messageSessionCallback) {
super(sessionFactory, messageSessionCallback);
this(new SftpRemoteFileTemplate(sessionFactory), messageSessionCallback);
}
/**
@@ -69,7 +70,7 @@ public class SftpOutboundGateway extends AbstractRemoteFileOutboundGateway<LsEnt
* @param expression the filename expression.
*/
public SftpOutboundGateway(SessionFactory<LsEntry> sessionFactory, String command, String expression) {
super(sessionFactory, command, expression);
this(new SftpRemoteFileTemplate(sessionFactory), command, expression);
}
/**

View File

@@ -0,0 +1,64 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.sftp.outbound;
import org.springframework.integration.file.remote.handler.FileTransferringMessageHandler;
import org.springframework.integration.file.remote.session.SessionFactory;
import org.springframework.integration.file.support.FileExistsMode;
import org.springframework.integration.sftp.session.SftpRemoteFileTemplate;
import com.jcraft.jsch.ChannelSftp.LsEntry;
/**
* Subclass of {@link FileTransferringMessageHandler} for SFTP.
*
* @author Gary Russell
* @since 4.3
*
*/
public class SftpMessageHandler extends FileTransferringMessageHandler<LsEntry> {
/**
* @param remoteFileTemplate the template.
* @see FileTransferringMessageHandler#FileTransferringMessageHandler
* (org.springframework.integration.file.remote.RemoteFileTemplate)
*/
public SftpMessageHandler(SftpRemoteFileTemplate remoteFileTemplate) {
super(remoteFileTemplate);
}
/**
*
* @param remoteFileTemplate the template.
* @param mode the file exists mode.
* @see FileTransferringMessageHandler#FileTransferringMessageHandler
* (org.springframework.integration.file.remote.RemoteFileTemplate, FileExistsMode)
*/
public SftpMessageHandler(SftpRemoteFileTemplate remoteFileTemplate, FileExistsMode mode) {
super(remoteFileTemplate, mode);
}
/**
* @param sessionFactory the session factory.
* @see FileTransferringMessageHandler#FileTransferringMessageHandler
* (SessionFactory)
*/
public SftpMessageHandler(SessionFactory<LsEntry> sessionFactory) {
this(new SftpRemoteFileTemplate(sessionFactory));
}
}

View File

@@ -0,0 +1,4 @@
/**
* Provides classes for the SFTP outbound channel adapter.
*/
package org.springframework.integration.sftp.outbound;

View File

@@ -25,7 +25,6 @@ import org.springframework.integration.file.remote.session.SessionFactory;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.ChannelSftp.LsEntry;
import com.jcraft.jsch.SftpException;
/**
* SFTP version of {@code RemoteFileTemplate} providing type-safe access to
@@ -57,22 +56,4 @@ public class SftpRemoteFileTemplate extends RemoteFileTemplate<LsEntry> {
});
}
@Override
public boolean exists(final String path) {
return executeWithClient(new ClientCallback<ChannelSftp, Boolean>() {
@Override
public Boolean doWithClient(ChannelSftp client) {
try {
return client.stat(path) != null;
}
catch (SftpException e) {
return false;
}
}
});
}
}