-
Notifications
You must be signed in to change notification settings - Fork 188
/
NotificationFuctionsImpl.java
355 lines (322 loc) · 15.8 KB
/
NotificationFuctionsImpl.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
* Copyright (c) 2010-2013 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;
import com.evolveum.midpoint.model.api.context.ModelContext;
import com.evolveum.midpoint.model.api.context.ModelElementContext;
import com.evolveum.midpoint.model.api.expr.MidpointFunctions;
import com.evolveum.midpoint.notifications.api.NotificationFunctions;
import com.evolveum.midpoint.notifications.api.OperationStatus;
import com.evolveum.midpoint.notifications.api.events.Event;
import com.evolveum.midpoint.notifications.api.events.ModelEvent;
import com.evolveum.midpoint.notifications.api.events.ResourceObjectEvent;
import com.evolveum.midpoint.notifications.api.events.SimpleObjectRef;
import com.evolveum.midpoint.notifications.impl.formatters.TextFormatter;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.PrismPropertyValue;
import com.evolveum.midpoint.prism.crypto.EncryptionException;
import com.evolveum.midpoint.prism.delta.ItemDelta;
import com.evolveum.midpoint.prism.delta.ObjectDelta;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.prism.polystring.PolyString;
import com.evolveum.midpoint.repo.api.RepositoryService;
import com.evolveum.midpoint.schema.DeltaConvertor;
import com.evolveum.midpoint.schema.processor.ResourceAttribute;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.schema.util.ObjectTypeUtil;
import com.evolveum.midpoint.schema.util.ShadowUtil;
import com.evolveum.midpoint.util.exception.CommonException;
import com.evolveum.midpoint.util.exception.ObjectNotFoundException;
import com.evolveum.midpoint.util.exception.SchemaException;
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.*;
import com.evolveum.prism.xml.ns._public.types_3.ObjectDeltaType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import java.util.*;
/**
* @author mederly
*/
@Component
public class NotificationFuctionsImpl implements NotificationFunctions {
private static final Trace LOGGER = TraceManager.getTrace(NotificationFuctionsImpl.class);
@Autowired
@Qualifier("cacheRepositoryService")
private RepositoryService cacheRepositoryService;
@Autowired
private MidpointFunctions midpointFunctions;
@Autowired
protected TextFormatter textFormatter;
@Autowired
private PrismContext prismContext;
private static final List<ItemPath> SYNCHRONIZATION_PATHS = Collections.unmodifiableList(Arrays.asList(
new ItemPath(ShadowType.F_SYNCHRONIZATION_SITUATION),
new ItemPath(ShadowType.F_SYNCHRONIZATION_SITUATION_DESCRIPTION),
new ItemPath(ShadowType.F_SYNCHRONIZATION_TIMESTAMP),
new ItemPath(ShadowType.F_FULL_SYNCHRONIZATION_TIMESTAMP)));
private static final List<ItemPath> AUXILIARY_PATHS = Collections.unmodifiableList(Arrays.asList(
new ItemPath(ShadowType.F_METADATA),
new ItemPath(ShadowType.F_ACTIVATION, ActivationType.F_VALIDITY_STATUS), // works for user activation as well
new ItemPath(ShadowType.F_ACTIVATION, ActivationType.F_VALIDITY_CHANGE_TIMESTAMP),
new ItemPath(ShadowType.F_ACTIVATION, ActivationType.F_EFFECTIVE_STATUS),
new ItemPath(ShadowType.F_ACTIVATION, ActivationType.F_DISABLE_TIMESTAMP),
new ItemPath(ShadowType.F_ACTIVATION, ActivationType.F_ARCHIVE_TIMESTAMP),
new ItemPath(ShadowType.F_ACTIVATION, ActivationType.F_ENABLE_TIMESTAMP),
new ItemPath(ShadowType.F_ITERATION),
new ItemPath(ShadowType.F_ITERATION_TOKEN),
new ItemPath(UserType.F_LINK_REF),
new ItemPath(ShadowType.F_TRIGGER))
);
// beware, may return null if there's any problem getting sysconfig (e.g. during initial import)
public static SystemConfigurationType getSystemConfiguration(RepositoryService repositoryService, OperationResult result) {
try {
return repositoryService.getObject(SystemConfigurationType.class, SystemObjectsType.SYSTEM_CONFIGURATION.value(),
null, result).asObjectable();
} catch (ObjectNotFoundException|SchemaException e) {
LoggingUtils.logException(LOGGER, "Notification(s) couldn't be processed, because the system configuration couldn't be retrieved", e);
return null;
}
}
public SystemConfigurationType getSystemConfiguration(OperationResult result) {
try {
return cacheRepositoryService.getObject(SystemConfigurationType.class, SystemObjectsType.SYSTEM_CONFIGURATION.value(),
null, result).asObjectable();
} catch (ObjectNotFoundException|SchemaException e) {
LoggingUtils.logException(LOGGER, "Notification(s) couldn't be processed, because the system configuration couldn't be retrieved", e);
return null;
}
}
public static SecurityPolicyType getSecurityPolicyConfiguration(ObjectReferenceType securityPolicyRef, RepositoryService repositoryService, OperationResult result) {
try {
if (securityPolicyRef == null) {
return null;
}
return repositoryService.getObject(SecurityPolicyType.class, securityPolicyRef.getOid(),
null, result).asObjectable();
} catch (ObjectNotFoundException|SchemaException e) {
LoggingUtils.logException(LOGGER, "Notification(s) couldn't be processed, because the security policy configuration couldn't be retrieved", e);
return null;
}
}
public static String getResourceNameFromRepo(RepositoryService repositoryService, String oid, OperationResult result) {
try {
PrismObject<ResourceType> resource = repositoryService.getObject(ResourceType.class, oid, null, result);
return PolyString.getOrig(resource.asObjectable().getName());
} catch (ObjectNotFoundException e) {
LoggingUtils.logException(LOGGER, "Couldn't get resource", e);
return null;
} catch (SchemaException e) {
LoggingUtils.logException(LOGGER, "Couldn't get resource", e);
return null;
}
}
public String getObjectName(String oid, OperationResult result) {
try {
PrismObject<? extends ObjectType> object = cacheRepositoryService.getObject(ObjectType.class, oid, null, result);
return PolyString.getOrig(object.asObjectable().getName());
} catch (CommonException|RuntimeException e) {
LoggingUtils.logUnexpectedException(LOGGER, "Couldn't get resource", e);
return null;
}
}
public ObjectType getObjectType(SimpleObjectRef simpleObjectRef, boolean allowNotFound, OperationResult result) {
if (simpleObjectRef == null) {
return null;
}
if (simpleObjectRef.getObjectType() != null) {
return simpleObjectRef.getObjectType();
}
if (simpleObjectRef.getOid() == null) {
return null;
}
ObjectType objectType;
try {
objectType = cacheRepositoryService.getObject(ObjectType.class, simpleObjectRef.getOid(), null, result).asObjectable();
} catch (ObjectNotFoundException e) { // todo correct error handling
if (allowNotFound) {
return null;
} else {
throw new SystemException(e);
}
} catch (SchemaException e) {
throw new SystemException(e);
}
simpleObjectRef.setObjectType(objectType);
return objectType;
}
public static boolean isAmongHiddenPaths(ItemPath path, List<ItemPath> hiddenPaths) {
if (hiddenPaths == null) {
return false;
}
for (ItemPath hiddenPath : hiddenPaths) {
if (hiddenPath.isSubPathOrEquivalent(path)) {
return true;
}
}
return false;
}
@Override
public String getShadowName(PrismObject<? extends ShadowType> shadow) {
if (shadow == null) {
return null;
} else if (shadow.asObjectable().getName() != null) {
return shadow.asObjectable().getName().getOrig();
} else {
Collection<ResourceAttribute<?>> secondaryIdentifiers = ShadowUtil.getSecondaryIdentifiers(shadow);
LOGGER.trace("secondary identifiers: {}", secondaryIdentifiers);
// first phase = looking for "name" identifier
for (ResourceAttribute ra : secondaryIdentifiers) {
if (ra.getElementName() != null && ra.getElementName().getLocalPart().contains("name")) {
LOGGER.trace("Considering {} as a name", ra);
return String.valueOf(ra.getAnyRealValue());
}
}
// second phase = returning any value ;)
if (!secondaryIdentifiers.isEmpty()) {
return String.valueOf(secondaryIdentifiers.iterator().next().getAnyRealValue());
} else {
return null;
}
}
}
// TODO move to some other class?
public void addRequesterAndChannelInformation(StringBuilder body, Event event, OperationResult result) {
if (event.getRequester() != null) {
body.append("Requester: ");
try {
ObjectType requester = event.getRequester().resolveObjectType(result, false);
if (requester instanceof UserType) {
UserType requesterUser = (UserType) requester;
body.append(requesterUser.getFullName()).append(" (").append(requester.getName()).append(")");
} else {
body.append(ObjectTypeUtil.toShortString(requester));
}
} catch (RuntimeException e) {
body.append("couldn't be determined: ").append(e.getMessage());
LoggingUtils.logUnexpectedException(LOGGER, "Couldn't determine requester for a notification", e);
}
body.append("\n");
}
body.append("Channel: ").append(event.getChannel()).append("\n\n");
}
@Override
public String getPlaintextPasswordFromDelta(ObjectDelta delta) {
try {
return midpointFunctions.getPlaintextAccountPasswordFromDelta(delta);
} catch (EncryptionException e) {
LoggingUtils.logException(LOGGER, "Couldn't decrypt password from shadow delta: {}", e, delta.debugDump());
return null;
}
}
@Override
public List<ItemPath> getSynchronizationPaths() {
return SYNCHRONIZATION_PATHS;
}
@Override
public List<ItemPath> getAuxiliaryPaths() {
return AUXILIARY_PATHS;
}
public String getContentAsFormattedList(Event event, boolean showSynchronizationItems, boolean showAuxiliaryAttributes) {
List<ItemPath> hiddenPaths = new ArrayList<>();
if (!showSynchronizationItems) {
hiddenPaths.addAll(SYNCHRONIZATION_PATHS);
}
if (!showAuxiliaryAttributes) {
hiddenPaths.addAll(AUXILIARY_PATHS);
}
if (event instanceof ResourceObjectEvent) {
final ResourceObjectEvent resourceObjectEvent = (ResourceObjectEvent) event;
final ObjectDelta<ShadowType> shadowDelta = resourceObjectEvent.getShadowDelta();
if (shadowDelta == null) {
return "";
}
if (shadowDelta.isAdd()) {
return getResourceObjectAttributesAsFormattedList(shadowDelta.getObjectToAdd().asObjectable(), hiddenPaths, showAuxiliaryAttributes);
} else if (shadowDelta.isModify()) {
return getResourceObjectModifiedAttributesAsFormattedList(resourceObjectEvent, shadowDelta, hiddenPaths, showAuxiliaryAttributes);
} else {
return "";
}
} else if (event instanceof ModelEvent) {
final ModelEvent modelEvent = (ModelEvent) event;
ModelContext<FocusType> modelContext = (ModelContext) modelEvent.getModelContext();
ModelElementContext<FocusType> focusContext = modelContext.getFocusContext();
ObjectDelta<? extends FocusType> summarizedDelta;
try {
summarizedDelta = modelEvent.getSummarizedFocusDeltas();
} catch (SchemaException e) {
LoggingUtils.logUnexpectedException(LOGGER, "Unable to determine the focus change; focus context = {}", e, focusContext.debugDump());
return("(unable to determine the change because of schema exception: " + e.getMessage() + ")\n");
}
if (summarizedDelta.isAdd()) {
return textFormatter.formatObject(summarizedDelta.getObjectToAdd(), hiddenPaths, showAuxiliaryAttributes);
} else if (summarizedDelta.isModify()) {
return textFormatter.formatObjectModificationDelta(summarizedDelta, hiddenPaths, showAuxiliaryAttributes, focusContext.getObjectOld(),
focusContext.getObjectNew());
} else {
return "";
}
} else {
return "";
}
}
private String getResourceObjectAttributesAsFormattedList(ShadowType shadowType, List<ItemPath> hiddenAttributes, boolean showAuxiliaryAttributes) {
return textFormatter.formatAccountAttributes(shadowType, hiddenAttributes, false);
}
private String getResourceObjectModifiedAttributesAsFormattedList(ResourceObjectEvent event, ObjectDelta<ShadowType> shadowDelta,
List<ItemPath> hiddenPaths, boolean showAuxiliaryAttributes) {
StringBuilder rv = new StringBuilder();
if (event.getOperationStatus() != OperationStatus.IN_PROGRESS) {
// todo we do not have objectOld + objectNew, only the current status
// it is used to explain modified containers with identifiers -- however, currently I don't know of use of such containers in shadows, which would be visible in notifications
rv.append(textFormatter.formatObjectModificationDelta(shadowDelta, hiddenPaths, showAuxiliaryAttributes,
event.getAccountOperationDescription().getCurrentShadow(), null));
} else {
// special case - here the attributes are 'result', 'failedOperationType', 'objectChange', 'attemptNumber'
// we have to unwrap attributes that are to be modified from the objectChange item
Collection<PrismPropertyValue<ObjectDeltaType>> changes = null;
if (shadowDelta.getModifications() != null) {
for (ItemDelta itemDelta : shadowDelta.getModifications()) {
if (itemDelta.getPath().equivalent(new ItemPath(ShadowType.F_OBJECT_CHANGE))) {
changes = itemDelta.getValuesToAdd() != null && !itemDelta.getValuesToAdd().isEmpty() ?
itemDelta.getValuesToAdd() : itemDelta.getValuesToReplace();
}
}
}
if (changes != null && !changes.isEmpty()) {
try {
List<ObjectDelta<ShadowType>> deltas = new ArrayList<>(changes.size());
for (PrismPropertyValue<ObjectDeltaType> change : changes) {
deltas.add((ObjectDelta) DeltaConvertor.createObjectDelta(change.getValue(), prismContext));
}
ObjectDelta<ShadowType> summarizedDelta = ObjectDelta.summarize(deltas);
rv.append(textFormatter.formatObjectModificationDelta(summarizedDelta, hiddenPaths, showAuxiliaryAttributes,
event.getAccountOperationDescription().getCurrentShadow(), null));
} catch (SchemaException e) {
LoggingUtils.logUnexpectedException(LOGGER, "Unable to determine the shadow change; operation = {}", e, event.getAccountOperationDescription().debugDump());
rv.append("(unable to determine the change because of schema exception: ").append(e.getMessage()).append(")\n");
}
} else {
rv.append("(unable to determine the change)\n");
}
}
return rv.toString();
}
}