You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@syncope.apache.org by il...@apache.org on 2019/12/11 10:24:01 UTC

[syncope] branch master updated: [SYNCOPE-1511] White noise: rework to allow easier extensions

This is an automated email from the ASF dual-hosted git repository.

ilgrosso pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/syncope.git


The following commit(s) were added to refs/heads/master by this push:
     new 7d15b4c  [SYNCOPE-1511] White noise: rework to allow easier extensions
7d15b4c is described below

commit 7d15b4c268b0950b49a55010cb5df6d3c5c0075d
Author: Francesco Chicchiriccò <il...@apache.org>
AuthorDate: Wed Dec 11 11:17:32 2019 +0100

    [SYNCOPE-1511] White noise: rework to allow easier extensions
---
 .../console/audit/AuditHistoryDirectoryPanel.java  | 87 ++++++++++------------
 .../client/console/audit/HistoryAuditDetails.java  | 46 ++++++------
 2 files changed, 59 insertions(+), 74 deletions(-)

diff --git a/client/idrepo/console/src/main/java/org/apache/syncope/client/console/audit/AuditHistoryDirectoryPanel.java b/client/idrepo/console/src/main/java/org/apache/syncope/client/console/audit/AuditHistoryDirectoryPanel.java
index e45775e..fccf1de 100644
--- a/client/idrepo/console/src/main/java/org/apache/syncope/client/console/audit/AuditHistoryDirectoryPanel.java
+++ b/client/idrepo/console/src/main/java/org/apache/syncope/client/console/audit/AuditHistoryDirectoryPanel.java
@@ -47,7 +47,6 @@ import org.apache.syncope.client.console.wicket.markup.html.form.ActionsPanel;
 import org.apache.syncope.client.ui.commons.DirectoryDataProvider;
 import org.apache.syncope.client.ui.commons.panels.ModalPanel;
 import org.apache.syncope.common.lib.AnyOperations;
-import org.apache.syncope.common.lib.SyncopeClientException;
 import org.apache.syncope.common.lib.request.AnyObjectUR;
 import org.apache.syncope.common.lib.request.GroupUR;
 import org.apache.syncope.common.lib.request.UserUR;
@@ -59,7 +58,6 @@ import org.apache.syncope.common.lib.to.ProvisioningResult;
 import org.apache.syncope.common.lib.to.UserTO;
 import org.apache.syncope.common.lib.types.AnyTypeKind;
 import org.apache.syncope.common.lib.types.AuditElements;
-import org.apache.syncope.common.lib.types.ClientExceptionType;
 import org.apache.syncope.common.lib.types.IdRepoEntitlement;
 import org.apache.wicket.PageReference;
 import org.apache.wicket.ajax.AjaxRequestTarget;
@@ -78,7 +76,7 @@ public class AuditHistoryDirectoryPanel extends
 
     private static final int TOTAL_AUDIT_HISTORY_COMPARISONS = 25;
 
-    private static final ObjectMapper MAPPER = new ObjectMapper();
+    protected static final ObjectMapper MAPPER = new ObjectMapper();
 
     private final BaseModal<?> baseModal;
 
@@ -100,60 +98,46 @@ public class AuditHistoryDirectoryPanel extends
         this.baseModal = baseModal;
         this.multiLevelPanelRef = multiLevelPanelRef;
         this.anyTO = anyTO;
+        this.anyTypeKind = AnyTypeKind.fromTOClass(anyTO.getClass());
 
-        anyTypeKind = AnyTypeKind.fromTOClass(anyTO.getClass());
+        this.restClient = new AuditHistoryRestClient();
         initResultTable();
     }
 
     /**
-     * Restore an object based on the audit record.
+     * Restore an object based on the audit record.Note that for user objects, the original audit record masks
+     * the password and the security answer; so we cannot use the audit record to resurrect the entry based on mask
+     * data.
      *
-     * Note that for user objects, the original audit record masks
-     * the password and the security answer; so we cannot use the audit
-     * record to resurrect the entry based on mask data. The method behavior
-     * below will reset the audit record such that the current security answer
-     * and the password for the object are always maintained, and such properties
-     * for the user cannot be restored using audit records.
+     * The method behavior below will reset the audit record such that the current security answer and the password for
+     * the object are always maintained, and such properties for the user cannot be restored using audit records.
      *
-     * @param entryBean the entry bean
+     * @param json the object from audit
      * @param anyTO the any to
      * @return the response
+     * @throws JsonProcessingException if json value cannot be parsed
      */
-    private static ProvisioningResult<? extends AnyTO> restore(final AuditEntryTO entryBean,
-            final AnyTO anyTO) {
-        try {
-            String json = getJSONFromAuditEntry(entryBean);
-            if (anyTO instanceof UserTO) {
-                UserTO userTO = MAPPER.readValue(json, UserTO.class);
-                UserUR req = AnyOperations.diff(userTO, anyTO, false);
-                req.setPassword(null);
-                req.setSecurityAnswer(null);
-                return new UserRestClient().update(anyTO.getETagValue(), req);
-            }
-            if (anyTO instanceof GroupTO) {
-                GroupTO groupTO = MAPPER.readValue(json, GroupTO.class);
-                GroupUR req = AnyOperations.diff(groupTO, anyTO, false);
-                return new GroupRestClient().update(anyTO.getETagValue(), req);
-            }
-            if (anyTO instanceof AnyObjectTO) {
-                AnyObjectTO anyObjectTO = MAPPER.readValue(json, AnyObjectTO.class);
-                AnyObjectUR req = AnyOperations.diff(anyObjectTO, anyTO, false);
-                return new AnyObjectRestClient().update(anyTO.getETagValue(), req);
-            }
-        } catch (final Exception e) {
-            LOG.error("Could not restore object for {}", anyTO, e);
+    protected ProvisioningResult<? extends AnyTO> restore(final String json, final AnyTO anyTO)
+            throws JsonProcessingException {
+
+        if (anyTO instanceof UserTO) {
+            UserTO userTO = MAPPER.readValue(json, UserTO.class);
+            UserUR req = AnyOperations.diff(userTO, anyTO, false);
+            req.setPassword(null);
+            req.setSecurityAnswer(null);
+            return new UserRestClient().update(anyTO.getETagValue(), req);
         }
-        throw SyncopeClientException.build(ClientExceptionType.InvalidAnyObject);
-    }
-
-    private static String getJSONFromAuditEntry(final AuditEntryTO entryBean) throws JsonProcessingException {
-        final String json;
-        if (entryBean.getBefore() == null) {
-            json = MAPPER.readTree(entryBean.getOutput()).get("entity").toPrettyString();
-        } else {
-            json = entryBean.getBefore();
+        if (anyTO instanceof GroupTO) {
+            GroupTO groupTO = MAPPER.readValue(json, GroupTO.class);
+            GroupUR req = AnyOperations.diff(groupTO, anyTO, false);
+            return new GroupRestClient().update(anyTO.getETagValue(), req);
+        }
+        if (anyTO instanceof AnyObjectTO) {
+            AnyObjectTO anyObjectTO = MAPPER.readValue(json, AnyObjectTO.class);
+            AnyObjectUR req = AnyOperations.diff(anyObjectTO, anyTO, false);
+            return new AnyObjectRestClient().update(anyTO.getETagValue(), req);
         }
-        return json;
+        throw new UnsupportedOperationException("Restore not supported for " + anyTO.getClass().getName());
     }
 
     private static SortParam<String> getSortParam() {
@@ -231,11 +215,16 @@ public class AuditHistoryDirectoryPanel extends
             public void onClick(final AjaxRequestTarget target, final AuditEntryTO modelObject) {
                 try {
                     AuditHistoryDirectoryPanel.this.getTogglePanel().close(target);
-                    ProvisioningResult<? extends AnyTO> result = restore(modelObject, anyTO);
+
+                    String json = modelObject.getBefore() == null
+                            ? MAPPER.readTree(modelObject.getOutput()).get("entity").toPrettyString()
+                            : modelObject.getBefore();
+                    ProvisioningResult<? extends AnyTO> result = restore(json, anyTO);
                     anyTO.setLastChangeDate(new Date(Long.parseLong(result.getEntity().getETagValue())));
+
                     target.add(container);
-                } catch (SyncopeClientException e) {
-                    LOG.error("While restoring {}", anyTypeKind, e);
+                } catch (Exception e) {
+                    LOG.error("While restoring {}", anyTO.getClass().getName(), e);
                     SyncopeConsoleSession.get().error(StringUtils.isBlank(e.getMessage())
                             ? e.getClass().getName() : e.getMessage());
                 }
@@ -262,7 +251,7 @@ public class AuditHistoryDirectoryPanel extends
         multiLevelPanelRef.next(
                 new StringResourceModel("audit.diff.view", this).getObject(),
                 new HistoryAuditDetails(modal, auditEntryBean,
-                        getPage().getPageReference(), toAuditEntryTOs(search), anyTO, anyTypeKind), target);
+                        getPage().getPageReference(), toAuditEntryTOs(search), anyTO), target);
     }
 
     private List<AuditEntryTO> toAuditEntryTOs(final List<AuditEntryTO> search) {
diff --git a/client/idrepo/console/src/main/java/org/apache/syncope/client/console/audit/HistoryAuditDetails.java b/client/idrepo/console/src/main/java/org/apache/syncope/client/console/audit/HistoryAuditDetails.java
index 5e47d7c..672f982 100644
--- a/client/idrepo/console/src/main/java/org/apache/syncope/client/console/audit/HistoryAuditDetails.java
+++ b/client/idrepo/console/src/main/java/org/apache/syncope/client/console/audit/HistoryAuditDetails.java
@@ -18,12 +18,12 @@
  */
 package org.apache.syncope.client.console.audit;
 
+import com.fasterxml.jackson.databind.ObjectMapper;
 import java.util.ArrayList;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.stream.Collectors;
-import com.fasterxml.jackson.databind.ObjectMapper;
 import org.apache.commons.lang3.tuple.Pair;
 import org.apache.syncope.client.console.SyncopeConsoleSession;
 import org.apache.syncope.client.console.panels.AbstractModalPanel;
@@ -36,7 +36,6 @@ import org.apache.syncope.client.ui.commons.markup.html.form.AjaxDropDownChoiceP
 import org.apache.syncope.common.lib.to.AnyTO;
 import org.apache.syncope.common.lib.to.AuditEntryTO;
 import org.apache.syncope.common.lib.to.UserTO;
-import org.apache.syncope.common.lib.types.AnyTypeKind;
 import org.apache.wicket.PageReference;
 import org.apache.wicket.ajax.AjaxRequestTarget;
 import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
@@ -58,21 +57,22 @@ public class HistoryAuditDetails extends MultilevelPanel.SecondLevel {
 
     private final List<AuditEntryTO> availableTOs;
 
-    private final AnyTypeKind anyTypeKind;
-
     private AbstractModalPanel<String> jsonPanel;
 
     private final AnyTO currentTO;
 
-    public HistoryAuditDetails(final BaseModal<?> baseModal, final AuditEntryTO selected,
-            final PageReference pageRef, final List<AuditEntryTO> availableTOs,
-            final AnyTO currentTO, final AnyTypeKind anyTypeKind) {
+    public HistoryAuditDetails(
+            final BaseModal<?> baseModal,
+            final AuditEntryTO selected,
+            final PageReference pageRef,
+            final List<AuditEntryTO> availableTOs,
+            final AnyTO currentTO) {
+
         super();
-        this.availableTOs = availableTOs.stream()
-                .filter(object -> !selected.equals(object) && selected.getBefore() != null)
-                .collect(Collectors.toList());
+        this.availableTOs = availableTOs.stream().
+                filter(object -> !selected.equals(object) && selected.getBefore() != null).
+                collect(Collectors.toList());
         this.selected = selected;
-        this.anyTypeKind = anyTypeKind;
         this.currentTO = currentTO;
 
         addCurrentInstanceConf();
@@ -119,7 +119,7 @@ public class HistoryAuditDetails extends MultilevelPanel.SecondLevel {
     }
 
     private String getSanitizedTOAsJSON(final AnyTO anyTO) throws Exception {
-        if (this.anyTypeKind == AnyTypeKind.USER) {
+        if (anyTO instanceof UserTO) {
             UserTO userTO = (UserTO) anyTO;
             userTO.setPassword(null);
             userTO.setSecurityAnswer(null);
@@ -130,15 +130,12 @@ public class HistoryAuditDetails extends MultilevelPanel.SecondLevel {
 
     private Pair<String, String> getJSONInfo(final AuditEntryTO auditEntryBean) {
         try {
-            final String content;
-            if (auditEntryBean.getBefore() == null) {
-                content = MAPPER.readTree(auditEntryBean.getOutput()).get("entity").toPrettyString();
-            } else {
-                content = auditEntryBean.getBefore();
-            }
+            String content = auditEntryBean.getBefore() == null
+                    ? MAPPER.readTree(auditEntryBean.getOutput()).get("entity").toPrettyString()
+                    : auditEntryBean.getBefore();
 
-            AnyTO userTO = MAPPER.readValue(content, anyTypeKind.getTOClass());
-            String json = getSanitizedTOAsJSON(userTO);
+            AnyTO anyTO = MAPPER.readValue(content, currentTO.getClass());
+            String json = getSanitizedTOAsJSON(anyTO);
             return Pair.of(auditEntryBean.getKey(), json);
         } catch (Exception e) {
             throw new RuntimeException(e);
@@ -147,14 +144,13 @@ public class HistoryAuditDetails extends MultilevelPanel.SecondLevel {
 
     private static <T extends AuditEntryTO> Map<String, String> getDropdownNamesMap(final List<T> entries) {
         Map<String, String> map = new LinkedHashMap<>();
-        for (AuditEntryTO audit : entries) {
-            String value = audit.getWho()
-                    + " - " + SyncopeConsoleSession.get().getDateFormat().format(audit.getDate());
+        entries.forEach(audit -> {
+            String value = audit.getWho() + " - " + SyncopeConsoleSession.get().getDateFormat().format(audit.getDate());
             if (audit.getKey().equalsIgnoreCase(KEY_CURRENT)) {
                 value += " - " + audit.getKey();
             }
             map.put(audit.getKey(), value);
-        }
+        });
         return map;
     }
 
@@ -163,7 +159,7 @@ public class HistoryAuditDetails extends MultilevelPanel.SecondLevel {
         form.setModel(new CompoundPropertyModel<>(selected));
         form.setOutputMarkupId(true);
 
-        final Map<String, String> namesMap = getDropdownNamesMap(availableTOs);
+        Map<String, String> namesMap = getDropdownNamesMap(availableTOs);
         List<String> keys = new ArrayList<>(namesMap.keySet());
 
         final AjaxDropDownChoicePanel<String> dropdownElem = new AjaxDropDownChoicePanel<>(