Example usage for org.apache.wicket.request.handler.resource ResourceStreamRequestHandler ResourceStreamRequestHandler

List of usage examples for org.apache.wicket.request.handler.resource ResourceStreamRequestHandler ResourceStreamRequestHandler

Introduction

In this page you can find the example usage for org.apache.wicket.request.handler.resource ResourceStreamRequestHandler ResourceStreamRequestHandler.

Prototype

public ResourceStreamRequestHandler(IResourceStream resourceStream) 

Source Link

Document

Construct.

Usage

From source file:au.org.theark.core.util.ByteDataResourceRequestHandler.java

License:Open Source License

public void respond(IRequestCycle requestCycle) {
    // Write out data as a file in temporary directory
    final String tempDir = System.getProperty("java.io.tmpdir");
    final java.io.File file = new File(tempDir, fileName);

    FileOutputStream fos = null;/* ww  w . j  a  v a2  s.c om*/
    try {
        fos = new FileOutputStream(file);
        fos.write(this.getData(null));
        fos.flush();
    } catch (IOException fe) {
        fe.printStackTrace();
    } finally {
        try {
            if (fos != null) {
                fos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // Send file back as attachment, and remove after download
    IResourceStream resourceStream = new FileResourceStream(new org.apache.wicket.util.file.File(file));
    requestCycle.scheduleRequestHandlerAfterCurrent(new ResourceStreamRequestHandler(resourceStream) {
        @Override
        public void respond(IRequestCycle requestCycle) {
            super.respond(requestCycle);
            Files.remove(file);
        }
    }.setFileName(fileName).setContentDisposition(ContentDisposition.ATTACHMENT));
}

From source file:au.org.theark.core.web.component.button.ArkDownloadTemplateButton.java

License:Open Source License

@Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
    byte[] data = writeOutXlsFileToBytes();
    if (data != null) {
        InputStream inputStream = new ByteArrayInputStream(data);
        OutputStream outputStream;
        try {//  ww  w  . j ava 2 s.  c  o  m
            final String tempDir = System.getProperty("java.io.tmpdir");
            final java.io.File file = new File(tempDir, templateFilename + ".xls");
            final String fileName = templateFilename + ".xls";
            outputStream = new FileOutputStream(file);
            IOUtils.copy(inputStream, outputStream);

            IResourceStream resourceStream = new FileResourceStream(new org.apache.wicket.util.file.File(file));
            getRequestCycle()
                    .scheduleRequestHandlerAfterCurrent(new ResourceStreamRequestHandler(resourceStream) {
                        @Override
                        public void respond(IRequestCycle requestCycle) {
                            super.respond(requestCycle);
                            Files.remove(file);
                        }
                    }.setFileName(fileName).setContentDisposition(ContentDisposition.ATTACHMENT));
        } catch (FileNotFoundException e) {
            log.error(e.getMessage());
        } catch (IOException e) {
            log.error(e.getMessage());
        }
    }
}

From source file:au.org.theark.lims.web.component.inventory.panel.box.display.GridBoxPanel.java

License:Open Source License

/**
 * Return a download link for the gridBox contents as an Excel Worksheet 
 * @param invCellList//  w  w w . j a  va  2s  .c om
 * @return
 */
protected Link<String> buildXLSDownloadLink(final List<InvCell> invCellList) {
    Link<String> link = new Link<String>("downloadGridBoxDataLink") {

        private static final long serialVersionUID = 1L;

        public void onClick() {
            byte[] data = createWorkBookAsByteArray(invCellList);
            if (data != null) {
                InputStream inputStream = new ByteArrayInputStream(data);
                OutputStream outputStream;
                try {
                    final String tempDir = System.getProperty("java.io.tmpdir");
                    final java.io.File file = new File(tempDir, limsVo.getInvBox().getName() + ".xls");
                    final String fileName = limsVo.getInvBox().getName() + ".xls";
                    outputStream = new FileOutputStream(file);
                    IOUtils.copy(inputStream, outputStream);

                    IResourceStream resourceStream = new FileResourceStream(
                            new org.apache.wicket.util.file.File(file));
                    getRequestCycle().scheduleRequestHandlerAfterCurrent(
                            new ResourceStreamRequestHandler(resourceStream) {
                                @Override
                                public void respond(IRequestCycle requestCycle) {
                                    super.respond(requestCycle);
                                    Files.remove(file);
                                }
                            }.setFileName(fileName).setContentDisposition(ContentDisposition.ATTACHMENT));
                } catch (FileNotFoundException e) {
                    log.error(e.getMessage());
                } catch (IOException e) {
                    log.error(e.getMessage());
                }
            }
        }
    };

    return link;
}

From source file:au.org.theark.study.web.component.attachments.SearchResultListPanel.java

License:Open Source License

private AjaxButton buildDownloadButton(final SubjectFile subjectFile) {
    AjaxButton ajaxButton = new AjaxButton(au.org.theark.study.web.Constants.DOWNLOAD_FILE) {

        private static final long serialVersionUID = 1L;

        @Override/*w  w w. j a v  a 2  s  . c  om*/
        protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
            // Attempt to download the Blob as an array of bytes
            byte[] data = null;
            try {

                //               data = subjectFile.getPayload();//.getBytes(1, (int) subjectFile.getPayload().length());

                Long studyId = subjectFile.getLinkSubjectStudy().getStudy().getId();
                String subjectUID = subjectFile.getLinkSubjectStudy().getSubjectUID();
                String fileId = subjectFile.getFileId();
                String checksum = subjectFile.getChecksum();

                data = arkCommonService.retriveArkFileAttachmentByteArray(studyId, subjectUID,
                        au.org.theark.study.web.Constants.ARK_SUBJECT_ATTACHEMENT_DIR, fileId, checksum);

                if (data != null) {
                    InputStream inputStream = new ByteArrayInputStream(data);
                    OutputStream outputStream;

                    final String tempDir = System.getProperty("java.io.tmpdir");
                    final java.io.File file = new File(tempDir, subjectFile.getFilename());
                    final String fileName = subjectFile.getFilename();
                    outputStream = new FileOutputStream(file);
                    IOUtils.copy(inputStream, outputStream);

                    IResourceStream resourceStream = new FileResourceStream(
                            new org.apache.wicket.util.file.File(file));
                    getRequestCycle().scheduleRequestHandlerAfterCurrent(
                            new ResourceStreamRequestHandler(resourceStream) {
                                @Override
                                public void respond(IRequestCycle requestCycle) {
                                    super.respond(requestCycle);
                                    Files.remove(file);
                                }
                            }.setFileName(fileName).setContentDisposition(ContentDisposition.ATTACHMENT));
                }
            } catch (ArkSystemException e) {
                this.error("Unexpected error: Download request could not be fulfilled.");
                log.error("ArkSystemException" + e.getMessage(), e);
            } catch (FileNotFoundException e) {
                this.error("Unexpected error: Download request could not be fulfilled.");
                log.error("FileNotFoundException" + e.getMessage(), e);
            } catch (IOException e) {
                this.error("Unexpected error: Download request could not be fulfilled.");
                log.error("IOException" + e.getMessage(), e);
            }

            target.add(arkCrudContainerVO.getSearchResultPanelContainer());
            target.add(containerForm);
        }

        @Override
        protected void onError(AjaxRequestTarget target, Form<?> form) {
            this.error("Unexpected error: Download request could not be fulfilled.");
            log.error("Unexpected error: Download request could not be fulfilled.");
        };
    };

    ajaxButton.setVisible(true);
    ajaxButton.setDefaultFormProcessing(false);

    //if (subjectFile.getPayload() == null)
    //ajaxButton.setVisible(false);

    return ajaxButton;
}

From source file:au.org.theark.study.web.component.subjectUpload.SearchResultListPanel.java

License:Open Source License

public SearchResultListPanel(String id, FeedbackPanel feedBackPanel, ContainerForm containerForm,
        ArkCrudContainerVO arkCrudContainerVO) {
    super(id);/*from   w  w  w  .  j  av a 2s  .  c o  m*/
    ArkDownloadTemplateButton downloadTemplateButton = new ArkDownloadTemplateButton("downloadTemplate",
            "SubjectUpload", au.org.theark.study.web.Constants.SUBJECT_TEMPLATE_CELLS) {
        private static final long serialVersionUID = 1L;

        @Override
        protected void onError(AjaxRequestTarget target, Form<?> form) {
            this.error("Unexpected Error: Could not proceed with download of the template.");
        }

    };
    ArkDownloadTemplateButton downloadCustomFieldTemplateButton = new ArkDownloadTemplateButton(
            "downloadCustomFieldTemplate", "SubjectOrFamilyCustomFieldDataUpload",
            au.org.theark.study.web.Constants.SUBJECT_OR_FAMILY_CUSTOM_FIELD_DATA_TEMPLATE_CELLS) {
        private static final long serialVersionUID = 1L;

        @Override
        protected void onError(AjaxRequestTarget target, Form<?> form) {
            this.error("Unexpected Error: Could not proceed with download of the template.");
        }

    };
    ArkDownloadTemplateButton downloadConsentFieldTemplateButton = new ArkDownloadTemplateButton(
            "downloadConsentFieldTemplate", "SubjectConsentFieldUpload",
            au.org.theark.study.web.Constants.SUBJECT_CONSENT_FIELD_TEMPLATE_CELLS) {
        private static final long serialVersionUID = 1L;

        @Override
        protected void onError(AjaxRequestTarget target, Form<?> form) {
            this.error("Unexpected Error: Could not proceed with download of the template.");
        }

    };

    AjaxButton downLoadPedFileButton = new AjaxButton("downloadPedigreeTemplate") {
        private static final long serialVersionUID = 1L;

        {
            this.setDefaultFormProcessing(false);
        }

        @Override
        protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
            try {
                Theme theme = new Theme();
                Chunk chunk = theme.makeChunk("map_template", "txt");

                String tmpDir = System.getProperty("java.io.tmpdir");
                String pedFileName = "Ark_pedigree_template" + ".ped";
                final File tempFile = new File(tmpDir, pedFileName);
                FileWriter out = new FileWriter(tempFile);
                chunk.render(out);

                IResourceStream resourceStream = new FileResourceStream(
                        new org.apache.wicket.util.file.File(tempFile));
                getRequestCycle()
                        .scheduleRequestHandlerAfterCurrent(new ResourceStreamRequestHandler(resourceStream) {
                            @Override
                            public void respond(IRequestCycle requestCycle) {
                                super.respond(requestCycle);
                                Files.remove(tempFile);
                            }
                        }.setFileName(pedFileName).setContentDisposition(ContentDisposition.ATTACHMENT));

                out.flush();
                out.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        @Override
        protected void onError(AjaxRequestTarget target, Form<?> form) {
            this.error("Unexpected Error: Could not proceed with download of the template.");
        }
    };

    ArkDownloadTemplateButton downloadSubjectAttachmentTemplateButton = new ArkDownloadTemplateButton(
            "downloadSubjectAttachmentTemplate", "SubjectAttachmentUpload",
            au.org.theark.study.web.Constants.SUBJECT_ATTACHMENT_TEMPLATE_CELLS) {
        private static final long serialVersionUID = 1L;

        @Override
        protected void onError(AjaxRequestTarget target, Form<?> form) {
            this.error("Unexpected Error: Could not proceed with download of the template.");
        }
    };

    add(downloadTemplateButton);
    add(downloadCustomFieldTemplateButton);
    add(downloadConsentFieldTemplateButton);
    add(downLoadPedFileButton);
    add(downloadSubjectAttachmentTemplateButton);
}

From source file:ca.travelagency.salesreports.SalesSummaryPanel.java

License:Apache License

public SalesSummaryPanel(String id, SalesSearch salesSearch) {
    super(id, new CompoundPropertyModel<InvoiceSales>(InvoiceSalesModel.make(salesSearch)));

    ListView<MonthlyDistribution> listView = new ListView<MonthlyDistribution>(
            InvoiceSales.Properties.monthlyDistribution.name()) {
        private static final long serialVersionUID = 1L;

        @Override/* w  ww  .  j  a  va 2 s. co m*/
        protected ListItem<MonthlyDistribution> newItem(int index, IModel<MonthlyDistribution> itemModel) {
            return new OddEvenListItem<MonthlyDistribution>(index, itemModel);
        }

        @Override
        protected void populateItem(final ListItem<MonthlyDistribution> item) {
            item.setModel(new CompoundPropertyModel<MonthlyDistribution>(item.getModelObject()));

            item.add(new Label(MonthlyDistribution.Properties.dateAsString.name()));

            Link<Void> link = new Link<Void>(LINK_TO_INVOICES) {
                private static final long serialVersionUID = 1L;

                @Override
                public void onClick() {
                    getAuthenticatedSession().clearInvoiceFilter();
                    InvoiceFilter invoiceFilter = getAuthenticatedSession().getInvoiceFilter();
                    invoiceFilter.setSystemUser(getSystemUser());
                    Date date = item.getModelObject().getDate();
                    invoiceFilter.setInvoiceDateFrom(date);
                    invoiceFilter.setInvoiceDateTo(DateUtils.addDays(DateUtils.addMonths(date, 1), -1));
                    setResponsePage(new InvoicesPage());
                }
            };
            link.add(new Label(MonthlyDistribution.Properties.count.name()));
            item.add(link);

            item.add(new Label(MonthlyDistribution.Properties.salesAmounts.name() + Condition.SEPARATOR
                    + SalesAmounts.Properties.saleAsString.name()));
            item.add(new Label(MonthlyDistribution.Properties.salesAmounts.name() + Condition.SEPARATOR
                    + SalesAmounts.Properties.costAsString.name()));
            item.add(new Label(MonthlyDistribution.Properties.salesAmounts.name() + Condition.SEPARATOR
                    + SalesAmounts.Properties.commissionAsString.name()));
            item.add(new Label(MonthlyDistribution.Properties.salesAmounts.name() + Condition.SEPARATOR
                    + SalesAmounts.Properties.commissionReceivedAsString.name()));
            item.add(new Label(MonthlyDistribution.Properties.salesAmounts.name() + Condition.SEPARATOR
                    + SalesAmounts.Properties.commissionVerifiedAsString.name()));
            item.add(new Label(MonthlyDistribution.Properties.salesAmounts.name() + Condition.SEPARATOR
                    + SalesAmounts.Properties.taxOnCommissionAsString.name()));
            item.add(new Label(MonthlyDistribution.Properties.salesAmounts.name() + Condition.SEPARATOR
                    + SalesAmounts.Properties.paidAsString.name()));
        }
    };
    add(listView);

    Link<String> downloadLink = new Link<String>(EXPORT_TO_CSV, Model.of(makeCsvOutput())) {
        private static final long serialVersionUID = 1L;

        @Override
        public void onClick() {
            StringBufferResourceStream resourceStream = new StringBufferResourceStream("text/csv");
            resourceStream.append((CharSequence) getDefaultModelObject());

            ResourceStreamRequestHandler handler = new ResourceStreamRequestHandler(resourceStream)
                    .setFileName("export.csv").setContentDisposition(ContentDisposition.ATTACHMENT);

            getRequestCycle().scheduleRequestHandlerAfterCurrent(handler);
        }
    };
    downloadLink.setVisible(!getSales().getMonthlyDistribution().isEmpty());
    add(downloadLink);
}

From source file:com.axway.ats.testexplorer.pages.testcase.statistics.CsvWriter.java

License:Apache License

public DownloadLink getDownloadChartDataLink() {

    final String fileName = "chartDataFile.csv";

    downloadFile = new DownloadLink("download", new File(fileName)) {

        private static final long serialVersionUID = 1L;

        @Override//from   ww w.j  a v  a 2 s  .  c o m
        public void onClick() {

            IResourceStream resourceStream = new FileResourceStream(
                    new org.apache.wicket.util.file.File(generateFile(fileName)));
            getRequestCycle()
                    .scheduleRequestHandlerAfterCurrent(new ResourceStreamRequestHandler(resourceStream) {
                    }.setFileName(fileName).setContentDisposition(ContentDisposition.ATTACHMENT));
            downloadFile.setDeleteAfterDownload(true);
        }
    };

    return downloadFile;
}

From source file:com.evolveum.midpoint.web.component.AbstractAjaxDownloadBehavior.java

License:Apache License

public void onRequest() {

    IResourceStream resourceStream = getResourceStream();
    if (resourceStream == null) {
        return; // We hope the error was already processed and will be shown.
    }/*from  w w w .ja v a 2 s.c  o  m*/

    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);
}

From source file:com.evolveum.midpoint.web.component.AjaxDownloadBehaviorFromFile.java

License:Apache License

public void onRequest() {
    final File file = initFile();
    IResourceStream resourceStream = new FileResourceStream(new File(file));
    getComponent().getRequestCycle()/*from  w  w  w  . j av  a 2 s .  co m*/
            .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));
}

From source file:com.evolveum.midpoint.web.component.AjaxDownloadBehaviorFromStream.java

License:Apache License

public void onRequest() {
    final InputStream byteStream = initStream();

    if (byteStream == null) {
        return;//ww  w . ja va 2 s.c  o  m
    }

    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();
        }

    };
    getComponent().getRequestCycle()
            .scheduleRequestHandlerAfterCurrent(new ResourceStreamRequestHandler(resourceStream) {
                @Override
                public void respond(IRequestCycle requestCycle) {
                    super.respond(requestCycle);
                }
            }.setContentDisposition(ContentDisposition.ATTACHMENT).setCacheDuration(Duration.ONE_SECOND));
}