Commit 10c32f63da47fa52aa7a6cbee2af025313301fc4

Authored by Andrew Shvayka
Committed by GitHub
2 parents 8f2217d9 d4236dc2

Merge pull request #68 from thingsboard/feature/TB-44

TB-44: Major bug in RPC functionality
@@ -25,6 +25,8 @@ import org.springframework.util.StringUtils; @@ -25,6 +25,8 @@ import org.springframework.util.StringUtils;
25 import org.springframework.web.context.request.async.DeferredResult; 25 import org.springframework.web.context.request.async.DeferredResult;
26 import org.thingsboard.server.common.data.DataConstants; 26 import org.thingsboard.server.common.data.DataConstants;
27 import org.thingsboard.server.common.data.id.DeviceId; 27 import org.thingsboard.server.common.data.id.DeviceId;
  28 +import org.thingsboard.server.common.data.id.TenantId;
  29 +import org.thingsboard.server.extensions.api.plugins.PluginApiCallSecurityContext;
28 import org.thingsboard.server.extensions.api.plugins.PluginCallback; 30 import org.thingsboard.server.extensions.api.plugins.PluginCallback;
29 import org.thingsboard.server.extensions.api.plugins.PluginContext; 31 import org.thingsboard.server.extensions.api.plugins.PluginContext;
30 import org.thingsboard.server.extensions.api.plugins.handlers.DefaultRestMsgHandler; 32 import org.thingsboard.server.extensions.api.plugins.handlers.DefaultRestMsgHandler;
@@ -40,6 +42,7 @@ import org.thingsboard.server.extensions.core.plugin.rpc.cmd.RpcRequest; @@ -40,6 +42,7 @@ import org.thingsboard.server.extensions.core.plugin.rpc.cmd.RpcRequest;
40 42
41 import javax.servlet.ServletException; 43 import javax.servlet.ServletException;
42 import java.io.IOException; 44 import java.io.IOException;
  45 +import java.util.Optional;
43 import java.util.UUID; 46 import java.util.UUID;
44 47
45 /** 48 /**
@@ -67,17 +70,19 @@ public class RpcRestMsgHandler extends DefaultRestMsgHandler { @@ -67,17 +70,19 @@ public class RpcRestMsgHandler extends DefaultRestMsgHandler {
67 70
68 RpcRequest cmd = new RpcRequest(rpcRequestBody.get("method").asText(), 71 RpcRequest cmd = new RpcRequest(rpcRequestBody.get("method").asText(),
69 jsonMapper.writeValueAsString(rpcRequestBody.get("params"))); 72 jsonMapper.writeValueAsString(rpcRequestBody.get("params")));
  73 + if (rpcRequestBody.has("timeout")) {
  74 + cmd.setTimeout(rpcRequestBody.get("timeout").asLong());
  75 + }
  76 +
  77 + final TenantId tenantId = ctx.getSecurityCtx().orElseThrow(() -> new IllegalStateException("Security context is empty!")).getTenantId();
70 78
71 ctx.checkAccess(deviceId, new PluginCallback<Void>() { 79 ctx.checkAccess(deviceId, new PluginCallback<Void>() {
72 @Override 80 @Override
73 public void onSuccess(PluginContext ctx, Void value) { 81 public void onSuccess(PluginContext ctx, Void value) {
74 - if (rpcRequestBody.has("timeout")) {  
75 - cmd.setTimeout(rpcRequestBody.get("timeout").asLong());  
76 - }  
77 long timeout = cmd.getTimeout() != null ? cmd.getTimeout() : defaultTimeout; 82 long timeout = cmd.getTimeout() != null ? cmd.getTimeout() : defaultTimeout;
78 ToDeviceRpcRequestBody body = new ToDeviceRpcRequestBody(cmd.getMethodName(), cmd.getRequestData()); 83 ToDeviceRpcRequestBody body = new ToDeviceRpcRequestBody(cmd.getMethodName(), cmd.getRequestData());
79 ToDeviceRpcRequest rpcRequest = new ToDeviceRpcRequest(UUID.randomUUID(), 84 ToDeviceRpcRequest rpcRequest = new ToDeviceRpcRequest(UUID.randomUUID(),
80 - ctx.getSecurityCtx().orElseThrow(() -> new IllegalStateException("Security context is empty!")).getTenantId(), 85 + tenantId,
81 deviceId, 86 deviceId,
82 DataConstants.ONEWAY.equals(method), 87 DataConstants.ONEWAY.equals(method),
83 System.currentTimeMillis() + timeout, 88 System.currentTimeMillis() + timeout,