Search in sources :

Example 1 with IResourceStream

use of org.apache.wicket.util.resource.IResourceStream in project midpoint by Evolveum.

the class AjaxDownloadBehaviorFromStream method onRequest.

public void onRequest() {
    final InputStream byteStream = initStream();
    if (byteStream == null) {
        return;
    }
    IResourceStream resourceStream = new AbstractResourceStream() {

        @Override
        public String getContentType() {
            return contentType;
        }

        @Override
        public InputStream getInputStream() throws ResourceStreamNotFoundException {
            return byteStream;
        }

        @Override
        public void close() throws IOException {
            byteStream.close();
        }
    };
    ResourceStreamRequestHandler reqHandler = new ResourceStreamRequestHandler(resourceStream) {

        @Override
        public void respond(IRequestCycle requestCycle) {
            super.respond(requestCycle);
        }
    }.setContentDisposition(ContentDisposition.ATTACHMENT).setCacheDuration(Duration.ONE_SECOND);
    if (StringUtils.isNotEmpty(getFileName())) {
        reqHandler.setFileName(getFileName());
    }
    getComponent().getRequestCycle().scheduleRequestHandlerAfterCurrent(reqHandler);
}
Also used : AbstractResourceStream(org.apache.wicket.util.resource.AbstractResourceStream) IResourceStream(org.apache.wicket.util.resource.IResourceStream) InputStream(java.io.InputStream) ResourceStreamRequestHandler(org.apache.wicket.request.handler.resource.ResourceStreamRequestHandler) IRequestCycle(org.apache.wicket.request.IRequestCycle)

Example 2 with IResourceStream

use of org.apache.wicket.util.resource.IResourceStream in project midpoint by Evolveum.

the class AjaxDownloadBehaviorFromFile method onRequest.

public void onRequest() {
    final File file = initFile();
    IResourceStream resourceStream = new FileResourceStream(new File(file));
    getComponent().getRequestCycle().scheduleRequestHandlerAfterCurrent(new ResourceStreamRequestHandler(resourceStream) {

        @Override
        public void respond(IRequestCycle requestCycle) {
            try {
                super.respond(requestCycle);
            } finally {
                if (removeFile) {
                    LOGGER.debug("Removing file '{}'.", new Object[] { file.getAbsolutePath() });
                    Files.remove(file);
                }
            }
        }
    }.setFileName(file.getName()).setContentDisposition(ContentDisposition.ATTACHMENT).setCacheDuration(Duration.ONE_SECOND));
}
Also used : FileResourceStream(org.apache.wicket.util.resource.FileResourceStream) IResourceStream(org.apache.wicket.util.resource.IResourceStream) File(org.apache.wicket.util.file.File) ResourceStreamRequestHandler(org.apache.wicket.request.handler.resource.ResourceStreamRequestHandler) IRequestCycle(org.apache.wicket.request.IRequestCycle)

Aggregations

IRequestCycle (org.apache.wicket.request.IRequestCycle)2 ResourceStreamRequestHandler (org.apache.wicket.request.handler.resource.ResourceStreamRequestHandler)2 IResourceStream (org.apache.wicket.util.resource.IResourceStream)2 InputStream (java.io.InputStream)1 File (org.apache.wicket.util.file.File)1 AbstractResourceStream (org.apache.wicket.util.resource.AbstractResourceStream)1 FileResourceStream (org.apache.wicket.util.resource.FileResourceStream)1