PageRenderTime 310ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/Mr.Tomato/linbox_server
Java | 177 lines | 133 code | 41 blank | 3 comment | 16 complexity | 504096fdc6f72df5b632a4fe5e960ed0 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.utils.IMUtils;
  10. import com.linbox.im.server.storage.UnreadLoopData;
  11. import org.apache.commons.lang.StringUtils;
  12. import org.apache.kafka.clients.consumer.ConsumerRecord;
  13. import org.slf4j.Logger;
  14. import org.slf4j.LoggerFactory;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.beans.factory.annotation.Qualifier;
  17. import org.springframework.stereotype.Service;
  18. import java.util.ArrayList;
  19. import java.util.LinkedList;
  20. import java.util.List;
  21. /**
  22. * Created by lrsec on 7/2/15.
  23. */
  24. @Service
  25. @Qualifier("syncUnreadHandler")
  26. public class SyncUnreadHandler implements Handler<String, String> {
  27. private static Logger logger = LoggerFactory.getLogger(SyncUnreadHandler.class);
  28. @Autowired
  29. private IInboxService inboxService;
  30. @Autowired
  31. private IOutboxService outboxService;
  32. @Override
  33. public void handle(ConsumerRecord<String, String> record) {
  34. String json = record.value();
  35. try {
  36. logger.debug("Start handling SyncUnreadRequest: {}", json);
  37. MessageWrapper wrapper = JSON.parseObject(json, MessageWrapper.class);
  38. SyncUnreadRequest request = JSON.parseObject(((JSONObject)wrapper.content).toJSONString(), SyncUnreadRequest.class);
  39. wrapper.content = request;
  40. if(request == null) {
  41. logger.error("SyncUnreadRequest format is not correct. Json string: {}", json);
  42. return;
  43. }
  44. String userId = request.userId;
  45. if(StringUtils.isBlank(userId)) {
  46. logger.error("Can not find avaiable user id for SyncUnreadRequest {}", json);
  47. return;
  48. }
  49. try {
  50. MessageType type = MessageType.parse(request.type);
  51. if (type == null) {
  52. logger.error("Can not parse MessageType in SyncUnreadRequest {}", json);
  53. throw new IMException("Unknown MessageType " + Integer.toString(request.type));
  54. }
  55. List<String> unreadMsgs = new LinkedList<>();
  56. long nextOffset = 0;
  57. switch(type) {
  58. case All:
  59. UnreadLoopData data = inboxService.getAllJson(userId, request.offset, request.limit);
  60. unreadMsgs.addAll(data.unreads);
  61. nextOffset = data.nextOffset;
  62. break;
  63. case Session:
  64. if(request.remoteId == null) {
  65. logger.error("The remote chat id is null when getting session unread message for user {}", userId);
  66. throw new IMException("The remote chat id is null when getting session unread message for user " + userId);
  67. } else {
  68. String remoteId = request.remoteId;
  69. if (StringUtils.isBlank(remoteId)) {
  70. logger.error("Can not find remote id for SyncUnreadRequest {}", json);
  71. throw new IMException("Non existing SyncUnreadRequest " + json);
  72. }
  73. unreadMsgs.add(inboxService.getSessionJson(userId, IMUtils.getSessionKey(userId, remoteId)));
  74. }
  75. break;
  76. case Group:
  77. if (request.groupId == null) {
  78. logger.error("The group id is null when getting group unread message for user {}", userId);
  79. throw new IMException("The group id is null when getting group unread message for user " + userId);
  80. } else {
  81. String groupId = request.groupId;
  82. if (StringUtils.isBlank(groupId)) {
  83. logger.error("Can not find group id for SyncUnreadRequest " + json);
  84. throw new IMException("Non existing SyncUnreadRequest " + json);
  85. }
  86. unreadMsgs.add(inboxService.getGroupJson(userId, groupId));
  87. }
  88. break;
  89. default:
  90. logger.error("Message type {} is not handled in SyncUnreadCallback. SyncUnreadRequest: {}", type.getValue(), json);
  91. throw new IMException("Unhandled MessageType " + type.getValue() + " for SyncUnreadCallback");
  92. }
  93. sendSuccessResponse(userId, request, unreadMsgs, nextOffset);
  94. } catch(Exception e) {
  95. logger.error("Exception when handling SyncUnreadCallback.", e);
  96. sendFailResponse(userId, request, e.getMessage());
  97. }
  98. } catch (Exception e) {
  99. throw new IMConsumerException(e, json);
  100. }
  101. }
  102. private void sendSuccessResponse(String userId, SyncUnreadRequest request, List<String> result, long nextOffset) {
  103. List<UnreadMsg> msgs = new ArrayList<>(result.size());
  104. for (String r : result) {
  105. UnreadMsg m = JSON.parseObject(r, UnreadMsg.class);
  106. if (m == null) {
  107. logger.error("Can not parse UnreadMsg from json: {}", r);
  108. continue;
  109. }
  110. msgs.add(m);
  111. }
  112. SyncUnreadResponse response = new SyncUnreadResponse();
  113. response.rId = request.rId;
  114. response.userId = request.userId;
  115. response.remoteId = request.remoteId;
  116. response.groupId = request.groupId;
  117. response.type = request.type;
  118. response.offset = nextOffset;
  119. response.unreads = msgs.toArray(new UnreadMsg[0]);
  120. response.status = 200;
  121. outboxService.put(userId, response.toWrapperJson());
  122. }
  123. private void sendFailResponse(String userId, SyncUnreadRequest request, String errMsg) {
  124. SyncUnreadResponse errResp = new SyncUnreadResponse();
  125. errResp.rId = request.rId;
  126. errResp.userId = request.userId;
  127. errResp.remoteId = request.remoteId;
  128. errResp.groupId = request.groupId;
  129. errResp.type = request.type;
  130. errResp.offset = 0;
  131. errResp.unreads = new UnreadMsg[0];
  132. errResp.status = 500;
  133. errResp.errMsg = errMsg;
  134. outboxService.put(userId, errResp.toWrapperJson());
  135. }
  136. }