SFTPConfig fixed

This commit is contained in:
Ivan Nikolaev-Axenov 2024-06-10 10:25:21 +03:00
parent e2d043e322
commit c0c6a9813a
4 changed files with 50 additions and 20 deletions

View file

@ -13,12 +13,12 @@ import org.springframework.context.annotation.Profile;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.integration.annotation.Gateway;
import org.springframework.integration.annotation.MessagingGateway;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.file.remote.gateway.AbstractRemoteFileOutboundGateway;
import org.springframework.integration.file.remote.session.CachingSessionFactory;
import org.springframework.integration.file.remote.session.SessionFactory;
import org.springframework.integration.gateway.AnnotationGatewayProxyFactoryBean;
import org.springframework.integration.sftp.gateway.SftpOutboundGateway;
import org.springframework.integration.sftp.outbound.SftpMessageHandler;
import org.springframework.integration.sftp.session.DefaultSftpSessionFactory;
@ -29,8 +29,8 @@ import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Payload;
import ru.spcex.clearing.xml.exporter.config.settings.ExportXMLServiceSettings;
@Configuration
@Profile("!test")
@Configuration
public class SFTPConfig {
protected Logger log = LoggerFactory.getLogger(getClass());
protected static final ExpressionParser EXPRESSION_PARSER = new SpelExpressionParser();
@ -55,14 +55,19 @@ public class SFTPConfig {
handler.setFileNameGenerator(message -> {
if (message.getPayload() instanceof File) {
return ((File) message.getPayload()).getName();
}else {
} else {
throw new IllegalArgumentException("File must expected as payload.");
}
});
return handler;
}
@MessagingGateway
// https://stackoverflow.com/questions/53655208/profile-doesnt-work-with-messaging-gateway
@Bean("xmlGateway")
public AnnotationGatewayProxyFactoryBean xmlGateway() {
return new AnnotationGatewayProxyFactoryBean(XmlGateway.class);
}
public interface XmlGateway {
@Gateway(requestChannel = "toSftpChannel")
void sendToSftp(@Payload File file, @Header("path") String path);

View file

@ -1,19 +1,18 @@
package ru.spcex.clearing.xml.exporter.config;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.integration.sftp.session.SftpFileInfo;
@Configuration
@Profile("test")
public class SFTPTestConfig {
@Bean
@Qualifier("mockSFTP")
public SFTPConfig.XmlGateway xmlGateway(){
@Configuration
public class SFTPMockConfig {
@Bean("xmlGateway")
public SFTPConfig.XmlGateway dbfGateway(){
return new XGateway();
}
@ -26,8 +25,7 @@ public class SFTPTestConfig {
@Override
public List<SftpFileInfo> listFiles(String dir) {
return null;
return new ArrayList<>();
}
}
}

View file

@ -9,30 +9,27 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.integration.annotation.Gateway;
import org.springframework.integration.annotation.MessagingGateway;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.file.remote.gateway.AbstractRemoteFileOutboundGateway;
import org.springframework.integration.file.remote.session.CachingSessionFactory;
import org.springframework.integration.file.remote.session.SessionFactory;
import org.springframework.integration.gateway.AnnotationGatewayProxyFactoryBean;
import org.springframework.integration.sftp.gateway.SftpOutboundGateway;
import org.springframework.integration.sftp.session.DefaultSftpSessionFactory;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;
import ru.spcex.clearing.xml.importer.config.settings.ImportXMLServiceSettings;
@Profile("!test")
@Configuration
public class SFTPConfig {
private final Logger log = LoggerFactory.getLogger(getClass());
private final ImportXMLServiceSettings settings;
public SFTPConfig(ImportXMLServiceSettings settings) {
this.settings = settings;
}
@Bean
public SessionFactory<ChannelSftp.LsEntry> sftpSessionFactory() {
public SessionFactory<ChannelSftp.LsEntry> sftpSessionFactory(ImportXMLServiceSettings settings) {
DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
factory.setHost(settings.getStore().getSftpIn().getServerIp());
factory.setPort(settings.getStore().getSftpIn().getServerPort());
@ -42,7 +39,12 @@ public class SFTPConfig {
return new CachingSessionFactory<>(factory);
}
@MessagingGateway
// https://stackoverflow.com/questions/53655208/profile-doesnt-work-with-messaging-gateway
@Bean("xmlGateway")
public AnnotationGatewayProxyFactoryBean xmlGateway() {
return new AnnotationGatewayProxyFactoryBean(XmlGateway.class);
}
public interface XmlGateway {
@Gateway(requestChannel = "listSftpChannel")
List<File> listFiles(String dir);

View file

@ -0,0 +1,25 @@
package ru.spcex.clearing.xml.importer.config;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Profile("test")
@Configuration
public class SFTPMockConfig {
@Bean("xmlGateway")
public SFTPConfig.XmlGateway xmlGateway(){
return new XGateway();
}
public static class XGateway implements SFTPConfig.XmlGateway{
@Override
public List<File> listFiles(String dir) {
return new ArrayList<>();
}
}
}