Example usage for org.apache.wicket.util.file Files remove

List of usage examples for org.apache.wicket.util.file Files remove

Introduction

In this page you can find the example usage for org.apache.wicket.util.file Files remove.

Prototype

public static boolean remove(final java.io.File file) 

Source Link

Document

Deletes a normal file.

Usage

From source file:at.ac.tuwien.ifs.tita.ui.importing.effort.csv.EffortImportCSVPage.java

License:Apache License

/**
 * Check whether the file already exists, and if so, try to delete it.
 *
 * @param newFile//from   ww  w  .  j  a v a2s  .  c  om
 *            the file to check
 */
private void checkFileExists(File newFile) {
    if (newFile.exists()) {
        // Try to delete the file
        if (!Files.remove(newFile)) {
            throw new IllegalStateException("Unable to overwrite " + newFile.getAbsolutePath());
        }
    }
}

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  av  a2  s  . com*/
    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//  ww w  . ja  v a2  s  . 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/*from  www  .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);//from  ww  w  . j a  v a 2  s  .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: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()/*ww  w .  java2  s.  c om*/
            .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.marc.lastweek.business.services.images.impl.ImageServiceImpl.java

License:Open Source License

public void saveAllImages(Folder temporalFolder) {
    Folder folder = createSaveFolder(temporalFolder);

    List<File> files = Arrays.asList(temporalFolder.listFiles());

    if (files != null) {

        for (File file : files) {
            // Create a new file
            File newFile = new File(folder, file.getName());

            // Check new file, delete if it allready existed
            checkFileExists(newFile);//  w  ww.  ja v a 2  s .c o  m
            try {

                //TODO check if creation succed
                Files.writeTo(newFile, new FileInputStream(file));
                Files.remove(file);

            } catch (Exception e) {
                throw new IllegalStateException("Unable to write file");
            }
        }
        temporalFolder.delete();
    }

}

From source file:com.marc.lastweek.business.services.images.impl.ImageServiceImpl.java

License:Open Source License

protected void checkFileExists(File newFile) {
    if (newFile.exists()) {
        // Try to delete the file
        if (!Files.remove(newFile)) {
            throw new IllegalStateException("Unable to overwrite " + newFile.getAbsolutePath());
        }/*from   ww w. ja  v  a 2  s .  co  m*/
    }
}

From source file:cz.zcu.kiv.eegdatabase.wui.components.utils.FileUtils.java

License:Apache License

/**
 * Prepared request handler for file download.
 * //ww w  . j a va  2  s .  co m
 * @param file
 * @return
 */
public static ResourceStreamRequestHandler prepareDownloadFile(final FileDTO file) {

    if (file == null || file.getFile() == null)
        return null;

    IResourceStream stream = new FileResourceStream(file.getFile());

    ResourceStreamRequestHandler resourceStreamRequestHandler = new ResourceStreamRequestHandler(stream) {

        @Override
        public void detach(IRequestCycle requestCycle) {
            super.detach(requestCycle);

            if (!Files.remove(file.getFile())) {
                log.error("Temporary file " + file.getFile().getAbsolutePath() + " cannot be deleted.");
            }
        }
    };
    return resourceStreamRequestHandler.setFileName(file.getFileName())
            .setContentDisposition(ContentDisposition.ATTACHMENT);
}