Search in sources :

Example 1 with WebApplicationConfiguration

use of com.evolveum.midpoint.web.security.WebApplicationConfiguration in project midpoint by Evolveum.

the class PageImportObject method getInputStream.

private InputStream getInputStream(boolean raw) throws Exception {
    if (raw) {
        return IOUtils.toInputStream(xmlEditorModel.getObject(), "utf-8");
    }
    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();
        }
        FileUpload uploadedFile = getUploadedFile();
        newFile = new File(folder, uploadedFile.getClientFileName());
        // Check new file, delete if it already exists
        if (newFile.exists()) {
            newFile.delete();
        }
        // Save file
        newFile.createNewFile();
        uploadedFile.writeTo(newFile);
        InputStreamReader reader = new InputStreamReader(new FileInputStream(newFile), "utf-8");
        return new ReaderInputStream(reader, reader.getEncoding());
    } finally {
        if (newFile != null) {
            FileUtils.deleteQuietly(newFile);
        }
    }
}
Also used : MidPointApplication(com.evolveum.midpoint.web.security.MidPointApplication) ReaderInputStream(org.apache.commons.io.input.ReaderInputStream) InputStreamReader(java.io.InputStreamReader) WebApplicationConfiguration(com.evolveum.midpoint.web.security.WebApplicationConfiguration) File(org.apache.wicket.util.file.File) FileUpload(org.apache.wicket.markup.html.form.upload.FileUpload) FileInputStream(java.io.FileInputStream)

Example 2 with WebApplicationConfiguration

use of com.evolveum.midpoint.web.security.WebApplicationConfiguration in project midpoint by Evolveum.

the class PageImportObject method getInputDescription.

@NotNull
private InputDescription getInputDescription(boolean editor) throws Exception {
    if (editor) {
        return new InputDescription(IOUtils.toInputStream(xmlEditorModel.getObject(), StandardCharsets.UTF_8), dataLanguage);
    }
    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();
        }
        FileUpload uploadedFile = getUploadedFile();
        newFile = new File(folder, uploadedFile.getClientFileName());
        // Check new file, delete if it already exists
        if (newFile.exists()) {
            newFile.delete();
        }
        // Save file
        newFile.createNewFile();
        FileUtils.copyInputStreamToFile(uploadedFile.getInputStream(), newFile);
        String language = getPrismContext().detectLanguage(newFile);
        return new InputDescription(new FileInputStream(newFile), language);
    } finally {
        if (newFile != null) {
            FileUtils.deleteQuietly(newFile);
        }
    }
}
Also used : MidPointApplication(com.evolveum.midpoint.web.security.MidPointApplication) WebApplicationConfiguration(com.evolveum.midpoint.web.security.WebApplicationConfiguration) File(org.apache.wicket.util.file.File) FileUpload(org.apache.wicket.markup.html.form.upload.FileUpload) FileInputStream(java.io.FileInputStream) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with WebApplicationConfiguration

use of com.evolveum.midpoint.web.security.WebApplicationConfiguration in project midpoint by Evolveum.

the class ProgressReporter method create.

/**
     * Creates and initializes a progress reporter instance. Should be called during initialization
     * of respective wicket page.
     *
     * @param parentPage The parent page (user, org, role, ...)
     * @param id Wicket ID of the progress panel
     * @return Progress reporter instance
     */
public static ProgressReporter create(String id, ProgressReportingAwarePage parentPage) {
    ProgressReporter reporter = new ProgressReporter();
    reporter.progressPanel = new ProgressPanel(id, new Model<>(new ProgressDto()), reporter, parentPage);
    reporter.progressPanel.setOutputMarkupId(true);
    reporter.progressPanel.hide();
    WebApplicationConfiguration config = parentPage.getWebApplicationConfiguration();
    reporter.refreshInterval = config.getProgressRefreshInterval();
    reporter.asynchronousExecution = config.isProgressReportingEnabled();
    reporter.abortEnabled = config.isAbortEnabled();
    reporter.parentPage = parentPage;
    return reporter;
}
Also used : Model(org.apache.wicket.model.Model) WebApplicationConfiguration(com.evolveum.midpoint.web.security.WebApplicationConfiguration)

Example 4 with WebApplicationConfiguration

use of com.evolveum.midpoint.web.security.WebApplicationConfiguration 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 5 with WebApplicationConfiguration

use of com.evolveum.midpoint.web.security.WebApplicationConfiguration in project midpoint by Evolveum.

the class ImportReportPopupPanel method importConfirmPerformed.

private void importConfirmPerformed(AjaxRequestTarget target, Model<String> nameModel, Model<String> fileStringImport) {
    String dataName;
    if (nameModel == null || StringUtils.isEmpty(nameModel.getObject())) {
        dataName = getModelObject().getName().getOrig() + "-IMPORT " + getDataTime();
    } else {
        dataName = nameModel.getObject();
    }
    // Create new file
    MidPointApplication application = getPageBase().getMidpointApplication();
    WebApplicationConfiguration config = application.getWebApplicationConfiguration();
    String midpointHome = System.getProperty(MidpointConfiguration.MIDPOINT_HOME_PROPERTY);
    File importDir = new File(midpointHome, "import");
    if (!importDir.exists() || !importDir.isDirectory()) {
        if (!importDir.mkdir()) {
            LOGGER.error("Couldn't create import dir {}", importDir);
            FeedbackAlerts feedback = getFeedbackPanel();
            feedback.error(getPageBase().createStringResource("ImportReportPopupPanel.message.error.createImportDir", importDir).getString());
            target.add(feedback);
            return;
        }
    }
    FileUpload uploadedFile = getUploadedFile();
    if (uploadedFile == null && StringUtils.isEmpty(fileStringImport.getObject())) {
        LOGGER.error("Please upload file for import");
        FeedbackAlerts feedback = getFeedbackPanel();
        feedback.error(getPageBase().createStringResource("ImportReportPopupPanel.message.error.uploadFile", importDir).getString());
        target.add(feedback);
        return;
    }
    String newFilePath;
    if (uploadedFile != null) {
        String fileName = FilenameUtils.removeExtension(uploadedFile.getClientFileName()) + " " + getDataTime() + "." + FilenameUtils.getExtension(uploadedFile.getClientFileName());
        File newFile = new File(importDir, fileName);
        // Check new file, delete if it already exists
        if (newFile.exists()) {
            newFile.delete();
        }
        try {
            newFile.createNewFile();
            FileUtils.copyInputStreamToFile(uploadedFile.getInputStream(), newFile);
            newFilePath = newFile.getAbsolutePath();
        } catch (IOException e) {
            LOGGER.error("Couldn't create new file " + newFile.getAbsolutePath(), e);
            FeedbackAlerts feedback = getFeedbackPanel();
            feedback.error(getPageBase().createStringResource("ImportReportPopupPanel.message.error.createImportFile", newFile.getAbsolutePath()).getString());
            target.add(feedback);
            return;
        }
    } else {
        newFilePath = new File(importDir, dataName).getAbsolutePath();
        try {
            Files.write(Paths.get(newFilePath), fileStringImport.getObject().getBytes());
        } catch (IOException e) {
            LOGGER.error("Couldn't create new file " + newFilePath, e);
            FeedbackAlerts feedback = getFeedbackPanel();
            feedback.error(getPageBase().createStringResource("ImportReportPopupPanel.message.error.createImportFile", newFilePath).getString());
            target.add(feedback);
            return;
        }
    }
    ReportDataType reportImportData = null;
    try {
        @NotNull PrismObject<ReportDataType> prismObject = getPrismContext().getSchemaRegistry().findObjectDefinitionByCompileTimeClass(ReportDataType.class).instantiate();
        reportImportData = prismObject.asObjectable();
    } catch (SchemaException e) {
        LOGGER.error("Couldn't instantiate new Report Data from definition", e);
        FeedbackAlerts feedback = getFeedbackPanel();
        feedback.error(getPageBase().createStringResource("ImportReportPopupPanel.message.error.createInstantiateReportData").getString());
        target.add(feedback);
        return;
    }
    reportImportData.setName(new PolyStringType(dataName));
    reportImportData.setFilePath(newFilePath);
    ObjectReferenceType reportRef = new ObjectReferenceType();
    reportRef.setType(ReportType.COMPLEX_TYPE);
    reportRef.setOid(getModelObject().getOid());
    reportImportData.setReportRef(reportRef);
    Collection<ObjectDelta<? extends ObjectType>> deltas = Collections.singleton(reportImportData.asPrismObject().createAddDelta());
    Task task = getPageBase().createSimpleTask(OPERATION_CREATE_REPORT_DATA);
    try {
        Collection<ObjectDeltaOperation<? extends ObjectType>> retDeltas = getPageBase().getModelService().executeChanges(deltas, null, task, task.getResult());
        reportImportData = (ReportDataType) retDeltas.iterator().next().getObjectDelta().getObjectToAdd().asObjectable();
    } catch (ObjectAlreadyExistsException e) {
        LOGGER.error("Report Data with name " + dataName + " already exists", e);
        FeedbackAlerts feedback = getFeedbackPanel();
        feedback.error(getPageBase().createStringResource("ImportReportPopupPanel.message.error.importReportDataAlreadyExists", dataName).getString());
        target.add(feedback);
        return;
    } catch (Exception e) {
        LOGGER.error("Couldn't create new Report Data with name " + dataName, e);
        FeedbackAlerts feedback = getFeedbackPanel();
        feedback.error(getPageBase().createStringResource("ImportReportPopupPanel.message.error.createImportReportData", dataName).getString());
        target.add(feedback);
        return;
    }
    importConfirmPerformed(target, reportImportData);
}
Also used : PolyStringType(com.evolveum.prism.xml.ns._public.types_3.PolyStringType) Task(com.evolveum.midpoint.task.api.Task) IOException(java.io.IOException) NotNull(org.jetbrains.annotations.NotNull) IOException(java.io.IOException) MidPointApplication(com.evolveum.midpoint.web.security.MidPointApplication) ObjectDeltaOperation(com.evolveum.midpoint.schema.ObjectDeltaOperation) FeedbackAlerts(com.evolveum.midpoint.web.component.message.FeedbackAlerts) WebApplicationConfiguration(com.evolveum.midpoint.web.security.WebApplicationConfiguration) ObjectDelta(com.evolveum.midpoint.prism.delta.ObjectDelta) File(org.apache.wicket.util.file.File) FileUpload(org.apache.wicket.markup.html.form.upload.FileUpload)

Aggregations

WebApplicationConfiguration (com.evolveum.midpoint.web.security.WebApplicationConfiguration)8 MidPointApplication (com.evolveum.midpoint.web.security.MidPointApplication)5 File (org.apache.wicket.util.file.File)5 FileUpload (org.apache.wicket.markup.html.form.upload.FileUpload)4 PageBase (com.evolveum.midpoint.gui.api.page.PageBase)3 FileInputStream (java.io.FileInputStream)3 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)2 InputStreamReader (java.io.InputStreamReader)2 ReaderInputStream (org.apache.commons.io.input.ReaderInputStream)2 NotNull (org.jetbrains.annotations.NotNull)2 ObjectDelta (com.evolveum.midpoint.prism.delta.ObjectDelta)1 ObjectDeltaOperation (com.evolveum.midpoint.schema.ObjectDeltaOperation)1 Task (com.evolveum.midpoint.task.api.Task)1 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)1 SystemException (com.evolveum.midpoint.util.exception.SystemException)1 AjaxDownloadBehaviorFromFile (com.evolveum.midpoint.web.component.AjaxDownloadBehaviorFromFile)1 FeedbackAlerts (com.evolveum.midpoint.web.component.message.FeedbackAlerts)1 ReportDto (com.evolveum.midpoint.web.page.admin.reports.dto.ReportDto)1 PolyStringType (com.evolveum.prism.xml.ns._public.types_3.PolyStringType)1 IOException (java.io.IOException)1