Unable to delete payload after pushing it to remote server using ExpressionEvaluatingRequestHandlerAdvice

277 Views Asked by At

I'm trying to delete the source file that has been pushed to remote server using the ExpressionEvaluatingRequestHandlerAdvice:

    @Bean
    public Advice expressionAdvice(GenericEndpointSpec<FileTransferringMessageHandler<ChannelSftp.LsEntry>> c) {
        ExpressionEvaluatingRequestHandlerAdvice advice = new ExpressionEvaluatingRequestHandlerAdvice();
        advice.setOnSuccessExpressionString("payload.delete()");
        advice.setOnFailureExpressionString("payload + ' failed to upload'");
        advice.setTrapException(true);
        return advice;
    }

in the code below:

    @Bean
    public IntegrationFlow integrationFlow() {
        return IntegrationFlows.from(fileReader(), spec -> spec.poller(Pollers.fixedDelay(1000)))
                .transform(transformer, "transform")
                .handle(
                        Sftp.outboundAdapter(sftpSessionFactory, FileExistsMode.REPLACE)
                        .remoteDirectory(sftpRemoteDirectory), 
                        c -> c.advice(expressionAdvice(c))
                )
                .get();
    }

    @Bean
    public FileReadingMessageSource fileReader() {
        FileReadingMessageSource source = new FileReadingMessageSource();
        source.setDirectory(new File(localSourceDirectory));
        return source;
    }

And my Transformer class:


@Component
public class Transformer {

    public String transform(String filePath) throws IOException {
        String content = new String(Files.readAllBytes(Paths.get(filePath)));
        return "Transformed content: " + content;
    }

}

However, when i check the source directory, the file is still there. What am I missing here? Help please.

I'm using Spring Integration 5.2.4.

Thanks in advance!


Here's the working code based on @ArtemBilan's answer:


    @Bean
    public Advice expressionAdvice(GenericEndpointSpec<FileTransferringMessageHandler<ChannelSftp.LsEntry>> c) {
        ExpressionEvaluatingRequestHandlerAdvice advice = new ExpressionEvaluatingRequestHandlerAdvice();
        // advice.setOnSuccessExpressionString("payload.delete()");
        advice.setOnSuccessExpressionString("headers[file_originalFile].delete()");
        advice.setOnFailureExpressionString("payload + ' failed to upload'");
        advice.setTrapException(true);
        return advice;
    }

1

There are 1 best solutions below

1
On BEST ANSWER

One more time:

public String transform(String filePath) throws IOException {
    String content = new String(Files.readAllBytes(Paths.get(filePath)));
    return "Transformed content: " + content;
}

So, your .transform(transformer, "transform") produces a String not a File. This is ok for the Sftp.outboundAdapter() because it is able to transform that string into a remote file content. But what is that advice.setOnSuccessExpressionString("payload.delete()"); should do for the String object? I believe you want to delete a file, so you need to have exactly File object for that advice to work properly.

The FileReadingMessageSource populates for us a FileHeaders.ORIGINAL_FILE header. So, you can change your expression for removal into this:

headers[file_originalFile].delete()

And you should be OK.