Can I implement "download" using WebService?

I find there is a feature in “sonar-cfamily-plugin”: user can download bundle file from server. “http://localhost:9000/static/cpp/build-wrapper-macosx-x86.zip

I want to develop similar feature, the below is my code.
It works but I wish to know whether it is right or not?

@ServerSide
@ExtensionPoint
public class DownloadService implements WebService {
    @Override
    public void define(Context context) {
        NewController controller = context.createController("static/bundle");
        controller.setDescription("Download demo");
        controller.createAction("download").setHandler(new RequestHandler() {
            @Override
            public void handle(Request request, Response response) throws Exception {
                Response.Stream stream = response.stream();
                stream.setMediaType("application/zip");
                OutputStream out = stream.output();
                out.write(.......); // copy stream in jar resources to out
                out.flush();
                out.close();
            }
        });
        controller.done();
    }
}

Thanks

Hi,

Static resources can be bundled in the special directory static/. For example the file static/foo/bar.txt packaged in the plugin JAR is available at runtime at http://{server}/static/{pluginKey}/foo/bar.txt. With this trick you don’t need to implement a web service.

Regards.

2 Likes