Search code examples
spring-integrationspring-integration-dslspring-integration-sftp

Rename the file after streaming the data from remote directory


I am reading the file using below command

.handle(Sftp.outboundGateway(sftpSessionFactory(), GET,"payload.remoteDirectory + payload.filename").options(STREAM))

Once the data is stream I have an advice for handling the success and failure. I want to rename the file in case of success, I am facing issue while renaming the remote file. I want to rename the file and do error handling after that using similar advice.

.handle(service(), e -> e.advice(after()))

 @Bean
    public ExpressionEvaluatingRequestHandlerAdvice after() {
        ExpressionEvaluatingRequestHandlerAdvice advice = new ExpressionEvaluatingRequestHandlerAdvice();
        advice.setSuccessChannelName("success.input");
        advice.setOnSuccessExpressionString("payload + ' was successful'");
        advice.setFailureChannelName("failure.input");
        advice.setOnFailureExpressionString("payload + ' was bad, with reason: ' + #exception.cause.message");
        advice.setTrapException(true);
        return advice;
    }



@Bean
    public IntegrationFlow success() {
        return f -> f.handle(Sftp.outboundGateway(sftpSessionFactory(), MV, "payload").renameExpression("headers[file_renameTo]='gileName_read'")).log();
    }

Solution

  • I also worked on the kind-off same flow. I hope below code might help you.

    .handle(service(), e -> e.advice(after()))
                    .enrichHeaders(h -> h
                            .headerExpression(FileHeaders.RENAME_TO, "headers[file_remoteDirectory]+'archive/' + headers[file_remoteFile]")
                            .headerExpression(FileHeaders.REMOTE_FILE, "headers[file_remoteFile]")
                            .header(FileHeaders.REMOTE_DIRECTORY, "headers[file_remoteDirectory]"))
                    .handle(Sftp.outboundGateway(sftpSessionFactory(), MV, "headers[file_remoteDirectory]+headers[file_remoteFile]").renameExpression("headers['file_renameTo']"))
                    .get();