-
Notifications
You must be signed in to change notification settings - Fork 188
/
OperationRequestTransformer.java
90 lines (74 loc) · 4.47 KB
/
OperationRequestTransformer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* Copyright (c) 2020 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.provisioning.ucf.impl.builtin.async.provisioning;
import com.evolveum.midpoint.provisioning.ucf.api.async.AsyncProvisioningRequest;
import com.evolveum.midpoint.provisioning.ucf.api.async.StringAsyncProvisioningRequest;
import com.evolveum.midpoint.schema.SchemaConstantsGenerated;
import com.evolveum.midpoint.schema.expression.VariablesMap;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.util.MiscUtil;
import com.evolveum.midpoint.util.exception.*;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.PredefinedOperationRequestTransformationType;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.util.List;
import static com.evolveum.midpoint.xml.ns._public.common.common_3.PredefinedOperationRequestTransformationType.SIMPLIFIED_JSON;
/**
* Transforms asynchronous operation (i.e. abstract description what has to be done) into specific request
* (basically a message that is to be send to a given target).
*/
public class OperationRequestTransformer {
@NotNull private final AsyncProvisioningConnectorInstance connectorInstance;
@NotNull private final TransformerHelper transformerHelper;
private static final String VAR_OPERATION_REQUESTED = "operationRequested";
private static final String VAR_TRANSFORMER_HELPER = "transformerHelper";
private static final String VAR_REQUEST_FORMATTER = "requestFormatter";
public OperationRequestTransformer(@NotNull AsyncProvisioningConnectorInstance connectorInstance) {
this.connectorInstance = connectorInstance;
this.transformerHelper = new TransformerHelper(connectorInstance);
}
@NotNull
public AsyncProvisioningRequest transformOperationRequested(@NotNull OperationRequested operationRequested,
Task task, OperationResult result) throws SchemaException, IOException, ExpressionEvaluationException,
SecurityViolationException, CommunicationException, ConfigurationException, ObjectNotFoundException {
PredefinedOperationRequestTransformationType predefinedTransformation = connectorInstance.getPredefinedTransformation();
if (predefinedTransformation != null) {
return transformerHelper.applyPredefinedTransformation(operationRequested, predefinedTransformation);
}
ExpressionType transformExpression = connectorInstance.getTransformExpression();
if (transformExpression != null) {
VariablesMap variables = new VariablesMap();
variables.put(VAR_OPERATION_REQUESTED, operationRequested, operationRequested.getClass());
variables.put(VAR_TRANSFORMER_HELPER, transformerHelper, TransformerHelper.class);
variables.put(VAR_REQUEST_FORMATTER, transformerHelper.jsonRequestFormatter(operationRequested), JsonRequestFormatter.class);
List<?> list = connectorInstance.getUcfExpressionEvaluator().evaluate(transformExpression, variables,
SchemaConstantsGenerated.C_ASYNC_PROVISIONING_REQUEST, "creating asynchronous provisioning request",
task, result);
if (list.isEmpty()) {
throw new IllegalStateException("Transformational script returned no value");
}
if (list.size() > 1) {
throw new IllegalStateException("Transformational script returned more than single value: " + list);
}
Object o = list.get(0);
if (o == null) {
// In the future we can call e.g. default request creator here
throw new IllegalStateException("Transformational script returned no value");
} else if (o instanceof AsyncProvisioningRequest) {
return (AsyncProvisioningRequest) o;
} else if (o instanceof String) {
return StringAsyncProvisioningRequest.of((String) o);
} else {
throw new IllegalStateException("Transformational script should provide an AsyncProvisioningRequest but created " + MiscUtil.getClass(o) + " instead");
}
} else {
return transformerHelper.applyPredefinedTransformation(operationRequested, SIMPLIFIED_JSON);
}
}
}