0

I do have multiple remote servers configured in our application based on the customers.

When report is ready for that particular customer I should connect to remote server of that particular customer and I should upload the report file.

There are two options in front of me a) using Spring's default session factory (getting session factory for every file upload)

    public SessionFactory<LsEntry> sftpSessionFactory() {
        DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
        factory.setHost(sftpHost);
        factory.setPort(sftpPort);
        factory.setUser(sftpUser);
        if (sftpPrivateKey != null) {
            factory.setPrivateKey(sftpPrivateKey);
            factory.setPrivateKeyPassphrase(sftpPrivateKeyPassphrase);
        } else {
            factory.setPassword(sftpPasword);
        }
        factory.setAllowUnknownKeys(true);
        return factory;
    }

    @Bean
    @ServiceActivator(inputChannel = "toSftpChannel")
    public MessageHandler handler() {
        SftpMessageHandler handler = new SftpMessageHandler(sftpSessionFactory());
        handler.setRemoteDirectoryExpression(new LiteralExpression(sftpRemoteDirectory));
        handler.setFileNameGenerator(new FileNameGenerator() {
            @Override
            public String generateFileName(Message<?> message) {
                if (message.getPayload() instanceof File) {
                    return ((File) message.getPayload()).getName();
                } else {
                    throw new IllegalArgumentException("File expected as payload.");
                }
            }
        });
        return handler;
    }

b) using plain sftp connection

 try {
        JSch jsch = new JSch();
        Session session = jsch.getSession("sftpuser", "sftphost");
        session.setPassword("sftppassword");
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        config.put("PreferredAuthentications",
                "publickey,keyboard-interactive,password");

        session.setConfig(config);
        session.connect();
        Channel channel = session.openChannel("sftp");
        channel.connect();
        System.out.println("sftp channel opened and connected.");
        ChannelSftp channelSftp = (ChannelSftp) channel;

which one is more suitable in this use case ?

1 Answers1

0

The second option is utilized underlying by the first option. So In your case if you are using spring framework, then use the first option. If you are not using spring then use the second option.

With Spring there are tools that you can utilize with your use case.