From 143e9cb877fb6f6e5b885184643b47d2c48e5905 Mon Sep 17 00:00:00 2001 From: ShvaykaD Date: Thu, 14 Jan 2021 10:21:51 +0200 Subject: [PATCH] added new methods to JacksonUtil & replaced objectMapper from AuditLogServiceImpl --- .../server/dao/audit/AuditLogServiceImpl.java | 16 +++++++--------- .../server/dao/util/mapping/JacksonUtil.java | 9 +++++++++ 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/dao/src/main/java/org/thingsboard/server/dao/audit/AuditLogServiceImpl.java b/dao/src/main/java/org/thingsboard/server/dao/audit/AuditLogServiceImpl.java index 0d1c708f68..6c8e2a25ff 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/audit/AuditLogServiceImpl.java +++ b/dao/src/main/java/org/thingsboard/server/dao/audit/AuditLogServiceImpl.java @@ -17,7 +17,6 @@ package org.thingsboard.server.dao.audit; import com.datastax.driver.core.utils.UUIDs; import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; import com.google.common.collect.Lists; @@ -48,6 +47,7 @@ import org.thingsboard.server.dao.audit.sink.AuditLogSink; import org.thingsboard.server.dao.entity.EntityService; import org.thingsboard.server.dao.exception.DataValidationException; import org.thingsboard.server.dao.service.DataValidator; +import org.thingsboard.server.dao.util.mapping.JacksonUtil; import java.io.PrintWriter; import java.io.StringWriter; @@ -61,8 +61,6 @@ import static org.thingsboard.server.dao.service.Validator.validateId; @ConditionalOnProperty(prefix = "audit-log", value = "enabled", havingValue = "true") public class AuditLogServiceImpl implements AuditLogService { - private static final ObjectMapper objectMapper = new ObjectMapper(); - private static final String INCORRECT_TENANT_ID = "Incorrect tenantId "; private static final int INSERTS_PER_ENTRY = 3; @@ -159,7 +157,7 @@ public class AuditLogServiceImpl implements AuditLogService { private JsonNode constructActionData(I entityId, E entity, ActionType actionType, Object... additionalInfo) { - ObjectNode actionData = objectMapper.createObjectNode(); + ObjectNode actionData = JacksonUtil.newObjectNode(); switch (actionType) { case ADDED: case UPDATED: @@ -168,7 +166,7 @@ public class AuditLogServiceImpl implements AuditLogService { case RELATIONS_DELETED: case ASSIGNED_TO_TENANT: if (entity != null) { - ObjectNode entityNode = objectMapper.valueToTree(entity); + ObjectNode entityNode = (ObjectNode) JacksonUtil.valueToTree(entity); if (entityId.getEntityType() == EntityType.DASHBOARD) { entityNode.put("configuration", ""); } @@ -177,7 +175,7 @@ public class AuditLogServiceImpl implements AuditLogService { if (entityId.getEntityType() == EntityType.RULE_CHAIN) { RuleChainMetaData ruleChainMetaData = extractParameter(RuleChainMetaData.class, additionalInfo); if (ruleChainMetaData != null) { - ObjectNode ruleChainMetaDataNode = objectMapper.valueToTree(ruleChainMetaData); + ObjectNode ruleChainMetaDataNode = (ObjectNode) JacksonUtil.valueToTree(ruleChainMetaData); actionData.set("metadata", ruleChainMetaDataNode); } } @@ -194,7 +192,7 @@ public class AuditLogServiceImpl implements AuditLogService { String scope = extractParameter(String.class, 0, additionalInfo); List attributes = extractParameter(List.class, 1, additionalInfo); actionData.put("scope", scope); - ObjectNode attrsNode = objectMapper.createObjectNode(); + ObjectNode attrsNode = JacksonUtil.newObjectNode(); if (attributes != null) { for (AttributeKvEntry attr : attributes) { attrsNode.put(attr.getKey(), attr.getValueAsString()); @@ -225,7 +223,7 @@ public class AuditLogServiceImpl implements AuditLogService { case CREDENTIALS_UPDATED: actionData.put("entityId", entityId.toString()); DeviceCredentials deviceCredentials = extractParameter(DeviceCredentials.class, additionalInfo); - actionData.set("credentials", objectMapper.valueToTree(deviceCredentials)); + actionData.set("credentials", JacksonUtil.valueToTree(deviceCredentials)); break; case ASSIGNED_TO_CUSTOMER: strEntityId = extractParameter(String.class, 0, additionalInfo); @@ -246,7 +244,7 @@ public class AuditLogServiceImpl implements AuditLogService { case RELATION_ADD_OR_UPDATE: case RELATION_DELETED: EntityRelation relation = extractParameter(EntityRelation.class, 0, additionalInfo); - actionData.set("relation", objectMapper.valueToTree(relation)); + actionData.set("relation", JacksonUtil.valueToTree(relation)); break; case LOGIN: case LOGOUT: diff --git a/dao/src/main/java/org/thingsboard/server/dao/util/mapping/JacksonUtil.java b/dao/src/main/java/org/thingsboard/server/dao/util/mapping/JacksonUtil.java index d654e85d5c..fd40d71189 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/util/mapping/JacksonUtil.java +++ b/dao/src/main/java/org/thingsboard/server/dao/util/mapping/JacksonUtil.java @@ -18,6 +18,7 @@ package org.thingsboard.server.dao.util.mapping; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; import java.io.IOException; @@ -66,7 +67,15 @@ public class JacksonUtil { } } + public static ObjectNode newObjectNode(){ + return OBJECT_MAPPER.createObjectNode(); + } + public static T clone(T value) { return fromString(toString(value), (Class) value.getClass()); } + + public static JsonNode valueToTree(T value) { + return OBJECT_MAPPER.valueToTree(value); + } }