Browse Source

Improve validation of request body for Telemetry Rest Calls.

pull/338/head
Igor Kulikov 9 years ago
parent
commit
be27f22e27
  1. 18
      extensions-core/src/main/java/org/thingsboard/server/extensions/core/plugin/telemetry/handlers/TelemetryRestMsgHandler.java

18
extensions-core/src/main/java/org/thingsboard/server/extensions/core/plugin/telemetry/handlers/TelemetryRestMsgHandler.java

@ -16,6 +16,7 @@
package org.thingsboard.server.extensions.core.plugin.telemetry.handlers; package org.thingsboard.server.extensions.core.plugin.telemetry.handlers;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException; import com.google.gson.JsonSyntaxException;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -214,7 +215,12 @@ public class TelemetryRestMsgHandler extends DefaultRestMsgHandler {
EntityId entityId, String scope) throws ServletException, IOException { EntityId entityId, String scope) throws ServletException, IOException {
if (DataConstants.SERVER_SCOPE.equals(scope) || if (DataConstants.SERVER_SCOPE.equals(scope) ||
DataConstants.SHARED_SCOPE.equals(scope)) { DataConstants.SHARED_SCOPE.equals(scope)) {
JsonNode jsonNode = jsonMapper.readTree(request.getRequestBody()); JsonNode jsonNode;
try {
jsonNode = jsonMapper.readTree(request.getRequestBody());
} catch (Exception e) {
throw new IllegalArgumentException("Unable to parse attributes payload: Invalid JSON body!");
}
if (jsonNode.isObject()) { if (jsonNode.isObject()) {
List<AttributeKvEntry> attributes = extractRequestAttributes(jsonNode); List<AttributeKvEntry> attributes = extractRequestAttributes(jsonNode);
if (attributes.isEmpty()) { if (attributes.isEmpty()) {
@ -264,10 +270,16 @@ public class TelemetryRestMsgHandler extends DefaultRestMsgHandler {
private void handleHttpPostTimeseries(PluginContext ctx, PluginRestMsg msg, RestRequest request, EntityId entityId, long ttl) { private void handleHttpPostTimeseries(PluginContext ctx, PluginRestMsg msg, RestRequest request, EntityId entityId, long ttl) {
TelemetryUploadRequest telemetryRequest; TelemetryUploadRequest telemetryRequest;
JsonElement telemetryJson;
try {
telemetryJson = new JsonParser().parse(request.getRequestBody());
} catch (Exception e) {
throw new IllegalArgumentException("Unable to parse timeseries payload: Invalid JSON body!");
}
try { try {
telemetryRequest = JsonConverter.convertToTelemetry(new JsonParser().parse(request.getRequestBody())); telemetryRequest = JsonConverter.convertToTelemetry(telemetryJson);
} catch (JsonSyntaxException e) { } catch (JsonSyntaxException e) {
throw new UncheckedApiException(new InvalidParametersException(e.getMessage())); throw new IllegalArgumentException(e.getMessage());
} }
List<TsKvEntry> entries = new ArrayList<>(); List<TsKvEntry> entries = new ArrayList<>();
if (entries.isEmpty()) { if (entries.isEmpty()) {

Loading…
Cancel
Save