Search in sources :

Example 1 with ResourceStreamRequestHandler

use of org.apache.wicket.request.handler.resource.ResourceStreamRequestHandler 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 ResourceStreamRequestHandler

use of org.apache.wicket.request.handler.resource.ResourceStreamRequestHandler in project ocvn by devgateway.

the class CustomDownloadLink method onClick.

@Override
public void onClick() {
    AbstractResourceStreamWriter rstream = new AbstractResourceStreamWriter() {

        /**
             * 
             */
        private static final long serialVersionUID = 1L;

        @Override
        public void write(final OutputStream output) throws IOException {
            output.write(getModelObject().getContent().getBytes());
        }

        @Override
        public String getContentType() {
            return getModelObject().getContentType();
        }
    };
    ResourceStreamRequestHandler handler = new ResourceStreamRequestHandler(rstream, getModelObject().getName());
    handler.setContentDisposition(ContentDisposition.ATTACHMENT);
    getRequestCycle().scheduleRequestHandlerAfterCurrent(handler);
}
Also used : OutputStream(java.io.OutputStream) AbstractResourceStreamWriter(org.apache.wicket.util.resource.AbstractResourceStreamWriter) ResourceStreamRequestHandler(org.apache.wicket.request.handler.resource.ResourceStreamRequestHandler)

Example 3 with ResourceStreamRequestHandler

use of org.apache.wicket.request.handler.resource.ResourceStreamRequestHandler 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)

Example 4 with ResourceStreamRequestHandler

use of org.apache.wicket.request.handler.resource.ResourceStreamRequestHandler in project ocvn by devgateway.

the class FileInputBootstrapFormComponentWrapper method addAlreadyUploadedFilesComponent.

/**
     * already uploaded files section
     */
private void addAlreadyUploadedFilesComponent() {
    alreadyUploadedFiles = new WebMarkupContainer("alreadyUploadedFiles") {

        private static final long serialVersionUID = 1L;

        @Override
        protected void onInitialize() {
            super.onInitialize();
            setVisibilityAllowed(isVisibleAlreadyUploadedFiles());
        }
    };
    alreadyUploadedFiles.setOutputMarkupPlaceholderTag(true);
    alreadyUploadedFiles.setOutputMarkupId(true);
    add(alreadyUploadedFiles);
    alreadyUploadedFiles.add(new Label("uploadedFilesTitle", new StringResourceModel("uploadedFilesTitle", this, null)));
    AbstractReadOnlyModel<List<FileMetadata>> alreadyUploadedFilesModel = new AbstractReadOnlyModel<List<FileMetadata>>() {

        private static final long serialVersionUID = 1L;

        @Override
        public List<FileMetadata> getObject() {
            List<FileMetadata> fileObject = new ArrayList<>();
            // get only the already uploaded files
            for (FileMetadata file : filesModel) {
                if (!file.isNew()) {
                    fileObject.add(file);
                }
            }
            return fileObject;
        }
    };
    ListView<FileMetadata> list = new ListView<FileMetadata>("list", alreadyUploadedFilesModel) {

        private static final long serialVersionUID = 1L;

        private List<IndicatingAjaxLink<Void>> deleteButtons = new ArrayList<>();

        @Override
        protected void populateItem(final ListItem<FileMetadata> item) {
            // make file name clickable
            Link<FileMetadata> downloadLink = new Link<FileMetadata>("downloadLink", item.getModel()) {

                private static final long serialVersionUID = 1L;

                @Override
                public void onClick() {
                    final FileMetadata modelObject = getModelObject();
                    AbstractResourceStreamWriter rstream = new AbstractResourceStreamWriter() {

                        private static final long serialVersionUID = 1L;

                        @Override
                        public void write(final OutputStream output) throws IOException {
                            output.write(modelObject.getContent().getBytes());
                        }

                        @Override
                        public String getContentType() {
                            return modelObject.getContentType();
                        }
                    };
                    ResourceStreamRequestHandler handler = new ResourceStreamRequestHandler(rstream, modelObject.getName());
                    handler.setContentDisposition(ContentDisposition.ATTACHMENT);
                    getRequestCycle().scheduleRequestHandlerAfterCurrent(handler);
                }
            };
            downloadLink.add(new Label("downloadText", item.getModelObject().getName()));
            downloadLink.add(new TooltipBehavior(new StringResourceModel("downloadUploadedFileTooltip", FileInputBootstrapFormComponentWrapper.this, null), TOOLTIP_CONFIG));
            item.add(downloadLink);
            Link<FileMetadata> download = new CustomDownloadLink("download", item.getModel());
            item.add(download);
            IndicatingAjaxLink<Void> delete = new IndicatingAjaxLink<Void>("delete") {

                private static final long serialVersionUID = 1L;

                @SuppressWarnings("unchecked")
                @Override
                public void onClick(final AjaxRequestTarget target) {
                    filesModel.remove(item.getModelObject());
                    FileInputBootstrapFormComponentWrapper.this.getModel().setObject((T) filesModel);
                    target.add(alreadyUploadedFiles);
                }
            };
            delete.add(new IconBehavior(GlyphIconType.remove));
            delete.add(new TooltipBehavior(new StringResourceModel("removeUploadedFileTooltip", FileInputBootstrapFormComponentWrapper.this, null), TOOLTIP_CONFIG));
            delete.setVisible(true);
            item.add(delete);
            deleteButtons.add(delete);
            // only to admins
            if (visibleOnlyToAdmin) {
                MetaDataRoleAuthorizationStrategy.authorize(delete, Component.RENDER, SecurityConstants.Roles.ROLE_ADMIN);
            }
            if (disableDeleteButton) {
                delete.setVisibilityAllowed(false);
            }
        }

        @Override
        public void onEvent(final IEvent<?> event) {
            /*
                 * disable 'delete' buttons based on the form state
                 */
            if (event.getPayload() instanceof EditingDisabledEvent) {
                for (IndicatingAjaxLink<?> del : deleteButtons) {
                    del.setVisibilityAllowed(false);
                }
            }
            if (event.getPayload() instanceof EditingEnabledEvent) {
                for (IndicatingAjaxLink<?> del : deleteButtons) {
                    del.setVisibilityAllowed(true);
                }
            }
        }
    };
    alreadyUploadedFiles.add(list);
}
Also used : AbstractReadOnlyModel(org.apache.wicket.model.AbstractReadOnlyModel) IEvent(org.apache.wicket.event.IEvent) OutputStream(java.io.OutputStream) Label(org.apache.wicket.markup.html.basic.Label) FileMetadata(org.devgateway.toolkit.persistence.dao.FileMetadata) ArrayList(java.util.ArrayList) AbstractResourceStreamWriter(org.apache.wicket.util.resource.AbstractResourceStreamWriter) CustomDownloadLink(org.devgateway.toolkit.forms.wicket.components.util.CustomDownloadLink) IconBehavior(de.agilecoders.wicket.core.markup.html.bootstrap.image.IconBehavior) WebMarkupContainer(org.apache.wicket.markup.html.WebMarkupContainer) ResourceStreamRequestHandler(org.apache.wicket.request.handler.resource.ResourceStreamRequestHandler) TooltipBehavior(de.agilecoders.wicket.core.markup.html.bootstrap.components.TooltipBehavior) ListView(org.apache.wicket.markup.html.list.ListView) IndicatingAjaxLink(org.apache.wicket.extensions.ajax.markup.html.IndicatingAjaxLink) ArrayList(java.util.ArrayList) List(java.util.List) EditingDisabledEvent(org.devgateway.toolkit.forms.wicket.events.EditingDisabledEvent) StringResourceModel(org.apache.wicket.model.StringResourceModel) EditingEnabledEvent(org.devgateway.toolkit.forms.wicket.events.EditingEnabledEvent) AjaxRequestTarget(org.apache.wicket.ajax.AjaxRequestTarget) ListItem(org.apache.wicket.markup.html.list.ListItem) CustomDownloadLink(org.devgateway.toolkit.forms.wicket.components.util.CustomDownloadLink) Link(org.apache.wicket.markup.html.link.Link) IndicatingAjaxLink(org.apache.wicket.extensions.ajax.markup.html.IndicatingAjaxLink)

Aggregations

ResourceStreamRequestHandler (org.apache.wicket.request.handler.resource.ResourceStreamRequestHandler)4 OutputStream (java.io.OutputStream)2 IRequestCycle (org.apache.wicket.request.IRequestCycle)2 AbstractResourceStreamWriter (org.apache.wicket.util.resource.AbstractResourceStreamWriter)2 IResourceStream (org.apache.wicket.util.resource.IResourceStream)2 TooltipBehavior (de.agilecoders.wicket.core.markup.html.bootstrap.components.TooltipBehavior)1 IconBehavior (de.agilecoders.wicket.core.markup.html.bootstrap.image.IconBehavior)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 AjaxRequestTarget (org.apache.wicket.ajax.AjaxRequestTarget)1 IEvent (org.apache.wicket.event.IEvent)1 IndicatingAjaxLink (org.apache.wicket.extensions.ajax.markup.html.IndicatingAjaxLink)1 WebMarkupContainer (org.apache.wicket.markup.html.WebMarkupContainer)1 Label (org.apache.wicket.markup.html.basic.Label)1 Link (org.apache.wicket.markup.html.link.Link)1 ListItem (org.apache.wicket.markup.html.list.ListItem)1 ListView (org.apache.wicket.markup.html.list.ListView)1 AbstractReadOnlyModel (org.apache.wicket.model.AbstractReadOnlyModel)1 StringResourceModel (org.apache.wicket.model.StringResourceModel)1