Consume webClient Flux<DataBuffer> directly by apache camel route

43 views Asked by At

I have a call to a rest api that return a json file (large file), I am using webClient in order to get the file. I want to consume this response by apache camel directly without store the file on the disk. Is there a way to do that ?

Please find above my api call

public File downloadFile(String url) {

        Flux<DataBuffer> dataBuffer = webClient.get()
                .uri(URI.create(url))
                .retrieve()
                .bodyToFlux(DataBuffer.class)
                .doOnComplete(() -> log.info("File has been downloaded successfully"))
                .doOnError(exception -> {
                    throw new FileException(exception);
                });

        Path path;
        try {
            path = Files.createTempFile("file_", LocalDateTime.now() + ".json");
            DataBufferUtils.write(dataBuffer, path, StandardOpenOption.CREATE).share().block();
        } catch (IOException exception) {
            throw new FileException(exeption);
        }
        return path.toFile();
}

And here is my camel route structure

public void configure() throws Exception {
    from(direct("test").getUri)
        .bean(ApiService.class, "downloadFile")
        .filter(body().isNotNull())
        .unmarshal().json(JsonLibrary.Jackson, MyObject[].class)
    .to(direct("destination").getUri);
}

My goal is to pass the webClient response (DataBuffer content) to camel route and unmarshal the content to my object MyObject[].class.

Any help would be appreciated.

0

There are 0 answers