简体   繁体   English

如何异步将HttpHeader写入ClientHttpResponse?

[英]How to async write HttpHeader to ClientHttpResponse?

I'm creating a webservice that should offer a file for download. 我正在创建一个应该提供下载文件的网络服务。 The file itself is requested from another external webservice under the hood. 文件本身是从内部的另一个外部Web服务请求的。 So my webservice is more like proxy. 所以我的网络服务更像是代理。

As files could be big, instead of fetching them completely, I'm writing the file directly out as stream. 由于文件可能很大,而不是完全获取它们,因此我将文件直接作为流写入。

Problem: the external webservice provides HttpHeaders like Content-Length , Content-Type , Content-Disposition that I would like to forward through my proxy servlet. 问题:外部Web服务提供了我希望通过代理Servlet转发的HttpHeadersContent-LengthContent-TypeContent-Disposition But as I stream the resource only, the headers are not known at this stage. 但是由于我仅流式传输资源,因此在此阶段尚不了解标头。

@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);
    }

As you see: the headers of the external file are available at ResponseExtractor stage. 如您所见:外部文件的标头在ResponseExtractor阶段可用。 But when I return those headers into StreamingResponseBody stage, the headers are null . 但是,当我将这些标头返回到StreamingResponseBody阶段时,标头为null

Question: is it possible at all to get the remote HttpHeaders in case of direct streaming? 问题:在直接流式传输的情况下,是否有可能获取远程HttpHeaders?

The HttpHeaders must be written directly into the HttpServletResponse , before writing the body stream: 在编写主体流之前,必须将HttpHeaders直接写入HttpServletResponse

private HttpHeaders download(OutputStream outputStream, HttpServletResponse response, String filename) {
    ResponseExtractor<Void> responseExtractor = clientHttpResponse -> {
        response.setHeader(HttpHeaders.CONTENT_DISPOSITION, clientHttpResponse.getHeaders().getFirst(HttpHeaders.CONTENT_DISPOSITION));

        InputStream inputStream = clientHttpResponse.getBody();
        StreamUtils.copy(inputStream, outputStream);
        return null;
    };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM