PageRenderTime 66ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/linbox/im/server/router/handlers/PullOldMsgHandler.java

https://gitlab.com/Mr.Tomato/linbox_server
Java | 179 lines | 137 code | 39 blank | 3 comment | 17 complexity | 0c829214dcb32476db3deca0107e2f86 MD5 | raw file
  1. package com.linbox.im.server.router.handlers;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.linbox.im.exceptions.IMConsumerException;
  5. import com.linbox.im.exceptions.IMException;
  6. import com.linbox.im.message.*;
  7. import com.linbox.im.server.service.IInboxService;
  8. import com.linbox.im.server.service.IOutboxService;
  9. import com.linbox.im.server.storage.dao.IGroupMessageDAO;
  10. import com.linbox.im.server.storage.dao.ISessionMessageDAO;
  11. import com.linbox.im.utils.IMUtils;
  12. import com.linbox.im.server.storage.dao.IGroupDAO;
  13. import com.linbox.im.server.storage.entity.GroupMessageEntity;
  14. import com.linbox.im.server.storage.entity.SessionMessageEntity;
  15. import org.apache.commons.lang.StringUtils;
  16. import org.apache.kafka.clients.consumer.ConsumerRecord;
  17. import org.slf4j.Logger;
  18. import org.slf4j.LoggerFactory;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.beans.factory.annotation.Qualifier;
  21. import org.springframework.stereotype.Service;
  22. import java.util.LinkedList;
  23. import java.util.List;
  24. /**
  25. * Created by lrsec on 7/2/15.
  26. */
  27. @Service
  28. @Qualifier("pullOldMsgHandler")
  29. public class PullOldMsgHandler implements Handler<String, String> {
  30. public static final Logger logger = LoggerFactory.getLogger(PullOldMsgHandler.class);
  31. @Autowired
  32. private IOutboxService outboxService;
  33. @Autowired
  34. private ISessionMessageDAO sessionMessageDAO;
  35. @Autowired
  36. private IInboxService inboxService;
  37. @Autowired
  38. private IGroupMessageDAO groupMessageDAO;
  39. @Override
  40. public void handle(ConsumerRecord<String, String> record) {
  41. String json = record.value();
  42. try {
  43. logger.debug("start handling PullOldMsgRequest: {}", json);
  44. MessageWrapper wrapper = JSON.parseObject(json, MessageWrapper.class);
  45. PullOldMsgRequest request = JSON.parseObject(((JSONObject)wrapper.content).toJSONString(), PullOldMsgRequest.class);
  46. wrapper.content = request;
  47. boolean shouldCleanUnread = false;
  48. if (request.maxMsgId == PullOldMsgRequest.MAX) {
  49. request.maxMsgId = Long.MAX_VALUE;
  50. shouldCleanUnread = true;
  51. }
  52. if (request.minMsgId < 0) {
  53. request.minMsgId = 0;
  54. }
  55. String userId = request.userId;
  56. if (StringUtils.isBlank(userId)) {
  57. logger.error("Can not find avaiable user id for PullOldMsgRequest {}", json);
  58. return;
  59. }
  60. try {
  61. MessageType type = MessageType.parse(request.type);
  62. if (type == null) {
  63. logger.error("Can not parse MessageType in PullOldMsgRequest {}", json);
  64. throw new IMException("Unknown MessageType " + Integer.toString(request.type));
  65. }
  66. List<Message> msgs = new LinkedList<Message>();
  67. switch (type) {
  68. case Session:
  69. String remoteId = request.remoteId;
  70. if (StringUtils.isBlank(remoteId)) {
  71. logger.error("Can not find corresponding remote id for PullOldMsgRequest: {}", json);
  72. throw new IMException("Cannot find remote id for PullOldMsgRequest: " + json);
  73. }
  74. String sessionId = IMUtils.getSessionKey(userId, remoteId);
  75. List<SessionMessageEntity> sessionDaos = sessionMessageDAO.findMsg(sessionId, request.maxMsgId, request.minMsgId, request.limit);
  76. for (SessionMessageEntity dao : sessionDaos) {
  77. Message m = Message.convertToMsg(dao, userId);
  78. msgs.add(m);
  79. }
  80. if (shouldCleanUnread && !msgs.isEmpty()) {
  81. inboxService.removeSessionMsg(userId, IMUtils.getSessionKey(userId, remoteId), msgs.get(0).msgId);
  82. }
  83. break;
  84. case Group:
  85. String groupId = request.groupId;
  86. if (StringUtils.isBlank(groupId)) {
  87. logger.error("Can not find corresponding groupId id for PullOldMsgRequest: {}", json);
  88. throw new IMException("Cannot find groupId id for PullOldMsgRequest: " + json);
  89. }
  90. List<GroupMessageEntity> groupDaos = groupMessageDAO.findMsg(groupId, request.maxMsgId, request.minMsgId, request.limit);
  91. for (GroupMessageEntity dao : groupDaos) {
  92. String fromUserId = Long.toString(dao.FromUserID);
  93. Message m = Message.convertToMsg(dao, fromUserId, groupId);
  94. msgs.add(m);
  95. }
  96. if (shouldCleanUnread && !msgs.isEmpty()) {
  97. inboxService.removeGroupMsg(userId, groupId, msgs.get(0).msgId);
  98. }
  99. break;
  100. default:
  101. logger.error("Message type {} is not handled in SyncUnreadCallback. PullOldMsgRequest: {}", type.getValue(), json);
  102. throw new IMException("Unhandled MessageType " + type.getValue() + " for PullOldMsgCallback");
  103. }
  104. sendSuccessResponse(userId, request, msgs);
  105. } catch (Exception e) {
  106. logger.error("Pull old message handler fail with exception. Send fail response. Message: " + json, e);
  107. sendFailResponse(userId, request, e.getMessage());
  108. }
  109. } catch (Exception e) {
  110. throw new IMConsumerException(e, json);
  111. }
  112. }
  113. private void sendSuccessResponse(String userId, PullOldMsgRequest request, List<Message> result) {
  114. PullOldMsgResponse response = new PullOldMsgResponse();
  115. response.rId = request.rId;
  116. response.userId = request.userId;
  117. response.remoteId = request.remoteId;
  118. response.groupId = request.groupId;
  119. response.msgs = result.toArray(new Message[0]);
  120. response.type = request.type;
  121. response.maxMsgIdInRequest = (request.maxMsgId == Long.MAX_VALUE) ? -1 : request.maxMsgId;
  122. response.requestType = request.requestType;
  123. response.status = 200;
  124. outboxService.put(userId, response.toWrapperJson());
  125. }
  126. private void sendFailResponse(String userId, PullOldMsgRequest request, String errMsg) {
  127. PullOldMsgResponse errResp = new PullOldMsgResponse();
  128. errResp.rId = request.rId;
  129. errResp.userId = request.userId;
  130. errResp.remoteId = request.remoteId;
  131. errResp.groupId = request.groupId;
  132. errResp.msgs = new Message[0];
  133. errResp.type = request.type;
  134. errResp.maxMsgIdInRequest = (request.maxMsgId == Long.MAX_VALUE) ? -1 : request.maxMsgId;
  135. errResp.requestType = request.requestType;
  136. errResp.status = 500;
  137. errResp.errMsg = errMsg;
  138. outboxService.put(userId, errResp.toWrapperJson());
  139. }
  140. }