-
Notifications
You must be signed in to change notification settings - Fork 188
/
DescriptorLoader.java
243 lines (194 loc) · 8.85 KB
/
DescriptorLoader.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
/*
* Copyright (c) 2010-2017 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.application;
import com.evolveum.midpoint.security.api.AuthorizationConstants;
import com.evolveum.midpoint.util.ClassPathUtil;
import com.evolveum.midpoint.util.DebugDumpable;
import com.evolveum.midpoint.util.DebugUtil;
import com.evolveum.midpoint.util.DisplayableValue;
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.security.MidPointApplication;
import com.evolveum.midpoint.web.util.ExactMatchMountedMapper;
import com.evolveum.midpoint.xml.ns._public.gui.admin_1.DescriptorType;
import com.evolveum.midpoint.xml.ns._public.gui.admin_1.ObjectFactory;
import org.apache.commons.lang3.StringUtils;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.request.mapper.parameter.IPageParametersEncoder;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Unmarshaller;
import java.io.InputStream;
import java.util.*;
import java.util.function.Consumer;
/**
* @author lazyman
*/
public final class DescriptorLoader implements DebugDumpable {
private static final Trace LOGGER = TraceManager.getTrace(DescriptorLoader.class);
private static Map<String, DisplayableValue<String>[]> actions = new HashMap<>();
private static List<String> permitAllUrls = new ArrayList<>();
private static Map<String, Class> urlClassMap = new HashMap<>();
private String baseFileName = "/descriptor.xml";
private String customFileName = "/descriptor.xml";
public static Map<String, DisplayableValue<String>[]> getActions() {
return actions;
}
public static Map<String, Class> getUrlClassMap() {
return urlClassMap;
}
public String getBaseFileName() {
return baseFileName;
}
public void setBaseFileName(String baseFileName) {
this.baseFileName = baseFileName;
}
public String getCustomFileName() {
return customFileName;
}
public void setCustomFileName(String customFileName) {
this.customFileName = customFileName;
}
public static Collection<String> getPermitAllUrls() {
return permitAllUrls;
}
public void loadData(MidPointApplication application) {
LOGGER.debug("Loading data from descriptor files.");
try (InputStream baseInput = DescriptorLoader.class.getResourceAsStream(baseFileName);
InputStream customInput = DescriptorLoader.class.getResourceAsStream(customFileName)) {
if (baseInput == null) {
LOGGER.error("Couldn't find " + baseFileName + " file, can't load application menu and other stuff.");
}
JAXBContext context = JAXBContext.newInstance(ObjectFactory.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
JAXBElement<DescriptorType> element = (JAXBElement) unmarshaller.unmarshal(baseInput);
DescriptorType descriptor = element.getValue();
LOGGER.debug("Loading menu bar from " + baseFileName + " .");
DescriptorType customDescriptor = null;
if (customInput != null) {
element = (JAXBElement) unmarshaller.unmarshal(customInput);
customDescriptor = element.getValue();
}
scanPackagesForPages(descriptor.getPackagesToScan(), application);
if (customDescriptor != null) {
scanPackagesForPages(customDescriptor.getPackagesToScan(), application);
}
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("loaded:\n{}", debugDump(1));
}
} catch (Exception ex) {
LoggingUtils.logUnexpectedException(LOGGER, "Couldn't process application descriptor", ex);
}
}
private void scanPackagesForPages(List<String> packages, MidPointApplication application)
throws InstantiationException, IllegalAccessException {
for (String pac : packages) {
LOGGER.debug("Scanning package package {} for page annotations", new Object[]{pac});
Set<Class> classes = ClassPathUtil.listClasses(pac);
for (Class clazz : classes) {
if (!WebPage.class.isAssignableFrom(clazz)) {
continue;
}
PageDescriptor descriptor = (PageDescriptor) clazz.getAnnotation(PageDescriptor.class);
if (descriptor == null) {
continue;
}
mountPage(descriptor, clazz, application);
loadActions(descriptor);
}
}
}
private void loadActions(PageDescriptor descriptor) {
if (descriptor.permitAll()) {
foreachUrl(descriptor, url -> permitAllUrls.add(url));
return;
}
List<AuthorizationActionValue> actions = new ArrayList<>();
//avoid of setting guiAll authz for "public" pages (e.g. login page)
if (descriptor.action() == null || descriptor.action().length == 0) {
return;
}
boolean canAccess = true;
for (AuthorizationAction action : descriptor.action()) {
actions.add(new AuthorizationActionValue(action.actionUri(), action.label(), action.description()));
if (AuthorizationConstants.AUTZ_NO_ACCESS_URL.equals(action.actionUri())) {
canAccess = false;
break;
}
}
//add http://.../..#guiAll authorization only for displayable pages, not for pages used for development..
if (canAccess) {
actions.add(new AuthorizationActionValue(AuthorizationConstants.AUTZ_GUI_ALL_DEPRECATED_URL,
AuthorizationConstants.AUTZ_GUI_ALL_LABEL, AuthorizationConstants.AUTZ_GUI_ALL_DESCRIPTION));
actions.add(new AuthorizationActionValue(AuthorizationConstants.AUTZ_GUI_ALL_URL,
AuthorizationConstants.AUTZ_GUI_ALL_LABEL, AuthorizationConstants.AUTZ_GUI_ALL_DESCRIPTION));
}
foreachUrl(descriptor, url -> this.actions.put(url, actions.toArray(new DisplayableValue[actions.size()])));
}
private void foreachUrl(PageDescriptor descriptor, Consumer<String> urlConsumer) {
for (String url : descriptor.url()) {
urlConsumer.accept(buildPrefixUrl(url));
}
for (Url url : descriptor.urls()) {
String urlForSecurity = url.matchUrlForSecurity();
if (StringUtils.isEmpty(urlForSecurity)) {
urlForSecurity = buildPrefixUrl(url.mountUrl());
}
urlConsumer.accept(urlForSecurity);
}
}
public String buildPrefixUrl(String url) {
StringBuilder sb = new StringBuilder();
sb.append(url);
if (!url.endsWith("/")) {
sb.append("/");
}
sb.append("**");
return sb.toString();
}
private void mountPage(PageDescriptor descriptor, Class clazz, MidPointApplication application)
throws InstantiationException, IllegalAccessException {
//todo remove for cycle later
for (String url : descriptor.url()) {
IPageParametersEncoder encoder = descriptor.encoder().newInstance();
LOGGER.trace("Mounting page '{}' to url '{}' with encoder '{}'.", new Object[]{
clazz.getName(), url, encoder.getClass().getSimpleName()});
application.mount(new ExactMatchMountedMapper(url, clazz, encoder));
urlClassMap.put(url, clazz);
}
for (Url url : descriptor.urls()) {
IPageParametersEncoder encoder = descriptor.encoder().newInstance();
LOGGER.trace("Mounting page '{}' to url '{}' with encoder '{}'.", new Object[]{
clazz.getName(), url, encoder.getClass().getSimpleName()});
application.mount(new ExactMatchMountedMapper(url.mountUrl(), clazz, encoder));
urlClassMap.put(url.mountUrl(), clazz);
}
}
@Override
public String debugDump() {
return debugDump(0);
}
@Override
public String debugDump(int indent) {
StringBuilder sb = new StringBuilder();
DebugUtil.indentDebugDump(sb, indent);
sb.append("DescriptorLoader\n");
DebugUtil.debugDumpWithLabelLn(sb, "actions", actions, indent + 1);
DebugUtil.debugDumpWithLabel(sb, "urlClassMap", urlClassMap, indent + 1);
return sb.toString();
}
}