0

Using apache http client, during file download I could use

response.getFirstHeader("Content-Disposition").getValue();

Now I am switching to webflux to download file like below:

    final Flux<DataBuffer> dataBufferFlux = client.get()
            .accept(MediaType.APPLICATION_OCTET_STREAM)
            .retrieve()
            .bodyToFlux(DataBuffer.class); 

    final Path path = FileSystems.getDefault().getPath("/file/path/name.pdf");
    DataBufferUtils
            .write(dataBufferFlux, path, StandardOpenOption.CREATE)
            .block();

Question: How do I get the content/header related options in webflux?

Update

I can very well do:

ClientResponse response = client.get().uri(uri)
                .accept(MediaType.APPLICATION_OCTET_STREAM)
                .headers(headers -> headers.setBasicAuth("admin", "admin"))
                .exchange()
                .block();

And it gives me what I want - System.out.println("File response asHttpHeaders :: "+response.headers().asHttpHeaders().getContentDisposition());

Output:

File response asHttpHeaders :: attachment; filename*=UTF-8''TestPdf.pdf

But I don't want to make it look ugly. I was wondering if I could get this info in my original code (using dataBufferFlux) above.

The Application:

I have an application where I need to download files in bulk (millions of files a week) in a staging area using rest API from another application, which is not fully reactive. So the whole idea is to intercept the header information in my first code (dataBufferFlux) so that I can set the filename and content type on the fly.

Simple (Re-framed) Question:

Is there a way I can retrieve the header information to get the filename and content type for the files while writing them on Staging Area (a NAS Drive).

Ajay Kumar
  • 2,906
  • 3
  • 23
  • 46
  • `But I don't want to make it look ugly` thats a very subjective thing. Also, you have not shown the purpose of the usage of the header, where you need it, and why. Also, you are using `block` which should not be done in spring webflux (im assuming you have a pure webflux app since you tagged that), and such is the case. calling `block` is just wrong. Voting to close since there is no clear question or intent. – Toerktumlare Dec 25 '20 at 06:19
  • Which book did you read which says that we cant use block and using it in webflux is wrong? Please enlighten us on that.! Do you have any idea on synchronous and asynchronous? When I said I dont want to make it ugly it means I dont want to have two calls to achieve one thing. – Ajay Kumar Dec 25 '20 at 13:14
  • @Toerktumlare, Here is your first read- your own answer by the way -https://stackoverflow.com/questions/57355725/springboot-how-to-use-webclient-instead-of-resttemplate-for-performing-non-blo/57365926#57365926 – Ajay Kumar Dec 25 '20 at 13:33
  • Yes in my answer i explicitly write that if you have a pure webflux application you should under no circumstance use block. So if you have that, then dont use block. And "the book" is the official reactor documentation which is the underlying library in spring webflux. reference: https://projectreactor.io/docs/core/release/reference/#_blocking_can_be_wasteful – Toerktumlare Dec 25 '20 at 16:11
  • the entire purpose of using webflux is to be "non-blocking" And if you don't understand that then you should read up on the basics of reactor and why webflux was invented in the first place. – Toerktumlare Dec 25 '20 at 16:25
  • I can not migrate my entire app due to some restrictions and limitations. And that is a valid situation. Why do you find it so hard to digest? – Ajay Kumar Dec 25 '20 at 17:00
  • please read what i have written. If you have a pure webflux application you should not block. If you dont have a pure webflux you can call block. You have not stated what kind of application you have in your question. So as i wrote in my first comment `i am assuming you have pure webflux application since thats what you tagged` (spring-webflux). So my answer is based on assumptions due to the lack of information you have provided in your question. – Toerktumlare Dec 25 '20 at 17:21
  • you have still not stated what type of application you have. And if i would write an answer for you there is a difference in the answers between having a fully reactive application and having spring mvc application just using webclient. But good luck! – Toerktumlare Dec 25 '20 at 17:25
  • Added the relevant info if that can help you understand the situation. Let me know if I am still missing something here. – Ajay Kumar Dec 25 '20 at 17:39

1 Answers1

0

I found the answer hidden in this thread.

Spring boot Webclient's retrieve vs exchange

Its more about using exchange rather than retrieve. As “exchange” gives you more control.

Ajay Kumar
  • 2,906
  • 3
  • 23
  • 46