0

I'm sending a new payload header to a success channel using an advice and in the success channel I'm sending the file_originalFile to FTP server using outboundAdapter but I'm facing an issue where the success channel is sending the file as .msg and in side the .msg I can see a text showing the name of the file_originalFile while I want to send foo.csv it self to the ftp server.

Any possible solution for that?

Here is the code of the application

public IntegrationFlow localToFtpFlow(Branch myBranch) {

    return IntegrationFlows.from(Files.inboundAdapter(new File(myBranch.getBranchCode()))
                    .filter(new ChainFileListFilter<File>()
                            .addFilter(new RegexPatternFileListFilter("final" + myBranch.getBranchCode() + ".csv"))
                            .addFilter(new FileSystemPersistentAcceptOnceFileListFilter(metadataStore(dataSource), "foo"))),//FileSystemPersistentAcceptOnceFileListFilter
            e -> e.poller(Pollers.fixedDelay(10_000)))
            .enrichHeaders(h ->h.header("file_originalFile", "new java.io.File('/BEY/FEFOexport" + myBranch.getBranchCode() + ".csv')",true))

            .transform(p -> {
                LOG1.info("Sending file " + p + " to FTP branch " + myBranch.getBranchCode());

                return p;
            })
            .log()
            .handle(Ftp.outboundAdapter(createNewFtpSessionFactory(myBranch), FileExistsMode.REPLACE)
                    .useTemporaryFileName(true)
                    .autoCreateDirectory(false)
                    .remoteDirectory(myBranch.getFolderPath()), e -> e.advice(expressionAdvice()))

            .get();
}

/*
* Creating the advice for routing the payload of the outbound message on different expressions (success, failure)
*
* */

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

/*
* Creating FTP connection based on the branch ftp data entered.
* */

public DefaultFtpSessionFactory createNewFtpSessionFactory(Branch branch) {
    final DefaultFtpSessionFactory factory = new DefaultFtpSessionFactory();
    factory.setHost(branch.getHost());
    factory.setUsername(branch.getUsern());
    factory.setPort(branch.getFtpPort());
    factory.setPassword(branch.getPassword());
    return factory;
}

public DefaultFtpSessionFactory createNewFtpSessionFactory() {
    final DefaultFtpSessionFactory factory = new DefaultFtpSessionFactory();
    factory.setHost("xxx");
    factory.setUsername("xxxxx");
    factory.setPort(xx);
    factory.setPassword("xxxx");
    return factory;
}

/*
* Creating a metadata store to be used across the application flows to prevent reprocessing the file if it is already processed.
* This will save the new file in a metadata table in the DB with the state of the report, so when a new copy comes with different date it will be processed only.
* */
@Bean
public ConcurrentMetadataStore metadataStore(final DataSource dataSource) {
    return new JdbcMetadataStore(dataSource);
}

/*
* Success channel that will handle the AdviceMessage from the outbound adapter
*
* */

@Bean
public IntegrationFlow success(){
    return f -> f.transform("inputMessage.headers['file_originalFile']")
                 .handle(Ftp.outboundAdapter(createNewFtpSessionFactory(), FileExistsMode.REPLACE)
                 .useTemporaryFileName(true)
                 .autoCreateDirectory(true)
                 .remoteDirectory("/ftp/erbranch/EDMS/FEFO/History/"));
           //f.handle(System.out::println);
}

Debug:

2019-02-08 20:18:34.264 DEBUG 1368 --- [ask-scheduler-2] o.s.integration.channel.DirectChannel    : postSend (sent=true) on channel '1o.channel#3', message: GenericMessage [payload=BEY\finalBEY.csv, headers={file_originalFile=new java.io.File('/BEY/FEFOexportBEY.csv'), id=e1b48bf4-ba44-bdd0-23bc-e335e3c377a1, file_name=finalBEY.csv, file_relativePath=finalBEY.csv, timestamp=1549649899453}]
2019-02-08 20:18:34.267 DEBUG 1368 --- [ask-scheduler-2] o.s.integration.channel.DirectChannel    : postSend (sent=true) on channel '1o.channel#1', message: GenericMessage [payload=BEY\finalBEY.csv, headers={file_originalFile=new java.io.File('/BEY/FEFOexportBEY.csv'), id=0c599e08-a972-2ffc-cc9b-f45ff266ca98, file_name=finalBEY.csv, file_relativePath=finalBEY.csv, timestamp=1549649899439}]
2019-02-08 20:18:34.268 DEBUG 1368 --- [ask-scheduler-2] o.s.integration.channel.DirectChannel    : postSend (sent=true) on channel '1o.channel#0', message: GenericMessage [payload=BEY\finalBEY.csv, headers={file_originalFile=BEY\finalBEY.csv, id=14336975-2e33-1990-63e0-3182726b47fe, file_name=finalBEY.csv, file_relativePath=finalBEY.csv, timestamp=1549649899439}]
Elias Khattar
  • 163
  • 2
  • 19
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Jens Feb 07 '19 at 08:51

1 Answers1

0

.enrichHeaders(h ->h.header("file_originalFile","/someDirectory/FEFOexport" + myBranch.getBranchCode() + ".csv",true))

The header contains just the file name, not a File. When you send a String, it becomes the content of the remote file. In that case, the remote file name uses the FileHeaders.FILENAME by default, or <messageId>.msg if there is no such header.

Use

.enrichHeaders(h ->h.headerExpression("file_originalFile", "new java.io.File('/someDirectory/FEFOexport" + myBranch.getBranchCode() + ".csv')",true))
Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • I used this suggestion to the header and I can see that it did not overwrite the file_originalFile , it added the whole thing to the end of the inputMessage and included in it another file_originalFile, in this case the success channel is reading the first file_originalFile, I added what I'm getting now in the debug for the advice, how can I point the success change to read the new java.io.File that is in the header of the inputMessage?@Gary Russell – Elias Khattar Feb 08 '19 at 08:04
  • Sorry, I made a mistake in my suggestion; fixed. – Gary Russell Feb 08 '19 at 14:11
  • Not a problem, I have updated the code as per your above suggestion and tested it, it is sending the same way as the first trial .msg not the actual file, seem the new java.io.File did not make it through, I have passed the debugging as well for the Advice Message, any issue with the syntax?@Gary Russell – Elias Khattar Feb 08 '19 at 18:30
  • Sorry - one more bug; I didn't notice you were using `h.header`; it needs to be `h.headerExpression`. – Gary Russell Feb 08 '19 at 18:39
  • works like a charm...now to the next challenge ...thanks Gary @Gary Russell – Elias Khattar Feb 08 '19 at 19:08