Search in sources :

Example 1 with ExecuteScriptResponseType

use of com.evolveum.midpoint.xml.ns._public.common.api_types_3.ExecuteScriptResponseType 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 2 with ExecuteScriptResponseType

use of com.evolveum.midpoint.xml.ns._public.common.api_types_3.ExecuteScriptResponseType in project midpoint by Evolveum.

the class ModelRestController method executeScript.

@PostMapping("/rpc/executeScript")
public ResponseEntity<?> executeScript(@RequestParam(value = "asynchronous", required = false) Boolean asynchronous, @RequestBody ExecuteScriptType command) {
    Task task = initRequest();
    OperationResult result = task.getResult().createSubresult("executeScript");
    ResponseEntity<?> response;
    try {
        if (Boolean.TRUE.equals(asynchronous)) {
            scriptingService.evaluateExpressionInBackground(command, task, result);
            response = createResponseWithLocation(HttpStatus.CREATED, uriGetObject(ObjectTypes.TASK.getRestType(), task.getOid()), result);
        } else {
            ScriptExecutionResult executionResult = scriptingService.evaluateExpression(command, VariablesMap.emptyMap(), false, task, result);
            ExecuteScriptResponseType responseData = new ExecuteScriptResponseType().result(result.createOperationResultType()).output(new ExecuteScriptOutputType().consoleOutput(executionResult.getConsoleOutput()).dataOutput(PipelineData.prepareXmlData(executionResult.getDataOutput(), command.getOptions())));
            response = createResponse(HttpStatus.OK, responseData, result);
        }
    } catch (Exception ex) {
        LoggingUtils.logUnexpectedException(logger, "Couldn't execute script.", ex);
        response = handleExceptionNoLog(result, ex);
    }
    result.computeStatus();
    finishRequest(task, result);
    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)

Example 3 with ExecuteScriptResponseType

use of com.evolveum.midpoint.xml.ns._public.common.api_types_3.ExecuteScriptResponseType in project midpoint by Evolveum.

the class TestAbstractRestService method extractItemProcessingResults.

private <X extends OperationSpecificData> List<ItemProcessingResult<X>> extractItemProcessingResults(ExecuteScriptResponseType response, Function<Object, X> operationDataExtractor) throws SchemaException {
    List<PipelineItemType> outputItems = response.getOutput().getDataOutput().getItem();
    List<ItemProcessingResult<X>> extractedResults = new ArrayList<>(outputItems.size());
    for (PipelineItemType outputItem : outputItems) {
        ItemProcessingResult<X> extractedResult = new ItemProcessingResult<>();
        Object value = outputItem.getValue();
        if (value instanceof RawType) {
            value = ((RawType) value).getParsedRealValue(Object.class);
        }
        if (value instanceof ObjectType) {
            ObjectType object = (ObjectType) value;
            extractedResult.oid = object.getOid();
            extractedResult.name = PolyString.getOrig(object.getName());
            if (operationDataExtractor != null) {
                extractedResult.data = operationDataExtractor.apply(value);
            }
        } else if (value instanceof Referencable) {
            extractedResult.oid = ((Referencable) value).getOid();
        } else {
            throw new IllegalStateException("Unexpected item value: " + value);
        }
        if (outputItem.getResult() != null) {
            extractedResult.status = outputItem.getResult().getStatus();
            extractedResult.message = outputItem.getResult().getMessage();
        }
        extractedResults.add(extractedResult);
    }
    return extractedResults;
}
Also used : Referencable(com.evolveum.midpoint.prism.Referencable) ArrayList(java.util.ArrayList) PipelineItemType(com.evolveum.midpoint.xml.ns._public.model.scripting_3.PipelineItemType) PrismObject(com.evolveum.midpoint.prism.PrismObject) RawType(com.evolveum.prism.xml.ns._public.types_3.RawType)

Example 4 with ExecuteScriptResponseType

use of com.evolveum.midpoint.xml.ns._public.common.api_types_3.ExecuteScriptResponseType in project midpoint by Evolveum.

the class TestAbstractRestService method test530ModifyValidToUsingScripting.

@Test
public void test530ModifyValidToUsingScripting() throws Exception {
    WebClient client = prepareClient();
    client.path("/rpc/executeScript");
    when();
    Response response = client.post(getRepoFile(SCRIPT_MODIFY_VALID_TO));
    then();
    displayResponse(response);
    traceResponse(response);
    assertEquals("Expected 200 but got " + response.getStatus(), 200, response.getStatus());
    displayDumpable("Audit", getDummyAuditService());
    getDummyAuditService().assertRecords(4);
    getDummyAuditService().assertLoginLogout(SchemaConstants.CHANNEL_REST_URI);
    ExecuteScriptResponseType responseData = response.readEntity(ExecuteScriptResponseType.class);
    displayValue("Response", getPrismContext().xmlSerializer().serializeRealValue(responseData));
    List<PipelineItemType> items = responseData.getOutput().getDataOutput().getItem();
    assertEquals("Wrong # of processed items", 2, items.size());
    List<ItemProcessingResult<OperationSpecificData>> extractedResults = extractItemProcessingResults(responseData, null);
    display("extractedResults", extractedResults);
    assertEquals("Wrong # of extractedResults", 2, extractedResults.size());
    ItemProcessingResult<OperationSpecificData> first = extractedResults.get(0);
    assertEquals("Wrong OID in first result", "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX", first.oid);
    assertEquals("Wrong status in first result", OperationResultStatusType.FATAL_ERROR, first.status);
    ItemProcessingResult<OperationSpecificData> second = extractedResults.get(1);
    assertEquals("Wrong OID in second result", USER_JACK_OID, second.oid);
    assertEquals("Wrong name in second result", "jack", second.name);
    assertEquals("Wrong status in second result", OperationResultStatusType.SUCCESS, second.status);
    UserType jackAfter = getRepositoryService().getObject(UserType.class, USER_JACK_OID, null, new OperationResult("getObject")).asObjectable();
    display("jack after", jackAfter);
    XMLGregorianCalendar expectedValidTo = XmlTypeConverter.createXMLGregorianCalendar("2018-08-31T00:00:00.000+00:00");
    assertEquals("Wrong validTo", expectedValidTo, jackAfter.getActivation().getValidTo());
}
Also used : Response(javax.ws.rs.core.Response) ExecuteScriptResponseType(com.evolveum.midpoint.xml.ns._public.common.api_types_3.ExecuteScriptResponseType) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) PipelineItemType(com.evolveum.midpoint.xml.ns._public.model.scripting_3.PipelineItemType) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) WebClient(org.apache.cxf.jaxrs.client.WebClient) Test(org.testng.annotations.Test)

Example 5 with ExecuteScriptResponseType

use of com.evolveum.midpoint.xml.ns._public.common.api_types_3.ExecuteScriptResponseType in project midpoint by Evolveum.

the class TestAbstractRestService method test520GeneratePasswordsUsingScripting.

@Test
public void test520GeneratePasswordsUsingScripting() throws Exception {
    WebClient client = prepareClient();
    client.path("/rpc/executeScript");
    when();
    Response response = client.post(getRepoFile(SCRIPT_GENERATE_PASSWORDS));
    then();
    displayResponse(response);
    traceResponse(response);
    assertEquals("Expected 200 but got " + response.getStatus(), 200, response.getStatus());
    ExecuteScriptResponseType responseData = response.readEntity(ExecuteScriptResponseType.class);
    displayValue("Response", getPrismContext().xmlSerializer().serializeRealValue(responseData));
    logger.info("Response: {}", getPrismContext().xmlSerializer().serializeRealValue(responseData));
    List<PipelineItemType> items = responseData.getOutput().getDataOutput().getItem();
    assertEquals("Wrong # of processed items", 2, items.size());
    List<ItemProcessingResult<PasswordGenerationData>> extractedResults = extractItemProcessingResults(responseData, new PasswordGenerationDataExtractor());
    display("extractedResults", extractedResults);
    assertEquals("Wrong # of extractedResults", 2, extractedResults.size());
    ItemProcessingResult<PasswordGenerationData> first = extractedResults.get(0);
    assertEquals("Wrong OID in first result", "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX", first.oid);
    assertEquals("Wrong status in first result", OperationResultStatusType.FATAL_ERROR, first.status);
    ItemProcessingResult<PasswordGenerationData> second = extractedResults.get(1);
    assertEquals("Wrong OID in second result", USER_JACK_OID, second.oid);
    assertEquals("Wrong name in second result", "jack", second.name);
    logger.info("pwd in second result {}", second.data.password);
    assertNotNull("Missing password in second result", second.data.password);
    assertEquals("Wrong status in second result", OperationResultStatusType.SUCCESS, second.status);
    UserType jackAfter = getRepositoryService().getObject(UserType.class, USER_JACK_OID, null, new OperationResult("getObject")).asObjectable();
    display("jack after", jackAfter);
    assertNotNull("password not set", jackAfter.getCredentials().getPassword().getValue());
    displayDumpable("Audit", getDummyAuditService());
    getDummyAuditService().assertRecords(4);
    getDummyAuditService().assertLoginLogout(SchemaConstants.CHANNEL_REST_URI);
}
Also used : Response(javax.ws.rs.core.Response) ExecuteScriptResponseType(com.evolveum.midpoint.xml.ns._public.common.api_types_3.ExecuteScriptResponseType) PipelineItemType(com.evolveum.midpoint.xml.ns._public.model.scripting_3.PipelineItemType) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) WebClient(org.apache.cxf.jaxrs.client.WebClient) Test(org.testng.annotations.Test)

Aggregations

OperationResult (com.evolveum.midpoint.schema.result.OperationResult)4 PipelineItemType (com.evolveum.midpoint.xml.ns._public.model.scripting_3.PipelineItemType)3 Task (com.evolveum.midpoint.task.api.Task)2 ExecuteScriptResponseType (com.evolveum.midpoint.xml.ns._public.common.api_types_3.ExecuteScriptResponseType)2 ExecuteScriptOutputType (com.evolveum.midpoint.xml.ns._public.model.scripting_3.ExecuteScriptOutputType)2 Response (javax.ws.rs.core.Response)2 WebClient (org.apache.cxf.jaxrs.client.WebClient)2 Test (org.testng.annotations.Test)2 PrismObject (com.evolveum.midpoint.prism.PrismObject)1 Referencable (com.evolveum.midpoint.prism.Referencable)1 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)1 RawType (com.evolveum.prism.xml.ns._public.types_3.RawType)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)1