0

I am following this spring integration example - https://github.com/iainporter/spring-file-poller

    @Bean
public IntegrationFlow writeToFile(@Qualifier("fileWritingMessageHandler") MessageHandler fileWritingMessageHandler) {
    return IntegrationFlows.from(ApplicationConfiguration.INBOUND_CHANNEL)
            .transform(m -> new StringBuilder((String)m).reverse().toString())
            .handle(fileWritingMessageHandler)
            .log(LoggingHandler.Level.INFO)
            .get();
}


@Bean (name = FILE_WRITING_MESSAGE_HANDLER)
public MessageHandler fileWritingMessageHandler(@Qualifier(OUTBOUND_FILENAME_GENERATOR) FileNameGenerator fileNameGenerator) {
    FileWritingMessageHandler handler = new FileWritingMessageHandler(inboundOutDirectory);
    handler.setAutoCreateDirectory(true);
    handler.setFileNameGenerator(fileNameGenerator);
    return handler;
}

Controller example

    @PostMapping(value ="/data/{id}")   
public String load( @RequestParam("jsonFile") MultipartFile jsonFile,               
       @PathVariable("id") Long id) throws JsonMappingException, JsonProcessingException{
//some business logic
        return "Controller is called";
    }

Instead of simple handling, I want to call a Rest endpoint that expects a file. i.e. calling a rest api in handler similar to fileWritingMessageHandler

https://github.com/spring-projects/spring-integration-samples/blob/261648bed136a076f76ed15b1017f5e5b6d8b9ae/intermediate/multipart-http/src/main/resources/META-INF/spring/integration/http-outbound-config.xml

How can I create Map

Map<String, Object> multipartMap = new HashMap<String, Object>();
multipartMap.put("jsonFile", ????);

and call a getway method like

HttpStatus postMultipartRequest(Map<String, Object> multipartRequest);
Kris Swat
  • 788
  • 1
  • 10
  • 39

1 Answers1

0

To send a multi-part request you need to have a payload as a Map<String, Object>. You can read files from a directory using FileReadingMessageSource and respective poller configuration: https://docs.spring.io/spring-integration/docs/current/reference/html/file.html#file-reading. This one emits messages with java.io.File as a payload. To create a Map for it you just need a simple transformer in Java DSL:

.<File, Map<String, File>>transform(file -> Collections.singletonMap("jsonFile", file))

and then you use standard .handle(Http.outboundChannelAdapter("/data/{id}").uriVariable("id", "headers.someId")): https://docs.spring.io/spring-integration/docs/current/reference/html/http.html#http-java-config

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
  • Thank you. how does the headers work ? I presume somewhere in the flow it should be set? – Kris Swat Sep 13 '22 at 19:54
  • Well, that was just a sample: you showed an `{id}` path variable in your code and I just assumed that you know what is that going to be and from where to take it. So, you say me what is that `{id}` and how it is supposed to be propagated to that HTTP request? – Artem Bilan Sep 13 '22 at 21:09