Search in sources :

Example 6 with File

use of org.apache.wicket.util.file.File in project midpoint by Evolveum.

the class PageDebugDownloadBehaviour method initFile.

@Override
protected File initFile() {
    PageBase page = getPage();
    OperationResult result = new OperationResult(OPERATION_CREATE_DOWNLOAD_FILE);
    MidPointApplication application = page.getMidpointApplication();
    WebApplicationConfiguration config = application.getWebApplicationConfiguration();
    File folder = new File(config.getExportFolder());
    if (!folder.exists() || !folder.isDirectory()) {
        folder.mkdir();
    }
    String suffix = isUseZip() ? "zip" : "xml";
    String fileName = "ExportedData_" + getType().getSimpleName() + "_" + System.currentTimeMillis() + "." + suffix;
    File file = new File(folder, fileName);
    Writer writer = null;
    try {
        LOGGER.debug("Creating file '{}'.", new Object[] { file.getAbsolutePath() });
        writer = createWriter(file);
        LOGGER.debug("Exporting objects.");
        dumpHeader(writer);
        dumpObjectsToStream(writer, result);
        dumpFooter(writer);
        LOGGER.debug("Export finished.");
        result.recomputeStatus();
    } catch (Exception ex) {
        LoggingUtils.logUnexpectedException(LOGGER, "Couldn't init download link", ex);
        result.recordFatalError("Couldn't init download link", ex);
    } finally {
        if (writer != null) {
            IOUtils.closeQuietly(writer);
        }
    }
    if (!WebComponentUtil.isSuccessOrHandledError(result)) {
        page.showResult(result);
        page.getSession().error(page.getString("pageDebugList.message.createFileException"));
        LOGGER.debug("Removing file '{}'.", new Object[] { file.getAbsolutePath() });
        Files.remove(file);
        throw new RestartResponseException(PageError.class);
    }
    return file;
}
Also used : MidPointApplication(com.evolveum.midpoint.web.security.MidPointApplication) RestartResponseException(org.apache.wicket.RestartResponseException) WebApplicationConfiguration(com.evolveum.midpoint.web.security.WebApplicationConfiguration) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) PageBase(com.evolveum.midpoint.gui.api.page.PageBase) File(org.apache.wicket.util.file.File) AjaxDownloadBehaviorFromFile(com.evolveum.midpoint.web.component.AjaxDownloadBehaviorFromFile) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) RestartResponseException(org.apache.wicket.RestartResponseException) SystemException(com.evolveum.midpoint.util.exception.SystemException)

Example 7 with File

use of org.apache.wicket.util.file.File in project midpoint by Evolveum.

the class PageNewReport method importReportFromFilePerformed.

private void importReportFromFilePerformed(AjaxRequestTarget target) {
    OperationResult result = new OperationResult(OPERATION_IMPORT_REPORT);
    FileUploadField file = (FileUploadField) get(createComponentPath(ID_MAIN_FORM, ID_INPUT, ID_INPUT_FILE, ID_FILE_INPUT));
    final FileUpload uploadedFile = file.getFileUpload();
    if (uploadedFile == null) {
        error(getString("PageNewReport.message.nullFile"));
        target.add(getFeedbackPanel());
        return;
    }
    InputStream stream = null;
    File newFile = null;
    try {
        // Create new file
        MidPointApplication application = getMidpointApplication();
        WebApplicationConfiguration config = application.getWebApplicationConfiguration();
        File folder = new File(config.getImportFolder());
        if (!folder.exists() || !folder.isDirectory()) {
            folder.mkdir();
        }
        newFile = new File(folder, uploadedFile.getClientFileName());
        // Check new file, delete if it already exists
        if (newFile.exists()) {
            newFile.delete();
        }
        // Save file
        //            Task task = createSimpleTask(OPERATION_IMPORT_FILE);
        newFile.createNewFile();
        uploadedFile.writeTo(newFile);
        InputStreamReader reader = new InputStreamReader(new FileInputStream(newFile), "utf-8");
        //            reader.
        stream = new ReaderInputStream(reader, reader.getEncoding());
        byte[] reportIn = IOUtils.toByteArray(stream);
        setResponsePage(new PageReport(new ReportDto(Base64.encodeBase64(reportIn))));
    } catch (Exception ex) {
        result.recordFatalError("Couldn't import file.", ex);
        LoggingUtils.logUnexpectedException(LOGGER, "Couldn't import file", ex);
    } finally {
        if (stream != null) {
            IOUtils.closeQuietly(stream);
        }
        if (newFile != null) {
            FileUtils.deleteQuietly(newFile);
        }
    }
    showResult(result);
    target.add(getFeedbackPanel());
}
Also used : InputStreamReader(java.io.InputStreamReader) ReaderInputStream(org.apache.commons.io.input.ReaderInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ReportDto(com.evolveum.midpoint.web.page.admin.reports.dto.ReportDto) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) FileInputStream(java.io.FileInputStream) FileUploadField(org.apache.wicket.markup.html.form.upload.FileUploadField) MidPointApplication(com.evolveum.midpoint.web.security.MidPointApplication) ReaderInputStream(org.apache.commons.io.input.ReaderInputStream) WebApplicationConfiguration(com.evolveum.midpoint.web.security.WebApplicationConfiguration) File(org.apache.wicket.util.file.File) FileUpload(org.apache.wicket.markup.html.form.upload.FileUpload)

Example 8 with File

use of org.apache.wicket.util.file.File in project midpoint by Evolveum.

the class PageAccounts method downloadPerformed.

private void downloadPerformed(AjaxRequestTarget target, String fileName, AjaxDownloadBehaviorFromFile downloadBehavior) {
    MidpointConfiguration config = getMidpointConfiguration();
    downloadFile = new File(config.getMidpointHome() + "/export/" + fileName);
    downloadBehavior.initiate(target);
}
Also used : MidpointConfiguration(com.evolveum.midpoint.common.configuration.api.MidpointConfiguration) AjaxDownloadBehaviorFromFile(com.evolveum.midpoint.web.component.AjaxDownloadBehaviorFromFile) File(org.apache.wicket.util.file.File)

Example 9 with File

use of org.apache.wicket.util.file.File in project midpoint by Evolveum.

the class PageAccounts method createFilesModel.

private LoadableModel<List<String>> createFilesModel() {
    return new LoadableModel<List<String>>(false) {

        @Override
        protected List<String> load() {
            String[] filesArray;
            try {
                MidpointConfiguration config = getMidpointConfiguration();
                File exportFolder = new File(config.getMidpointHome() + "/export");
                filesArray = exportFolder.list(new FilenameFilter() {

                    @Override
                    public boolean accept(java.io.File dir, String name) {
                        return name.endsWith("xml");
                    }
                });
            } catch (Exception ex) {
                LoggingUtils.logUnexpectedException(LOGGER, "Couldn't list files", ex);
                getSession().error("Couldn't list files, reason: " + ex.getMessage());
                throw new RestartResponseException(PageDashboard.class);
            }
            if (filesArray == null) {
                return new ArrayList<>();
            }
            List<String> list = Arrays.asList(filesArray);
            Collections.sort(list);
            return list;
        }
    };
}
Also used : FilenameFilter(java.io.FilenameFilter) RestartResponseException(org.apache.wicket.RestartResponseException) MidpointConfiguration(com.evolveum.midpoint.common.configuration.api.MidpointConfiguration) LoadableModel(com.evolveum.midpoint.gui.api.model.LoadableModel) ArrayList(java.util.ArrayList) PageDashboard(com.evolveum.midpoint.web.page.admin.home.PageDashboard) AjaxDownloadBehaviorFromFile(com.evolveum.midpoint.web.component.AjaxDownloadBehaviorFromFile) File(org.apache.wicket.util.file.File) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) RestartResponseException(org.apache.wicket.RestartResponseException) IOException(java.io.IOException) CommonException(com.evolveum.midpoint.util.exception.CommonException) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException)

Aggregations

File (org.apache.wicket.util.file.File)9 AjaxDownloadBehaviorFromFile (com.evolveum.midpoint.web.component.AjaxDownloadBehaviorFromFile)5 MidpointConfiguration (com.evolveum.midpoint.common.configuration.api.MidpointConfiguration)4 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)4 RestartResponseException (org.apache.wicket.RestartResponseException)4 CommonException (com.evolveum.midpoint.util.exception.CommonException)3 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)3 MidPointApplication (com.evolveum.midpoint.web.security.MidPointApplication)3 WebApplicationConfiguration (com.evolveum.midpoint.web.security.WebApplicationConfiguration)3 IOException (java.io.IOException)3 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)2 FileInputStream (java.io.FileInputStream)2 InputStreamReader (java.io.InputStreamReader)2 ReaderInputStream (org.apache.commons.io.input.ReaderInputStream)2 FileUpload (org.apache.wicket.markup.html.form.upload.FileUpload)2 LoadableModel (com.evolveum.midpoint.gui.api.model.LoadableModel)1 PageBase (com.evolveum.midpoint.gui.api.page.PageBase)1 SystemException (com.evolveum.midpoint.util.exception.SystemException)1 PageDashboard (com.evolveum.midpoint.web.page.admin.home.PageDashboard)1 ReportDto (com.evolveum.midpoint.web.page.admin.reports.dto.ReportDto)1