Not able to add response headers to RestTemplate streaming response

587 Views Asked by At

In my spring-boot application, I have a GET end-point. When we call this GET endpoint, the application is sending a GET request to another service with RestTemplate and returns the same response file as the response of the GET request. With the below code I'm able to receive the response file. But I need to set the same headers that I have received to RestTempate request. How to do that.

@GetMapping(value = URL_CONTENT_ID, produces = {MediaType.APPLICATION_OCTET_STREAM_VALUE, APPLICATION_ZIP_VALUE,
    MediaType.TEXT_HTML_VALUE})
@ControllerLogging
public ResponseEntity<StreamingResponseBody> getContentFile(@PathVariable String contentId) {

    StreamingResponseBody responseBody = outputStream -> {
        getContentFile(outputStream, contentId);
        outputStream.close();
    };

    return ResponseEntity.ok()
        .body(responseBody);

}

public void getContentFile(OutputStream outputStream, String nodeId) {

    RequestCallback requestCallBack = request -> {
        HttpHeaders headers = new HttpHeaders();
        authenticationHelper.apply(headers::set);
        request.getHeaders().addAll(headers);
    };

    ResponseExtractor<OutputStream> responseExtractor = clientHttpResponse -> {
        InputStream inputStream = clientHttpResponse.getBody();
        StreamUtils.copy(inputStream, outputStream);
        return null;
    };

    restTemplate.execute(dcmUrl + nodeId, HttpMethod.GET, requestCallBack, responseExtractor);
}
1

There are 1 best solutions below

0
On

Please find the solution to this issue, the change I did was adding "HttpServletResponse" to the parameters of the controller method.

@GetMapping(value = URL_CONTENT_ID, produces = {MediaType.APPLICATION_OCTET_STREAM_VALUE, APPLICATION_ZIP_VALUE,
    MediaType.TEXT_HTML_VALUE})
@ControllerLogging
public ResponseEntity<StreamingResponseBody> getContentFile(@PathVariable String contentId, HttpServletResponse response) {

    StreamingResponseBody responseBody = outputStream -> {
        getContentFile(outputStream, response, contentId);
        outputStream.close();
    };

    return ResponseEntity.ok(responseBody);

}

public void getContentFile(OutputStream outputStream, HttpServletResponse response, String nodeId) {

    RequestCallback requestCallBack = request -> {
        HttpHeaders headers = new HttpHeaders();
        authenticationHelper.apply(headers::set);
        request.getHeaders().addAll(headers);
    };

    ResponseExtractor<OutputStream> responseExtractor = clientHttpResponse -> {
        InputStream inputStream = clientHttpResponse.getBody();
        response.setHeader(HttpHeaders.CONTENT_DISPOSITION, modifyContentDispositionHeader(clientHttpResponse));
        response.setHeader(HttpHeaders.CONTENT_TYPE, modifyContentTypeHeader(clientHttpResponse).toString());
        StreamUtils.copy(inputStream, outputStream);
        return null;
    };

    restTemplate.execute(dcmUrl + nodeId, HttpMethod.GET, requestCallBack, responseExtractor);
}