Search in sources :

Example 1 with UncheckedApiException

use of org.thingsboard.server.service.telemetry.exception.UncheckedApiException in project thingsboard by thingsboard.

the class TelemetryController method extractRequestAttributes.

private List<AttributeKvEntry> extractRequestAttributes(JsonNode jsonNode) {
    long ts = System.currentTimeMillis();
    List<AttributeKvEntry> attributes = new ArrayList<>();
    jsonNode.fields().forEachRemaining(entry -> {
        String key = entry.getKey();
        JsonNode value = entry.getValue();
        if (entry.getValue().isObject() || entry.getValue().isArray()) {
            attributes.add(new BaseAttributeKvEntry(new JsonDataEntry(key, toJsonStr(value)), ts));
        } else if (entry.getValue().isTextual()) {
            if (maxStringValueLength > 0 && entry.getValue().textValue().length() > maxStringValueLength) {
                String message = String.format("String value length [%d] for key [%s] is greater than maximum allowed [%d]", entry.getValue().textValue().length(), key, maxStringValueLength);
                throw new UncheckedApiException(new InvalidParametersException(message));
            }
            attributes.add(new BaseAttributeKvEntry(new StringDataEntry(key, value.textValue()), ts));
        } else if (entry.getValue().isBoolean()) {
            attributes.add(new BaseAttributeKvEntry(new BooleanDataEntry(key, value.booleanValue()), ts));
        } else if (entry.getValue().isDouble()) {
            attributes.add(new BaseAttributeKvEntry(new DoubleDataEntry(key, value.doubleValue()), ts));
        } else if (entry.getValue().isNumber()) {
            if (entry.getValue().isBigInteger()) {
                throw new UncheckedApiException(new InvalidParametersException("Big integer values are not supported!"));
            } else {
                attributes.add(new BaseAttributeKvEntry(new LongDataEntry(key, value.longValue()), ts));
            }
        }
    });
    return attributes;
}
Also used : BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) InvalidParametersException(org.thingsboard.server.service.telemetry.exception.InvalidParametersException) UncheckedApiException(org.thingsboard.server.service.telemetry.exception.UncheckedApiException) BooleanDataEntry(org.thingsboard.server.common.data.kv.BooleanDataEntry) DoubleDataEntry(org.thingsboard.server.common.data.kv.DoubleDataEntry) StringDataEntry(org.thingsboard.server.common.data.kv.StringDataEntry) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) LongDataEntry(org.thingsboard.server.common.data.kv.LongDataEntry) JsonDataEntry(org.thingsboard.server.common.data.kv.JsonDataEntry)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ArrayList (java.util.ArrayList)1 AttributeKvEntry (org.thingsboard.server.common.data.kv.AttributeKvEntry)1 BaseAttributeKvEntry (org.thingsboard.server.common.data.kv.BaseAttributeKvEntry)1 BooleanDataEntry (org.thingsboard.server.common.data.kv.BooleanDataEntry)1 DoubleDataEntry (org.thingsboard.server.common.data.kv.DoubleDataEntry)1 JsonDataEntry (org.thingsboard.server.common.data.kv.JsonDataEntry)1 LongDataEntry (org.thingsboard.server.common.data.kv.LongDataEntry)1 StringDataEntry (org.thingsboard.server.common.data.kv.StringDataEntry)1 InvalidParametersException (org.thingsboard.server.service.telemetry.exception.InvalidParametersException)1 UncheckedApiException (org.thingsboard.server.service.telemetry.exception.UncheckedApiException)1