Reputation: 11
Is there any camel restful web service example to provide file download as below api
@GET
@Path("/jar")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response downloadFile() {
File file = new File("/home/user/Downloads/classes.jar");
ResponseBuilder response = Response.ok((Object) file);
response.header("Content-Disposition", "attachment;filename=classes.jar");
return response.build();
}
Upvotes: 1
Views: 2385
Reputation: 1947
You can use a combination of Camel REST DSL and Content Enricher (specifically - pollEnrich
).
An example implementation for your case could look like this:
// you can configure it as you want, it's just an example
restConfiguration().component("restlet").host("localhost").port(8081);
rest("/jar")
.get()
.produces(MediaType.APPLICATION_OCTET_STREAM_VALUE)
.route()
.routeId("downloadFile")
.pollEnrich("file:/home/user/Downloads?fileName=classes.jar&noop=true")
.setHeader("Content-Disposition", simple("attachment;filename=classes.jar"));
Please note, that if the file is not found in the specified path, pollEnrich
will block until the file arrives. You can set a timeout in milliseconds by providing a second argument of type long
to the call to pollEnrich
.
Upvotes: 1