use of org.thingsboard.server.extensions.core.plugin.rpc.cmd.RpcRequest in project thingsboard by thingsboard.
the class RpcRestMsgHandler method handleHttpPostRequest.
@Override
public void handleHttpPostRequest(PluginContext ctx, PluginRestMsg msg) throws ServletException {
boolean valid = false;
RestRequest request = msg.getRequest();
try {
String[] pathParams = request.getPathParams();
if (pathParams.length == 2) {
String method = pathParams[0].toUpperCase();
if (DataConstants.ONEWAY.equals(method) || DataConstants.TWOWAY.equals(method)) {
final TenantId tenantId = ctx.getSecurityCtx().orElseThrow(() -> new IllegalStateException("Security context is empty!")).getTenantId();
JsonNode rpcRequestBody = jsonMapper.readTree(request.getRequestBody());
RpcRequest cmd = new RpcRequest(rpcRequestBody.get("method").asText(), jsonMapper.writeValueAsString(rpcRequestBody.get("params")));
if (rpcRequestBody.has("timeout")) {
cmd.setTimeout(rpcRequestBody.get("timeout").asLong());
}
boolean oneWay = DataConstants.ONEWAY.equals(method);
DeviceId deviceId = DeviceId.fromString(pathParams[1]);
valid = handleDeviceRPCRequest(ctx, msg, tenantId, deviceId, cmd, oneWay);
}
}
} catch (IOException e) {
log.debug("Failed to process POST request due to IO exception", e);
} catch (RuntimeException e) {
log.debug("Failed to process POST request due to Runtime exception", e);
}
if (!valid) {
msg.getResponseHolder().setResult(new ResponseEntity<>(HttpStatus.BAD_REQUEST));
}
}
Aggregations