0

I have following controller in a Spring Boot application which accept and return a binary stream. I need to accept a steam, encrypt it and at the same time return the result. This streams can be pretty large and cannot be held in memory:

    @PostMapping(value = "/encrypt-stream/", consumes = MediaType.APPLICATION_OCTET_STREAM_VALUE, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void encryptStream(HttpServletRequest request, HttpServletResponse response){

        OutputStream os = response.getOutputStream();
        InputStream is = request.getInputStream();

        String checksum = encryptionService.encrypt(is, os);

        response.setHeader("checksum", checksum);
    }

I need to send the a header (checksum) based on the result of the service call (encryptionService.encrypt()) within the controller. Setting header on HttpServletResponse is not working.

I have looked at implementing Filter class however it only works when you set the header before .doFilter() call. My issue is the header value is calculated after.

Also, same issue with implementing a HandlerInterceptorAdapter.

adesai
  • 370
  • 3
  • 22

1 Answers1

0

I think HTTP might be a bad choice for a streaming encryption server. The protocol is at its core REQUEST then RESPONSE and is not designed for the use case where you might start returning response before the request has finished. There are exceptions for early returned error codes but support for this behavior looks spotty.

HTTP Response before Request

Deadron
  • 5,135
  • 1
  • 16
  • 27