Commit 2344d6d62f4fb96b5a9904630e047c40be1a6e21
Committed by
GitHub
1 parent
593f95a7
Fixed RPC string params in protobuf payload type. Reverted changes from the previous fix (#4356)
* Revert "added a fix for string value RPC params: removed redundant escaped characters" This reverts commit 1891af54 * removed redundant escape characters for string RPC params in Proto payload * cleanup code
Showing
4 changed files
with
30 additions
and
18 deletions
@@ -97,13 +97,9 @@ public class RpcController extends BaseController { | @@ -97,13 +97,9 @@ public class RpcController extends BaseController { | ||
97 | private DeferredResult<ResponseEntity> handleDeviceRPCRequest(boolean oneWay, DeviceId deviceId, String requestBody) throws ThingsboardException { | 97 | private DeferredResult<ResponseEntity> handleDeviceRPCRequest(boolean oneWay, DeviceId deviceId, String requestBody) throws ThingsboardException { |
98 | try { | 98 | try { |
99 | JsonNode rpcRequestBody = jsonMapper.readTree(requestBody); | 99 | JsonNode rpcRequestBody = jsonMapper.readTree(requestBody); |
100 | - String requestData; | ||
101 | - if (rpcRequestBody.get("params").isTextual()) { | ||
102 | - requestData = rpcRequestBody.get("params").asText(); | ||
103 | - } else { | ||
104 | - requestData = jsonMapper.writeValueAsString(rpcRequestBody.get("params")); | ||
105 | - } | ||
106 | - RpcRequest cmd = new RpcRequest(rpcRequestBody.get("method").asText(), requestData); | 100 | + RpcRequest cmd = new RpcRequest(rpcRequestBody.get("method").asText(), |
101 | + jsonMapper.writeValueAsString(rpcRequestBody.get("params"))); | ||
102 | + | ||
107 | if (rpcRequestBody.has("timeout")) { | 103 | if (rpcRequestBody.has("timeout")) { |
108 | cmd.setTimeout(rpcRequestBody.get("timeout").asLong()); | 104 | cmd.setTimeout(rpcRequestBody.get("timeout").asLong()); |
109 | } | 105 | } |
@@ -146,7 +146,7 @@ public class ProtoMqttAdaptor implements MqttTransportAdaptor { | @@ -146,7 +146,7 @@ public class ProtoMqttAdaptor implements MqttTransportAdaptor { | ||
146 | 146 | ||
147 | @Override | 147 | @Override |
148 | public Optional<MqttMessage> convertToPublish(MqttDeviceAwareSessionContext ctx, TransportProtos.ToDeviceRpcRequestMsg rpcRequest) { | 148 | public Optional<MqttMessage> convertToPublish(MqttDeviceAwareSessionContext ctx, TransportProtos.ToDeviceRpcRequestMsg rpcRequest) { |
149 | - return Optional.of(createMqttPublishMsg(ctx, MqttTopics.DEVICE_RPC_REQUESTS_TOPIC + rpcRequest.getRequestId(), rpcRequest.toByteArray())); | 149 | + return Optional.of(createMqttPublishMsg(ctx, MqttTopics.DEVICE_RPC_REQUESTS_TOPIC + rpcRequest.getRequestId(), ProtoConverter.convertToRpcRequest(rpcRequest))); |
150 | } | 150 | } |
151 | 151 | ||
152 | @Override | 152 | @Override |
@@ -22,7 +22,6 @@ import com.google.gson.JsonObject; | @@ -22,7 +22,6 @@ import com.google.gson.JsonObject; | ||
22 | import com.google.gson.JsonParser; | 22 | import com.google.gson.JsonParser; |
23 | import com.google.gson.JsonPrimitive; | 23 | import com.google.gson.JsonPrimitive; |
24 | import com.google.gson.JsonSyntaxException; | 24 | import com.google.gson.JsonSyntaxException; |
25 | -import com.google.gson.stream.MalformedJsonException; | ||
26 | import org.apache.commons.lang3.math.NumberUtils; | 25 | import org.apache.commons.lang3.math.NumberUtils; |
27 | import org.springframework.util.StringUtils; | 26 | import org.springframework.util.StringUtils; |
28 | import org.thingsboard.server.common.data.DataConstants; | 27 | import org.thingsboard.server.common.data.DataConstants; |
@@ -158,15 +157,7 @@ public class JsonConverter { | @@ -158,15 +157,7 @@ public class JsonConverter { | ||
158 | result.addProperty("id", msg.getRequestId()); | 157 | result.addProperty("id", msg.getRequestId()); |
159 | } | 158 | } |
160 | result.addProperty("method", msg.getMethodName()); | 159 | result.addProperty("method", msg.getMethodName()); |
161 | - try { | ||
162 | - result.add("params", JSON_PARSER.parse(msg.getParams())); | ||
163 | - } catch (JsonSyntaxException ex) { | ||
164 | - if (ex.getCause() instanceof MalformedJsonException) { | ||
165 | - result.addProperty("params", msg.getParams()); | ||
166 | - } else { | ||
167 | - throw ex; | ||
168 | - } | ||
169 | - } | 160 | + result.add("params", JSON_PARSER.parse(msg.getParams())); |
170 | return result; | 161 | return result; |
171 | } | 162 | } |
172 | 163 |
@@ -15,7 +15,9 @@ | @@ -15,7 +15,9 @@ | ||
15 | */ | 15 | */ |
16 | package org.thingsboard.server.common.transport.adaptor; | 16 | package org.thingsboard.server.common.transport.adaptor; |
17 | 17 | ||
18 | +import com.google.gson.JsonElement; | ||
18 | import com.google.gson.JsonParser; | 19 | import com.google.gson.JsonParser; |
20 | +import com.google.gson.JsonPrimitive; | ||
19 | import com.google.protobuf.InvalidProtocolBufferException; | 21 | import com.google.protobuf.InvalidProtocolBufferException; |
20 | import lombok.extern.slf4j.Slf4j; | 22 | import lombok.extern.slf4j.Slf4j; |
21 | import org.springframework.util.CollectionUtils; | 23 | import org.springframework.util.CollectionUtils; |
@@ -167,4 +169,27 @@ public class ProtoConverter { | @@ -167,4 +169,27 @@ public class ProtoConverter { | ||
167 | }); | 169 | }); |
168 | return kvList; | 170 | return kvList; |
169 | } | 171 | } |
172 | + | ||
173 | + public static byte[] convertToRpcRequest(TransportProtos.ToDeviceRpcRequestMsg toDeviceRpcRequestMsg) { | ||
174 | + TransportProtos.ToDeviceRpcRequestMsg.Builder toDeviceRpcRequestMsgBuilder = toDeviceRpcRequestMsg.newBuilderForType(); | ||
175 | + toDeviceRpcRequestMsgBuilder.mergeFrom(toDeviceRpcRequestMsg); | ||
176 | + toDeviceRpcRequestMsgBuilder.setParams(parseParams(toDeviceRpcRequestMsg)); | ||
177 | + TransportProtos.ToDeviceRpcRequestMsg result = toDeviceRpcRequestMsgBuilder.build(); | ||
178 | + return result.toByteArray(); | ||
179 | + } | ||
180 | + | ||
181 | + private static String parseParams(TransportProtos.ToDeviceRpcRequestMsg toDeviceRpcRequestMsg) { | ||
182 | + String params = toDeviceRpcRequestMsg.getParams(); | ||
183 | + JsonElement jsonElementParams = JSON_PARSER.parse(params); | ||
184 | + if (!jsonElementParams.isJsonPrimitive()) { | ||
185 | + return params; | ||
186 | + } else { | ||
187 | + JsonPrimitive primitiveParams = jsonElementParams.getAsJsonPrimitive(); | ||
188 | + if (jsonElementParams.getAsJsonPrimitive().isString()) { | ||
189 | + return primitiveParams.getAsString(); | ||
190 | + } else { | ||
191 | + return params; | ||
192 | + } | ||
193 | + } | ||
194 | + } | ||
170 | } | 195 | } |