Reputation: 3538
When using the Apache httpcore 5 for java how does the handle() method in an AsyncServerRequestHandler add headers to the response?
There are a couple of examples at https://hc.apache.org/httpcomponents-core-5.0.x/examples.html such as https://hc.apache.org/httpcomponents-core-5.0.x/httpcore5-h2/examples/org/apache/hc/core5/http/examples/Http2FileServerExample.java , but they are somewhat dense.
Under the old httpcore <= 4 you would do something like
response.setHeader("Access-Control-Allow-Origin", "*")
but it is unclear which of the MANY layers of indirection that httpcore 5 uses is the layer with access to the response headers, and which object has the method corresponding to setHeader.
Upvotes: 1
Views: 95
Reputation: 10931
You can pass an HttpResponse
into the constructor of BasicResponseProducer
.
So for example in the Http2FileServerExample in the question:
responseTrigger.submitResponse(new BasicResponseProducer(
HttpStatus.SC_OK, new FileEntityProducer(file, contentType)));
can become:
BasicHttpResponse response = new BasicHttpResponse(HttpStatus.SC_OK);
response.setHeader("Access-Control-Allow-Origin", "*");
responseTrigger.submitResponse(new BasicResponseProducer(
response, new FileEntityProducer(file, contentType)));
Upvotes: 1