I'd like to migrate a JAX-RS-REST-Restservice (running under Tomcat) to Quarkus. I could solve most of my problems along the way but I still have a problem with one method.
In this function I do a OTA-download (firmware for a device). I set some headers and the MediaType and the content length.
In the original service my code looked as follows:
public HomeAutomationService
{
...
@Context
private HttpServletRequest request;
...
@GET
@Produces(MediaType.APPLICATION_OCTET_STREAM)
@Path("/v1/DownloadFirmware")
public Response getFirmware()
{
...
response.setHeader("X-OTA-SIGNATURE", signatureString);
response.setContentLength((int) file.length());
response.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return Response.ok(file, MediaType.APPLICATION_OCTET_STREAM).build();
}
}
Unfortunately I can't find anything like a HttpServletResponse in Quarkus. So I now use a ResponseBuilder to create a Response, where I can add headers as needed:
ResponseBuilder responseBuilder;
However, I am not sure how to instantiate the ResponseBuilder. There is a method to set headers for the ResponseBuilder, but I did not find anything on how to the content length and the content type.
I am not sure if I have to set the content-type since I already use a @Produces-annotation - but what about the content length? Is it set automatically? If no (that's what I guess) how can I set it correctly?
Thanks for reading and answering,
Rudi
CodePudding user response:
You don't need to use @Produces in this case and your return should be something like the code below:
return Response.ok(yourFileBytes[])
.type(MediaType.APPLICATION_OCTET_STREAM)
.header(HttpHeaders.CONTENT_LENGTH, <yourFileLength>)
.header(HttpHeaders.CONTENT_DISPOSITION, "inline; ")
.build();