2
votes

I'm having an issue with the SFTP outbound gateway using DSL. I want to use a outbound gateway to send a file, then continue my flow. The problem is that I have an exception telling me:

IllegalArgumentException: 'remoteDirectoryExpression' is required

I saw that I can use a RemoteFileTemplate where I can set the sftp session factory plus the remote directory information, but the directory I wan't is defined in my flow by the code put in the header just before the launch of the batch.

    @Bean
public IntegrationFlow orderServiceFlow() {
    return f -> f
            .handleWithAdapter(h -> h.httpGateway("myUrl")
                    .httpMethod(HttpMethod.GET)
                    .expectedResponseType(List.class)
            )
            .split()
            .channel(batchLaunchChannel());
}

@Bean
public DirectChannel batchLaunchChannel() {
    return MessageChannels.direct("batchLaunchChannel").get();
}

@Bean
public IntegrationFlow batchLaunchFlow() {
    return IntegrationFlows.from(batchLaunchChannel())
            .enrichHeaders(h -> h
                    .headerExpression("oiCode", "payload")
            )
            .transform((GenericTransformer<String, JobLaunchRequest>) message -> {
                JobParameters jobParameters = new JobParametersBuilder()
                        .addDate("exec_date", new Date())
                        .addString("oiCode", message)
                        .toJobParameters();
                return new JobLaunchRequest(orderServiceJob, jobParameters);
            })
            .handle(new JobLaunchingMessageHandler(jobLauncher))

            .enrichHeaders(h -> h
                    .headerExpression("jobExecution", "payload")
            )
            .handle((p, h) -> {
                //Logic to retreive file...
                return new File("");
            })
            .handle(Sftp.outboundGateway(sftpSessionFactory,
                    AbstractRemoteFileOutboundGateway.Command.PUT,
                    "payload")
            )
            .get();
}

I don't see how I can tell my outbound gateway which will be the directory depending what is in my header.

2

2 Answers

2
votes

The Sftp.outboundGateway() has an overloaded version with the RemoteFileTemplate. So, you need to instantiate SftpRemoteFileTemplate bean and configure its:

/**
 * Set the remote directory expression used to determine the remote directory to which
 * files will be sent.
 * @param remoteDirectoryExpression the remote directory expression.
 */
public void setRemoteDirectoryExpression(Expression remoteDirectoryExpression) {

This one can be like FunctionExpression:

setRemoteDirectoryExpression(m -> m.getHeaders().get("remoteDireHeader"))
1
votes

Before I got an answer, I came with this solution but I'm not sure it's a good one.

// flow //
 .handle((p, h) -> {
    //Logic to retreive file...
    return new File("");
})
.handle(
        Sftp.outboundGateway(
                remoteFileTemplate(new SpelExpressionParser().parseExpression("headers['oiCode']")),
                AbstractRemoteFileOutboundGateway.Command.PUT,
                "payload")
)
.handle(// next steps //)
.get();

public RemoteFileTemplate remoteFileTemplate(Expression directory) throws Exception {
        RemoteFileTemplate template = new SftpRemoteFileTemplate(sftpSessionFactory);
        template.setRemoteDirectoryExpression(directory);
        template.setAutoCreateDirectory(true);
        template.afterPropertiesSet();
        return template;
}

But this provoke a warn because of exception thrown by the ExpresionUtils java.lang.RuntimeException: No beanFactory