0

I use DSL configuration and spring.

My route looks like this:

@Component
public class UploadRoutesDefinition extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        from("seda:rest_upload")
            .process(new Processor() {
                @Override
                public void process(Exchange exchange) {
                    ...
                    String sftPathForAdditionalFile = ....
                    String AdditionalFileContent = ...
                    ...
                }
             ).to(String.format(SFTP_BASE_URL,systemSettingsService.getSystemSettings().getSftpUserName(),
                    systemSettingsService.getSystemSettings().getSftpHost(),
                    systemSettingsService.getSystemSettings().getSftpPort(),
                    systemSettingsService.getSystemSettings().getSftpAttachmentsPushFailedPath(),
                    systemSettingsService.getSystemSettings().getSftpPassword()))

It allows me to read file from seda:rest_upload and then move it to the sftp folder.

I want to move one more file additionally. I know path and content inside process method.

How can I achieve it?

UPDATE

my current code;

.process(new Processor() {
                @Override
                public void process(Exchange exchange) throws Exception {
                    exchange.getIn().setHeader("CamelFilename", "ololo.txt");
                    exchange.getIn().setBody(exchange.getProperty(PUSH_ERROR_MESSAGE, String.class).getBytes());
                    exchange.getIn().setHeader("destFilePath", sftpErrorFileTextPath);
                }
            })
            .to(String.format(SFTP_BASE_URL + "&fileExist=Append",
                    systemSettingsService.getSystemSettings().getSftpUserName(),
                    systemSettingsService.getSystemSettings().getSftpHost(),
                    systemSettingsService.getSystemSettings().getSftpPort(),
                    "${header.destFilePath}",
                    systemSettingsService.getSystemSettings().getSftpPassword()))
            .end();
pvpkiran
  • 25,582
  • 8
  • 87
  • 134
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
  • you can save the file coming from rest_upload and your addition file in a location. And then create a route which listenes to this directory and uploads to ftp – pvpkiran Mar 23 '18 at 09:38
  • @pvpkiran yes, this will work, but it is not very convenient – gstackoverflow Mar 23 '18 at 09:49
  • I don't think you can achieve this any other way. The reason being, what is being sent to the ftp is what is there in the exchange body(set from your previous step i.e. process(..) in your case). And you cannot have multiple bodies. – pvpkiran Mar 23 '18 at 10:00
  • @pvpkiran but anyway I can't save file using camel. I need to do ot manually – gstackoverflow Mar 23 '18 at 12:16
  • The path to this additional file does it change based on what you get from rest_upload or is it independent of it? – pvpkiran Mar 23 '18 at 12:18
  • @pvpkiran - you are correct, path changes depends on whant I got from rest_upload – gstackoverflow Mar 23 '18 at 12:19

1 Answers1

1

Here is one way to do it,

@Override
public void configure() throws Exception {


from("seda:rest_upload")
 .multicast()
 .to("direct::sendMainFile")
 .to("direct:sendAnotherFile") // You could also use seda:
 .end();



from("direct:sendMainFile")
.process(new Processor() {
 @Override
      public void process(Exchange exchange) throws Exception {
        String filepath = <calculate filepath>;
        String completeFilePath = systemSettingsService.getSystemSettings().getSftpAttachmentsPushFailedPath() + filepath
        exchange.getIn().setHeader("destFilePath", completeFilePath);
         exchange.getIn().setHeader("CamelFileName", fileNameforMainFile);
    }
 }.toD(sftpRoute()) // It is toD not to


from("direct:sendAnotherfile")
.process(new Processor() {
          @Override
          public void process(Exchange exchange) throws Exception {
            // Here you have the same body which was sent from rest_upload
            // extract the info from exchange.getIn().getBody() 
            // Read the file and set it as exchange body

            String fileContent = <Your logic to read file>
            exchange.getIn().setBody(fileContent);
            exchange.getIn().setHeader("CamelFileName", fileNameforYourAdditionalFile)

            String completeFilePath = systemSettingsService.getSystemSettings().getSftpAttachmentsPushFailedPath() + filepath
            exchange.getIn().setHeader("destFilePath", completeFilePath);
          }
        })
 .toD(sftpRoute());  // It is toD not to

}    

private String sftpRoute() {

 return String.format(SFTP_BASE_URL,systemSettingsService.getSystemSettings().getSftpUserName(),               
  systemSettingsService.getSystemSettings().getSftpHost(),
  systemSettingsService.getSystemSettings().getSftpPort(),
                "${header.destFilePath}",                  
  systemSettingsService.getSystemSettings().getSftpPassword())
  }
 }
pvpkiran
  • 25,582
  • 8
  • 87
  • 134
  • On sftp I have folder */output*. Lets say I need to put file with name *file1.txt*. Its parent folder calculates depends on loginc in process. So I need to put file according the path *output/some_calculated_name/file1.txt* Looks like I can't use your approach for my task. Actually I need to put additional file near the main file – gstackoverflow Mar 23 '18 at 12:55
  • Could not understand completely what you are trying to say. when you have the same body you can use the same logic to calculate which directory to put. It would be better if you add more details in your question with an example – pvpkiran Mar 23 '18 at 13:00
  • I don't understand how does your code allow to use directory name calculated inside the process method – gstackoverflow Mar 23 '18 at 13:08
  • You mean the destination directory on ftp server? What is this `systemSettingsService.getSystemSettings().getSftpAttachmentsPushFailedPath(),` ? Can you show what you have in SFTP_BASE_URL, if you want both the files to be on same path on ftp, you can save the path and use it in sftpRoute() function – pvpkiran Mar 23 '18 at 13:12
  • Yes, I am about detination on sftp. final destination should be **systemSettingsService.getSystemSettings().getSftpAttachmentsPushFailedPath()/SOME_CALCULATED_FOLDER_NAME/FILE_NAME** – gstackoverflow Mar 23 '18 at 13:14
  • Edited my answer. check it out – pvpkiran Mar 23 '18 at 13:38
  • for this camel created folder with name */{$header.destFilePath}* – gstackoverflow Mar 23 '18 at 14:14
  • sorry, it has to be ${header.destFilePath} – pvpkiran Mar 23 '18 at 14:15
  • It is `toD` not `to`. I have specifically added a comment in my code. – pvpkiran Mar 23 '18 at 14:33
  • looks like secrete APi. Thank you for uour help. Do you recommend to use *toD* everywhere instead of *to* ? – gstackoverflow Mar 23 '18 at 14:39
  • Yes, You could. But it makes sense only when you have dynamic variables in your `to(...)`. For example in this case, you need to inform camel that it needs to evaluate `${header.destFilePath}` dynamically at runtime. Hence it doesn't work with just `to`. If you don't have any such evaluation to be done in your route, even `to` will work – pvpkiran Mar 23 '18 at 14:43