-
Notifications
You must be signed in to change notification settings - Fork 188
/
BaseHandler.java
202 lines (171 loc) · 10.1 KB
/
BaseHandler.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* Copyright (c) 2010-2017 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.evolveum.midpoint.notifications.impl.handlers;
import com.evolveum.midpoint.notifications.impl.formatters.TextFormatter;
import com.evolveum.midpoint.repo.common.expression.Expression;
import com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext;
import com.evolveum.midpoint.repo.common.expression.ExpressionFactory;
import com.evolveum.midpoint.repo.common.expression.ExpressionVariables;
import com.evolveum.midpoint.model.impl.expr.ModelExpressionThreadLocalHolder;
import com.evolveum.midpoint.notifications.api.EventHandler;
import com.evolveum.midpoint.notifications.api.events.Event;
import com.evolveum.midpoint.notifications.impl.NotificationManagerImpl;
import com.evolveum.midpoint.notifications.impl.NotificationFunctionsImpl;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.PrismPropertyDefinition;
import com.evolveum.midpoint.prism.PrismPropertyDefinitionImpl;
import com.evolveum.midpoint.prism.PrismPropertyValue;
import com.evolveum.midpoint.prism.delta.PrismValueDeltaSetTriple;
import com.evolveum.midpoint.schema.constants.SchemaConstants;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.util.DOMUtil;
import com.evolveum.midpoint.util.exception.CommunicationException;
import com.evolveum.midpoint.util.exception.ConfigurationException;
import com.evolveum.midpoint.util.exception.ExpressionEvaluationException;
import com.evolveum.midpoint.util.exception.ObjectNotFoundException;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.exception.SecurityViolationException;
import com.evolveum.midpoint.util.exception.SystemException;
import com.evolveum.midpoint.util.logging.LoggingUtils;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.xml.ns._public.common.common_3.EventHandlerType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.xml.namespace.QName;
import java.util.*;
/**
* TODO remove duplicate code
*
* @author mederly
*/
@Component
public abstract class BaseHandler implements EventHandler {
private static final Trace LOGGER = TraceManager.getTrace(BaseHandler.class);
@Autowired protected NotificationManagerImpl notificationManager;
@Autowired protected NotificationFunctionsImpl notificationsUtil;
@Autowired protected PrismContext prismContext;
@Autowired protected ExpressionFactory expressionFactory;
@Autowired protected TextFormatter textFormatter;
protected void register(Class<? extends EventHandlerType> clazz) {
notificationManager.registerEventHandler(clazz, this);
}
protected void logStart(Trace LOGGER, Event event, EventHandlerType eventHandlerType) {
logStart(LOGGER, event, eventHandlerType, null);
}
protected void logStart(Trace LOGGER, Event event, EventHandlerType eventHandlerType, Object additionalData) {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Starting processing event " + event.shortDump() + " with handler " +
getHumanReadableHandlerDescription(eventHandlerType) + "\n parameters: " +
(additionalData != null ? ("\n parameters: " + additionalData) :
("\n configuration: " + eventHandlerType)));
}
}
protected void logNotApplicable(Event event, String reason) {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace(
"{} is not applicable for event {}, continuing in the handler chain; reason: {}",
this.getClass().getSimpleName(), event.shortDump(), reason);
}
}
protected String getHumanReadableHandlerDescription(EventHandlerType eventHandlerType) {
if (eventHandlerType.getName() != null) {
return eventHandlerType.getName();
} else {
return eventHandlerType.getClass().getSimpleName();
}
}
public static void staticLogStart(Trace LOGGER, Event event, String description, Object additionalData) {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Starting processing event " + event + " with handler " +
description +
(additionalData != null ? (", parameters: " + additionalData) : ""));
}
}
public void logEnd(Trace LOGGER, Event event, EventHandlerType eventHandlerType, boolean result) {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Finishing processing event " + event + " result = " + result);
}
}
// expressions
// shortDesc = what is to be evaluated e.g. "event filter expression"
protected boolean evaluateBooleanExpressionChecked(ExpressionType expressionType, ExpressionVariables expressionVariables,
String shortDesc, Task task, OperationResult result) {
Throwable failReason;
try {
return evaluateBooleanExpression(expressionType, expressionVariables, shortDesc, task, result);
} catch (ObjectNotFoundException | SchemaException | ExpressionEvaluationException | CommunicationException | ConfigurationException | SecurityViolationException e) {
failReason = e;
}
LoggingUtils.logException(LOGGER, "Couldn't evaluate {} {}", failReason, shortDesc, expressionType);
result.recordFatalError("Couldn't evaluate " + shortDesc, failReason);
throw new SystemException(failReason);
}
protected boolean evaluateBooleanExpression(ExpressionType expressionType, ExpressionVariables expressionVariables, String shortDesc,
Task task, OperationResult result) throws ObjectNotFoundException, SchemaException, ExpressionEvaluationException, CommunicationException, ConfigurationException, SecurityViolationException {
QName resultName = new QName(SchemaConstants.NS_C, "result");
PrismPropertyDefinition<Boolean> resultDef = new PrismPropertyDefinitionImpl<>(resultName, DOMUtil.XSD_BOOLEAN, prismContext);
Expression<PrismPropertyValue<Boolean>,PrismPropertyDefinition<Boolean>> expression = expressionFactory.makeExpression(expressionType, resultDef, shortDesc, task, result);
ExpressionEvaluationContext params = new ExpressionEvaluationContext(null, expressionVariables, shortDesc, task, result);
PrismValueDeltaSetTriple<PrismPropertyValue<Boolean>> exprResultTriple = ModelExpressionThreadLocalHolder.evaluateExpressionInContext(expression, params, task, result);
Collection<PrismPropertyValue<Boolean>> exprResult = exprResultTriple.getZeroSet();
if (exprResult.size() == 0) {
return false;
} else if (exprResult.size() > 1) {
throw new IllegalStateException("Filter expression should return exactly one boolean value; it returned " + exprResult.size() + " ones");
}
Boolean boolResult = exprResult.iterator().next().getValue();
return boolResult != null ? boolResult : false;
}
protected List<String> evaluateExpressionChecked(ExpressionType expressionType, ExpressionVariables expressionVariables,
String shortDesc, Task task, OperationResult result) {
Throwable failReason;
try {
return evaluateExpression(expressionType, expressionVariables, shortDesc, task, result);
} catch (ObjectNotFoundException | SchemaException | ExpressionEvaluationException | CommunicationException | ConfigurationException | SecurityViolationException e) {
failReason = e;
}
LoggingUtils.logException(LOGGER, "Couldn't evaluate {} {}", failReason, shortDesc, expressionType);
result.recordFatalError("Couldn't evaluate " + shortDesc, failReason);
throw new SystemException(failReason);
}
private List<String> evaluateExpression(ExpressionType expressionType, ExpressionVariables expressionVariables,
String shortDesc, Task task, OperationResult result) throws ObjectNotFoundException, SchemaException, ExpressionEvaluationException, CommunicationException, ConfigurationException, SecurityViolationException {
QName resultName = new QName(SchemaConstants.NS_C, "result");
PrismPropertyDefinition<String> resultDef = new PrismPropertyDefinitionImpl<>(resultName, DOMUtil.XSD_STRING, prismContext);
resultDef.setMaxOccurs(-1);
Expression<PrismPropertyValue<String>,PrismPropertyDefinition<String>> expression = expressionFactory.makeExpression(expressionType, resultDef, shortDesc, task, result);
ExpressionEvaluationContext params = new ExpressionEvaluationContext(null, expressionVariables, shortDesc, task, result);
PrismValueDeltaSetTriple<PrismPropertyValue<String>> exprResult = ModelExpressionThreadLocalHolder
.evaluateExpressionInContext(expression, params, task, result);
List<String> retval = new ArrayList<>();
for (PrismPropertyValue<String> item : exprResult.getZeroSet()) {
retval.add(item.getValue());
}
return retval;
}
protected ExpressionVariables getDefaultVariables(Event event, OperationResult result) {
ExpressionVariables expressionVariables = new ExpressionVariables();
Map<QName, Object> variables = new HashMap<>();
event.createExpressionVariables(variables, result);
variables.put(SchemaConstants.C_TEXT_FORMATTER, textFormatter);
variables.put(SchemaConstants.C_NOTIFICATION_FUNCTIONS, notificationsUtil);
expressionVariables.addVariableDefinitions(variables);
return expressionVariables;
}
}