Search in sources :

Example 1 with LocalizableMessage

use of com.evolveum.midpoint.util.LocalizableMessage in project midpoint by Evolveum.

the class MidpointAbstractHttpMessageConverter method writeInternal.

@Override
protected void writeInternal(@NotNull T object, @NotNull HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    // TODO implement in the standard serializer; also change root name
    QName fakeQName = new QName(PrismConstants.NS_TYPES, "object");
    String serializedForm;
    PrismSerializer<String> serializer = getSerializer().options(SerializationOptions.createSerializeReferenceNames());
    try {
        if (object instanceof ObjectType) {
            ObjectType ot = (ObjectType) object;
            serializedForm = serializer.serialize(ot.asPrismObject());
        } else if (object instanceof PrismObject) {
            serializedForm = serializer.serialize((PrismObject<?>) object);
        } else if (object instanceof OperationResult) {
            Function<LocalizableMessage, String> resolveKeys = msg -> localizationService.translate(msg, Locale.US);
            OperationResultType operationResultType = ((OperationResult) object).createOperationResultType(resolveKeys);
            serializedForm = serializer.serializeAnyData(operationResultType, fakeQName);
        } else {
            serializedForm = serializer.serializeAnyData(object, fakeQName);
        }
        outputMessage.getBody().write(serializedForm.getBytes(StandardCharsets.UTF_8));
    } catch (SchemaException | RuntimeException e) {
        LoggingUtils.logException(LOGGER, "Couldn't marshal element to string: {}", e, object);
    }
}
Also used : ObjectType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) Trace(com.evolveum.midpoint.util.logging.Trace) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) LocalizationService(com.evolveum.midpoint.common.LocalizationService) Function(java.util.function.Function) ScriptingBeansUtil(com.evolveum.midpoint.schema.util.ScriptingBeansUtil) OperationResultType(com.evolveum.midpoint.xml.ns._public.common.common_3.OperationResultType) Locale(java.util.Locale) LocalizableMessage(com.evolveum.midpoint.util.LocalizableMessage) com.evolveum.midpoint.prism(com.evolveum.midpoint.prism) AbstractHttpMessageConverter(org.springframework.http.converter.AbstractHttpMessageConverter) MediaType(org.springframework.http.MediaType) IOException(java.io.IOException) StandardCharsets(java.nio.charset.StandardCharsets) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) LoggingUtils(com.evolveum.midpoint.util.logging.LoggingUtils) HttpInputMessage(org.springframework.http.HttpInputMessage) ScriptingExpressionType(com.evolveum.midpoint.xml.ns._public.model.scripting_3.ScriptingExpressionType) ExecuteScriptType(com.evolveum.midpoint.xml.ns._public.model.scripting_3.ExecuteScriptType) QName(javax.xml.namespace.QName) NotNull(org.jetbrains.annotations.NotNull) TraceManager(com.evolveum.midpoint.util.logging.TraceManager) InputStream(java.io.InputStream) HttpOutputMessage(org.springframework.http.HttpOutputMessage) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) QName(javax.xml.namespace.QName) LocalizableMessage(com.evolveum.midpoint.util.LocalizableMessage) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ObjectType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType) OperationResultType(com.evolveum.midpoint.xml.ns._public.common.common_3.OperationResultType)

Example 2 with LocalizableMessage

use of com.evolveum.midpoint.util.LocalizableMessage in project midpoint by Evolveum.

the class ObjectPolicyAspectPart method prepareObjectRelatedTaskInstructions.

private <T extends ObjectType> void prepareObjectRelatedTaskInstructions(List<PcpStartInstruction> instructions, ApprovalSchemaBuilder.Result builderResult, List<ObjectDelta<T>> deltasToApprove, PrismObject<? extends FocusType> requester, ModelInvocationContext<T> ctx, OperationResult result) throws SchemaException {
    ModelContext<T> modelContext = ctx.modelContext;
    for (ObjectDelta<T> deltaToApprove : deltasToApprove) {
        LocalizableMessage processName = main.createProcessName(builderResult, null, ctx, result);
        if (main.useDefaultProcessName(processName)) {
            processName = createDefaultProcessName(ctx, deltaToApprove);
        }
        String processNameInDefaultLocale = localizationService.translate(processName, Locale.getDefault(), "(unnamed)");
        PcpStartInstruction instruction = PcpStartInstruction.createItemApprovalInstruction(main.getChangeProcessor(), builderResult.schemaType, builderResult.attachedRules);
        instruction.prepareCommonAttributes(main, modelContext, requester);
        instruction.setDeltasToApprove(deltaToApprove);
        instruction.setObjectRef(ctx);
        instruction.setName(processNameInDefaultLocale, processName);
        instructions.add(instruction);
    }
}
Also used : LocalizableMessage(com.evolveum.midpoint.util.LocalizableMessage) PcpStartInstruction(com.evolveum.midpoint.wf.impl.processors.primary.PcpStartInstruction)

Example 3 with LocalizableMessage

use of com.evolveum.midpoint.util.LocalizableMessage in project midpoint by Evolveum.

the class ObjectModificationConstraintEvaluator method createMessage.

private <AH extends AssignmentHolderType> LocalizableMessage createMessage(JAXBElement<ModificationPolicyConstraintType> constraint, PolicyRuleEvaluationContext<AH> rctx, OperationResult result) throws ExpressionEvaluationException, ObjectNotFoundException, SchemaException, CommunicationException, ConfigurationException, SecurityViolationException {
    String keyPostfix = createStateKey(rctx) + createOperationKey(rctx);
    LocalizableMessage builtInMessage = new LocalizableMessageBuilder().key(SchemaConstants.DEFAULT_POLICY_CONSTRAINT_KEY_PREFIX + CONSTRAINT_KEY_PREFIX + keyPostfix).args(ObjectTypeUtil.createDisplayInformation(rctx.focusContext.getObjectAny(), true)).build();
    return evaluatorHelper.createLocalizableMessage(constraint, rctx, builtInMessage, result);
}
Also used : LocalizableMessage(com.evolveum.midpoint.util.LocalizableMessage) LocalizableMessageBuilder(com.evolveum.midpoint.util.LocalizableMessageBuilder)

Example 4 with LocalizableMessage

use of com.evolveum.midpoint.util.LocalizableMessage in project midpoint by Evolveum.

the class ObjectModificationConstraintEvaluator method createShortMessage.

private <AH extends AssignmentHolderType> LocalizableMessage createShortMessage(JAXBElement<ModificationPolicyConstraintType> constraint, PolicyRuleEvaluationContext<AH> rctx, OperationResult result) throws ExpressionEvaluationException, ObjectNotFoundException, SchemaException, CommunicationException, ConfigurationException, SecurityViolationException {
    String keyPostfix = createStateKey(rctx) + createOperationKey(rctx);
    LocalizableMessage builtInMessage = new LocalizableMessageBuilder().key(SchemaConstants.DEFAULT_POLICY_CONSTRAINT_SHORT_MESSAGE_KEY_PREFIX + CONSTRAINT_KEY_PREFIX + keyPostfix).args(ObjectTypeUtil.createDisplayInformation(rctx.focusContext.getObjectAny(), false)).build();
    return evaluatorHelper.createLocalizableShortMessage(constraint, rctx, builtInMessage, result);
}
Also used : LocalizableMessage(com.evolveum.midpoint.util.LocalizableMessage) LocalizableMessageBuilder(com.evolveum.midpoint.util.LocalizableMessageBuilder)

Example 5 with LocalizableMessage

use of com.evolveum.midpoint.util.LocalizableMessage in project midpoint by Evolveum.

the class StateConstraintEvaluator method evaluateForObject.

private <AH extends AssignmentHolderType> EvaluatedStateTrigger evaluateForObject(JAXBElement<StatePolicyConstraintType> constraintElement, PolicyRuleEvaluationContext<AH> ctx, OperationResult result) throws SchemaException, ExpressionEvaluationException, ObjectNotFoundException, CommunicationException, ConfigurationException, SecurityViolationException {
    StatePolicyConstraintType constraint = constraintElement.getValue();
    int count = (constraint.getFilter() != null ? 1 : 0) + (constraint.getExpression() != null ? 1 : 0) + (constraint.getMessageExpression() != null ? 1 : 0) + (constraint.getExecuteScript() != null ? 1 : 0);
    if (count != 1) {
        throw new SchemaException("Exactly one of filter, expression, messageExpression, executeScript element must be present.");
    }
    PrismObject<AH> object = ctx.getObject();
    if (object == null) {
        return null;
    }
    if (constraint.getFilter() != null) {
        ObjectFilter filter = prismContext.getQueryConverter().parseFilter(constraint.getFilter(), object.asObjectable().getClass());
        if (!filter.match(object.getValue(), matchingRuleRegistry)) {
            return null;
        }
    }
    if (constraint.getExecuteScript() != null) {
        VariablesMap variables = new VariablesMap();
        variables.put(VAR_OBJECT, object, object.getDefinition());
        variables.put(VAR_RULE_EVALUATION_CONTEXT, ctx, PolicyRuleEvaluationContext.class);
        ExecutionContext resultingContext;
        try {
            resultingContext = scriptingExpressionEvaluator.evaluateExpressionPrivileged(constraint.getExecuteScript(), variables, ctx.task, result);
        } catch (ScriptExecutionException e) {
            // TODO
            throw new SystemException(e);
        }
        PipelineData output = resultingContext.getFinalOutput();
        LOGGER.trace("Scripting expression returned {} item(s); console output is:\n{}", output != null ? output.getData().size() : null, resultingContext.getConsoleOutput());
        List<PipelineItem> items = output != null ? output.getData() : emptyList();
        if (items.isEmpty()) {
            return null;
        }
    // TODO retrieve localization messages from output
    }
    if (constraint.getMessageExpression() != null) {
        LocalizableMessageType messageBean = evaluatorHelper.evaluateLocalizableMessageType(constraint.getMessageExpression(), evaluatorHelper.createVariablesMap(ctx, constraintElement), "message expression in object state constraint " + constraint.getName() + " (" + ctx.state + ")", ctx.task, result);
        if (messageBean == null) {
            return null;
        } else {
            LocalizableMessage message = LocalizationUtil.toLocalizableMessage(messageBean);
            return new EvaluatedStateTrigger(OBJECT_STATE, constraint, message, message);
        }
    }
    if (constraint.getExpression() != null) {
        if (!evaluatorHelper.evaluateBoolean(constraint.getExpression(), evaluatorHelper.createVariablesMap(ctx, constraintElement), "expression in object state constraint " + constraint.getName() + " (" + ctx.state + ")", ctx.task, result)) {
            return null;
        }
    }
    return new EvaluatedStateTrigger(OBJECT_STATE, constraint, createMessage(OBJECT_CONSTRAINT_KEY_PREFIX, constraintElement, ctx, false, result), createShortMessage(OBJECT_CONSTRAINT_KEY_PREFIX, constraintElement, ctx, false, result));
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ScriptExecutionException(com.evolveum.midpoint.util.exception.ScriptExecutionException) LocalizableMessageType(com.evolveum.midpoint.xml.ns._public.common.common_3.LocalizableMessageType) PipelineData(com.evolveum.midpoint.model.impl.scripting.PipelineData) LocalizableMessage(com.evolveum.midpoint.util.LocalizableMessage) StatePolicyConstraintType(com.evolveum.midpoint.xml.ns._public.common.common_3.StatePolicyConstraintType) ObjectFilter(com.evolveum.midpoint.prism.query.ObjectFilter) EvaluatedStateTrigger(com.evolveum.midpoint.model.api.context.EvaluatedStateTrigger) ExecutionContext(com.evolveum.midpoint.model.impl.scripting.ExecutionContext) PipelineItem(com.evolveum.midpoint.model.api.PipelineItem) SystemException(com.evolveum.midpoint.util.exception.SystemException) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap)

Aggregations

LocalizableMessage (com.evolveum.midpoint.util.LocalizableMessage)33 LocalizableMessageBuilder (com.evolveum.midpoint.util.LocalizableMessageBuilder)18 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)17 StringLimitationResult (com.evolveum.midpoint.model.api.validator.StringLimitationResult)8 NotNull (org.jetbrains.annotations.NotNull)8 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)6 ObjectType (com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType)5 PolyStringTranslationType (com.evolveum.prism.xml.ns._public.types_3.PolyStringTranslationType)5 PolyStringType (com.evolveum.prism.xml.ns._public.types_3.PolyStringType)5 Autowired (org.springframework.beans.factory.annotation.Autowired)5 PrismObject (com.evolveum.midpoint.prism.PrismObject)4 QName (javax.xml.namespace.QName)4 EvaluatedPolicyRule (com.evolveum.midpoint.model.api.context.EvaluatedPolicyRule)3 EvaluatedAssignmentImpl (com.evolveum.midpoint.model.impl.lens.assignments.EvaluatedAssignmentImpl)3 AssignmentPolicyRuleEvaluationContext (com.evolveum.midpoint.model.impl.lens.projector.policy.AssignmentPolicyRuleEvaluationContext)3 PolicyRuleEvaluationContext (com.evolveum.midpoint.model.impl.lens.projector.policy.PolicyRuleEvaluationContext)3 SchemaConstants (com.evolveum.midpoint.schema.constants.SchemaConstants)3 SystemException (com.evolveum.midpoint.util.exception.SystemException)3 Trace (com.evolveum.midpoint.util.logging.Trace)3 TraceManager (com.evolveum.midpoint.util.logging.TraceManager)3