Commit 436d37ff42a1d01d5ed5a5db68d83889de777623
1 parent
416c3fd1
Refactoring and backward-compatiblity improvements
Showing
3 changed files
with
67 additions
and
29 deletions
@@ -69,6 +69,7 @@ import org.thingsboard.server.service.rpc.FromDeviceRpcResponse; | @@ -69,6 +69,7 @@ import org.thingsboard.server.service.rpc.FromDeviceRpcResponse; | ||
69 | import org.thingsboard.server.service.rpc.ToDeviceRpcRequestActorMsg; | 69 | import org.thingsboard.server.service.rpc.ToDeviceRpcRequestActorMsg; |
70 | import org.thingsboard.server.service.rpc.ToServerRpcResponseActorMsg; | 70 | import org.thingsboard.server.service.rpc.ToServerRpcResponseActorMsg; |
71 | import org.thingsboard.server.service.transport.msg.TransportToDeviceActorMsgWrapper; | 71 | import org.thingsboard.server.service.transport.msg.TransportToDeviceActorMsgWrapper; |
72 | +import org.thingsboard.server.utils.JsonUtils; | ||
72 | 73 | ||
73 | import javax.annotation.Nullable; | 74 | import javax.annotation.Nullable; |
74 | import java.util.ArrayList; | 75 | import java.util.ArrayList; |
@@ -102,7 +103,6 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor { | @@ -102,7 +103,6 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor { | ||
102 | private final Map<Integer, ToServerRpcRequestMetadata> toServerRpcPendingMap; | 103 | private final Map<Integer, ToServerRpcRequestMetadata> toServerRpcPendingMap; |
103 | 104 | ||
104 | private final Gson gson = new Gson(); | 105 | private final Gson gson = new Gson(); |
105 | - private final JsonParser jsonParser = new JsonParser(); | ||
106 | 106 | ||
107 | private int rpcSeq = 0; | 107 | private int rpcSeq = 0; |
108 | private String deviceName; | 108 | private String deviceName; |
@@ -327,7 +327,7 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor { | @@ -327,7 +327,7 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor { | ||
327 | } | 327 | } |
328 | 328 | ||
329 | private void handlePostAttributesRequest(ActorContext context, SessionInfoProto sessionInfo, PostAttributeMsg postAttributes) { | 329 | private void handlePostAttributesRequest(ActorContext context, SessionInfoProto sessionInfo, PostAttributeMsg postAttributes) { |
330 | - JsonObject json = getJsonObject(postAttributes.getKvList()); | 330 | + JsonObject json = JsonUtils.getJsonObject(postAttributes.getKvList()); |
331 | TbMsg tbMsg = new TbMsg(UUIDs.timeBased(), SessionMsgType.POST_ATTRIBUTES_REQUEST.name(), deviceId, defaultMetaData.copy(), | 331 | TbMsg tbMsg = new TbMsg(UUIDs.timeBased(), SessionMsgType.POST_ATTRIBUTES_REQUEST.name(), deviceId, defaultMetaData.copy(), |
332 | TbMsgDataType.JSON, gson.toJson(json), null, null, 0L); | 332 | TbMsgDataType.JSON, gson.toJson(json), null, null, 0L); |
333 | pushToRuleEngine(context, tbMsg); | 333 | pushToRuleEngine(context, tbMsg); |
@@ -335,7 +335,7 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor { | @@ -335,7 +335,7 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor { | ||
335 | 335 | ||
336 | private void handlePostTelemetryRequest(ActorContext context, SessionInfoProto sessionInfo, PostTelemetryMsg postTelemetry) { | 336 | private void handlePostTelemetryRequest(ActorContext context, SessionInfoProto sessionInfo, PostTelemetryMsg postTelemetry) { |
337 | for (TsKvListProto tsKv : postTelemetry.getTsKvListList()) { | 337 | for (TsKvListProto tsKv : postTelemetry.getTsKvListList()) { |
338 | - JsonObject json = getJsonObject(tsKv.getKvList()); | 338 | + JsonObject json = JsonUtils.getJsonObject(tsKv.getKvList()); |
339 | TbMsgMetaData metaData = defaultMetaData.copy(); | 339 | TbMsgMetaData metaData = defaultMetaData.copy(); |
340 | metaData.putValue("ts", tsKv.getTs() + ""); | 340 | metaData.putValue("ts", tsKv.getTs() + ""); |
341 | TbMsg tbMsg = new TbMsg(UUIDs.timeBased(), SessionMsgType.POST_TELEMETRY_REQUEST.name(), deviceId, metaData, TbMsgDataType.JSON, gson.toJson(json), null, null, 0L); | 341 | TbMsg tbMsg = new TbMsg(UUIDs.timeBased(), SessionMsgType.POST_TELEMETRY_REQUEST.name(), deviceId, metaData, TbMsgDataType.JSON, gson.toJson(json), null, null, 0L); |
@@ -347,7 +347,7 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor { | @@ -347,7 +347,7 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor { | ||
347 | UUID sessionId = getSessionId(sessionInfo); | 347 | UUID sessionId = getSessionId(sessionInfo); |
348 | JsonObject json = new JsonObject(); | 348 | JsonObject json = new JsonObject(); |
349 | json.addProperty("method", request.getMethodName()); | 349 | json.addProperty("method", request.getMethodName()); |
350 | - json.add("params", jsonParser.parse(request.getParams())); | 350 | + json.add("params", JsonUtils.parse(request.getParams())); |
351 | 351 | ||
352 | TbMsgMetaData requestMetaData = defaultMetaData.copy(); | 352 | TbMsgMetaData requestMetaData = defaultMetaData.copy(); |
353 | requestMetaData.putValue("requestId", Integer.toString(request.getRequestId())); | 353 | requestMetaData.putValue("requestId", Integer.toString(request.getRequestId())); |
@@ -551,30 +551,6 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor { | @@ -551,30 +551,6 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor { | ||
551 | this.defaultMetaData.putValue("deviceType", deviceType); | 551 | this.defaultMetaData.putValue("deviceType", deviceType); |
552 | } | 552 | } |
553 | 553 | ||
554 | - private JsonObject getJsonObject(List<KeyValueProto> tsKv) { | ||
555 | - JsonObject json = new JsonObject(); | ||
556 | - for (KeyValueProto kv : tsKv) { | ||
557 | - switch (kv.getType()) { | ||
558 | - case BOOLEAN_V: | ||
559 | - json.addProperty(kv.getKey(), kv.getBoolV()); | ||
560 | - break; | ||
561 | - case LONG_V: | ||
562 | - json.addProperty(kv.getKey(), kv.getLongV()); | ||
563 | - break; | ||
564 | - case DOUBLE_V: | ||
565 | - json.addProperty(kv.getKey(), kv.getDoubleV()); | ||
566 | - break; | ||
567 | - case STRING_V: | ||
568 | - json.addProperty(kv.getKey(), kv.getStringV()); | ||
569 | - break; | ||
570 | - case JSON_V: | ||
571 | - json.add(kv.getKey(), jsonParser.parse(kv.getJsonV())); | ||
572 | - break; | ||
573 | - } | ||
574 | - } | ||
575 | - return json; | ||
576 | - } | ||
577 | - | ||
578 | private void sendToTransport(GetAttributeResponseMsg responseMsg, SessionInfoProto sessionInfo) { | 554 | private void sendToTransport(GetAttributeResponseMsg responseMsg, SessionInfoProto sessionInfo) { |
579 | DeviceActorToTransportMsg msg = DeviceActorToTransportMsg.newBuilder() | 555 | DeviceActorToTransportMsg msg = DeviceActorToTransportMsg.newBuilder() |
580 | .setSessionIdMSB(sessionInfo.getSessionIdMSB()) | 556 | .setSessionIdMSB(sessionInfo.getSessionIdMSB()) |
@@ -103,7 +103,14 @@ public class DefaultMailService implements MailService { | @@ -103,7 +103,14 @@ public class DefaultMailService implements MailService { | ||
103 | javaMailProperties.put(MAIL_PROP + protocol + ".port", jsonConfig.get("smtpPort").asText()); | 103 | javaMailProperties.put(MAIL_PROP + protocol + ".port", jsonConfig.get("smtpPort").asText()); |
104 | javaMailProperties.put(MAIL_PROP + protocol + ".timeout", jsonConfig.get("timeout").asText()); | 104 | javaMailProperties.put(MAIL_PROP + protocol + ".timeout", jsonConfig.get("timeout").asText()); |
105 | javaMailProperties.put(MAIL_PROP + protocol + ".auth", String.valueOf(StringUtils.isNotEmpty(jsonConfig.get("username").asText()))); | 105 | javaMailProperties.put(MAIL_PROP + protocol + ".auth", String.valueOf(StringUtils.isNotEmpty(jsonConfig.get("username").asText()))); |
106 | - boolean enableTls = jsonConfig.has("enableTls") && jsonConfig.get("enableTls").booleanValue(); | 106 | + boolean enableTls = false; |
107 | + if (jsonConfig.has("enableTls")) { | ||
108 | + if (jsonConfig.get("enableTls").isBoolean() && jsonConfig.get("enableTls").booleanValue()) { | ||
109 | + enableTls = true; | ||
110 | + } else if (jsonConfig.get("enableTls").isTextual()) { | ||
111 | + enableTls = "true".equalsIgnoreCase(jsonConfig.get("enableTls").asText()); | ||
112 | + } | ||
113 | + } | ||
107 | javaMailProperties.put(MAIL_PROP + protocol + ".starttls.enable", enableTls); | 114 | javaMailProperties.put(MAIL_PROP + protocol + ".starttls.enable", enableTls); |
108 | if (enableTls && jsonConfig.has("tlsVersion") && StringUtils.isNoneEmpty(jsonConfig.get("tlsVersion").asText())) { | 115 | if (enableTls && jsonConfig.has("tlsVersion") && StringUtils.isNoneEmpty(jsonConfig.get("tlsVersion").asText())) { |
109 | javaMailProperties.put(MAIL_PROP + protocol + ".ssl.protocols", jsonConfig.get("tlsVersion").asText()); | 116 | javaMailProperties.put(MAIL_PROP + protocol + ".ssl.protocols", jsonConfig.get("tlsVersion").asText()); |
1 | +/** | ||
2 | + * Copyright © 2016-2020 The Thingsboard Authors | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | +package org.thingsboard.server.utils; | ||
17 | + | ||
18 | +import com.google.gson.JsonElement; | ||
19 | +import com.google.gson.JsonObject; | ||
20 | +import com.google.gson.JsonParser; | ||
21 | +import org.thingsboard.server.gen.transport.TransportProtos.KeyValueProto; | ||
22 | +import java.util.List; | ||
23 | + | ||
24 | +public class JsonUtils { | ||
25 | + | ||
26 | + private static final JsonParser jsonParser = new JsonParser(); | ||
27 | + | ||
28 | + public static JsonObject getJsonObject(List<KeyValueProto> tsKv) { | ||
29 | + JsonObject json = new JsonObject(); | ||
30 | + for (KeyValueProto kv : tsKv) { | ||
31 | + switch (kv.getType()) { | ||
32 | + case BOOLEAN_V: | ||
33 | + json.addProperty(kv.getKey(), kv.getBoolV()); | ||
34 | + break; | ||
35 | + case LONG_V: | ||
36 | + json.addProperty(kv.getKey(), kv.getLongV()); | ||
37 | + break; | ||
38 | + case DOUBLE_V: | ||
39 | + json.addProperty(kv.getKey(), kv.getDoubleV()); | ||
40 | + break; | ||
41 | + case STRING_V: | ||
42 | + json.addProperty(kv.getKey(), kv.getStringV()); | ||
43 | + break; | ||
44 | + case JSON_V: | ||
45 | + json.add(kv.getKey(), jsonParser.parse(kv.getJsonV())); | ||
46 | + break; | ||
47 | + } | ||
48 | + } | ||
49 | + return json; | ||
50 | + } | ||
51 | + | ||
52 | + public static JsonElement parse(String params) { | ||
53 | + return jsonParser.parse(params); | ||
54 | + } | ||
55 | +} |