List of usage examples for org.apache.wicket.request.resource ContentDisposition ATTACHMENT
ContentDisposition ATTACHMENT
To view the source code for org.apache.wicket.request.resource ContentDisposition ATTACHMENT.
Click Source Link
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;/*w ww .j av a 2 s . co m*/ 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 {/*from w w w. j ava2 s . c om*/ 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 .co m*/ * @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//from w w w. j av a2 s. c o m 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);/* w ww. ja v a 2 s. c om*/ 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/*from ww w. j a v a 2 s. c om*/ 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/* w w w .j av a 2 s .c om*/ 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.chitek.ignition.drivers.generictcp.meta.config.ui.MessageConfigUI.java
License:Apache License
private void addComponents() { // Select the first message for initial display if (getConfig().messages.isEmpty()) { // No messages configured. Create a new message with id=1. getConfig().addMessageConfig(new MessageConfig(1)); }//from ww w. j a va 2 s . co m currentMessage = getConfig().getMessageList().get(0); currentMessage.calcOffsets(getConfig().getMessageIdType().getByteSize()); currentMessageId = currentMessage.getMessageId(); // ******************************************************************************************* // *** Form for XML import final FileUploadField uploadField = new FileUploadField("upload-field", new ListModel<FileUpload>()); Form<?> uploadForm = new Form<Object>("upload-form") { @Override protected void onSubmit() { try { FileUpload upload = uploadField.getFileUpload(); if (upload != null) { handleOnUpload(upload.getInputStream()); } else { warn(new StringResourceModel("warn.noFileToUpload", this, null).getString()); } } catch (Exception e) { this.error(new StringResourceModel("import.error", this, null).getString() + " Exception: " + e.toString()); } } }; uploadForm.add(uploadField); SubmitLink importLink = new SubmitLink("import-link"); uploadForm.add(importLink); add(uploadForm); // ******************************************************************************************* // *** The message configuration currentMessageModel = new PropertyModel<MessageConfig>(this, "currentMessage"); editForm = new Form<MessageConfig>("edit-form", new CompoundPropertyModel<MessageConfig>(currentMessageModel)) { @Override protected void onError() { // Validation error - reset the message dropdown to the original value // Clear input causes the component to reload the model currentMessageIdDropdown.clearInput(); super.onError(); } }; editForm.add(new MessageFormValidator()); WebMarkupContainer tableContainer = new WebMarkupContainer("table-container"); messageIdTypeDropDown = getMessageIdTypeDropdown(); tableContainer.add(messageIdTypeDropDown); currentMessageIdDropdown = getCurrentMessageIdDropdown(); tableContainer.add(currentMessageIdDropdown); Button buttonNew = new Button("new"); buttonNew.add(new AjaxFormSubmitBehavior("onclick") { @Override protected void onSubmit(AjaxRequestTarget target) { handleNew(target); } @Override protected void onError(AjaxRequestTarget target) { handleError(target); } }); tableContainer.add(buttonNew); Button buttonCopy = new Button("copy"); buttonCopy.add(new AjaxFormSubmitBehavior("onclick") { @Override protected void onSubmit(AjaxRequestTarget target) { handleCopy(target); } @Override protected void onError(AjaxRequestTarget target) { handleError(target); } }); tableContainer.add(buttonCopy); Button deleteButton = new Button("delete"); deleteButton.add(new AjaxEventBehavior("onclick") { @Override protected void onEvent(AjaxRequestTarget target) { handleDelete(target); } }); tableContainer.add(deleteButton); messageTypeDropdown = getMessageTypeDropdown(); tableContainer.add(messageTypeDropdown); tableContainer.add(getQueueModeDropdown()); tableContainer.add(new CheckBox("usePersistance").setOutputMarkupId(true)); WebMarkupContainer listEditorContainer = new WebMarkupContainer("list-editor"); messageIdTextField = getMessageIdTextField(); listEditorContainer.add(messageIdTextField); messageAliasTextField = getMessageAliasTextField(); listEditorContainer.add(messageAliasTextField); // Create the list editor editor = new ListEditor<TagConfig>("tags", new PropertyModel<List<TagConfig>>(currentMessageModel, "tags")) { @Override protected void onPopulateItem(EditorListItem<TagConfig> item) { item.setModel(new CompoundPropertyModel<TagConfig>(item.getModelObject())); BinaryDataType dataType = item.getModelObject().getDataType(); boolean enable = dataType.isSpecial() ? false : true; // Offset is displayed only for information item.add(new Label("offset").setOutputMarkupId(true)); if (enable) { item.add(getIdTextField()); } else { // The static TextField has no validation. Validation would fail for special tags. item.add(getSpecialIdTextField().setEnabled(false)); } item.add(getAliasTextField().setVisible(enable)); item.add(getSizeTextField().setEnabled(dataType.isArrayAllowed())); item.add(getTagLengthTypeDropDown().setEnabled(dataType.supportsVariableLength())); item.add(getDataTypeDropdown()); // Create the edit links to be used in the list editor item.add(getInsertLink()); item.add(getDeleteLink()); item.add(getMoveUpLink().setVisible(item.getIndex() > 0)); item.add(getMoveDownLink().setVisible(item.getIndex() < getList().size() - 1)); } }; listEditorContainer.add(editor); Label noItemsLabel = new Label("no-items-label", new StringResourceModel("noitems", this, null)) { @Override public boolean isVisible() { return editor.getList().size() == 0; } }; listEditorContainer.add(noItemsLabel); listEditorContainer.add(new EditorSubmitLink("add-row-link") { @Override public void onSubmit() { editor.addItem(new TagConfig()); // Adjust the visibility of the edit links updateListEditor(editor); } }); listEditorContainer.setOutputMarkupId(true); tableContainer.add(listEditorContainer); editForm.add(tableContainer); // XML export SubmitLink exportLink = new SubmitLink("export-link", editForm) { @Override public void onSubmit() { ResourceStreamRequestHandler handler = new ResourceStreamRequestHandler(getResourceStream(), getFileName()); handler.setContentDisposition(ContentDisposition.ATTACHMENT); handler.setCacheDuration(Duration.NONE); getRequestCycle().scheduleRequestHandlerAfterCurrent(handler); } private String getFileName() { return String.format("MsgConfig_%s.xml", currentMessageModel.getObject().getMessageId()); } private IResourceStream getResourceStream() { String config = currentMessageModel.getObject().toXMLString(); return new StringResourceStream(config, "text/xml"); } }; editForm.add(exportLink); uploadForm.add(editForm); }
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. }// w ww .j a v a 2s .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()/* w ww .j av a 2s. c o 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)); }