Skip to content

Commit

Permalink
Merge pull request #8 from ShvaykaD/feature/node_originator_attribute…
Browse files Browse the repository at this point in the history
…s_fetch_to_msg_data

added ALLOW_UNQUOTED_FIELD_NAMES_MAPPER to JacksonUtil
  • Loading branch information
YuriyLytvynchuk authored Nov 2, 2022
2 parents 1d77a1c + 41a5f8d commit 2c3b9b9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
*/
package org.thingsboard.common.util;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.json.JsonWriteFeature;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.MapperFeature;
Expand Down Expand Up @@ -46,6 +48,10 @@ public class JacksonUtil {
.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true)
.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true)
.build();
public static ObjectMapper ALLOW_UNQUOTED_FIELD_NAMES_MAPPER = JsonMapper.builder()
.configure(JsonWriteFeature.QUOTE_FIELD_NAMES.mappedFeature(), false)
.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true)
.build();

public static <T> T convertValue(Object fromValue, Class<T> toValueType) {
try {
Expand Down Expand Up @@ -119,11 +125,15 @@ public static String toPrettyString(Object o) {
}

public static JsonNode toJsonNode(String value) {
return toJsonNode(value, OBJECT_MAPPER);
}

public static JsonNode toJsonNode(String value, ObjectMapper mapper) {
if (value == null || value.isEmpty()) {
return null;
}
try {
return OBJECT_MAPPER.readTree(value);
return mapper.readTree(value);
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
Expand All @@ -138,7 +148,11 @@ public static <T> T treeToValue(JsonNode node, Class<T> clazz) {
}

public static ObjectNode newObjectNode() {
return OBJECT_MAPPER.createObjectNode();
return newObjectNode(OBJECT_MAPPER);
}

public static ObjectNode newObjectNode(ObjectMapper mapper) {
return mapper.createObjectNode();
}

public static <T> T clone(T value) {
Expand Down Expand Up @@ -216,6 +230,10 @@ public static void replaceUuidsRecursively(JsonNode node, Set<String> skipFields
}

public static void addKvEntry(ObjectNode entityNode, KvEntry kvEntry, String key) {
addKvEntry(entityNode, kvEntry, key, OBJECT_MAPPER);
}

public static void addKvEntry(ObjectNode entityNode, KvEntry kvEntry, String key, ObjectMapper mapper) {
if (kvEntry.getDataType() == DataType.BOOLEAN) {
kvEntry.getBooleanValue().ifPresent(value -> entityNode.put(key, value));
} else if (kvEntry.getDataType() == DataType.DOUBLE) {
Expand All @@ -224,7 +242,7 @@ public static void addKvEntry(ObjectNode entityNode, KvEntry kvEntry, String key
kvEntry.getLongValue().ifPresent(value -> entityNode.put(key, value));
} else if (kvEntry.getDataType() == DataType.JSON) {
if (kvEntry.getJsonValue().isPresent()) {
entityNode.set(key, toJsonNode(kvEntry.getJsonValue().get()));
entityNode.set(key, toJsonNode(kvEntry.getJsonValue().get(), mapper));
}
} else {
entityNode.put(key, kvEntry.getValueAsString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,11 @@
*/
package org.thingsboard.rule.engine.metadata;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.json.JsonWriteFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.gson.JsonParseException;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.thingsboard.common.util.JacksonUtil;
Expand All @@ -35,14 +31,12 @@
import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.kv.AttributeKvEntry;
import org.thingsboard.server.common.data.kv.BasicTsKvEntry;
import org.thingsboard.server.common.data.kv.DataType;
import org.thingsboard.server.common.data.kv.JsonDataEntry;
import org.thingsboard.server.common.data.kv.KvEntry;
import org.thingsboard.server.common.data.kv.TsKvEntry;
import org.thingsboard.server.common.msg.TbMsg;
import org.thingsboard.server.common.msg.TbMsgMetaData;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -60,8 +54,6 @@

public abstract class TbAbstractGetAttributesNode<C extends TbGetAttributesNodeConfiguration, T extends EntityId> implements TbNode {

private static ObjectMapper mapper = new ObjectMapper();

private static final String VALUE = "value";
private static final String TS = "ts";

Expand All @@ -73,8 +65,6 @@ public abstract class TbAbstractGetAttributesNode<C extends TbGetAttributesNodeC
@Override
public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException {
this.config = loadGetAttributesNodeConfig(configuration);
mapper.configure(JsonWriteFeature.QUOTE_FIELD_NAMES.mappedFeature(), false);
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
this.fetchToData = config.isFetchToData();
this.getLatestValueWithTs = config.isGetLatestValueWithTs();
this.isTellFailureIfAbsent = BooleanUtils.toBooleanDefaultIfNull(this.config.isTellFailureIfAbsent(), true);
Expand All @@ -101,7 +91,7 @@ private void safePutAttributes(TbContext ctx, TbMsg msg, T entityId) {
ctx.tellNext(msg, FAILURE);
return;
}
JsonNode msgDataNode = toJsonNode(msg.getData());
JsonNode msgDataNode = JacksonUtil.toJsonNode(msg.getData(), JacksonUtil.ALLOW_UNQUOTED_FIELD_NAMES_MAPPER);
if (fetchToData) {
if (!msgDataNode.isObject()) {
ctx.tellFailure(msg, new IllegalArgumentException("Msg body is not an object!"));
Expand All @@ -125,7 +115,7 @@ private void safePutAttributes(TbContext ctx, TbMsg msg, T entityId) {
String prefix = getPrefix(keyScope);
kvEntryList.forEach(kvEntry -> {
if (fetchToData) {
addKvEntryToJson((ObjectNode) msgDataNode, kvEntry, prefix + kvEntry.getKey());
JacksonUtil.addKvEntry((ObjectNode) msgDataNode, kvEntry, prefix + kvEntry.getKey(), JacksonUtil.ALLOW_UNQUOTED_FIELD_NAMES_MAPPER);
} else {
msgMetaData.putValue(prefix + kvEntry.getKey(), kvEntry.getValueAsString());
}
Expand Down Expand Up @@ -180,9 +170,9 @@ private ListenableFuture<Map<String, List<TsKvEntry>>> getLatestTelemetry(TbCont
}

private TsKvEntry getValueWithTs(TsKvEntry tsKvEntry) {
ObjectNode value = mapper.createObjectNode();
ObjectNode value = JacksonUtil.newObjectNode(JacksonUtil.ALLOW_UNQUOTED_FIELD_NAMES_MAPPER);
value.put(TS, tsKvEntry.getTs());
addKvEntryToJson(value, tsKvEntry, VALUE);
JacksonUtil.addKvEntry(value, tsKvEntry, VALUE, JacksonUtil.ALLOW_UNQUOTED_FIELD_NAMES_MAPPER);
return new BasicTsKvEntry(tsKvEntry.getTs(), new JsonDataEntry(tsKvEntry.getKey(), value.toString()));
}

Expand All @@ -202,30 +192,6 @@ private String getPrefix(String scope) {
return prefix;
}

private static void addKvEntryToJson(ObjectNode entityNode, KvEntry kvEntry, String key) {
if (kvEntry.getDataType() == DataType.BOOLEAN) {
kvEntry.getBooleanValue().ifPresent(value -> entityNode.put(key, value));
} else if (kvEntry.getDataType() == DataType.DOUBLE) {
kvEntry.getDoubleValue().ifPresent(value -> entityNode.put(key, value));
} else if (kvEntry.getDataType() == DataType.LONG) {
kvEntry.getLongValue().ifPresent(value -> entityNode.put(key, value));
} else if (kvEntry.getDataType() == DataType.JSON) {
if (kvEntry.getJsonValue().isPresent()) {
entityNode.set(key, toJsonNode(kvEntry.getJsonValue().get()));
}
} else {
entityNode.put(key, kvEntry.getValueAsString());
}
}

private static JsonNode toJsonNode(String value) {
try {
return mapper.readTree(value);
} catch (IOException e) {
throw new JsonParseException("Can't parse jsonValue: " + value, e);
}
}

private List<String> getNotExistingKeys(List<AttributeKvEntry> existingAttributesKvEntry, List<String> allKeys) {
List<String> existingKeys = existingAttributesKvEntry.stream().map(KvEntry::getKey).collect(Collectors.toList());
return allKeys.stream().filter(key -> !existingKeys.contains(key)).collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,29 @@
*/
package org.thingsboard.rule.engine.metadata;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.json.JsonWriteFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.gson.JsonParseException;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.thingsboard.server.common.data.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.thingsboard.common.util.DonAsynchron;
import org.thingsboard.common.util.JacksonUtil;
import org.thingsboard.rule.engine.api.RuleNode;
import org.thingsboard.rule.engine.api.TbContext;
import org.thingsboard.rule.engine.api.TbNode;
import org.thingsboard.rule.engine.api.TbNodeConfiguration;
import org.thingsboard.rule.engine.api.TbNodeException;
import org.thingsboard.rule.engine.api.util.TbNodeUtils;
import org.thingsboard.server.common.data.StringUtils;
import org.thingsboard.server.common.data.kv.Aggregation;
import org.thingsboard.server.common.data.kv.BaseReadTsKvQuery;
import org.thingsboard.server.common.data.kv.ReadTsKvQuery;
import org.thingsboard.server.common.data.kv.TsKvEntry;
import org.thingsboard.server.common.data.plugin.ComponentType;
import org.thingsboard.server.common.msg.TbMsg;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -75,7 +71,6 @@ public class TbGetTelemetryNode implements TbNode {
private TbGetTelemetryNodeConfiguration config;
private List<String> tsKeyNames;
private int limit;
private ObjectMapper mapper;
private String fetchMode;
private String orderByFetchAll;
private Aggregation aggregation;
Expand All @@ -91,10 +86,6 @@ public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNode
orderByFetchAll = ASC_ORDER;
}
aggregation = parseAggregationConfig(config.getAggregation());

mapper = new ObjectMapper();
mapper.configure(JsonWriteFeature.QUOTE_FIELD_NAMES.mappedFeature(), false);
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
}

Aggregation parseAggregationConfig(String aggName) {
Expand Down Expand Up @@ -146,7 +137,7 @@ private String getOrderBy() {
}

private void process(List<TsKvEntry> entries, TbMsg msg, List<String> keys) {
ObjectNode resultNode = mapper.createObjectNode();
ObjectNode resultNode = JacksonUtil.newObjectNode(JacksonUtil.ALLOW_UNQUOTED_FIELD_NAMES_MAPPER);
if (FETCH_MODE_ALL.equals(fetchMode)) {
entries.forEach(entry -> processArray(resultNode, entry));
} else {
Expand All @@ -169,36 +160,16 @@ private void processArray(ObjectNode node, TsKvEntry entry) {
ArrayNode arrayNode = (ArrayNode) node.get(entry.getKey());
arrayNode.add(buildNode(entry));
} else {
ArrayNode arrayNode = mapper.createArrayNode();
ArrayNode arrayNode = JacksonUtil.ALLOW_UNQUOTED_FIELD_NAMES_MAPPER.createArrayNode();
arrayNode.add(buildNode(entry));
node.set(entry.getKey(), arrayNode);
}
}

private ObjectNode buildNode(TsKvEntry entry) {
ObjectNode obj = mapper.createObjectNode()
.put("ts", entry.getTs());
switch (entry.getDataType()) {
case STRING:
obj.put("value", entry.getValueAsString());
break;
case LONG:
obj.put("value", entry.getLongValue().get());
break;
case BOOLEAN:
obj.put("value", entry.getBooleanValue().get());
break;
case DOUBLE:
obj.put("value", entry.getDoubleValue().get());
break;
case JSON:
try {
obj.set("value", mapper.readTree(entry.getJsonValue().get()));
} catch (IOException e) {
throw new JsonParseException("Can't parse jsonValue: " + entry.getJsonValue().get(), e);
}
break;
}
ObjectNode obj = JacksonUtil.newObjectNode(JacksonUtil.ALLOW_UNQUOTED_FIELD_NAMES_MAPPER);
obj.put("ts", entry.getTs());
JacksonUtil.addKvEntry(obj, entry, "value", JacksonUtil.ALLOW_UNQUOTED_FIELD_NAMES_MAPPER);
return obj;
}

Expand Down

0 comments on commit 2c3b9b9

Please sign in to comment.