From 04c89d1fc101634d58c2a5e651a47686bce89701 Mon Sep 17 00:00:00 2001 From: heavyrian2012 Date: Wed, 31 Aug 2022 20:24:46 +0800 Subject: [PATCH] =?UTF-8?q?gson=E5=8D=95=E4=BE=8B=E5=8C=96=EF=BC=8C?= =?UTF-8?q?=E5=8F=A6=E5=A4=96=E5=85=B3=E6=8E=89gson=20html=E8=BD=AC?= =?UTF-8?q?=E4=B9=89=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/wildfirechat/push/PushServer.java | 5 +++-- .../com/xiaoleilu/loServer/action/Action.java | 9 +++++---- .../xiaoleilu/loServer/action/VersionAction.java | 2 +- .../loServer/action/admin/AdminAction.java | 2 +- .../admin/ApplicationGetUserInfoAction.java | 2 +- .../loServer/action/admin/BlockUserAction.java | 2 +- .../action/admin/CheckUserOnlineAction.java | 2 +- .../action/admin/CreateChatroomAction.java | 2 +- .../loServer/action/admin/CreateRobotAction.java | 2 +- .../action/admin/DestoryChatroomAction.java | 2 +- .../admin/ModifyGroupMemberExtraAction.java | 2 +- .../channel/ApplicationGetUserInfoAction.java | 2 +- .../loServer/action/channel/ChannelAction.java | 4 ++-- .../action/channel/SubscriberChannelAction.java | 2 +- .../robot/ApplicationGetUserInfoAction.java | 2 +- .../robot/ModifyGroupMemberExtraAction.java | 2 +- .../loServer/action/robot/RobotAction.java | 4 ++-- .../java/io/moquette/imhandler/IMHandler.java | 3 ++- .../moquette/imhandler/SendMessageHandler.java | 7 ++----- .../persistence/MemoryMessagesStore.java | 16 ++++++++-------- .../io/moquette/spi/impl/MessagesPublisher.java | 11 ++++++----- .../io/moquette/spi/impl/ProtocolProcessor.java | 3 ++- .../io/moquette/spi/impl/Qos1PublishHandler.java | 3 ++- .../src/main/java/win/liyufan/im/GsonUtil.java | 8 ++++++++ broker/src/main/java/win/liyufan/im/Utility.java | 4 ++-- .../cn/wildfirechat/pojos/ArticleContent.java | 3 ++- .../pojos/GroupNotificationBinaryContent.java | 3 ++- sdk/src/main/java/cn/wildfirechat/sdk/Main.java | 3 ++- .../sdk/utilities/AdminHttpUtils.java | 4 +++- .../sdk/utilities/ChannelHttpUtils.java | 4 +++- .../cn/wildfirechat/sdk/utilities/JsonUtils.java | 5 +++-- .../sdk/utilities/RobotHttpUtils.java | 4 +++- 32 files changed, 75 insertions(+), 54 deletions(-) create mode 100644 broker/src/main/java/win/liyufan/im/GsonUtil.java diff --git a/broker/src/main/java/cn/wildfirechat/push/PushServer.java b/broker/src/main/java/cn/wildfirechat/push/PushServer.java index b1099fc6..b203c55b 100644 --- a/broker/src/main/java/cn/wildfirechat/push/PushServer.java +++ b/broker/src/main/java/cn/wildfirechat/push/PushServer.java @@ -11,6 +11,7 @@ package cn.wildfirechat.push; import cn.wildfirechat.common.IMExceptionEvent; import cn.wildfirechat.proto.ProtoConstants; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import io.moquette.persistence.MemorySessionStore; import io.moquette.server.config.IConfig; import io.moquette.spi.ISessionsStore; @@ -29,7 +30,7 @@ import static win.liyufan.im.HttpUtils.HttpPostType.POST_TYPE_Push; public class PushServer { private static final Logger LOG = LoggerFactory.getLogger(PushServer.class); - + protected static final Gson gson = new GsonBuilder().disableHtmlEscaping().create(); public interface PushMessageType { int PUSH_MESSAGE_TYPE_NORMAL = 0; int PUSH_MESSAGE_TYPE_VOIP_INVITE = 1; @@ -100,7 +101,7 @@ public class PushServer { url = iOSPushServerUrl; pushMessage.voipDeviceToken = session.getVoipDeviceToken(); } - HttpUtils.httpJsonPost(url, new Gson().toJson(pushMessage, pushMessage.getClass()), POST_TYPE_Push); + HttpUtils.httpJsonPost(url, gson.toJson(pushMessage, pushMessage.getClass()), POST_TYPE_Push); } else { LOG.info("Not mobile platform {}", session.getPlatform()); } diff --git a/broker/src/main/java/com/xiaoleilu/loServer/action/Action.java b/broker/src/main/java/com/xiaoleilu/loServer/action/Action.java index 44a376b4..8d64d9b5 100755 --- a/broker/src/main/java/com/xiaoleilu/loServer/action/Action.java +++ b/broker/src/main/java/com/xiaoleilu/loServer/action/Action.java @@ -9,6 +9,7 @@ package com.xiaoleilu.loServer.action; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import com.xiaoleilu.loServer.RestResult; import com.xiaoleilu.loServer.annotation.RequireAuthentication; import com.xiaoleilu.loServer.handler.Request; @@ -39,7 +40,7 @@ import static io.moquette.BrokerConstants.*; abstract public class Action { public static IMessagesStore messagesStore = null; public static ISessionsStore sessionsStore = null; - + protected static final Gson gson = new GsonBuilder().disableHtmlEscaping().create(); public ChannelHandlerContext ctx; protected static RateLimiter adminLimiter = null; @@ -129,7 +130,7 @@ abstract public class Action { } RestResult result = RestResult.resultOf(errorCode, errorCode.getMsg(), RestResult.resultOf(errorCode)); - response.setContent(new Gson().toJson(result)); + response.setContent(gson.toJson(result)); response.send(); } @@ -145,7 +146,7 @@ abstract public class Action { FullHttpRequest fullHttpRequest = (FullHttpRequest) request; byte[] bytes = Utils.readBytesAndRewind(fullHttpRequest.content()); String content = new String(bytes, StandardCharsets.UTF_8);//contribute by JiaRG from github - Gson gson = new Gson(); + T t = gson.fromJson(content, cls); return t; } @@ -154,6 +155,6 @@ abstract public class Action { protected void setResponseContent(RestResult result, Response response) { response.setStatus(HttpResponseStatus.OK); - response.setContent(new Gson().toJson(result)); + response.setContent(gson.toJson(result)); } } diff --git a/broker/src/main/java/com/xiaoleilu/loServer/action/VersionAction.java b/broker/src/main/java/com/xiaoleilu/loServer/action/VersionAction.java index bce1a08c..ef3b1bd5 100755 --- a/broker/src/main/java/com/xiaoleilu/loServer/action/VersionAction.java +++ b/broker/src/main/java/com/xiaoleilu/loServer/action/VersionAction.java @@ -34,7 +34,7 @@ public class VersionAction extends Action { try { - response.setContent(Utility.formatJson(new Gson().toJson(GitRepositoryState.getGitRepositoryState()))); + response.setContent(Utility.formatJson(gson.toJson(GitRepositoryState.getGitRepositoryState()))); } catch (IOException e) { e.printStackTrace(); response.setContent("{\"version\":\"unknown\"}"); diff --git a/broker/src/main/java/com/xiaoleilu/loServer/action/admin/AdminAction.java b/broker/src/main/java/com/xiaoleilu/loServer/action/admin/AdminAction.java index 50e138f0..62a596f5 100644 --- a/broker/src/main/java/com/xiaoleilu/loServer/action/admin/AdminAction.java +++ b/broker/src/main/java/com/xiaoleilu/loServer/action/admin/AdminAction.java @@ -99,7 +99,7 @@ abstract public class AdminAction extends Action { } RestResult result = RestResult.resultOf(errorCode, errorCode.getMsg(), data); - response.setContent(new Gson().toJson(result)); + response.setContent(gson.toJson(result)); response.send(); } } diff --git a/broker/src/main/java/com/xiaoleilu/loServer/action/admin/ApplicationGetUserInfoAction.java b/broker/src/main/java/com/xiaoleilu/loServer/action/admin/ApplicationGetUserInfoAction.java index 865c2198..8d6c2f62 100644 --- a/broker/src/main/java/com/xiaoleilu/loServer/action/admin/ApplicationGetUserInfoAction.java +++ b/broker/src/main/java/com/xiaoleilu/loServer/action/admin/ApplicationGetUserInfoAction.java @@ -50,7 +50,7 @@ public class ApplicationGetUserInfoAction extends AdminAction { } response.setStatus(HttpResponseStatus.OK); - response.setContent(new Gson().toJson(result)); + response.setContent(gson.toJson(result)); } return true; } diff --git a/broker/src/main/java/com/xiaoleilu/loServer/action/admin/BlockUserAction.java b/broker/src/main/java/com/xiaoleilu/loServer/action/admin/BlockUserAction.java index 0d4d74cf..8323b76a 100755 --- a/broker/src/main/java/com/xiaoleilu/loServer/action/admin/BlockUserAction.java +++ b/broker/src/main/java/com/xiaoleilu/loServer/action/admin/BlockUserAction.java @@ -42,7 +42,7 @@ public class BlockUserAction extends AdminAction { response.setStatus(HttpResponseStatus.OK); RestResult result; result = RestResult.resultOf(errorCode); - response.setContent(new Gson().toJson(result)); + response.setContent(gson.toJson(result)); if (inputUserBlock.getStatus() == 2) { sendApiMessage(null, null, ServerAPIHelper.KICKOFF_USER_REQUEST, inputUserBlock.getUserId().getBytes(), null); diff --git a/broker/src/main/java/com/xiaoleilu/loServer/action/admin/CheckUserOnlineAction.java b/broker/src/main/java/com/xiaoleilu/loServer/action/admin/CheckUserOnlineAction.java index da893ed2..7c87eacc 100755 --- a/broker/src/main/java/com/xiaoleilu/loServer/action/admin/CheckUserOnlineAction.java +++ b/broker/src/main/java/com/xiaoleilu/loServer/action/admin/CheckUserOnlineAction.java @@ -36,7 +36,7 @@ public class CheckUserOnlineAction extends AdminAction { InputGetUserInfo inputUserId = getRequestBody(request.getNettyRequest(), InputGetUserInfo.class); if (inputUserId == null || !StringUtil.isNullOrEmpty(inputUserId.getUserId())) { sendApiMessage(response, inputUserId.getUserId(), ServerAPIHelper.CHECK_USER_ONLINE_REQUEST, inputUserId.getUserId().getBytes(), res -> { - OutputCheckUserOnline out = new Gson().fromJson(new String(res), OutputCheckUserOnline.class); + OutputCheckUserOnline out = gson.fromJson(new String(res), OutputCheckUserOnline.class); return new Result(ErrorCode.ERROR_CODE_SUCCESS, out); }); return false; diff --git a/broker/src/main/java/com/xiaoleilu/loServer/action/admin/CreateChatroomAction.java b/broker/src/main/java/com/xiaoleilu/loServer/action/admin/CreateChatroomAction.java index 6e7fc096..8b30f012 100755 --- a/broker/src/main/java/com/xiaoleilu/loServer/action/admin/CreateChatroomAction.java +++ b/broker/src/main/java/com/xiaoleilu/loServer/action/admin/CreateChatroomAction.java @@ -39,7 +39,7 @@ public class CreateChatroomAction extends AdminAction { FullHttpRequest fullHttpRequest = (FullHttpRequest)request.getNettyRequest(); byte[] bytes = Utils.readBytesAndRewind(fullHttpRequest.content()); String content = new String(bytes); - Gson gson = new Gson(); + InputCreateChatroom inputCreateChatroom = gson.fromJson(content, InputCreateChatroom.class); if (inputCreateChatroom != null && !StringUtil.isNullOrEmpty(inputCreateChatroom.getTitle())) { diff --git a/broker/src/main/java/com/xiaoleilu/loServer/action/admin/CreateRobotAction.java b/broker/src/main/java/com/xiaoleilu/loServer/action/admin/CreateRobotAction.java index 219dbcd0..1e3d7978 100755 --- a/broker/src/main/java/com/xiaoleilu/loServer/action/admin/CreateRobotAction.java +++ b/broker/src/main/java/com/xiaoleilu/loServer/action/admin/CreateRobotAction.java @@ -67,7 +67,7 @@ public class CreateRobotAction extends AdminAction { Utility.printExecption(LOG, e, IMExceptionEvent.EventType.ADMIN_API_Exception); response.setStatus(HttpResponseStatus.OK); RestResult result = RestResult.resultOf(ErrorCode.ERROR_CODE_SERVER_ERROR, e.getMessage()); - response.setContent(new Gson().toJson(result)); + response.setContent(gson.toJson(result)); return true; } diff --git a/broker/src/main/java/com/xiaoleilu/loServer/action/admin/DestoryChatroomAction.java b/broker/src/main/java/com/xiaoleilu/loServer/action/admin/DestoryChatroomAction.java index bce700b5..34bc9e5c 100755 --- a/broker/src/main/java/com/xiaoleilu/loServer/action/admin/DestoryChatroomAction.java +++ b/broker/src/main/java/com/xiaoleilu/loServer/action/admin/DestoryChatroomAction.java @@ -37,7 +37,7 @@ public class DestoryChatroomAction extends AdminAction { FullHttpRequest fullHttpRequest = (FullHttpRequest)request.getNettyRequest(); byte[] bytes = Utils.readBytesAndRewind(fullHttpRequest.content()); String content = new String(bytes); - Gson gson = new Gson(); + InputDestoryChatroom inputDestoryChatroom = gson.fromJson(content, InputDestoryChatroom.class); if (inputDestoryChatroom != null && !StringUtil.isNullOrEmpty(inputDestoryChatroom.getChatroomId())) { diff --git a/broker/src/main/java/com/xiaoleilu/loServer/action/admin/ModifyGroupMemberExtraAction.java b/broker/src/main/java/com/xiaoleilu/loServer/action/admin/ModifyGroupMemberExtraAction.java index d3564b3c..196781ea 100755 --- a/broker/src/main/java/com/xiaoleilu/loServer/action/admin/ModifyGroupMemberExtraAction.java +++ b/broker/src/main/java/com/xiaoleilu/loServer/action/admin/ModifyGroupMemberExtraAction.java @@ -50,7 +50,7 @@ public class ModifyGroupMemberExtraAction extends AdminAction { } else { response.setStatus(HttpResponseStatus.OK); RestResult result = RestResult.resultOf(ErrorCode.INVALID_PARAMETER); - response.setContent(new Gson().toJson(result)); + response.setContent(gson.toJson(result)); } } return true; diff --git a/broker/src/main/java/com/xiaoleilu/loServer/action/channel/ApplicationGetUserInfoAction.java b/broker/src/main/java/com/xiaoleilu/loServer/action/channel/ApplicationGetUserInfoAction.java index 06e6cb82..0bf1f76b 100755 --- a/broker/src/main/java/com/xiaoleilu/loServer/action/channel/ApplicationGetUserInfoAction.java +++ b/broker/src/main/java/com/xiaoleilu/loServer/action/channel/ApplicationGetUserInfoAction.java @@ -52,7 +52,7 @@ public class ApplicationGetUserInfoAction extends ChannelAction { } response.setStatus(HttpResponseStatus.OK); - response.setContent(new Gson().toJson(result)); + response.setContent(gson.toJson(result)); } return true; } diff --git a/broker/src/main/java/com/xiaoleilu/loServer/action/channel/ChannelAction.java b/broker/src/main/java/com/xiaoleilu/loServer/action/channel/ChannelAction.java index 2a7ba4d8..7df035a6 100644 --- a/broker/src/main/java/com/xiaoleilu/loServer/action/channel/ChannelAction.java +++ b/broker/src/main/java/com/xiaoleilu/loServer/action/channel/ChannelAction.java @@ -101,7 +101,7 @@ abstract public class ChannelAction extends Action { } RestResult result = RestResult.resultOf(errorCode, errorCode.getMsg(), data); - response.setContent(new Gson().toJson(result)); + response.setContent(gson.toJson(result)); response.send(); } @@ -110,7 +110,7 @@ abstract public class ChannelAction extends Action { FullHttpRequest fullHttpRequest = (FullHttpRequest) request; byte[] bytes = Utils.readBytesAndRewind(fullHttpRequest.content()); String content = new String(bytes, StandardCharsets.UTF_8); - Gson gson = new Gson(); + T t = gson.fromJson(content, cls); return t; } diff --git a/broker/src/main/java/com/xiaoleilu/loServer/action/channel/SubscriberChannelAction.java b/broker/src/main/java/com/xiaoleilu/loServer/action/channel/SubscriberChannelAction.java index f12e2a70..bef4808a 100755 --- a/broker/src/main/java/com/xiaoleilu/loServer/action/channel/SubscriberChannelAction.java +++ b/broker/src/main/java/com/xiaoleilu/loServer/action/channel/SubscriberChannelAction.java @@ -38,7 +38,7 @@ public class SubscriberChannelAction extends ChannelAction { if(input.getSubscribe() > 0 && (channelInfo.getStatus() & ProtoConstants.ChannelState.Channel_State_Mask_Active_Subscribe) == 0) { response.setStatus(HttpResponseStatus.OK); RestResult result = RestResult.resultOf(ErrorCode.ERROR_CODE_NOT_RIGHT); - response.setContent(new Gson().toJson(result)); + response.setContent(gson.toJson(result)); return true; } WFCMessage.ListenChannel listenChannel = WFCMessage.ListenChannel.newBuilder().setChannelId(channelInfo.getTargetId()).setListen(input.getSubscribe()).build(); diff --git a/broker/src/main/java/com/xiaoleilu/loServer/action/robot/ApplicationGetUserInfoAction.java b/broker/src/main/java/com/xiaoleilu/loServer/action/robot/ApplicationGetUserInfoAction.java index 097f992b..4ffdad29 100755 --- a/broker/src/main/java/com/xiaoleilu/loServer/action/robot/ApplicationGetUserInfoAction.java +++ b/broker/src/main/java/com/xiaoleilu/loServer/action/robot/ApplicationGetUserInfoAction.java @@ -52,7 +52,7 @@ public class ApplicationGetUserInfoAction extends RobotAction { } response.setStatus(HttpResponseStatus.OK); - response.setContent(new Gson().toJson(result)); + response.setContent(gson.toJson(result)); } return true; } diff --git a/broker/src/main/java/com/xiaoleilu/loServer/action/robot/ModifyGroupMemberExtraAction.java b/broker/src/main/java/com/xiaoleilu/loServer/action/robot/ModifyGroupMemberExtraAction.java index 385d4e9c..83f43580 100755 --- a/broker/src/main/java/com/xiaoleilu/loServer/action/robot/ModifyGroupMemberExtraAction.java +++ b/broker/src/main/java/com/xiaoleilu/loServer/action/robot/ModifyGroupMemberExtraAction.java @@ -53,7 +53,7 @@ public class ModifyGroupMemberExtraAction extends RobotAction { } else { response.setStatus(HttpResponseStatus.OK); RestResult result = RestResult.resultOf(ErrorCode.INVALID_PARAMETER); - response.setContent(new Gson().toJson(result)); + response.setContent(gson.toJson(result)); } } return true; diff --git a/broker/src/main/java/com/xiaoleilu/loServer/action/robot/RobotAction.java b/broker/src/main/java/com/xiaoleilu/loServer/action/robot/RobotAction.java index c32cdc52..2bdcfc22 100644 --- a/broker/src/main/java/com/xiaoleilu/loServer/action/robot/RobotAction.java +++ b/broker/src/main/java/com/xiaoleilu/loServer/action/robot/RobotAction.java @@ -102,7 +102,7 @@ abstract public class RobotAction extends Action { } RestResult result = RestResult.resultOf(errorCode, errorCode.getMsg(), data); - response.setContent(new Gson().toJson(result)); + response.setContent(gson.toJson(result)); response.send(); } @@ -113,7 +113,7 @@ abstract public class RobotAction extends Action { String content = null; try { content = new String(bytes, "UTF-8"); - Gson gson = new Gson(); + T t = gson.fromJson(content, cls); return t; } catch (UnsupportedEncodingException e) { diff --git a/broker/src/main/java/io/moquette/imhandler/IMHandler.java b/broker/src/main/java/io/moquette/imhandler/IMHandler.java index 0727f7ba..f9809ec4 100755 --- a/broker/src/main/java/io/moquette/imhandler/IMHandler.java +++ b/broker/src/main/java/io/moquette/imhandler/IMHandler.java @@ -25,6 +25,7 @@ import io.netty.util.internal.StringUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import cn.wildfirechat.common.ErrorCode; +import win.liyufan.im.GsonUtil; import win.liyufan.im.RateLimiter; import win.liyufan.im.Utility; @@ -141,7 +142,7 @@ abstract public class IMHandler { // } //json ? - return (T)(new Gson().fromJson(new String(bytes), dataCls)); + return (T)(GsonUtil.gson.fromJson(new String(bytes), dataCls)); } public static void init(IMessagesStore ms, ISessionsStore ss, MessagesPublisher p, ThreadPoolExecutorWrapper businessExecutor, Server server) { diff --git a/broker/src/main/java/io/moquette/imhandler/SendMessageHandler.java b/broker/src/main/java/io/moquette/imhandler/SendMessageHandler.java index 99457cf0..b0ebb0f5 100644 --- a/broker/src/main/java/io/moquette/imhandler/SendMessageHandler.java +++ b/broker/src/main/java/io/moquette/imhandler/SendMessageHandler.java @@ -20,10 +20,7 @@ import io.moquette.spi.impl.Qos1PublishHandler; import io.netty.buffer.ByteBuf; import cn.wildfirechat.common.ErrorCode; import io.netty.buffer.Unpooled; -import win.liyufan.im.HttpUtils; -import win.liyufan.im.IMTopic; -import win.liyufan.im.MessageShardingUtil; -import win.liyufan.im.Utility; +import win.liyufan.im.*; import java.util.HashSet; import java.util.Set; @@ -219,7 +216,7 @@ public class SendMessageHandler extends IMHandler { if(StringUtil.isNullOrEmpty(content)) { saveAndPublish(fromUser, clientID, finalMsg, requestSourceType); } else { - MessagePayload payload = new Gson().fromJson(content, MessagePayload.class); + MessagePayload payload = GsonUtil.gson.fromJson(content, MessagePayload.class); if (payload != null && payload.getType() > 0) { WFCMessage.Message newMsg = finalMsg.toBuilder().setContent(payload.toProtoMessageContent()).build(); saveAndPublish(fromUser, clientID, newMsg, requestSourceType); diff --git a/broker/src/main/java/io/moquette/persistence/MemoryMessagesStore.java b/broker/src/main/java/io/moquette/persistence/MemoryMessagesStore.java index 8cae9690..54e2803b 100755 --- a/broker/src/main/java/io/moquette/persistence/MemoryMessagesStore.java +++ b/broker/src/main/java/io/moquette/persistence/MemoryMessagesStore.java @@ -1143,7 +1143,7 @@ public class MemoryMessagesStore implements IMessagesStore { event.type = type; m_Server.getCallbackScheduler().execute(() -> { try { - HttpUtils.httpJsonPost(mGroupInfoUpdateCallback, new Gson().toJson(event), HttpUtils.HttpPostType.POST_TYPE_Grout_Event_Callback); + HttpUtils.httpJsonPost(mGroupInfoUpdateCallback, GsonUtil.gson.toJson(event), HttpUtils.HttpPostType.POST_TYPE_Grout_Event_Callback); } catch (Exception e) { e.printStackTrace(); Utility.printExecption(LOG, e, EVENT_CALLBACK_Exception); @@ -1162,7 +1162,7 @@ public class MemoryMessagesStore implements IMessagesStore { event.value = value; m_Server.getCallbackScheduler().execute(() -> { try { - HttpUtils.httpJsonPost(mGroupMemberUpdateCallback, new Gson().toJson(event), HttpUtils.HttpPostType.POST_TYPE_Grout_Member_Event_Callback); + HttpUtils.httpJsonPost(mGroupMemberUpdateCallback, GsonUtil.gson.toJson(event), HttpUtils.HttpPostType.POST_TYPE_Grout_Member_Event_Callback); } catch (Exception e) { e.printStackTrace(); Utility.printExecption(LOG, e, EVENT_CALLBACK_Exception); @@ -1180,7 +1180,7 @@ public class MemoryMessagesStore implements IMessagesStore { event.value = value; m_Server.getCallbackScheduler().execute(() -> { try { - HttpUtils.httpJsonPost(mRelationUpdateCallback, new Gson().toJson(event), HttpUtils.HttpPostType.POST_TYPE_User_Relation_Event_Callback); + HttpUtils.httpJsonPost(mRelationUpdateCallback, GsonUtil.gson.toJson(event), HttpUtils.HttpPostType.POST_TYPE_User_Relation_Event_Callback); } catch (Exception e) { e.printStackTrace(); Utility.printExecption(LOG, e, EVENT_CALLBACK_Exception); @@ -1193,7 +1193,7 @@ public class MemoryMessagesStore implements IMessagesStore { if (!StringUtil.isNullOrEmpty(mUserInfoUpdateCallback)) { m_Server.getCallbackScheduler().execute(() -> { try { - HttpUtils.httpJsonPost(mUserInfoUpdateCallback, new Gson().toJson(InputOutputUserInfo.fromPbUser(user)), HttpUtils.HttpPostType.POST_TYPE_User_Info_Event_Callback); + HttpUtils.httpJsonPost(mUserInfoUpdateCallback, GsonUtil.gson.toJson(InputOutputUserInfo.fromPbUser(user)), HttpUtils.HttpPostType.POST_TYPE_User_Info_Event_Callback); } catch (Exception e) { e.printStackTrace(); Utility.printExecption(LOG, e, EVENT_CALLBACK_Exception); @@ -1210,7 +1210,7 @@ public class MemoryMessagesStore implements IMessagesStore { event.type = type; m_Server.getCallbackScheduler().execute(() -> { try { - HttpUtils.httpJsonPost(mChannelInfoUpdateCallback, new Gson().toJson(event), HttpUtils.HttpPostType.POST_TYPE_Channel_Info_Event_Callback); + HttpUtils.httpJsonPost(mChannelInfoUpdateCallback, GsonUtil.gson.toJson(event), HttpUtils.HttpPostType.POST_TYPE_Channel_Info_Event_Callback); } catch (Exception e) { e.printStackTrace(); Utility.printExecption(LOG, e, EVENT_CALLBACK_Exception); @@ -1226,7 +1226,7 @@ public class MemoryMessagesStore implements IMessagesStore { event.type = type; m_Server.getCallbackScheduler().execute(() -> { try { - HttpUtils.httpJsonPost(mChatroomInfoUpdateCallback, new Gson().toJson(event), HttpUtils.HttpPostType.POST_TYPE_Chatroom_Info_Event_Callback); + HttpUtils.httpJsonPost(mChatroomInfoUpdateCallback, GsonUtil.gson.toJson(event), HttpUtils.HttpPostType.POST_TYPE_Chatroom_Info_Event_Callback); } catch (Exception e) { e.printStackTrace(); Utility.printExecption(LOG, e, EVENT_CALLBACK_Exception); @@ -1244,7 +1244,7 @@ public class MemoryMessagesStore implements IMessagesStore { event.type = type; m_Server.getCallbackScheduler().execute(() -> { try { - HttpUtils.httpJsonPost(mChatroomMemberUpdateCallback, new Gson().toJson(event), HttpUtils.HttpPostType.POST_TYPE_Chatroom_Member_Event_Callback); + HttpUtils.httpJsonPost(mChatroomMemberUpdateCallback, GsonUtil.gson.toJson(event), HttpUtils.HttpPostType.POST_TYPE_Chatroom_Member_Event_Callback); } catch (Exception e) { e.printStackTrace(); Utility.printExecption(LOG, e, EVENT_CALLBACK_Exception); @@ -3889,7 +3889,7 @@ public class MemoryMessagesStore implements IMessagesStore { Utility.printExecption(LOG, e); } } else if(modifyType == Modify_Channel_Menu) { - OutputGetChannelInfo.OutputMenuList outputMenuButtons = new Gson().fromJson(value, OutputGetChannelInfo.OutputMenuList.class); + OutputGetChannelInfo.OutputMenuList outputMenuButtons = GsonUtil.gson.fromJson(value, OutputGetChannelInfo.OutputMenuList.class); if (!outputMenuButtons.isEmpty()) { for (OutputGetChannelInfo.OutputMenu outputMenuButton : outputMenuButtons) { newInfoBuilder.addMenu(outputMenuButton.toPbInfo()); diff --git a/broker/src/main/java/io/moquette/spi/impl/MessagesPublisher.java b/broker/src/main/java/io/moquette/spi/impl/MessagesPublisher.java index 7944fe9e..d53cb0a0 100755 --- a/broker/src/main/java/io/moquette/spi/impl/MessagesPublisher.java +++ b/broker/src/main/java/io/moquette/spi/impl/MessagesPublisher.java @@ -35,6 +35,7 @@ import io.moquette.spi.ISessionsStore; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.handler.codec.mqtt.*; +import win.liyufan.im.GsonUtil; import win.liyufan.im.HttpUtils; import win.liyufan.im.IMTopic; @@ -188,7 +189,7 @@ public class MessagesPublisher { OutputClient finalOutputClient = outputClient; Server.getServer().getCallbackScheduler().execute(() -> { try { - HttpUtils.httpJsonPost(robot.getCallback(), new Gson().toJson(OutputMessageData.fromProtoMessage(finalMsg, finalOutputClient), OutputMessageData.class), HttpUtils.HttpPostType.POST_TYPE_Robot_Message_Callback); + HttpUtils.httpJsonPost(robot.getCallback(), GsonUtil.gson.toJson(OutputMessageData.fromProtoMessage(finalMsg, finalOutputClient), OutputMessageData.class), HttpUtils.HttpPostType.POST_TYPE_Robot_Message_Callback); } catch (Exception e) { e.printStackTrace(); Utility.printExecption(LOG, e, EVENT_CALLBACK_Exception); @@ -616,7 +617,7 @@ public class MessagesPublisher { OutputClient finalOutputClient = outputClient; Server.getServer().getCallbackScheduler().execute(() -> { try { - HttpUtils.httpJsonPost(channelInfo.getCallback() + "/message", new Gson().toJson(OutputMessageData.fromProtoMessage(message, finalOutputClient), OutputMessageData.class), HttpUtils.HttpPostType.POST_TYPE_Channel_Message_Callback); + HttpUtils.httpJsonPost(channelInfo.getCallback() + "/message", GsonUtil.gson.toJson(OutputMessageData.fromProtoMessage(message, finalOutputClient), OutputMessageData.class), HttpUtils.HttpPostType.POST_TYPE_Channel_Message_Callback); } catch (Exception e) { e.printStackTrace(); Utility.printExecption(LOG, e, EVENT_CALLBACK_Exception); @@ -669,7 +670,7 @@ public class MessagesPublisher { public void forwardMessage(final WFCMessage.Message message, String forwardUrl, OutputClient outputClient) { Server.getServer().getCallbackScheduler().execute(() -> { try { - HttpUtils.httpJsonPost(forwardUrl, new Gson().toJson(OutputMessageData.fromProtoMessage(message, outputClient), OutputMessageData.class), HttpUtils.HttpPostType.POST_TYPE_Forward_Message_Callback); + HttpUtils.httpJsonPost(forwardUrl, GsonUtil.gson.toJson(OutputMessageData.fromProtoMessage(message, outputClient), OutputMessageData.class), HttpUtils.HttpPostType.POST_TYPE_Forward_Message_Callback); } catch (Exception e) { e.printStackTrace(); Utility.printExecption(LOG, e, EVENT_CALLBACK_Exception); @@ -679,7 +680,7 @@ public class MessagesPublisher { public void forwardMessageWithCallback(final WFCMessage.Message message, String forwardUrl, HttpUtils.HttpCallback callback) { try { - HttpUtils.httpJsonPost(forwardUrl, new Gson().toJson(OutputMessageData.fromProtoMessage(message), OutputMessageData.class), new HttpUtils.HttpCallback() { + HttpUtils.httpJsonPost(forwardUrl, GsonUtil.gson.toJson(OutputMessageData.fromProtoMessage(message), OutputMessageData.class), new HttpUtils.HttpCallback() { @Override public void onSuccess(String content) { Server.getServer().getImBusinessScheduler().execute(()->{ @@ -707,7 +708,7 @@ public class MessagesPublisher { Server.getServer().getCallbackScheduler().execute(() -> { try { - HttpUtils.httpJsonPost(channelInfo.getCallback() + "/subscribe", new Gson().toJson(new OutputNotifyChannelSubscribeStatus(user, channelInfo.getTargetId(), listen), OutputNotifyChannelSubscribeStatus.class), HttpUtils.HttpPostType.POST_TYPE_Channel_Subscriber_Event_Callback); + HttpUtils.httpJsonPost(channelInfo.getCallback() + "/subscribe", GsonUtil.gson.toJson(new OutputNotifyChannelSubscribeStatus(user, channelInfo.getTargetId(), listen), OutputNotifyChannelSubscribeStatus.class), HttpUtils.HttpPostType.POST_TYPE_Channel_Subscriber_Event_Callback); } catch (Exception e) { e.printStackTrace(); Utility.printExecption(LOG, e, EVENT_CALLBACK_Exception); diff --git a/broker/src/main/java/io/moquette/spi/impl/ProtocolProcessor.java b/broker/src/main/java/io/moquette/spi/impl/ProtocolProcessor.java index 8de1d898..132d76c9 100755 --- a/broker/src/main/java/io/moquette/spi/impl/ProtocolProcessor.java +++ b/broker/src/main/java/io/moquette/spi/impl/ProtocolProcessor.java @@ -43,6 +43,7 @@ import io.netty.handler.codec.mqtt.*; import io.netty.handler.timeout.IdleStateHandler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import win.liyufan.im.GsonUtil; import win.liyufan.im.HttpUtils; import win.liyufan.im.IMTopic; import win.liyufan.im.Utility; @@ -264,7 +265,7 @@ public class ProtocolProcessor { if (!StringUtil.isNullOrEmpty(mUserOnlineStatusCallback)) { mServer.getCallbackScheduler().execute(() -> { try { - HttpUtils.httpJsonPost(mUserOnlineStatusCallback, new Gson().toJson(new UserOnlineStatus(userId, clientId, platform, status, packageName)), HttpUtils.HttpPostType.POST_TYPE_User_Online_Event_Callback); + HttpUtils.httpJsonPost(mUserOnlineStatusCallback, GsonUtil.gson.toJson(new UserOnlineStatus(userId, clientId, platform, status, packageName)), HttpUtils.HttpPostType.POST_TYPE_User_Online_Event_Callback); } catch (Exception e) { e.printStackTrace(); Utility.printExecption(LOG, e, EVENT_CALLBACK_Exception); diff --git a/broker/src/main/java/io/moquette/spi/impl/Qos1PublishHandler.java b/broker/src/main/java/io/moquette/spi/impl/Qos1PublishHandler.java index 9eef8bb2..d729e60e 100755 --- a/broker/src/main/java/io/moquette/spi/impl/Qos1PublishHandler.java +++ b/broker/src/main/java/io/moquette/spi/impl/Qos1PublishHandler.java @@ -57,6 +57,7 @@ import io.netty.handler.codec.mqtt.MqttFixedHeader; import io.netty.handler.codec.mqtt.MqttMessageType; import io.netty.handler.codec.mqtt.MqttPublishMessage; import cn.wildfirechat.common.ErrorCode; +import win.liyufan.im.GsonUtil; import win.liyufan.im.IMTopic; import win.liyufan.im.RateLimiter; import win.liyufan.im.Utility; @@ -142,7 +143,7 @@ public class Qos1PublishHandler extends QosPublishHandler { out.addSession(userId, session.getClientID(), session.getPlatform(), status, session.getLastActiveTime()); } - callback.onRouteHandled(new Gson().toJson(out).getBytes()); + callback.onRouteHandled(GsonUtil.gson.toJson(out).getBytes()); }); } diff --git a/broker/src/main/java/win/liyufan/im/GsonUtil.java b/broker/src/main/java/win/liyufan/im/GsonUtil.java new file mode 100644 index 00000000..4c469c00 --- /dev/null +++ b/broker/src/main/java/win/liyufan/im/GsonUtil.java @@ -0,0 +1,8 @@ +package win.liyufan.im; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +public class GsonUtil { + public static final Gson gson = new GsonBuilder().disableHtmlEscaping().create(); +} diff --git a/broker/src/main/java/win/liyufan/im/Utility.java b/broker/src/main/java/win/liyufan/im/Utility.java index 908e62fb..f735a0b0 100644 --- a/broker/src/main/java/win/liyufan/im/Utility.java +++ b/broker/src/main/java/win/liyufan/im/Utility.java @@ -98,7 +98,7 @@ public class Utility { if(now - t >= SendExceptionDuration) { for (Map.Entry entry : exceptionEventHashMap.entrySet()) { if(entry.getValue().count > 0) { - String jsonStr = new Gson().toJson(entry.getValue()); + String jsonStr = GsonUtil.gson.toJson(entry.getValue()); Server.getServer().getCallbackScheduler().execute(() -> HttpUtils.httpJsonPost(gMonitorEventAddress, jsonStr, HttpUtils.HttpPostType.POST_TYPE_Server_Exception_Callback)); entry.getValue().count = 0; lastSendTime = now; @@ -111,7 +111,7 @@ public class Utility { event.msg = "监控心跳通知"; event.call_stack = "别担心!这个是心跳消息,已经有24小时没有出现异常消息了!"; event.count = IMExceptionEvent.EventType.HEART_BEAT; - String jsonStr = new Gson().toJson(event); + String jsonStr = GsonUtil.gson.toJson(event); Server.getServer().getCallbackScheduler().execute(() -> HttpUtils.httpJsonPost(gMonitorEventAddress, jsonStr, HttpUtils.HttpPostType.POST_TYPE_Server_Exception_Callback)); } } diff --git a/common/src/main/java/cn/wildfirechat/pojos/ArticleContent.java b/common/src/main/java/cn/wildfirechat/pojos/ArticleContent.java index 7716b5bf..cfd224ff 100644 --- a/common/src/main/java/cn/wildfirechat/pojos/ArticleContent.java +++ b/common/src/main/java/cn/wildfirechat/pojos/ArticleContent.java @@ -1,6 +1,7 @@ package cn.wildfirechat.pojos; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import java.nio.charset.StandardCharsets; import java.util.ArrayList; @@ -47,7 +48,7 @@ public class ArticleContent { MessagePayload payload = new MessagePayload(); payload.setType(13); payload.setSearchableContent(top.title); - payload.setBase64edData(Base64.getEncoder().encodeToString(new Gson().toJson(this).getBytes(StandardCharsets.UTF_8))); + payload.setBase64edData(Base64.getEncoder().encodeToString(new GsonBuilder().disableHtmlEscaping().create().toJson(this).getBytes(StandardCharsets.UTF_8))); return payload; } } diff --git a/common/src/main/java/cn/wildfirechat/pojos/GroupNotificationBinaryContent.java b/common/src/main/java/cn/wildfirechat/pojos/GroupNotificationBinaryContent.java index 14e89ce7..a5e7e6fc 100644 --- a/common/src/main/java/cn/wildfirechat/pojos/GroupNotificationBinaryContent.java +++ b/common/src/main/java/cn/wildfirechat/pojos/GroupNotificationBinaryContent.java @@ -11,6 +11,7 @@ package cn.wildfirechat.pojos; import cn.wildfirechat.proto.ProtoConstants; import cn.wildfirechat.proto.WFCMessage; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import com.google.protobuf.ByteString; import java.util.List; @@ -89,7 +90,7 @@ public class GroupNotificationBinaryContent { } public WFCMessage.MessageContent getGroupNotifyContent(int groupContentType) { - return WFCMessage.MessageContent.newBuilder().setType(groupContentType).setData(ByteString.copyFromUtf8(new Gson().toJson(this))).build(); + return WFCMessage.MessageContent.newBuilder().setType(groupContentType).setData(ByteString.copyFromUtf8(new GsonBuilder().disableHtmlEscaping().create().toJson(this))).build(); } public WFCMessage.MessageContent getAddGroupNotifyContent() { diff --git a/sdk/src/main/java/cn/wildfirechat/sdk/Main.java b/sdk/src/main/java/cn/wildfirechat/sdk/Main.java index bea92107..27718553 100644 --- a/sdk/src/main/java/cn/wildfirechat/sdk/Main.java +++ b/sdk/src/main/java/cn/wildfirechat/sdk/Main.java @@ -9,6 +9,7 @@ import cn.wildfirechat.sdk.model.IMResult; import cn.wildfirechat.sdk.utilities.AdminHttpUtils; import cn.wildfirechat.sdk.utilities.RobotHttpUtils; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import io.netty.util.internal.StringUtil; @@ -1510,7 +1511,7 @@ public class Main { menu21.url = "http://www.sohu.com"; menu2.subMenus.add(menu21); - String menuStr = new Gson().toJson(menus); + String menuStr = new GsonBuilder().disableHtmlEscaping().create().toJson(menus); voidIMResult = channelServiceApi.modifyChannelInfo(ProtoConstants.ModifyChannelInfoType.Modify_Channel_Menu, menuStr); if (voidIMResult != null && voidIMResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { System.out.println("modify channel menu success"); diff --git a/sdk/src/main/java/cn/wildfirechat/sdk/utilities/AdminHttpUtils.java b/sdk/src/main/java/cn/wildfirechat/sdk/utilities/AdminHttpUtils.java index 18bcbe1e..68153c3e 100644 --- a/sdk/src/main/java/cn/wildfirechat/sdk/utilities/AdminHttpUtils.java +++ b/sdk/src/main/java/cn/wildfirechat/sdk/utilities/AdminHttpUtils.java @@ -2,6 +2,7 @@ package cn.wildfirechat.sdk.utilities; import cn.wildfirechat.sdk.model.IMResult; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import ikidou.reflect.TypeBuilder; import org.apache.commons.codec.digest.DigestUtils; import org.apache.http.HttpResponse; @@ -29,6 +30,7 @@ import java.util.concurrent.TimeUnit; public class AdminHttpUtils extends JsonUtils { private static final Logger LOG = LoggerFactory.getLogger(AdminHttpUtils.class); + public static final Gson gson = new GsonBuilder().disableHtmlEscaping().create(); private static String adminUrl; private static String adminSecret; @@ -124,7 +126,7 @@ public class AdminHttpUtils extends JsonUtils { String jsonStr = ""; if (object != null) { - jsonStr = new Gson().toJson(object); + jsonStr = gson.toJson(object); } LOG.info("http request content: {}", jsonStr); diff --git a/sdk/src/main/java/cn/wildfirechat/sdk/utilities/ChannelHttpUtils.java b/sdk/src/main/java/cn/wildfirechat/sdk/utilities/ChannelHttpUtils.java index e6663247..58a2f847 100644 --- a/sdk/src/main/java/cn/wildfirechat/sdk/utilities/ChannelHttpUtils.java +++ b/sdk/src/main/java/cn/wildfirechat/sdk/utilities/ChannelHttpUtils.java @@ -2,6 +2,7 @@ package cn.wildfirechat.sdk.utilities; import cn.wildfirechat.sdk.model.IMResult; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import ikidou.reflect.TypeBuilder; import org.apache.commons.codec.digest.DigestUtils; import org.apache.http.HttpResponse; @@ -26,6 +27,7 @@ import java.util.concurrent.TimeUnit; public class ChannelHttpUtils extends JsonUtils { private static final Logger LOG = LoggerFactory.getLogger(ChannelHttpUtils.class); + public static final Gson gson = new GsonBuilder().disableHtmlEscaping().create(); private String imurl; private String channelId; @@ -74,7 +76,7 @@ public class ChannelHttpUtils extends JsonUtils { String jsonStr = ""; if (object != null) { - jsonStr = new Gson().toJson(object); + jsonStr = gson.toJson(object); } LOG.info("http request content: {}", jsonStr); diff --git a/sdk/src/main/java/cn/wildfirechat/sdk/utilities/JsonUtils.java b/sdk/src/main/java/cn/wildfirechat/sdk/utilities/JsonUtils.java index 00ed154c..de8b6fae 100644 --- a/sdk/src/main/java/cn/wildfirechat/sdk/utilities/JsonUtils.java +++ b/sdk/src/main/java/cn/wildfirechat/sdk/utilities/JsonUtils.java @@ -2,16 +2,17 @@ package cn.wildfirechat.sdk.utilities; import cn.wildfirechat.sdk.model.IMResult; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import ikidou.reflect.TypeBuilder; public class JsonUtils { - + public static final Gson gson = new GsonBuilder().disableHtmlEscaping().create(); public static IMResult fromJsonObject(String content, Class clazz) { TypeBuilder builder = TypeBuilder.newInstance(IMResult.class); if (!clazz.equals(Void.class)) { builder.addTypeParam(clazz); } - return new Gson().fromJson(content, builder.build()); + return gson.fromJson(content, builder.build()); } public static boolean isNullOrEmpty(String str) { diff --git a/sdk/src/main/java/cn/wildfirechat/sdk/utilities/RobotHttpUtils.java b/sdk/src/main/java/cn/wildfirechat/sdk/utilities/RobotHttpUtils.java index fdf36479..913f3fd9 100644 --- a/sdk/src/main/java/cn/wildfirechat/sdk/utilities/RobotHttpUtils.java +++ b/sdk/src/main/java/cn/wildfirechat/sdk/utilities/RobotHttpUtils.java @@ -2,6 +2,7 @@ package cn.wildfirechat.sdk.utilities; import cn.wildfirechat.sdk.model.IMResult; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import ikidou.reflect.TypeBuilder; import io.netty.util.internal.StringUtil; import org.apache.commons.codec.digest.DigestUtils; @@ -27,6 +28,7 @@ import java.util.concurrent.TimeUnit; public class RobotHttpUtils extends JsonUtils { private static final Logger LOG = LoggerFactory.getLogger(RobotHttpUtils.class); + public static final Gson gson = new GsonBuilder().disableHtmlEscaping().create(); private final String url; private final String robotId; @@ -75,7 +77,7 @@ public class RobotHttpUtils extends JsonUtils { String jsonStr = null; if (object != null) { - jsonStr = new Gson().toJson(object); + jsonStr = gson.toJson(object); } LOG.info("http request content: {}", jsonStr);