Я создаю веб-сервис, который должен предложить файл для загрузки.Сам файл запрашивается из другого внешнего веб-сервиса под капотом.Так что мой веб-сервис больше похож на прокси.
Поскольку файлы могут быть большими, вместо того, чтобы извлекать их полностью, я записываю файл прямо как поток.
Проблема: внешний веб-сервис предоставляет HttpHeaders
как Content-Length
, Content-Type
, Content-Disposition
, которые я хотел бы переслать через мой прокси-сервлет.Но так как я передаю только ресурс, заголовки на этом этапе неизвестны.
@GetMapping(value = "/files/{filename}")
public ResponseEntity<StreamingResponseBody> getDocument(@PathVariable String filename) {
StreamingResponseBody responseBody = outputStream -> {
HttpHeaders headers = download(outputStream, filename);
outputStream.close();
System.out.println(headers); //always 'null' at this stage
};
return ResponseEntity.ok(responseBody); //TODO how to get the header in?
}
private HttpHeaders download(OutputStream outputStream, String filename) {
ResponseExtractor<HttpHeaders> responseExtractor = clientHttpResponse -> {
//directly stream the remote file into the servlet response
InputStream inputStream = clientHttpResponse.getBody();
StreamUtils.copy(inputStream, outputStream);
HttpHeaders headers = clientHttpResponse.getHeaders();
System.out.println(headers); //external headers are shown correctly
//is it possible to write the headers into the servlet response at this stage??
return headers;
};
return restTemplate.execute("https://www.external-webservice.com?file=" + filename, HttpMethod.GET, null, responseExtractor);
}
Как видите: заголовки внешнего файла доступны на этапе ResponseExtractor
.Но когда я возвращаю эти заголовки в стадию StreamingResponseBody
, заголовки равны null
.
Вопрос: возможно ли вообще получить удаленные HttpHeaders в случае прямой потоковой передачи?