Search in sources :

Example 1 with ExecuteScriptType

use of com.evolveum.midpoint.xml.ns._public.model.scripting_3.ExecuteScriptType in project midpoint by Evolveum.

the class ScriptExecutionHandlerDto method getScript.

public String getScript() {
    ExecuteScriptType script = taskDto.getExtensionPropertyRealValue(SchemaConstants.SE_EXECUTE_SCRIPT, ExecuteScriptType.class);
    if (script == null) {
        return null;
    }
    PrismContext prismContext = ((MidPointApplication) Application.get()).getPrismContext();
    try {
        return WebXmlUtil.stripNamespaceDeclarations(prismContext.xmlSerializer().serializeAnyData(script, SchemaConstants.SE_EXECUTE_SCRIPT));
    } catch (SchemaException e) {
        throw new SystemException("Couldn't serialize script: " + e.getMessage(), e);
    }
}
Also used : MidPointApplication(com.evolveum.midpoint.web.security.MidPointApplication) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ExecuteScriptType(com.evolveum.midpoint.xml.ns._public.model.scripting_3.ExecuteScriptType) SystemException(com.evolveum.midpoint.util.exception.SystemException) PrismContext(com.evolveum.midpoint.prism.PrismContext)

Example 2 with ExecuteScriptType

use of com.evolveum.midpoint.xml.ns._public.model.scripting_3.ExecuteScriptType in project midpoint by Evolveum.

the class ScriptExecutionTaskHandler method run.

@Override
public TaskRunResult run(Task task) {
    OperationResult result = task.getResult().createSubresult(DOT_CLASS + "run");
    TaskRunResult runResult = new TaskRunResult();
    PrismProperty<ExecuteScriptType> executeScriptProperty = task.getExtensionProperty(SchemaConstants.SE_EXECUTE_SCRIPT);
    if (executeScriptProperty == null || executeScriptProperty.getValue().getValue() == null || executeScriptProperty.getValue().getValue().getScriptingExpression() == null) {
        throw new IllegalStateException("There's no script to be run in task " + task + " (property " + SchemaConstants.SE_EXECUTE_SCRIPT + ")");
    }
    try {
        task.startCollectingOperationStatsFromZero(true, false, true);
        task.setProgress(0);
        ScriptExecutionResult executionResult = scriptingService.evaluateExpression(executeScriptProperty.getRealValue(), task, result);
        LOGGER.debug("Execution output: {} item(s)", executionResult.getDataOutput().size());
        LOGGER.debug("Execution result:\n", executionResult.getConsoleOutput());
        result.computeStatus();
        runResult.setRunResultStatus(TaskRunResult.TaskRunResultStatus.FINISHED);
    } catch (ScriptExecutionException | SecurityViolationException | SchemaException e) {
        result.recordFatalError("Couldn't execute script: " + e.getMessage(), e);
        LoggingUtils.logUnexpectedException(LOGGER, "Couldn't execute script", e);
        runResult.setRunResultStatus(TaskRunResult.TaskRunResultStatus.PERMANENT_ERROR);
    } finally {
        task.storeOperationStats();
    }
    task.getResult().computeStatus();
    runResult.setOperationResult(task.getResult());
    // incremented directly in actions implementations
    runResult.setProgress(task.getProgress());
    return runResult;
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) TaskRunResult(com.evolveum.midpoint.task.api.TaskRunResult) ExecuteScriptType(com.evolveum.midpoint.xml.ns._public.model.scripting_3.ExecuteScriptType) SecurityViolationException(com.evolveum.midpoint.util.exception.SecurityViolationException) ScriptExecutionException(com.evolveum.midpoint.model.api.ScriptExecutionException) ScriptExecutionResult(com.evolveum.midpoint.model.api.ScriptExecutionResult) OperationResult(com.evolveum.midpoint.schema.result.OperationResult)

Example 3 with ExecuteScriptType

use of com.evolveum.midpoint.xml.ns._public.model.scripting_3.ExecuteScriptType in project midpoint by Evolveum.

the class ModelRestService method executeScript.

@POST
@Path("/rpc/executeScript")
//	@Produces({"text/html", "application/xml"})
@Consumes({ "application/xml" })
public Response executeScript(@Convertor(ExecuteScriptConvertor.class) ExecuteScriptType command, @QueryParam("asynchronous") Boolean asynchronous, @Context UriInfo uriInfo, @Context MessageContext mc) {
    Task task = RestServiceUtil.initRequest(mc);
    OperationResult result = task.getResult().createSubresult(OPERATION_EXECUTE_SCRIPT);
    Response response;
    try {
        if (Boolean.TRUE.equals(asynchronous)) {
            scriptingService.evaluateExpression(command, task, result);
            URI resourceUri = uriInfo.getAbsolutePathBuilder().path(task.getOid()).build(task.getOid());
            response = RestServiceUtil.createResponse(Response.Status.CREATED, resourceUri, result);
        } else {
            ScriptExecutionResult executionResult = scriptingService.evaluateExpression(command, task, result);
            ExecuteScriptResponseType responseData = new ExecuteScriptResponseType().result(result.createOperationResultType()).output(new ExecuteScriptOutputType().consoleOutput(executionResult.getConsoleOutput()).dataOutput(ModelWebService.prepareXmlData(executionResult.getDataOutput(), command.getOptions())));
            response = RestServiceUtil.createResponse(Response.Status.OK, responseData, result);
        }
    } catch (Exception ex) {
        response = RestServiceUtil.handleException(result, ex);
        LoggingUtils.logUnexpectedException(LOGGER, "Couldn't execute script.", ex);
    }
    result.computeStatus();
    finishRequest(task);
    return response;
}
Also used : Task(com.evolveum.midpoint.task.api.Task) ExecuteScriptOutputType(com.evolveum.midpoint.xml.ns._public.model.scripting_3.ExecuteScriptOutputType) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) URI(java.net.URI) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 4 with ExecuteScriptType

use of com.evolveum.midpoint.xml.ns._public.model.scripting_3.ExecuteScriptType in project midpoint by Evolveum.

the class PageBulkAction method startPerformed.

private void startPerformed(AjaxRequestTarget target) {
    Task task = createSimpleTask(OPERATION_PERFORM_BULK);
    OperationResult result = new OperationResult(OPERATION_PERFORM_BULK);
    BulkActionDto bulkActionDto = model.getObject();
    if (StringUtils.isEmpty(bulkActionDto.getScript())) {
        warn(getString("PageBulkAction.message.emptyString"));
        target.add(getFeedbackPanel());
        return;
    }
    Object parsed = null;
    try {
        parsed = getPrismContext().parserFor(bulkActionDto.getScript()).parseRealValue();
        if (parsed == null) {
            result.recordFatalError("No bulk action object was provided.");
        } else if (!(parsed instanceof ExecuteScriptType) && !(parsed instanceof ScriptingExpressionType)) {
            result.recordFatalError("Provided text is not a bulk action object. An instance of {scripting-3}ScriptingExpressionType is expected; you have provided " + parsed.getClass() + " instead.");
        }
    } catch (SchemaException | RuntimeException e) {
        result.recordFatalError("Couldn't parse bulk action object", e);
        LoggingUtils.logUnexpectedException(LOGGER, "Couldn't parse bulk action object", e);
    }
    if (parsed != null) {
        if (bulkActionDto.isAsync()) {
            try {
                if (parsed instanceof ExecuteScriptType) {
                    getScriptingService().evaluateExpressionInBackground((ExecuteScriptType) parsed, task, result);
                } else {
                    //noinspection ConstantConditions
                    getScriptingService().evaluateExpressionInBackground((ScriptingExpressionType) parsed, task, result);
                }
                result.recordStatus(OperationResultStatus.IN_PROGRESS, task.getName() + " has been successfully submitted to execution");
            } catch (SchemaException | SecurityViolationException e) {
                result.recordFatalError("Couldn't submit bulk action to execution", e);
                LoggingUtils.logUnexpectedException(LOGGER, "Couldn't submit bulk action to execution", e);
            }
        } else {
            try {
                //noinspection ConstantConditions
                ScriptExecutionResult executionResult = parsed instanceof ExecuteScriptType ? getScriptingService().evaluateExpression((ExecuteScriptType) parsed, task, result) : getScriptingService().evaluateExpression((ScriptingExpressionType) parsed, task, result);
                result.recordStatus(OperationResultStatus.SUCCESS, "Action executed. Returned " + executionResult.getDataOutput().size() + " item(s). Console and data output available via 'Export to XML' function.");
                result.addReturn("console", executionResult.getConsoleOutput());
                result.addCollectionOfSerializablesAsReturn("data", executionResult.getDataOutput());
            } catch (ScriptExecutionException | SchemaException | SecurityViolationException e) {
                result.recordFatalError("Couldn't execute bulk action", e);
                LoggingUtils.logUnexpectedException(LOGGER, "Couldn't execute bulk action", e);
            }
        }
    }
    showResult(result);
    target.add(getFeedbackPanel());
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) Task(com.evolveum.midpoint.task.api.Task) ExecuteScriptType(com.evolveum.midpoint.xml.ns._public.model.scripting_3.ExecuteScriptType) SecurityViolationException(com.evolveum.midpoint.util.exception.SecurityViolationException) ScriptExecutionException(com.evolveum.midpoint.model.api.ScriptExecutionException) ScriptExecutionResult(com.evolveum.midpoint.model.api.ScriptExecutionResult) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ScriptingExpressionType(com.evolveum.midpoint.xml.ns._public.model.scripting_3.ScriptingExpressionType) BulkActionDto(com.evolveum.midpoint.web.page.admin.configuration.dto.BulkActionDto)

Aggregations

OperationResult (com.evolveum.midpoint.schema.result.OperationResult)3 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)3 ExecuteScriptType (com.evolveum.midpoint.xml.ns._public.model.scripting_3.ExecuteScriptType)3 ScriptExecutionException (com.evolveum.midpoint.model.api.ScriptExecutionException)2 ScriptExecutionResult (com.evolveum.midpoint.model.api.ScriptExecutionResult)2 Task (com.evolveum.midpoint.task.api.Task)2 SecurityViolationException (com.evolveum.midpoint.util.exception.SecurityViolationException)2 PrismContext (com.evolveum.midpoint.prism.PrismContext)1 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)1 TaskRunResult (com.evolveum.midpoint.task.api.TaskRunResult)1 SystemException (com.evolveum.midpoint.util.exception.SystemException)1 BulkActionDto (com.evolveum.midpoint.web.page.admin.configuration.dto.BulkActionDto)1 MidPointApplication (com.evolveum.midpoint.web.security.MidPointApplication)1 ExecuteScriptOutputType (com.evolveum.midpoint.xml.ns._public.model.scripting_3.ExecuteScriptOutputType)1 ScriptingExpressionType (com.evolveum.midpoint.xml.ns._public.model.scripting_3.ScriptingExpressionType)1 URI (java.net.URI)1