-
Notifications
You must be signed in to change notification settings - Fork 188
/
PageDebugView.java
381 lines (309 loc) · 17.2 KB
/
PageDebugView.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
/*
* Copyright (c) 2010-2015 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.web.page.admin.configuration;
import java.io.Serializable;
import java.util.Collection;
import javax.xml.namespace.QName;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.wicket.RestartResponseException;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxCheckBox;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.util.string.StringValue;
import com.evolveum.midpoint.gui.api.component.form.CheckBoxPanel;
import com.evolveum.midpoint.gui.api.model.LoadableModel;
import com.evolveum.midpoint.gui.api.util.WebComponentUtil;
import com.evolveum.midpoint.gui.api.util.WebModelServiceUtils;
import com.evolveum.midpoint.model.api.ModelExecuteOptions;
import com.evolveum.midpoint.prism.Objectable;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.delta.ObjectDelta;
import com.evolveum.midpoint.schema.GetOperationOptions;
import com.evolveum.midpoint.schema.SchemaConstantsGenerated;
import com.evolveum.midpoint.schema.SelectorOptions;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.security.api.AuthorizationConstants;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.util.Holder;
import com.evolveum.midpoint.util.MiscUtil;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.web.application.AuthorizationAction;
import com.evolveum.midpoint.web.application.PageDescriptor;
import com.evolveum.midpoint.web.component.AceEditor;
import com.evolveum.midpoint.web.component.AjaxButton;
import com.evolveum.midpoint.web.component.AjaxSubmitButton;
import com.evolveum.midpoint.web.component.form.Form;
import com.evolveum.midpoint.web.component.input.DataLanguagePanel;
import com.evolveum.midpoint.web.component.util.VisibleBehaviour;
import com.evolveum.midpoint.web.page.admin.dto.ObjectViewDto;
import com.evolveum.midpoint.web.security.MidPointApplication;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;
@PageDescriptor(url = "/admin/config/debug", action = {
@AuthorizationAction(actionUri = PageAdminConfiguration.AUTH_CONFIGURATION_ALL,
label = PageAdminConfiguration.AUTH_CONFIGURATION_ALL_LABEL, description = PageAdminConfiguration.AUTH_CONFIGURATION_ALL_DESCRIPTION),
@AuthorizationAction(actionUri = AuthorizationConstants.AUTZ_UI_CONFIGURATION_DEBUG_URL,
label = "PageDebugView.auth.debug.label", description = "PageDebugView.auth.debug.description")})
public class PageDebugView extends PageAdminConfiguration {
private static final long serialVersionUID = 1L;
private static final String DOT_CLASS = PageDebugView.class.getName() + ".";
private static final String OPERATION_LOAD_OBJECT = DOT_CLASS + "initObjectViewObject";
private static final String OPERATION_SAVE_OBJECT = DOT_CLASS + "saveObject";
private static final String ID_PLAIN_TEXTAREA = "plain-textarea";
private static final String ID_VIEW_BUTTON_PANEL = "viewButtonPanel";
private static final String ID_FORM = "mainForm";
private static final Trace LOGGER = TraceManager.getTrace(PageDebugView.class);
public static final String PARAM_OBJECT_ID = "objectId";
public static final String PARAM_OBJECT_TYPE = "objectType";
private String dataLanguage;
private IModel<ObjectViewDto<?>> objectViewDtoModel;
private DebugViewOptions debugViewConfiguration = new DebugViewOptions();
public PageDebugView() {
}
@Override
protected void onInitialize(){
super.onInitialize();
dataLanguage = determineDataLanguage();
objectViewDtoModel = initObjectViewObject();
initLayout();
}
@Override
protected IModel<String> createPageTitleModel() {
return createStringResource("PageDebugView.title", getName());
}
private String getName() {
if (objectViewDtoModel == null || objectViewDtoModel.getObject() == null) {
return "";
}
return objectViewDtoModel.getObject().getName();
}
private LoadableModel<ObjectViewDto<?>> initObjectViewObject() {
return new LoadableModel<ObjectViewDto<?>>(false) {
private static final long serialVersionUID = 1L;
@Override
protected ObjectViewDto<?> load() {
ObjectViewDto<?> objectViewDto = new ObjectViewDto<>();
StringValue objectOid = getPageParameters().get(PARAM_OBJECT_ID);
if (objectOid == null || StringUtils.isEmpty(objectOid.toString())) {
getSession().error(getString("pageDebugView.message.oidNotDefined"));
throw new RestartResponseException(PageDebugList.class);
}
Task task = createSimpleTask(OPERATION_LOAD_OBJECT);
OperationResult result = task.getResult(); //todo is this result != null ?
try {
MidPointApplication application = PageDebugView.this.getMidpointApplication();
GetOperationOptions rootOptions = GetOperationOptions.createRaw();
rootOptions.setResolveNames(true);
rootOptions.setTolerateRawData(true);
Collection<SelectorOptions<GetOperationOptions>> options = SelectorOptions.createCollection(rootOptions);
// FIXME: ObjectType.class will not work well here. We need more specific type.
//todo on page debug list create page params, put there oid and class for object type and send that to this page....read it here
Class<? extends ObjectType> type = getTypeFromParameters();
// TODO make this configurable (or at least do not show campaign cases in production)
WebModelServiceUtils.addIncludeOptionsForExportOrView(options, type);
PrismObject<? extends ObjectType> object = getModelService().getObject(type, objectOid.toString(), options, task, result);
PrismContext context = application.getPrismContext();
String lex = context.serializerFor(dataLanguage).serialize(object);
objectViewDto = new ObjectViewDto<>(object.getOid(), WebComponentUtil.getName(object), object, lex);
result.recomputeStatus();
} catch (Exception ex) {
result.recordFatalError("Couldn't load object.", ex);
}
showResult(result, false);
if (!WebComponentUtil.isSuccessOrHandledErrorOrWarning(result)) {
showResult(result, false);
throw new RestartResponseException(PageDebugList.class);
}
return objectViewDto;
}
};
}
private Class<? extends ObjectType> getTypeFromParameters() {
StringValue objectType = getPageParameters().get(PARAM_OBJECT_TYPE);
if (objectType != null && StringUtils.isNotBlank(objectType.toString())) {
return getPrismContext().getSchemaRegistry().determineCompileTimeClass(new QName(SchemaConstantsGenerated.NS_COMMON, objectType.toString()));
}
return ObjectType.class;
}
private void initLayout() {
final Form<?> mainForm = new Form<>(ID_FORM);
add(mainForm);
mainForm.add(createOptionCheckbox(DebugViewOptions.ID_ENCRYPT, new PropertyModel<>(debugViewConfiguration, DebugViewOptions.ID_ENCRYPT), "pageDebugView.encrypt", "pageDebugView.encrypt.help"));
mainForm.add(createOptionCheckbox(DebugViewOptions.ID_VALIDATE_SCHEMA, new PropertyModel<>(debugViewConfiguration, DebugViewOptions.ID_VALIDATE_SCHEMA), "pageDebugView.validateSchema", "pageDebugView.validateSchema.help"));
mainForm.add(createOptionCheckbox(DebugViewOptions.ID_SAVE_AS_RAW, new PropertyModel<>(debugViewConfiguration, DebugViewOptions.ID_SAVE_AS_RAW), "pageDebugView.saveAsRaw", "pageDebugView.saveAsRaw.help"));
mainForm.add(createOptionCheckbox(DebugViewOptions.ID_REEVALUATE_SEARCH_FILTERS, new PropertyModel<>(debugViewConfiguration, DebugViewOptions.ID_REEVALUATE_SEARCH_FILTERS), "pageDebugView.reevaluateSearchFilters", "pageDebugView.reevaluateSearchFilters.help"));
mainForm.add(createOptionCheckbox(DebugViewOptions.ID_SWITCH_TO_PLAINTEXT, new PropertyModel<>(debugViewConfiguration, DebugViewOptions.ID_SWITCH_TO_PLAINTEXT), "pageDebugView.switchToPlainText", "pageDebugView.switchToPlainText.help"));
TextArea<String> plainTextarea = new TextArea<>(ID_PLAIN_TEXTAREA, new PropertyModel<>(objectViewDtoModel, ObjectViewDto.F_XML));
plainTextarea.add(new VisibleBehaviour(() -> isTrue(DebugViewOptions.ID_SWITCH_TO_PLAINTEXT)));
mainForm.add(plainTextarea);
initAceEditor(mainForm);
initButtons(mainForm);
initViewButton(mainForm);
}
private CheckBoxPanel createOptionCheckbox(String id, IModel<Boolean> model, String labelKey, String helpKey) {
return new CheckBoxPanel(id, model, null, createStringResource(labelKey), createStringResource(helpKey)) {
private static final long serialVersionUID = 1L;
@Override
public void onUpdate(AjaxRequestTarget target) {
if (DebugViewOptions.ID_SWITCH_TO_PLAINTEXT.equals(id)) {
target.add(getMainForm());
}
}
};
}
private boolean isTrue(String panelId) {
CheckBoxPanel panel = (CheckBoxPanel) get(createComponentPath(ID_FORM, panelId));
if (panel == null) {
LOGGER.error("Cannot find panel: {}", panelId);
return false;
}
return panel.getValue();
}
private Form<?> getMainForm() {
return (Form<?>) get(ID_FORM);
}
private void initAceEditor(Form<?> mainForm){
AceEditor editor = new AceEditor("aceEditor", new PropertyModel<>(objectViewDtoModel, ObjectViewDto.F_XML));
editor.setModeForDataLanguage(dataLanguage);
editor.add(new VisibleBehaviour(() -> !isTrue(DebugViewOptions.ID_SWITCH_TO_PLAINTEXT)));
mainForm.add(editor);
}
private void initViewButton(Form mainForm) {
DataLanguagePanel<Objectable> dataLanguagePanel =
new DataLanguagePanel<Objectable>(ID_VIEW_BUTTON_PANEL, dataLanguage, Objectable.class, PageDebugView.this) {
private static final long serialVersionUID = 1L;
@Override
protected void onLanguageSwitched(AjaxRequestTarget target, int updatedIndex, String updatedLanguage,
String objectString) {
objectViewDtoModel.getObject().setXml(objectString);
dataLanguage = updatedLanguage;
target.add(mainForm);
}
@Override
protected String getObjectStringRepresentation() {
return objectViewDtoModel.getObject().getXml();
}
@Override
protected boolean isValidateSchema() {
return isTrue(DebugViewOptions.ID_VALIDATE_SCHEMA);
}
};
dataLanguagePanel.setOutputMarkupId(true);
mainForm.add(dataLanguagePanel);
}
private void initButtons(final Form<?> mainForm) {
AjaxSubmitButton saveButton = new AjaxSubmitButton("saveButton",
createStringResource("pageDebugView.button.save")) {
private static final long serialVersionUID = 1L;
@Override
protected void onSubmit(AjaxRequestTarget target, org.apache.wicket.markup.html.form.Form<?> form) {
savePerformed(target);
}
@Override
protected void onError(AjaxRequestTarget target, org.apache.wicket.markup.html.form.Form<?> form) {
target.add(getFeedbackPanel());
}
};
mainForm.add(saveButton);
AjaxButton backButton = new AjaxButton("backButton",
createStringResource("pageDebugView.button.back")) {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target) {
redirectBack();
}
};
mainForm.add(backButton);
}
public void savePerformed(AjaxRequestTarget target) {
if (StringUtils.isEmpty(objectViewDtoModel.getObject().getXml())) {
error(getString("pageDebugView.message.cantSaveEmpty"));
target.add(getFeedbackPanel());
return;
}
Task task = createSimpleTask(OPERATION_SAVE_OBJECT);
OperationResult result = task.getResult();
try {
PrismObject<? extends ObjectType> oldObject = objectViewDtoModel.getObject().getObject();
oldObject.revive(getPrismContext());
Holder<? extends ObjectType> objectHolder = new Holder<>(null);
validateObject(result, (Holder) objectHolder);
if (result.isAcceptable()) {
PrismObject<? extends ObjectType> newObject = objectHolder.getValue().asPrismObject();
ObjectDelta<? extends ObjectType> delta = oldObject.diff((PrismObject) newObject, true, true);
if (delta.getPrismContext() == null) {
LOGGER.warn("No prism context in delta {} after diff, adding it", delta);
delta.revive(getPrismContext());
}
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Delta to be applied:\n{}", delta.debugDump());
}
//quick fix for now (MID-1910), maybe it should be somewhere in objectViewModel..
// if (isReport(oldObject)){
// ReportTypeUtil.applyConfigurationDefinition((PrismObject)newObject, delta, getPrismContext());
// }
Collection<ObjectDelta<? extends ObjectType>> deltas = (Collection) MiscUtil.createCollection(delta);
ModelExecuteOptions options = new ModelExecuteOptions();
if (isTrue(DebugViewOptions.ID_SAVE_AS_RAW)) {
options.setRaw(true);
}
if (isTrue(DebugViewOptions.ID_REEVALUATE_SEARCH_FILTERS)) {
options.setReevaluateSearchFilters(true);
}
if(!isTrue(DebugViewOptions.ID_ENCRYPT)) {
options.setNoCrypt(true);
}
getModelService().executeChanges(deltas, options, task, result);
result.computeStatus();
}
} catch (Exception ex) {
result.recordFatalError("Couldn't save object.", ex);
}
if (result.isError()) {
showResult(result);
target.add(getFeedbackPanel());
} else {
showResult(result);
//to handle returning back to list objects page instead of edit object page
if (getBreadcrumbs().size() >= 3){
redirectBack(3);
} else {
redirectBack();
}
}
}
private void validateObject(OperationResult result, Holder<Objectable> objectHolder) {
parseObject(objectViewDtoModel.getObject().getXml(), (Holder<Objectable>) objectHolder, dataLanguage, isTrue(DebugViewOptions.ID_VALIDATE_SCHEMA), false, Objectable.class, result);
}
class DebugViewOptions implements Serializable {
private static final long serialVersionUID = 1L;
private final static String ID_ENCRYPT = "encrypt";
private final static String ID_SAVE_AS_RAW = "saveAsRaw";
private final static String ID_REEVALUATE_SEARCH_FILTERS = "reevaluateSearchFilters";
private final static String ID_VALIDATE_SCHEMA = "validateSchema";
private final static String ID_SWITCH_TO_PLAINTEXT = "switchToPlainText";
private final boolean encrypt=true;
private final boolean saveAsRaw=true;
private final boolean reevaluateSearchFilters = false;
private final boolean validateSchema = false;
private final boolean switchToPlainText = false;
}
}