-
Notifications
You must be signed in to change notification settings - Fork 188
/
PageEvaluateMapping.java
215 lines (187 loc) · 8.28 KB
/
PageEvaluateMapping.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
/*
* Copyright (c) 2010-2016 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 com.evolveum.midpoint.gui.api.model.NonEmptyModel;
import com.evolveum.midpoint.gui.api.model.NonEmptyWrapperModel;
import com.evolveum.midpoint.prism.PrismContext;
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.exception.CommonException;
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.web.application.AuthorizationAction;
import com.evolveum.midpoint.web.application.PageDescriptor;
import com.evolveum.midpoint.web.component.AceEditor;
import com.evolveum.midpoint.web.component.AjaxSubmitButton;
import com.evolveum.midpoint.web.page.admin.configuration.dto.ExecuteMappingDto;
import com.evolveum.midpoint.web.util.StringResourceChoiceRenderer;
import com.evolveum.midpoint.xml.ns._public.common.common_3.MappingEvaluationRequestType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.MappingEvaluationResponseType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.MappingType;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.OnChangeAjaxBehavior;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.List;
/**
* @author mederly
*/
@PageDescriptor(url = "/admin/config/evaluateMapping", 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_EVALUATE_MAPPING_URL,
label = "PageEvaluateMapping.auth.mapping.label", description = "PageEvaluateMapping.auth.mapping.description")
})
public class PageEvaluateMapping extends PageAdminConfiguration {
private static final Trace LOGGER = TraceManager.getTrace(PageEvaluateMapping.class);
private static final String DOT_CLASS = PageEvaluateMapping.class.getName() + ".";
private static final String OPERATION_EXECUTE_MAPPING = DOT_CLASS + "evaluateMapping";
private static final String ID_MAIN_FORM = "mainForm";
private static final String ID_EXECUTE = "execute";
private static final String ID_EDITOR_REQUEST = "editorRequest";
private static final String ID_EDITOR_MAPPING = "editorMapping";
private static final String ID_RESULT_TEXT = "resultText";
private static final String ID_MAPPING_SAMPLE = "mappingSample";
private static final String SAMPLES_DIR = "mapping-samples";
private static final List<String> SAMPLES = Arrays.asList(
"FullName_NoDelta",
"FullName_Delta",
"FullName_Delta_Ref",
"FullName_Delta_Cond",
"OrgName"
);
private final NonEmptyModel<ExecuteMappingDto> model = new NonEmptyWrapperModel<>(new Model<>(new ExecuteMappingDto()));
public PageEvaluateMapping() {
initLayout();
}
private void initLayout() {
Form mainForm = new Form(ID_MAIN_FORM);
add(mainForm);
AceEditor editorMapping = new AceEditor(
ID_EDITOR_MAPPING, new PropertyModel<String>(model, ExecuteMappingDto.F_MAPPING));
editorMapping.setHeight(400);
editorMapping.setResizeToMaxHeight(false);
mainForm.add(editorMapping);
AceEditor editorRequest = new AceEditor(
ID_EDITOR_REQUEST, new PropertyModel<String>(model, ExecuteMappingDto.F_REQUEST));
editorRequest.setHeight(430);
editorRequest.setResizeToMaxHeight(false);
mainForm.add(editorRequest);
AjaxSubmitButton evaluateMapping = new AjaxSubmitButton(ID_EXECUTE, createStringResource("PageEvaluateMapping.button.evaluateMapping")) {
@Override
protected void onError(AjaxRequestTarget target, Form<?> form) {
target.add(getFeedbackPanel());
}
@Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
executeMappingPerformed(target);
}
};
mainForm.add(evaluateMapping);
final DropDownChoice<String> sampleChoice = new DropDownChoice<>(ID_MAPPING_SAMPLE,
Model.of(""),
new AbstractReadOnlyModel<List<String>>() {
@Override
public List<String> getObject() {
return SAMPLES;
}
},
new StringResourceChoiceRenderer("PageEvaluateMapping.sample"));
sampleChoice.setNullValid(true);
sampleChoice.add(new OnChangeAjaxBehavior() {
@Override
protected void onUpdate(AjaxRequestTarget target) {
String sampleName = sampleChoice.getModelObject();
if (StringUtils.isEmpty(sampleName)) {
return;
}
model.getObject().setMapping(readResource(SAMPLES_DIR + "/" + sampleName + ".map.xml.data"));
model.getObject().setRequest(readResource(SAMPLES_DIR + "/" + sampleName + ".req.xml.data"));
model.getObject().setResultText("");
target.add(PageEvaluateMapping.this);
}
private String readResource(String name) {
InputStream is = PageEvaluateMapping.class.getResourceAsStream(name);
if (is != null) {
try {
return IOUtils.toString(is, "UTF-8");
} catch (IOException e) {
LoggingUtils.logUnexpectedException(LOGGER, "Couldn't read sample from resource {}", e, name);
} finally {
IOUtils.closeQuietly(is);
}
} else {
LOGGER.warn("Resource {} containing sample couldn't be found", name);
}
return null;
}
});
mainForm.add(sampleChoice);
AceEditor resultText = new AceEditor(ID_RESULT_TEXT, new PropertyModel<String>(model, ExecuteMappingDto.F_RESULT_TEXT));
resultText.setReadonly(true);
resultText.setHeight(300);
resultText.setResizeToMaxHeight(false);
resultText.setMode(null);
mainForm.add(resultText);
}
private void executeMappingPerformed(AjaxRequestTarget target) {
Task task = createSimpleTask(OPERATION_EXECUTE_MAPPING);
OperationResult result = new OperationResult(OPERATION_EXECUTE_MAPPING);
ExecuteMappingDto dto = model.getObject();
if (StringUtils.isBlank(dto.getMapping())) {
warn(getString("PageEvaluateMapping.message.emptyString"));
target.add(getFeedbackPanel());
return;
}
try {
MappingEvaluationRequestType request;
if (StringUtils.isNotBlank(dto.getRequest())) {
request = getPrismContext().parserFor(dto.getRequest()).xml().parseRealValue(MappingEvaluationRequestType.class);
} else {
request = new MappingEvaluationRequestType();
}
if (StringUtils.isNotBlank(dto.getMapping())) {
request.setMapping(getPrismContext().parserFor(dto.getMapping()).xml().parseRealValue(MappingType.class));
}
MappingEvaluationResponseType response = getModelDiagnosticService().evaluateMapping(request, task, result);
dto.setResultText(response.getResponse());
} catch (CommonException | RuntimeException e) {
result.recordFatalError("Couldn't execute mapping", e);
LoggingUtils.logUnexpectedException(LOGGER, "Couldn't execute mapping", e);
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
pw.close();
dto.setResultText(sw.toString());
} finally {
result.computeStatus();
}
showResult(result);
target.add(this);
}
}