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;
}
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;
}
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;
}
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());
}
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);
}
Aggregations