-
Notifications
You must be signed in to change notification settings - Fork 188
/
WebMiscUtil.java
621 lines (521 loc) · 22.6 KB
/
WebMiscUtil.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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
/*
* 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.web.util;
import com.evolveum.midpoint.prism.PrismContainer;
import com.evolveum.midpoint.prism.PrismContainerValue;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.PrismProperty;
import com.evolveum.midpoint.prism.PrismPropertyValue;
import com.evolveum.midpoint.prism.crypto.EncryptionException;
import com.evolveum.midpoint.prism.crypto.Protector;
import com.evolveum.midpoint.prism.*;
import com.evolveum.midpoint.prism.delta.ObjectDelta;
import com.evolveum.midpoint.prism.delta.PropertyDelta;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.prism.polystring.PolyString;
import com.evolveum.midpoint.prism.xml.XmlTypeConverter;
import com.evolveum.midpoint.schema.SchemaConstantsGenerated;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.security.api.Authorization;
import com.evolveum.midpoint.security.api.AuthorizationConstants;
import com.evolveum.midpoint.security.api.MidPointPrincipal;
import com.evolveum.midpoint.util.exception.SystemException;
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.component.data.BaseSortableDataProvider;
import com.evolveum.midpoint.web.component.data.TablePanel;
import com.evolveum.midpoint.web.component.input.DropDownChoicePanel;
import com.evolveum.midpoint.web.component.prism.ObjectWrapper;
import com.evolveum.midpoint.web.component.util.LoadableModel;
import com.evolveum.midpoint.web.component.util.Selectable;
import com.evolveum.midpoint.web.component.wf.processes.itemApproval.ItemApprovalPanel;
import com.evolveum.midpoint.web.page.PageBase;
import com.evolveum.midpoint.web.security.MidPointApplication;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
import com.evolveum.prism.xml.ns._public.types_3.PolyStringType;
import com.evolveum.prism.xml.ns._public.types_3.ProtectedStringType;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;
import org.apache.wicket.Component;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.Session;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.authroles.authentication.AuthenticatedWebApplication;
import org.apache.wicket.authroles.authorization.strategies.role.Roles;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DataTable;
import org.apache.wicket.feedback.IFeedback;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.util.visit.IVisit;
import org.apache.wicket.util.visit.IVisitor;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.namespace.QName;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.RuntimeMXBean;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Locale;
/**
* @author lazyman
*/
public final class WebMiscUtil {
private static final Trace LOGGER = TraceManager.getTrace(WebMiscUtil.class);
private static DatatypeFactory df = null;
static {
try {
df = DatatypeFactory.newInstance();
} catch (DatatypeConfigurationException dce) {
throw new IllegalStateException("Exception while obtaining Datatype Factory instance", dce);
}
}
private WebMiscUtil() {
}
public static boolean isAuthorized(String... action) {
if (action == null) {
return true;
}
List<String> actions = Arrays.asList(action);
Roles roles = new Roles(AuthorizationConstants.AUTZ_ALL_URL);
roles.addAll(actions);
if (((AuthenticatedWebApplication) AuthenticatedWebApplication.get()).hasAnyRole(roles)) {
return true;
}
return false;
}
public static Integer safeLongToInteger(Long l) {
if (l == null) {
return null;
}
if (l > Integer.MAX_VALUE || l < Integer.MIN_VALUE) {
throw new IllegalArgumentException("Couldn't transform long '" + l + "' to int, too big or too small.");
}
return (int) l.longValue();
}
public static <T extends Enum> IModel<String> createLocalizedModelForEnum(T value, Component comp) {
String key = value != null ? value.getClass().getSimpleName() + "." + value.name() : "";
return new StringResourceModel(key, comp, null);
}
public static <T extends Enum> IModel<List<T>> createReadonlyModelFromEnum(final Class<T> type) {
return new AbstractReadOnlyModel<List<T>>() {
@Override
public List<T> getObject() {
List<T> list = new ArrayList<T>();
Collections.addAll(list, type.getEnumConstants());
return list;
}
};
}
public static DropDownChoicePanel createActivationStatusPanel(String id, final IModel<ActivationStatusType> model,
final Component component) {
return new DropDownChoicePanel(id, model,
WebMiscUtil.createReadonlyModelFromEnum(ActivationStatusType.class),
new IChoiceRenderer<ActivationStatusType>() {
@Override
public Object getDisplayValue(ActivationStatusType object) {
return WebMiscUtil.createLocalizedModelForEnum(object, component).getObject();
}
@Override
public String getIdValue(ActivationStatusType object, int index) {
return Integer.toString(index);
}
}, true);
}
public static DropDownChoicePanel createLockoutStatsPanel(String id, final IModel<LockoutStatusType> model,
final Component component){
return new DropDownChoicePanel(id, model,
WebMiscUtil.createReadonlyModelFromEnum(LockoutStatusType.class),
new IChoiceRenderer<LockoutStatusType>() {
@Override
public Object getDisplayValue(LockoutStatusType object) {
return WebMiscUtil.createLocalizedModelForEnum(object, component).getObject();
}
@Override
public String getIdValue(LockoutStatusType object, int index) {
return Integer.toString(index);
}
}, true);
}
public static String getName(ObjectType object) {
if (object == null) {
return null;
}
return getName(object.asPrismObject());
}
public static String getName(PrismObject object) {
if (object == null) {
return null;
}
PolyString name = getValue(object, ObjectType.F_NAME, PolyString.class);
return name != null ? name.getOrig() : null;
}
public static String getIdentification(ObjectType object) {
if (object == null) {
return null;
}
return getName(object.asPrismObject()) + " (" + object.getOid() + ")";
}
public static PolyStringType createPolyFromOrigString(String str) {
if (str == null) {
return null;
}
PolyStringType poly = new PolyStringType();
poly.setOrig(str);
return poly;
}
public static String getOrigStringFromPoly(PolyString str) {
return str != null ? str.getOrig() : null;
}
public static String getOrigStringFromPoly(PolyStringType str) {
return str != null ? str.getOrig() : null;
}
public static <T> T getValue(PrismContainerValue object, QName propertyName, Class<T> type) {
if (object == null) {
return null;
}
PrismProperty property = object.findProperty(propertyName);
if (property == null || property.isEmpty()) {
return null;
}
return (T) property.getRealValue(type);
}
public static <T> T getContainerValue(PrismContainerValue object, QName containerName,
Class<T> type) {
if (object == null) {
return null;
}
PrismContainer container = object.findContainer(containerName);
if (container == null || container.isEmpty()) {
return null;
}
PrismContainerValue containerValue = container.getValue();
if (containerValue == null || containerValue.isEmpty()) {
return null;
}
return (T) containerValue.getValue();
}
public static <T> T getValue(PrismContainer object, QName propertyName, Class<T> type) {
if (object == null) {
return null;
}
return getValue(object.getValue(), propertyName, type);
}
public static Locale getLocaleFromString(String localeString) {
if (localeString == null) {
return null;
}
localeString = localeString.trim();
if (localeString.toLowerCase().equals("default")) {
return Locale.getDefault();
}
// Extract language
int languageIndex = localeString.indexOf('_');
String language = null;
if (languageIndex == -1) {
// No further "_" so is "{language}" only
return new Locale(localeString, "");
} else {
language = localeString.substring(0, languageIndex);
}
// Extract country
int countryIndex = localeString.indexOf('_', languageIndex + 1);
String country = null;
if (countryIndex == -1) {
// No further "_" so is "{language}_{country}"
country = localeString.substring(languageIndex + 1);
return new Locale(language, country);
} else {
// Assume all remaining is the variant so is "{language}_{country}_{variant}"
country = localeString.substring(languageIndex + 1, countryIndex);
String variant = localeString.substring(countryIndex + 1);
return new Locale(language, country, variant);
}
}
public static void encryptCredentials(ObjectDelta delta, boolean encrypt, MidPointApplication app) {
if (delta == null || delta.isEmpty()) {
return;
}
PropertyDelta propertyDelta = delta.findPropertyDelta(new ItemPath(
SchemaConstantsGenerated.C_CREDENTIALS, CredentialsType.F_PASSWORD,
PasswordType.F_VALUE));
if (propertyDelta == null) {
return;
}
Collection<PrismPropertyValue<ProtectedStringType>> values = propertyDelta
.getValues(ProtectedStringType.class);
for (PrismPropertyValue<ProtectedStringType> value : values) {
ProtectedStringType string = value.getValue();
encryptProtectedString(string, encrypt, app);
}
}
public static void encryptCredentials(PrismObject object, boolean encrypt, MidPointApplication app) {
PrismContainer password = object.findContainer(new ItemPath(
SchemaConstantsGenerated.C_CREDENTIALS, CredentialsType.F_PASSWORD));
if (password == null) {
return;
}
PrismProperty protectedStringProperty = password.findProperty(PasswordType.F_VALUE);
if (protectedStringProperty == null
|| protectedStringProperty.getRealValue(ProtectedStringType.class) == null) {
return;
}
ProtectedStringType string = (ProtectedStringType) protectedStringProperty
.getRealValue(ProtectedStringType.class);
encryptProtectedString(string, encrypt, app);
}
public static void encryptProtectedString(ProtectedStringType string, boolean encrypt, MidPointApplication app) {
if (string == null) {
return;
}
Protector protector = app.getProtector();
try {
if (encrypt) {
if (StringUtils.isEmpty(string.getClearValue())) {
return;
}
protector.encrypt(string);
} else {
if (string.getEncryptedDataType() == null) {
return;
}
protector.decrypt(string);
}
} catch (EncryptionException ex) {
LoggingUtils.logException(LOGGER, "Couldn't encrypt protected string", ex);
} catch (SchemaException e) {
LoggingUtils.logException(LOGGER, "Couldn't encrypt/decrypt protected string", e);
}
}
public static <T extends Selectable> List<T> getSelectedData(TablePanel panel) {
DataTable table = panel.getDataTable();
BaseSortableDataProvider<T> provider = (BaseSortableDataProvider<T>) table.getDataProvider();
List<T> selected = new ArrayList<T>();
for (T bean : provider.getAvailableData()) {
if (bean.isSelected()) {
selected.add(bean);
}
}
return selected;
}
public static Collection<ObjectDelta<? extends ObjectType>> createDeltaCollection(ObjectDelta<? extends ObjectType>... deltas) {
Collection<ObjectDelta<? extends ObjectType>> collection = new ArrayList<ObjectDelta<? extends ObjectType>>();
for (ObjectDelta delta : deltas) {
collection.add(delta);
}
return collection;
}
public static boolean showResultInPage(OperationResult result) {
if (result == null) {
return false;
}
return !result.isSuccess() && !result.isHandledError() && !result.isInProgress();
}
public static String formatDate(XMLGregorianCalendar calendar) {
if (calendar == null) {
return null;
}
return formatDate(XmlTypeConverter.toDate(calendar));
}
public static String formatDate(Date date) {
return formatDate(null, date);
}
public static String formatDate(String format, Date date) {
if (date == null) {
return null;
}
if (StringUtils.isEmpty(format)) {
format = "EEEE, d. MMM yyyy HH:mm:ss";
}
Locale locale = Session.get().getLocale();
if (locale == null) {
locale = Locale.US;
}
SimpleDateFormat dateFormat = new SimpleDateFormat(format, locale);
return dateFormat.format(date);
}
public static boolean isActivationEnabled(PrismObject object) {
Validate.notNull(object);
PrismContainer activation = object.findContainer(UserType.F_ACTIVATION); //this is equal to account activation...
if (activation == null) {
return false;
}
ActivationStatusType status = (ActivationStatusType) activation.getPropertyRealValue(ActivationType.F_ADMINISTRATIVE_STATUS, ActivationStatusType.class);
if (status == null) {
return false;
}
//todo imrove with activation dates...
return ActivationStatusType.ENABLED.equals(status);
}
public static boolean isSuccessOrHandledError(OperationResult result) {
if (result == null) {
return false;
}
return result.isSuccess() || result.isHandledError();
}
public static String createUserIcon(PrismObject<UserType> object) {
UserType user = object.asObjectable();
//if user has superuser role assigned, it's superuser
for (AssignmentType assignment : user.getAssignment()) {
ObjectReferenceType targetRef = assignment.getTargetRef();
if (targetRef == null) {
continue;
}
if (StringUtils.equals(targetRef.getOid(), SystemObjectsType.ROLE_SUPERUSER.value())) {
return "fa fa-male text-danger";
}
}
ActivationType activation = user.getActivation();
if (activation != null && ActivationStatusType.DISABLED.equals(activation.getEffectiveStatus())){
return "fa fa-male text-muted";
}
return "fa fa-male";
}
public static String createUserIconTitle(PrismObject<UserType> object) {
UserType user = object.asObjectable();
//if user has superuser role assigned, it's superuser
for (AssignmentType assignment : user.getAssignment()) {
ObjectReferenceType targetRef = assignment.getTargetRef();
if (targetRef == null) {
continue;
}
if (StringUtils.equals(targetRef.getOid(), SystemObjectsType.ROLE_SUPERUSER.value())) {
return "User.superuser";
}
}
ActivationType activation = user.getActivation();
if (activation != null && ActivationStatusType.DISABLED.equals(activation.getEffectiveStatus())){
return "User.disabled";
}
return null;
}
public static double getSystemLoad() {
com.sun.management.OperatingSystemMXBean operatingSystemMXBean = (com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
int availableProcessors = operatingSystemMXBean.getAvailableProcessors();
long prevUpTime = runtimeMXBean.getUptime();
long prevProcessCpuTime = operatingSystemMXBean.getProcessCpuTime();
try {
Thread.sleep(30);
} catch (Exception ignored) {
}
operatingSystemMXBean = (com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
long upTime = runtimeMXBean.getUptime();
long processCpuTime = operatingSystemMXBean.getProcessCpuTime();
long elapsedCpu = processCpuTime - prevProcessCpuTime;
long elapsedTime = upTime - prevUpTime;
double cpuUsage = Math.min(99F, elapsedCpu / (elapsedTime * 10000F * availableProcessors));
return cpuUsage;
}
public static double getMaxRam() {
int MB = 1024 * 1024;
MemoryMXBean mBean = ManagementFactory.getMemoryMXBean();
long maxHeap = mBean.getHeapMemoryUsage().getMax();
long maxNonHeap = mBean.getNonHeapMemoryUsage().getMax();
return (maxHeap + maxNonHeap) / MB;
}
public static double getRamUsage() {
int MB = 1024 * 1024;
MemoryMXBean mBean = ManagementFactory.getMemoryMXBean();
long usedHead = mBean.getHeapMemoryUsage().getUsed();
long usedNonHeap = mBean.getNonHeapMemoryUsage().getUsed();
return (usedHead + usedNonHeap) / MB;
}
/**
* Checks table if has any selected rows ({@link Selectable} interface dtos), adds "single"
* parameter to selected items if it's not null. If table has no selected rows warn message
* is added to feedback panel, and feedback is refreshed through {@link AjaxRequestTarget}
*
* @param target
* @param single this parameter is used for row actions when action must be done only on chosen row.
* @param table
* @param page
* @param nothingWarnMessage
* @param <T>
* @return
*/
public static <T extends Selectable> List<T> isAnythingSelected(AjaxRequestTarget target, T single, TablePanel table,
PageBase page, String nothingWarnMessage) {
List<T> selected;
if (single != null) {
selected = new ArrayList<T>();
selected.add(single);
} else {
selected = WebMiscUtil.getSelectedData(table);
if (selected.isEmpty()) {
page.warn(page.getString(nothingWarnMessage));
target.add(page.getFeedbackPanel());
}
}
return selected;
}
public static void refreshFeedbacks(MarkupContainer component, final AjaxRequestTarget target) {
component.visitChildren(IFeedback.class, new IVisitor<Component, Void>() {
@Override
public void component(final Component component, final IVisit<Void> visit) {
target.add(component);
}
});
}
/*
* Methods used for providing prismContext into various objects.
*/
public static void revive(LoadableModel<?> loadableModel, PrismContext prismContext) throws SchemaException {
if (loadableModel != null) {
loadableModel.revive(prismContext);
}
}
public static void revive(IModel<?> model, PrismContext prismContext) throws SchemaException {
if (model != null && model.getObject() != null) {
reviveObject(model.getObject(), prismContext);
}
}
public static void reviveObject(Object object, PrismContext prismContext) throws SchemaException {
if (object == null) {
return;
}
if (object instanceof Collection) {
for (Object item : (Collection) object) {
reviveObject(item, prismContext);
}
} else if (object instanceof Revivable) {
((Revivable) object).revive(prismContext);
}
}
// useful for components other than those inheriting from PageBase
public static PrismContext getPrismContext(Component component) {
return ((MidPointApplication) component.getApplication()).getPrismContext();
}
public static void reviveIfNeeded(ObjectType objectType, Component component) {
if (objectType != null && objectType.asPrismObject().getPrismContext() == null) {
try {
objectType.asPrismObject().revive(getPrismContext(component));
} catch (SchemaException e) {
throw new SystemException("Couldn't revive " + objectType + " because of schema exception", e);
}
}
}
}