-
Notifications
You must be signed in to change notification settings - Fork 188
/
ShadowUtil.java
384 lines (335 loc) · 14.5 KB
/
ShadowUtil.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
* 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.schema.util;
import com.evolveum.midpoint.prism.*;
import com.evolveum.midpoint.schema.processor.ObjectClassComplexTypeDefinition;
import com.evolveum.midpoint.schema.processor.ResourceAttribute;
import com.evolveum.midpoint.schema.processor.ResourceAttributeContainer;
import com.evolveum.midpoint.schema.processor.ResourceAttributeContainerDefinition;
import com.evolveum.midpoint.schema.processor.ResourceSchema;
import com.evolveum.midpoint.util.MiscUtil;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.exception.SystemException;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
import com.evolveum.prism.xml.ns._public.types_3.ProtectedStringType;
import javax.xml.namespace.QName;
import org.apache.commons.lang.StringUtils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* Methods that would belong to the ResourceObjectShadowType class but cannot go there
* because of JAXB.
*
* @author Radovan Semancik
*/
public class ShadowUtil {
public static Collection<ResourceAttribute<?>> getIdentifiers(ShadowType shadowType) {
return getIdentifiers(shadowType.asPrismObject());
}
public static Collection<ResourceAttribute<?>> getIdentifiers(PrismObject<? extends ShadowType> shadow) {
ResourceAttributeContainer attributesContainer = getAttributesContainer(shadow);
if (attributesContainer == null) {
return null;
}
return attributesContainer.getIdentifiers();
}
public static Collection<ResourceAttribute<?>> getSecondaryIdentifiers(ShadowType shadowType) {
return getSecondaryIdentifiers(shadowType.asPrismObject());
}
public static Collection<ResourceAttribute<?>> getSecondaryIdentifiers(PrismObject<? extends ShadowType> shadow) {
ResourceAttributeContainer attributesContainer = getAttributesContainer(shadow);
if (attributesContainer == null) {
return null;
}
return attributesContainer.getSecondaryIdentifiers();
}
public static Collection<ResourceAttribute<?>> getAttributes(ShadowType shadowType) {
return getAttributes(shadowType.asPrismObject());
}
public static Collection<ResourceAttribute<?>> getAttributes(PrismObject<? extends ShadowType> shadow) {
return getAttributesContainer(shadow).getAttributes();
}
public static <T> ResourceAttribute<T> getAttribute(PrismObject<? extends ShadowType> shadow, QName attrName) {
return getAttributesContainer(shadow).findAttribute(attrName);
}
public static ResourceAttributeContainer getAttributesContainer(ShadowType shadowType) {
return getAttributesContainer(shadowType.asPrismObject());
}
public static ResourceAttributeContainer getAttributesContainer(PrismObject<? extends ShadowType> shadow) {
return getAttributesContainer(shadow, ShadowType.F_ATTRIBUTES);
}
public static ResourceAttributeContainer getAttributesContainer(PrismObject<? extends ShadowType> shadow, QName containerName) {
return getAttributesContainer(shadow.getValue(), containerName);
}
public static ResourceAttributeContainer getAttributesContainer(PrismContainerValue<?> cval, QName containerName) {
PrismContainer attributesContainer = cval.findContainer(containerName);
if (attributesContainer == null) {
return null;
}
if (attributesContainer instanceof ResourceAttributeContainer) {
return (ResourceAttributeContainer)attributesContainer;
} else {
throw new SystemException("Expected that <"+containerName.getLocalPart()+"> will be ResourceAttributeContainer but it is "+attributesContainer.getClass());
}
}
public static ResourceAttributeContainer getOrCreateAttributesContainer(PrismObject<? extends ShadowType> shadow,
ObjectClassComplexTypeDefinition objectClassDefinition) {
ResourceAttributeContainer attributesContainer = getAttributesContainer(shadow);
if (attributesContainer != null) {
return attributesContainer;
}
ResourceAttributeContainer emptyContainer = ResourceAttributeContainer.createEmptyContainer(ShadowType.F_ATTRIBUTES, objectClassDefinition);
try {
shadow.add(emptyContainer);
} catch (SchemaException e) {
throw new SystemException("Unexpected schema error: "+e.getMessage(), e);
}
return emptyContainer;
}
public static ObjectClassComplexTypeDefinition getObjectClassDefinition(ShadowType shadow) {
// TODO: maybe we can do something more intelligent here
ResourceAttributeContainer attributesContainer = getAttributesContainer(shadow);
return attributesContainer.getDefinition().getComplexTypeDefinition();
}
public static ObjectClassComplexTypeDefinition getObjectClassDefinition(PrismObject<? extends ShadowType> shadow) {
// TODO: maybe we can do something more intelligent here
ResourceAttributeContainer attributesContainer = getAttributesContainer(shadow);
return attributesContainer.getDefinition().getComplexTypeDefinition();
}
public static String getResourceOid(ShadowType shadowType) {
return getResourceOid(shadowType.asPrismObject());
}
public static String getResourceOid(PrismObject<ShadowType> shadow) {
PrismReference resourceRef = shadow.findReference(ShadowType.F_RESOURCE_REF);
if (resourceRef == null) {
return null;
}
return resourceRef.getOid();
}
public static String getSingleStringAttributeValue(ShadowType shadow, QName attrName) {
return getSingleStringAttributeValue(shadow.asPrismObject(), attrName);
}
private static String getSingleStringAttributeValue(PrismObject<ShadowType> shadow, QName attrName) {
PrismContainer<?> attributesContainer = shadow.findContainer(ShadowType.F_ATTRIBUTES);
if (attributesContainer == null) {
return null;
}
PrismProperty<String> attribute = attributesContainer.findProperty(attrName);
if (attribute == null) {
return null;
}
return attribute.getRealValue();
}
public static String getMultiStringAttributeValueAsSingle(ShadowType shadow, QName attrName) {
return getMultiStringAttributeValueAsSingle(shadow.asPrismObject(), attrName);
}
private static String getMultiStringAttributeValueAsSingle(PrismObject<ShadowType> shadow, QName attrName) {
PrismContainer<?> attributesContainer = shadow.findContainer(ShadowType.F_ATTRIBUTES);
if (attributesContainer == null) {
return null;
}
PrismProperty<String> attribute = attributesContainer.findProperty(attrName);
if (attribute == null) {
return null;
}
Collection<String> realValues = attribute.getRealValues();
if (realValues == null || realValues.isEmpty()) {
return null;
}
if (realValues.size() > 1) {
throw new IllegalStateException("More than one value in attribute "+attrName);
}
return realValues.iterator().next();
}
public static <T> List<T> getAttributeValues(ShadowType shadowType, QName attrName) {
return getAttributeValues(shadowType.asPrismObject(), attrName);
}
public static <T> List<T> getAttributeValues(PrismObject<? extends ShadowType> shadow, QName attrName) {
PrismContainer<?> attributesContainer = shadow.findContainer(ShadowType.F_ATTRIBUTES);
if (attributesContainer == null || attributesContainer.isEmpty()) {
return null;
}
PrismProperty<T> attr = attributesContainer.findProperty(attrName);
if (attr == null) {
return null;
}
List<T> values = new ArrayList<T>();
for (PrismPropertyValue<T> pval : attr.getValues()) {
values.add(pval.getValue());
}
if (values.isEmpty()) {
return null;
}
return values;
}
public static <T> T getAttributeValue(ShadowType shadowType, QName attrName) throws SchemaException {
return (T) getAttributeValue(shadowType.asPrismObject(), attrName);
}
public static <T> T getAttributeValue(PrismObject<? extends ShadowType> shadow, QName attrName) throws SchemaException {
Collection<T> values = getAttributeValues(shadow, attrName);
if (values == null || values.isEmpty()) {
return null;
}
if (values.size() > 1) {
throw new SchemaException("Attempt to get single value from multi-valued attribute "+attrName);
}
return values.iterator().next();
}
public static void setPassword(ShadowType shadowType, ProtectedStringType password) {
CredentialsType credentialsType = shadowType.getCredentials();
if (credentialsType == null) {
credentialsType = new CredentialsType();
shadowType.setCredentials(credentialsType);
}
PasswordType passwordType = credentialsType.getPassword();
if (passwordType == null) {
passwordType = new PasswordType();
credentialsType.setPassword(passwordType);
}
passwordType.setValue(password);
}
public static ActivationType getOrCreateActivation(ShadowType shadowType) {
ActivationType activation = shadowType.getActivation();
if (activation == null) {
activation = new ActivationType();
shadowType.setActivation(activation);
}
return activation;
}
/**
* This is not supposed to be used in production code! It is just for the tests.
*/
public static void applyResourceSchema(PrismObject<? extends ShadowType> shadow,
ResourceSchema resourceSchema) throws SchemaException {
ShadowType shadowType = shadow.asObjectable();
QName objectClass = shadowType.getObjectClass();
ObjectClassComplexTypeDefinition objectClassDefinition = resourceSchema.findObjectClassDefinition(objectClass);
applyObjectClass(shadow, objectClassDefinition);
}
private static void applyObjectClass(PrismObject<? extends ShadowType> shadow,
ObjectClassComplexTypeDefinition objectClassDefinition) throws SchemaException {
PrismContainer<?> attributesContainer = shadow.findContainer(ShadowType.F_ATTRIBUTES);
ResourceAttributeContainerDefinition racDef = new ResourceAttributeContainerDefinition(ShadowType.F_ATTRIBUTES,
objectClassDefinition, objectClassDefinition.getPrismContext());
attributesContainer.applyDefinition(racDef, true);
}
public static PrismObjectDefinition<ShadowType> applyObjectClass(PrismObjectDefinition<ShadowType> shadowDefinition,
ObjectClassComplexTypeDefinition objectClassDefinition) throws SchemaException {
PrismObjectDefinition<ShadowType> shadowDefClone = shadowDefinition.cloneWithReplacedDefinition(ShadowType.F_ATTRIBUTES,
objectClassDefinition.toResourceAttributeContainerDefinition());
return shadowDefClone;
}
/**
* Returns intent from the shadow. Backwards compatible with older accountType. May also adjust for default
* intent if necessary.
*/
public static String getIntent(ShadowType shadow) {
if (shadow == null) {
return null;
}
String intent = shadow.getIntent();
if (intent != null) {
return intent;
}
return null;
}
public static ShadowKindType getKind(ShadowType shadow) {
if (shadow == null) {
return null;
}
ShadowKindType kind = shadow.getKind();
if (kind != null) {
return kind;
}
return ShadowKindType.ACCOUNT;
}
public static <T> Collection<T> getAttributeValues(ShadowType shadow, QName attributeQname, Class<T> type) {
ResourceAttributeContainer attributesContainer = getAttributesContainer(shadow);
if (attributesContainer == null) {
return null;
}
ResourceAttribute<T> attribute = attributesContainer.findAttribute(attributeQname);
if (attribute == null) {
return null;
}
return attribute.getRealValues(type);
}
public static void checkConsistence(PrismObject<? extends ShadowType> shadow, String desc) {
PrismReference resourceRef = shadow.findReference(ShadowType.F_RESOURCE_REF);
if (resourceRef == null) {
throw new IllegalStateException("No resourceRef in "+desc);
}
if (StringUtils.isBlank(resourceRef.getOid())) {
throw new IllegalStateException("Null or empty OID in resourceRef in "+desc);
}
ShadowType shadowType = shadow.asObjectable();
if (shadowType.getObjectClass() == null) {
throw new IllegalStateException("Null objectClass in "+desc);
}
PrismContainer<ShadowAttributesType> attributesContainer = shadow.findContainer(ShadowType.F_ATTRIBUTES);
if (attributesContainer != null) {
if (!(attributesContainer instanceof ResourceAttributeContainer)) {
throw new IllegalStateException("The attributes element expected to be ResourceAttributeContainer but it is "
+attributesContainer.getClass()+" instead in "+desc);
}
checkConsistency(attributesContainer.getDefinition(), " container definition in "+desc);
}
PrismContainerDefinition<ShadowAttributesType> attributesDefinition =
shadow.getDefinition().findContainerDefinition(ShadowType.F_ATTRIBUTES);
checkConsistency(attributesDefinition, " object definition in "+desc);
}
public static void checkConsistency(PrismContainerDefinition<ShadowAttributesType> attributesDefinition, String desc) {
if (attributesDefinition == null) {
throw new IllegalStateException("No definition for <attributes> in "+desc);
}
if (!(attributesDefinition instanceof ResourceAttributeContainerDefinition)) {
throw new IllegalStateException("The attributes element definition expected to be ResourceAttributeContainerDefinition but it is "
+attributesDefinition.getClass()+" instead in "+desc);
}
}
// TODO is this correct?
public static boolean isAccount(ShadowType shadowType) {
if (shadowType.getKind() != null) {
return shadowType.getKind() == ShadowKindType.ACCOUNT;
} else {
return true; // ???
}
}
public static boolean isProtected(PrismObject<? extends ShadowType> shadow) {
ShadowType shadowType = shadow.asObjectable();
Boolean protectedObject = shadowType.isProtectedObject();
return (protectedObject != null && protectedObject);
}
public static boolean isDead(ShadowType shadow){
return shadow.isDead() != null && shadow.isDead();
}
public static boolean matches(ShadowType shadowType, String resourceOid, ShadowKindType kind, String intent) {
if (shadowType == null) {
return false;
}
if (!resourceOid.equals(shadowType.getResourceRef().getOid())) {
return false;
}
if (!MiscUtil.equals(kind, shadowType.getKind())) {
return false;
}
if (intent == null) {
return true;
}
return MiscUtil.equals(intent, shadowType.getIntent());
}
}