added new methods to JacksonUtil & replaced objectMapper from AuditLogServiceImpl

This commit is contained in:
ShvaykaD 2021-01-14 10:21:51 +02:00
parent 2ea3b18738
commit 143e9cb877
2 changed files with 16 additions and 9 deletions

View File

@ -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 <E extends HasName, I extends EntityId> 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<AttributeKvEntry> 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:

View File

@ -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> T clone(T value) {
return fromString(toString(value), (Class<T>) value.getClass());
}
public static <T> JsonNode valueToTree(T value) {
return OBJECT_MAPPER.valueToTree(value);
}
}