-
Notifications
You must be signed in to change notification settings - Fork 188
/
ModelWebService.java
459 lines (408 loc) · 20 KB
/
ModelWebService.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
* 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.model.impl;
import com.evolveum.midpoint.model.api.ModelExecuteOptions;
import com.evolveum.midpoint.model.api.ModelPort;
import com.evolveum.midpoint.model.common.util.AbstractModelWebService;
import com.evolveum.midpoint.model.impl.controller.ModelController;
import com.evolveum.midpoint.model.impl.scripting.Data;
import com.evolveum.midpoint.model.impl.scripting.ExecutionContext;
import com.evolveum.midpoint.model.api.ScriptExecutionException;
import com.evolveum.midpoint.model.impl.scripting.ScriptingExpressionEvaluator;
import com.evolveum.midpoint.prism.Item;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.PrismProperty;
import com.evolveum.midpoint.prism.delta.ObjectDelta;
import com.evolveum.midpoint.prism.query.ObjectQuery;
import com.evolveum.midpoint.prism.query.QueryJaxbConvertor;
import com.evolveum.midpoint.schema.DeltaConvertor;
import com.evolveum.midpoint.schema.GetOperationOptions;
import com.evolveum.midpoint.schema.ObjectDeltaOperation;
import com.evolveum.midpoint.schema.SelectorOptions;
import com.evolveum.midpoint.schema.constants.ObjectTypes;
import com.evolveum.midpoint.schema.constants.SchemaConstants;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.schema.util.MiscSchemaUtil;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.util.exception.CommunicationException;
import com.evolveum.midpoint.util.exception.ConfigurationException;
import com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException;
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.logging.LoggingUtils;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.xml.ns._public.common.api_types_3.ExecuteScriptsOptionsType;
import com.evolveum.midpoint.xml.ns._public.common.api_types_3.ObjectDeltaListType;
import com.evolveum.midpoint.xml.ns._public.common.api_types_3.ObjectDeltaOperationListType;
import com.evolveum.midpoint.xml.ns._public.common.api_types_3.ObjectListType;
import com.evolveum.midpoint.xml.ns._public.common.api_types_3.OutputFormatType;
import com.evolveum.midpoint.xml.ns._public.common.api_types_3.ScriptOutputsType;
import com.evolveum.midpoint.xml.ns._public.common.api_types_3.SelectorQualifiedGetOptionsType;
import com.evolveum.midpoint.xml.ns._public.common.api_types_3.SingleScriptOutputType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ModelExecuteOptionsType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectDeltaOperationType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.OperationResultType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceObjectShadowChangeDescriptionType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.TaskType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType;
import com.evolveum.midpoint.xml.ns._public.common.fault_3.FaultMessage;
import com.evolveum.midpoint.xml.ns._public.common.fault_3.FaultType;
import com.evolveum.midpoint.xml.ns._public.common.fault_3.IllegalArgumentFaultType;
import com.evolveum.midpoint.xml.ns._public.common.fault_3.ObjectAlreadyExistsFaultType;
import com.evolveum.midpoint.xml.ns._public.common.fault_3.ObjectNotFoundFaultType;
import com.evolveum.midpoint.xml.ns._public.common.fault_3.SystemFaultType;
import com.evolveum.midpoint.xml.ns._public.model.model_3.ExecuteScriptsResponseType;
import com.evolveum.midpoint.xml.ns._public.model.model_3.ExecuteScriptsType;
import com.evolveum.midpoint.xml.ns._public.model.model_3.ModelPortType;
import com.evolveum.midpoint.xml.ns._public.model.scripting_3.ItemListType;
import com.evolveum.midpoint.xml.ns._public.model.scripting_3.ScriptingExpressionType;
import com.evolveum.prism.xml.ns._public.query_3.QueryType;
import com.evolveum.prism.xml.ns._public.types_3.RawType;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.namespace.QName;
import javax.xml.ws.Holder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
*
* @author lazyman
*
*/
@Service
public class ModelWebService extends AbstractModelWebService implements ModelPortType, ModelPort {
private static final Trace LOGGER = TraceManager.getTrace(ModelWebService.class);
@Autowired(required = true)
private ModelCrudService model;
// for more complicated interactions (like executeChanges)
@Autowired
private ModelController modelController;
@Autowired
private ScriptingExpressionEvaluator scriptingExpressionEvaluator;
@Override
public void getObject(QName objectType, String oid, SelectorQualifiedGetOptionsType optionsType,
Holder<ObjectType> objectHolder, Holder<OperationResultType> resultHolder) throws FaultMessage {
notNullArgument(objectType, "Object type must not be null.");
notEmptyArgument(oid, "Oid must not be null or empty.");
Task task = createTaskInstance(GET_OBJECT);
auditLogin(task);
OperationResult operationResult = task.getResult();
try {
Class objectClass = ObjectTypes.getObjectTypeFromTypeQName(objectType).getClassDefinition();
Collection<SelectorOptions<GetOperationOptions>> options = MiscSchemaUtil.optionsTypeToOptions(optionsType);
PrismObject<? extends ObjectType> object = model.getObject(objectClass, oid, options, task, operationResult);
handleOperationResult(operationResult, resultHolder);
objectHolder.value = object.asObjectable();
return;
} catch (Exception ex) {
LoggingUtils.logException(LOGGER, "# MODEL getObject() failed", ex);
auditLogout(task);
throw createSystemFault(ex, operationResult);
}
}
@Override
public void searchObjects(QName objectType, QueryType query, SelectorQualifiedGetOptionsType optionsType,
Holder<ObjectListType> objectListHolder, Holder<OperationResultType> result) throws FaultMessage {
notNullArgument(objectType, "Object type must not be null.");
Task task = createTaskInstance(SEARCH_OBJECTS);
auditLogin(task);
OperationResult operationResult = task.getResult();
try {
Class objectClass = ObjectTypes.getObjectTypeFromTypeQName(objectType).getClassDefinition();
Collection<SelectorOptions<GetOperationOptions>> options = MiscSchemaUtil.optionsTypeToOptions(optionsType);
ObjectQuery q = QueryJaxbConvertor.createObjectQuery(objectClass, query, prismContext);
List<PrismObject<? extends ObjectType>> list = (List)model.searchObjects(objectClass, q, options, task, operationResult);
handleOperationResult(operationResult, result);
ObjectListType listType = new ObjectListType();
for (PrismObject<? extends ObjectType> o : list) {
listType.getObject().add(o.asObjectable());
}
objectListHolder.value = listType;
} catch (Exception ex) {
LoggingUtils.logException(LOGGER, "# MODEL searchObjects() failed", ex);
auditLogout(task);
throw createSystemFault(ex, operationResult);
}
}
@Override
public ObjectDeltaOperationListType executeChanges(ObjectDeltaListType deltaList, ModelExecuteOptionsType optionsType) throws FaultMessage {
notNullArgument(deltaList, "Object delta list must not be null.");
Task task = createTaskInstance(EXECUTE_CHANGES);
auditLogin(task);
OperationResult operationResult = task.getResult();
try {
Collection<ObjectDelta> deltas = DeltaConvertor.createObjectDeltas(deltaList, prismContext);
for (ObjectDelta delta : deltas) {
prismContext.adopt(delta);
}
ModelExecuteOptions options = ModelExecuteOptions.fromModelExecutionOptionsType(optionsType);
Collection<ObjectDeltaOperation<? extends ObjectType>> objectDeltaOperations = modelController.executeChanges((Collection) deltas, options, task, operationResult); // brutally eliminating type-safety compiler barking
ObjectDeltaOperationListType retval = new ObjectDeltaOperationListType();
for (ObjectDeltaOperation objectDeltaOperation : objectDeltaOperations) {
ObjectDeltaOperationType objectDeltaOperationType = new ObjectDeltaOperationType();
objectDeltaOperationType.setObjectDelta(DeltaConvertor.toObjectDeltaType(objectDeltaOperation.getObjectDelta()));
objectDeltaOperationType.setExecutionResult(objectDeltaOperation.getExecutionResult().createOperationResultType());
retval.getDeltaOperation().add(objectDeltaOperationType);
}
return retval;
} catch (Exception ex) {
LoggingUtils.logException(LOGGER, "# MODEL executeChanges() failed", ex);
auditLogout(task);
throw createSystemFault(ex, operationResult);
}
}
@Override
public void findShadowOwner(String accountOid, Holder<UserType> userHolder, Holder<OperationResultType> result)
throws FaultMessage {
notEmptyArgument(accountOid, "Account oid must not be null or empty.");
Task task = createTaskInstance(LIST_ACCOUNT_SHADOW_OWNER);
auditLogin(task);
OperationResult operationResult = task.getResult();
try {
PrismObject<UserType> user = model.findShadowOwner(accountOid, task, operationResult);
handleOperationResult(operationResult, result);
if (user != null) {
userHolder.value = user.asObjectable();
}
return;
} catch (Exception ex) {
LoggingUtils.logException(LOGGER, "# MODEL findShadowOwner() failed", ex);
auditLogout(task);
throw createSystemFault(ex, operationResult);
}
}
@Override
public OperationResultType testResource(String resourceOid) throws FaultMessage {
notEmptyArgument(resourceOid, "Resource oid must not be null or empty.");
Task task = createTaskInstance(TEST_RESOURCE);
auditLogin(task);
try {
OperationResult testResult = model.testResource(resourceOid, task);
return handleOperationResult(testResult);
} catch (Exception ex) {
LoggingUtils.logException(LOGGER, "# MODEL testResource() failed", ex);
auditLogout(task);
throw createSystemFault(ex, null);
}
}
@Override
public ExecuteScriptsResponseType executeScripts(ExecuteScriptsType parameters) throws FaultMessage {
Task task = createTaskInstance(EXECUTE_SCRIPTS);
auditLogin(task);
OperationResult result = task.getResult();
try {
List<JAXBElement<?>> scriptsToExecute = parseScripts(parameters);
return doExecuteScripts(scriptsToExecute, parameters.getOptions(), task, result);
} catch (Exception ex) {
LoggingUtils.logException(LOGGER, "# MODEL executeScripts() failed", ex);
auditLogout(task);
throw createSystemFault(ex, null);
}
}
private List<JAXBElement<?>> parseScripts(ExecuteScriptsType parameters) throws JAXBException, SchemaException {
List<JAXBElement<?>> scriptsToExecute = new ArrayList<>();
if (parameters.getXmlScripts() != null) {
for (Object scriptAsObject : parameters.getXmlScripts().getAny()) {
if (scriptAsObject instanceof JAXBElement) {
scriptsToExecute.add((JAXBElement) scriptAsObject);
} else {
throw new IllegalArgumentException("Invalid script type: " + scriptAsObject.getClass());
}
}
} else {
// here comes MSL script decoding (however with a quick hack to allow passing XML as text here)
String scriptsAsString = parameters.getMslScripts();
if (scriptsAsString.startsWith("<?xml")) {
PrismProperty expressionType = (PrismProperty) prismContext.parseAnyData(scriptsAsString, PrismContext.LANG_XML);
if (expressionType.size() != 1) {
throw new IllegalArgumentException("Unexpected number of scripting expressions at input: " + expressionType.size() + " (expected 1)");
}
scriptsToExecute.add(expressionType.getAnyValue().toJaxbElement());
}
}
return scriptsToExecute;
}
private ExecuteScriptsResponseType doExecuteScripts(List<JAXBElement<?>> scriptsToExecute, ExecuteScriptsOptionsType options, Task task, OperationResult result) {
ExecuteScriptsResponseType response = new ExecuteScriptsResponseType();
ScriptOutputsType outputs = new ScriptOutputsType();
response.setOutputs(outputs);
try {
for (JAXBElement<?> script : scriptsToExecute) {
ExecutionContext outputContext = scriptingExpressionEvaluator.evaluateExpression((ScriptingExpressionType) script.getValue(), task, result);
SingleScriptOutputType output = new SingleScriptOutputType();
outputs.getOutput().add(output);
output.setTextOutput(outputContext.getConsoleOutput());
if (options == null || options.getOutputFormat() == null || options.getOutputFormat() == OutputFormatType.XML) {
output.setXmlData(prepareXmlData(outputContext.getFinalOutput()));
} else {
// temporarily we send serialized XML in the case of MSL output
ItemListType jaxbOutput = prepareXmlData(outputContext.getFinalOutput());
output.setMslData(prismContext.serializeAnyData(jaxbOutput, SchemaConstants.C_VALUE, PrismContext.LANG_XML));
}
}
result.computeStatusIfUnknown();
} catch (ScriptExecutionException|JAXBException|SchemaException|RuntimeException e) {
result.recordFatalError(e.getMessage(), e);
LoggingUtils.logException(LOGGER, "Exception while executing script", e);
}
result.summarize();
response.setResult(result.createOperationResultType());
return response;
}
private ItemListType prepareXmlData(Data output) throws JAXBException, SchemaException {
ItemListType itemListType = new ItemListType();
if (output != null) {
for (Item item : output.getData()) {
RawType rawType = prismContext.toRawType(item);
itemListType.getItem().add(rawType);
}
}
return itemListType;
}
private void handleOperationResult(OperationResult result, Holder<OperationResultType> holder) {
result.recordSuccess();
OperationResultType resultType = result.createOperationResultType();
if (holder.value == null) {
holder.value = resultType;
} else {
holder.value.getPartialResults().add(resultType);
}
}
private OperationResultType handleOperationResult(OperationResult result) {
result.recordSuccess();
return result.createOperationResultType();
}
private void notNullResultHolder(Holder<OperationResultType> holder) throws FaultMessage {
notNullArgument(holder, "Holder must not be null.");
notNullArgument(holder.value, "Result type must not be null.");
}
private <T> void notNullHolder(Holder<T> holder) throws FaultMessage {
notNullArgument(holder, "Holder must not be null.");
notNullArgument(holder.value, holder.getClass().getSimpleName() + " must not be null (in Holder).");
}
private void notEmptyArgument(String object, String message) throws FaultMessage {
if (StringUtils.isEmpty(object)) {
throw createIllegalArgumentFault(message);
}
}
private void notNullArgument(Object object, String message) throws FaultMessage {
if (object == null) {
throw createIllegalArgumentFault(message);
}
}
public FaultMessage createIllegalArgumentFault(String message) {
FaultType faultType = new IllegalArgumentFaultType();
return new FaultMessage(message, faultType);
}
public FaultMessage createSystemFault(Exception ex, OperationResult result) {
if (result != null) {
result.recordFatalError(ex.getMessage(), ex);
}
FaultType faultType;
if (ex instanceof ObjectNotFoundException) {
faultType = new ObjectNotFoundFaultType();
} else if (ex instanceof IllegalArgumentException) {
faultType = new IllegalArgumentFaultType();
} else if (ex instanceof ObjectAlreadyExistsException){
faultType = new ObjectAlreadyExistsFaultType();
} else{
faultType = new SystemFaultType();
}
faultType.setMessage(ex.getMessage());
if (result != null) {
faultType.setOperationResult(result.createOperationResultType());
}
return new FaultMessage(ex.getMessage(), faultType, ex);
}
@Override
public TaskType importFromResource(String resourceOid, QName objectClass)
throws FaultMessage {
notEmptyArgument(resourceOid, "Resource oid must not be null or empty.");
notNullArgument(objectClass, "Object class must not be null.");
Task task = createTaskInstance(IMPORT_FROM_RESOURCE);
auditLogin(task);
OperationResult operationResult = task.getResult();
try {
model.importFromResource(resourceOid, objectClass, task, operationResult);
operationResult.computeStatus();
return handleTaskResult(task);
} catch (Exception ex) {
LoggingUtils.logException(LOGGER, "# MODEL importFromResource() failed", ex);
auditLogout(task);
throw createSystemFault(ex, operationResult);
}
}
@Override
public TaskType notifyChange(ResourceObjectShadowChangeDescriptionType changeDescription)
throws FaultMessage {
// TODO Auto-generated method stub
notNullArgument(changeDescription, "Change description must not be null");
LOGGER.trace("notify change started");
Task task = createTaskInstance(NOTIFY_CHANGE);
OperationResult parentResult = task.getResult();
try {
model.notifyChange(changeDescription, parentResult, task);
} catch (ObjectNotFoundException ex) {
LoggingUtils.logException(LOGGER, "# MODEL notifyChange() failed", ex);
auditLogout(task);
throw createSystemFault(ex, parentResult);
} catch (SchemaException ex) {
LoggingUtils.logException(LOGGER, "# MODEL notifyChange() failed", ex);
auditLogout(task);
throw createSystemFault(ex, parentResult);
} catch (CommunicationException ex) {
LoggingUtils.logException(LOGGER, "# MODEL notifyChange() failed", ex);
auditLogout(task);
throw createSystemFault(ex, parentResult);
} catch (ConfigurationException ex) {
LoggingUtils.logException(LOGGER, "# MODEL notifyChange() failed", ex);
auditLogout(task);
throw createSystemFault(ex, parentResult);
} catch (SecurityViolationException ex) {
LoggingUtils.logException(LOGGER, "# MODEL notifyChange() failed", ex);
auditLogout(task);
throw createSystemFault(ex, parentResult);
} catch (RuntimeException ex){
LoggingUtils.logException(LOGGER, "# MODEL notifyChange() failed", ex);
auditLogout(task);
throw createSystemFault(ex, parentResult);
} catch (ObjectAlreadyExistsException ex){
LoggingUtils.logException(LOGGER, "# MODEL notifyChange() failed", ex);
auditLogout(task);
throw createSystemFault(ex, parentResult);
}
LOGGER.info("notify change ended.");
LOGGER.info("result of notify change: {}", parentResult.debugDump());
return handleTaskResult(task);
}
/**
* return appropriate form of taskType (and result) to
* return back to a web service caller.
*
* @param task
*/
private TaskType handleTaskResult(Task task) {
return task.getTaskPrismObject().asObjectable();
}
}