Search in sources :

Example 1 with OperationResultBuilder

use of com.evolveum.midpoint.schema.result.OperationResultBuilder in project midpoint by Evolveum.

the class TransformationalAsyncUpdateMessageListener method onMessage.

@Override
public void onMessage(AsyncUpdateMessageType message, AcknowledgementSink acknowledgementSink) {
    int messageNumber = messagesSeen.getAndIncrement();
    LOGGER.trace("Got message number {}: {}", messageNumber, message);
    SecurityContextManager securityContextManager = connectorInstance.getSecurityContextManager();
    Authentication oldAuthentication = securityContextManager.getAuthentication();
    try {
        securityContextManager.setupPreAuthenticatedSecurityContext(authentication);
        Task task = connectorInstance.getTaskManager().createTaskInstance(OP_ON_MESSAGE_PREPARATION);
        task.setChannel(CHANNEL_ASYNC_UPDATE_URI);
        if (authentication != null && authentication.getPrincipal() instanceof MidPointPrincipal) {
            task.setOwner(((MidPointPrincipal) authentication.getPrincipal()).getFocus().asPrismObject().clone());
        }
        Tracer tracer = connectorInstance.getTracer();
        OperationResult result = task.getResult();
        OperationResultBuilder resultBuilder = OperationResult.createFor(OP_ON_MESSAGE);
        try {
            ActivityTracingDefinitionType tracing = connectorInstance.getConfiguration().getProcessTracingConfiguration();
            if (tracing != null) {
                int interval = defaultIfNull(tracing.getInterval(), 1);
                boolean matches = interval > 0 && messageNumber % interval == 0;
                if (matches) {
                    task.setTracingProfile(tracing.getTracingProfile());
                    if (tracing.getTracingPoint().isEmpty()) {
                        task.addTracingRequest(TracingRootType.ASYNCHRONOUS_MESSAGE_PROCESSING);
                    } else {
                        tracing.getTracingPoint().forEach(task::addTracingRequest);
                    }
                }
            }
            if (task.getTracingRequestedFor().contains(TracingRootType.ASYNCHRONOUS_MESSAGE_PROCESSING)) {
                TracingProfileType profile = task.getTracingProfile() != null ? task.getTracingProfile() : tracer.getDefaultProfile();
                resultBuilder.tracingProfile(tracer.compileProfile(profile, task.getResult()));
            }
            // replace task result with the newly-built one
            result = resultBuilder.build();
            task.setResult(result);
            VariablesMap variables = new VariablesMap();
            variables.put(VAR_MESSAGE, message, AsyncUpdateMessageType.class);
            List<UcfChangeType> changeBeans;
            try {
                ExpressionType transformExpression = connectorInstance.getTransformExpression();
                if (transformExpression != null) {
                    changeBeans = connectorInstance.getUcfExpressionEvaluator().evaluate(transformExpression, variables, SchemaConstantsGenerated.C_UCF_CHANGE, "computing UCF change from async update", task, result);
                } else {
                    changeBeans = unwrapMessage(message);
                }
            } catch (RuntimeException | SchemaException | ObjectNotFoundException | SecurityViolationException | CommunicationException | ConfigurationException | ExpressionEvaluationException e) {
                throw new SystemException("Couldn't evaluate message transformation expression: " + e.getMessage(), e);
            }
            if (changeBeans.isEmpty()) {
                acknowledgementSink.acknowledge(true, result);
            } else {
                AcknowledgementSink aggregatedSink = createAggregatingAcknowledgeSink(acknowledgementSink, changeBeans.size());
                for (UcfChangeType changeBean : changeBeans) {
                    // For this to work reliably, we have to run in a single thread. But that's ok.
                    // If we receive messages in multiple threads, there is no message ordering.
                    int changeSequentialNumber = changesProduced.incrementAndGet();
                    // intentionally in this order - to process changes even after failure
                    // (if listener wants to fail fast, it can throw an exception)
                    UcfAsyncUpdateChange change = createChange(changeBean, result, changeSequentialNumber, aggregatedSink);
                    changeListener.onChange(change, task, result);
                }
            }
        } catch (Exception e) {
            LoggingUtils.logUnexpectedException(LOGGER, "Got exception while processing asynchronous message in {}", e, task);
            result.recordFatalError(e.getMessage(), e);
            int changeSequentialNumber = changesProduced.incrementAndGet();
            UcfAsyncUpdateChange change = new UcfAsyncUpdateChange(changeSequentialNumber, UcfErrorState.error(e), acknowledgementSink);
            changeListener.onChange(change, task, result);
        } finally {
            result.computeStatusIfUnknown();
            // (Otherwise it captures only the pre-processing activities.)
            if (result.isTraced()) {
                tracer.storeTrace(task, result, null);
            }
        }
    } finally {
        securityContextManager.setupPreAuthenticatedSecurityContext(oldAuthentication);
    }
}
Also used : Task(com.evolveum.midpoint.task.api.Task) AcknowledgementSink(com.evolveum.midpoint.schema.AcknowledgementSink) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) UcfAsyncUpdateChange(com.evolveum.midpoint.provisioning.ucf.api.UcfAsyncUpdateChange) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) OperationResultBuilder(com.evolveum.midpoint.schema.result.OperationResultBuilder) MidPointPrincipal(com.evolveum.midpoint.security.api.MidPointPrincipal) Tracer(com.evolveum.midpoint.task.api.Tracer) Authentication(org.springframework.security.core.Authentication) SecurityContextManager(com.evolveum.midpoint.security.api.SecurityContextManager)

Example 2 with OperationResultBuilder

use of com.evolveum.midpoint.schema.result.OperationResultBuilder in project midpoint by Evolveum.

the class WorkItemManager method completeWorkItem.

public void completeWorkItem(WorkItemId workItemId, @NotNull AbstractWorkItemOutputType output, WorkItemEventCauseInformationType causeInformation, Task task, OperationResult parentResult) throws SecurityViolationException, ObjectNotFoundException, ExpressionEvaluationException, CommunicationException, ConfigurationException, SchemaException, ObjectAlreadyExistsException {
    OperationResultBuilder builder = parentResult.subresult(OPERATION_COMPLETE_WORK_ITEM).addArbitraryObjectAsParam("workItemId", workItemId).addParam("decision", output.getOutcome()).addParam("comment", output.getComment());
    boolean tracingRequested = startTracingIfRequested(builder, task, parentResult);
    OperationResult result = builder.build();
    try {
        LOGGER.trace("Completing work item {} with decision of {} ['{}']; cause: {}", workItemId, output.getOutcome(), output.getComment(), causeInformation);
        CompleteWorkItemsRequest request = new CompleteWorkItemsRequest(workItemId.caseOid, causeInformation);
        request.getCompletions().add(new CompleteWorkItemsRequest.SingleCompletion(workItemId.id, output));
        caseEngine.executeRequest(request, task, result);
    } catch (SecurityViolationException | RuntimeException | SchemaException | ObjectAlreadyExistsException e) {
        result.recordFatalError("Couldn't complete the work item " + workItemId + ": " + e.getMessage(), e);
        throw e;
    } finally {
        result.computeStatusIfUnknown();
        storeTraceIfRequested(tracingRequested, task, result, parentResult);
    }
}
Also used : OperationResult(com.evolveum.midpoint.schema.result.OperationResult) OperationResultBuilder(com.evolveum.midpoint.schema.result.OperationResultBuilder) CompleteWorkItemsRequest(com.evolveum.midpoint.cases.api.request.CompleteWorkItemsRequest)

Example 3 with OperationResultBuilder

use of com.evolveum.midpoint.schema.result.OperationResultBuilder in project midpoint by Evolveum.

the class WorkItemManager method releaseWorkItem.

// We can eventually provide bulk version of this method as well.
public void releaseWorkItem(WorkItemId workItemId, Task task, OperationResult parentResult) throws ObjectNotFoundException, SecurityViolationException, SchemaException, ObjectAlreadyExistsException, CommunicationException, ConfigurationException, ExpressionEvaluationException {
    OperationResultBuilder builder = parentResult.subresult(OPERATION_RELEASE_WORK_ITEM).addArbitraryObjectAsParam("workItemId", workItemId);
    boolean tracingRequested = startTracingIfRequested(builder, task, parentResult);
    OperationResult result = builder.build();
    try {
        LOGGER.trace("Releasing work item {}", workItemId);
        ReleaseWorkItemsRequest request = new ReleaseWorkItemsRequest(workItemId.caseOid);
        request.getReleases().add(new ReleaseWorkItemsRequest.SingleRelease(workItemId.id));
        caseEngine.executeRequest(request, task, result);
    } catch (ObjectNotFoundException | SecurityViolationException | RuntimeException | SchemaException | ObjectAlreadyExistsException | ExpressionEvaluationException | ConfigurationException | CommunicationException e) {
        result.recordFatalError("Couldn't release work item " + workItemId + ": " + e.getMessage(), e);
        throw e;
    } finally {
        result.computeStatusIfUnknown();
        storeTraceIfRequested(tracingRequested, task, result, parentResult);
    }
}
Also used : OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ReleaseWorkItemsRequest(com.evolveum.midpoint.cases.api.request.ReleaseWorkItemsRequest) OperationResultBuilder(com.evolveum.midpoint.schema.result.OperationResultBuilder)

Example 4 with OperationResultBuilder

use of com.evolveum.midpoint.schema.result.OperationResultBuilder in project midpoint by Evolveum.

the class WorkItemManager method claimWorkItem.

// We can eventually provide bulk version of this method as well.
public void claimWorkItem(WorkItemId workItemId, Task task, OperationResult parentResult) throws SecurityViolationException, ObjectNotFoundException, SchemaException, ObjectAlreadyExistsException, CommunicationException, ConfigurationException, ExpressionEvaluationException {
    OperationResultBuilder builder = parentResult.subresult(OPERATION_CLAIM_WORK_ITEM).addArbitraryObjectAsParam("workItemId", workItemId);
    boolean tracingRequested = startTracingIfRequested(builder, task, parentResult);
    OperationResult result = builder.build();
    try {
        LOGGER.trace("Claiming work item {}", workItemId);
        ClaimWorkItemsRequest request = new ClaimWorkItemsRequest(workItemId.caseOid);
        request.getClaims().add(new ClaimWorkItemsRequest.SingleClaim(workItemId.id));
        caseEngine.executeRequest(request, task, result);
    } catch (ObjectNotFoundException | SecurityViolationException | RuntimeException | SchemaException | ObjectAlreadyExistsException | ExpressionEvaluationException | ConfigurationException | CommunicationException e) {
        result.recordFatalError("Couldn't claim the work item " + workItemId + ": " + e.getMessage(), e);
        throw e;
    } finally {
        result.computeStatusIfUnknown();
        storeTraceIfRequested(tracingRequested, task, result, parentResult);
    }
}
Also used : ClaimWorkItemsRequest(com.evolveum.midpoint.cases.api.request.ClaimWorkItemsRequest) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) OperationResultBuilder(com.evolveum.midpoint.schema.result.OperationResultBuilder)

Example 5 with OperationResultBuilder

use of com.evolveum.midpoint.schema.result.OperationResultBuilder in project midpoint by Evolveum.

the class WorkItemManager method completeWorkItems.

/**
 * Bulk version of completeWorkItem method. It's necessary when we need to complete more items at the same time,
 * to provide accurate completion information (avoiding completion of the first one and automated closure of other ones).
 */
public void completeWorkItems(CompleteWorkItemsRequest request, Task task, OperationResult parentResult) throws SecurityViolationException, ObjectNotFoundException, ExpressionEvaluationException, CommunicationException, ConfigurationException, SchemaException, ObjectAlreadyExistsException {
    OperationResultBuilder builder = parentResult.subresult(OPERATION_COMPLETE_WORK_ITEMS);
    boolean tracingRequested = startTracingIfRequested(builder, task, parentResult);
    OperationResult result = builder.build();
    try {
        caseEngine.executeRequest(request, task, result);
    } catch (SecurityViolationException | RuntimeException | CommunicationException | ConfigurationException | SchemaException | ObjectAlreadyExistsException e) {
        result.recordFatalError("Couldn't complete work items: " + e.getMessage(), e);
        throw e;
    } finally {
        result.computeStatusIfUnknown();
        storeTraceIfRequested(tracingRequested, task, result, parentResult);
    }
}
Also used : OperationResult(com.evolveum.midpoint.schema.result.OperationResult) OperationResultBuilder(com.evolveum.midpoint.schema.result.OperationResultBuilder)

Aggregations

OperationResultBuilder (com.evolveum.midpoint.schema.result.OperationResultBuilder)8 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)7 ClaimWorkItemsRequest (com.evolveum.midpoint.cases.api.request.ClaimWorkItemsRequest)1 CompleteWorkItemsRequest (com.evolveum.midpoint.cases.api.request.CompleteWorkItemsRequest)1 DelegateWorkItemsRequest (com.evolveum.midpoint.cases.api.request.DelegateWorkItemsRequest)1 ReleaseWorkItemsRequest (com.evolveum.midpoint.cases.api.request.ReleaseWorkItemsRequest)1 HookOperationMode (com.evolveum.midpoint.model.api.hooks.HookOperationMode)1 PrismContext (com.evolveum.midpoint.prism.PrismContext)1 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)1 UcfAsyncUpdateChange (com.evolveum.midpoint.provisioning.ucf.api.UcfAsyncUpdateChange)1 AcknowledgementSink (com.evolveum.midpoint.schema.AcknowledgementSink)1 VariablesMap (com.evolveum.midpoint.schema.expression.VariablesMap)1 MidPointPrincipal (com.evolveum.midpoint.security.api.MidPointPrincipal)1 SecurityContextManager (com.evolveum.midpoint.security.api.SecurityContextManager)1 Task (com.evolveum.midpoint.task.api.Task)1 Tracer (com.evolveum.midpoint.task.api.Tracer)1 Authentication (org.springframework.security.core.Authentication)1