dimanche 26 juin 2016

Generate a download or show an error message in Wicket

I'm using the following code to dynamically generate a download in Wicket, using the ResourceLink approach (since the download is not a static file, it needs to be generated on the fly, and I was told this was the correct approach):

IResource res = new AbstractResource() {

    @Override
    protected ResourceResponse newResourceResponse(Attributes attributes) {
        ResourceResponse resourceResponse = new ResourceResponse();
        resourceResponse.setContentType("application/pdf");
        resourceResponse.setFileName("output.pdf");
        resourceResponse.setContentDisposition(ContentDisposition.ATTACHMENT);
        resourceResponse.setWriteCallback(new WriteCallback() {

            @Override
            public void writeData(Attributes attributes) throws IOException {
                OutputStream outputStream = attributes.getResponse().getOutputStream();
                try {
                    outputStream.write(generateDocument());
                } catch (Exception e) {
                    //Generation failed... Here I'd like to either show a popup message or alter the current page to show an error somewhere in the page
                }                       
            }
        });
        return resourceResponse;
    }   
};

ResourceLink<Void> resLink = new ResourceLink<Void>("resLink", res);
myForm.add(resLink);

The comment in the code above shows where I'm having trouble. If the generation of the download fails (which can happen, if certain conditions are not met) I'd like to show an error message, either by showing a popup or altering the page to show some error text (but in either case I want to avoid leaving/reloading the entire page)

Is this possible?

Aucun commentaire:

Enregistrer un commentaire