Marcos Tanaka
Marcos Tanaka

Reputation: 3336

Unmarshal InputStream using BeanIO and Apache Camel

I have a service that receives a file and send it to a Camel route. On that route, I’d like to unmarshal that file using BeanIO, but it doesn’t recognize inputs of type InputStream.

@RestController
@RequestMapping(value = "/file")
public class FileController {

    @Autowired
    private CamelContext camelContext;

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public void upload(@RequestParam("file") MultipartFile multipartFile) throws IOException {
        ProducerTemplate template = camelContext.createProducerTemplate();
        template.sendBody("direct:routeTest", new ByteArrayInputStream(multipartFile.getBytes()));
    }

}

@Component
public class SampleRouter extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        from("direct:routeTest")
                .log("${body}")
                .to("dataformat:beanio:unmarshal?mapping=mapping.xml&streamName=testFile")
                .process(msg -> msg.getIn()
                        .getBody(List.class)
                        .forEach(o -> {...}))
                .end();
    }

}

I have tested a route that reads the file using the File component, and sends the result to the BeanIO component. In that case it works.

How can I use BeanIO with inputs of type InputStream on Apache Camel? Is there any component that can transform my InputStream into something compatible with the BeanIO component?

Upvotes: 1

Views: 1670

Answers (2)

mgyongyosi
mgyongyosi

Reputation: 2667

Other solution can be to add .streamCaching() to the route. Then you can read the same stream multiple times. Official documentation

Upvotes: 1

Marcos Tanaka
Marcos Tanaka

Reputation: 3336

As mgyongyosi said, when .log("${body}") reads the stream, the position go to the end of it. To solve this we can reset the position of the InputStream after reading it:

from("direct:routeTest")
        .log("${body}")
        .process(exchange -> ((InputStream) exchange.getIn().getBody()).reset())
        .to("dataformat:beanio:unmarshal?mapping=mapping.xml&streamName=testFile")

or remove the line .log("${body}").

Upvotes: 3

Related Questions