Improve validation of request body for Telemetry Rest Calls.
This commit is contained in:
parent
582ef1f63c
commit
d644d2d5fd
@ -217,22 +217,23 @@ public class TelemetryRestMsgHandler extends DefaultRestMsgHandler {
|
|||||||
JsonNode jsonNode = jsonMapper.readTree(request.getRequestBody());
|
JsonNode jsonNode = jsonMapper.readTree(request.getRequestBody());
|
||||||
if (jsonNode.isObject()) {
|
if (jsonNode.isObject()) {
|
||||||
List<AttributeKvEntry> attributes = extractRequestAttributes(jsonNode);
|
List<AttributeKvEntry> attributes = extractRequestAttributes(jsonNode);
|
||||||
if (!attributes.isEmpty()) {
|
if (attributes.isEmpty()) {
|
||||||
ctx.saveAttributes(ctx.getSecurityCtx().orElseThrow(IllegalArgumentException::new).getTenantId(), entityId, scope, attributes, new PluginCallback<Void>() {
|
throw new IllegalArgumentException("No attributes data found in request body!");
|
||||||
@Override
|
|
||||||
public void onSuccess(PluginContext ctx, Void value) {
|
|
||||||
msg.getResponseHolder().setResult(new ResponseEntity<>(HttpStatus.OK));
|
|
||||||
subscriptionManager.onAttributesUpdateFromServer(ctx, entityId, scope, attributes);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onFailure(PluginContext ctx, Exception e) {
|
|
||||||
log.error("Failed to save attributes", e);
|
|
||||||
handleError(e, msg, HttpStatus.BAD_REQUEST);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
ctx.saveAttributes(ctx.getSecurityCtx().orElseThrow(IllegalArgumentException::new).getTenantId(), entityId, scope, attributes, new PluginCallback<Void>() {
|
||||||
|
@Override
|
||||||
|
public void onSuccess(PluginContext ctx, Void value) {
|
||||||
|
msg.getResponseHolder().setResult(new ResponseEntity<>(HttpStatus.OK));
|
||||||
|
subscriptionManager.onAttributesUpdateFromServer(ctx, entityId, scope, attributes);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onFailure(PluginContext ctx, Exception e) {
|
||||||
|
log.error("Failed to save attributes", e);
|
||||||
|
handleError(e, msg, HttpStatus.BAD_REQUEST);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@ -269,6 +270,9 @@ public class TelemetryRestMsgHandler extends DefaultRestMsgHandler {
|
|||||||
throw new UncheckedApiException(new InvalidParametersException(e.getMessage()));
|
throw new UncheckedApiException(new InvalidParametersException(e.getMessage()));
|
||||||
}
|
}
|
||||||
List<TsKvEntry> entries = new ArrayList<>();
|
List<TsKvEntry> entries = new ArrayList<>();
|
||||||
|
if (entries.isEmpty()) {
|
||||||
|
throw new IllegalArgumentException("No timeseries data found in request body!");
|
||||||
|
}
|
||||||
for (Map.Entry<Long, List<KvEntry>> entry : telemetryRequest.getData().entrySet()) {
|
for (Map.Entry<Long, List<KvEntry>> entry : telemetryRequest.getData().entrySet()) {
|
||||||
for (KvEntry kv : entry.getValue()) {
|
for (KvEntry kv : entry.getValue()) {
|
||||||
entries.add(new BasicTsKvEntry(entry.getKey(), kv));
|
entries.add(new BasicTsKvEntry(entry.getKey(), kv));
|
||||||
@ -398,7 +402,9 @@ public class TelemetryRestMsgHandler extends DefaultRestMsgHandler {
|
|||||||
private void handleError(Exception e, PluginRestMsg msg, HttpStatus defaultErrorStatus) {
|
private void handleError(Exception e, PluginRestMsg msg, HttpStatus defaultErrorStatus) {
|
||||||
ResponseEntity responseEntity;
|
ResponseEntity responseEntity;
|
||||||
if (e != null && e instanceof ToErrorResponseEntity) {
|
if (e != null && e instanceof ToErrorResponseEntity) {
|
||||||
responseEntity = ((ToErrorResponseEntity)e).toErrorResponseEntity();
|
responseEntity = ((ToErrorResponseEntity) e).toErrorResponseEntity();
|
||||||
|
} else if (e != null && e instanceof IllegalArgumentException) {
|
||||||
|
responseEntity = new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
|
||||||
} else {
|
} else {
|
||||||
responseEntity = new ResponseEntity<>(defaultErrorStatus);
|
responseEntity = new ResponseEntity<>(defaultErrorStatus);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user