PageRenderTime 164ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/Mr.Tomato/linbox_server
Java | 133 lines | 99 code | 31 blank | 3 comment | 5 complexity | e81f62cf785e20d9b8864d05c5d154ff 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.MessageType;
  7. import com.linbox.im.server.service.IOutboxService;
  8. import com.linbox.im.message.MessageWrapper;
  9. import com.linbox.im.message.ReadAckRequest;
  10. import com.linbox.im.message.ReadAckResponse;
  11. import com.linbox.im.server.service.IInboxService;
  12. import com.linbox.im.utils.IMUtils;
  13. import org.apache.commons.lang.StringUtils;
  14. import org.apache.kafka.clients.consumer.ConsumerRecord;
  15. import org.slf4j.Logger;
  16. import org.slf4j.LoggerFactory;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.beans.factory.annotation.Qualifier;
  19. import org.springframework.stereotype.Service;
  20. /**
  21. * Created by lrsec on 7/2/15.
  22. */
  23. @Service
  24. @Qualifier("readAckHandler")
  25. public class ReadAckHandler implements Handler<String, String> {
  26. private static Logger logger = LoggerFactory.getLogger(ReadAckHandler.class);
  27. @Autowired
  28. private IInboxService inboxService;
  29. @Autowired
  30. private IOutboxService outboxService;
  31. @Override
  32. public void handle(ConsumerRecord<String, String> record) {
  33. String json = record.value();
  34. try {
  35. logger.debug("Start handling ReadAckRequest: {}", json);
  36. MessageWrapper wrapper = JSON.parseObject(json, MessageWrapper.class);
  37. ReadAckRequest request = JSON.parseObject(((JSONObject)wrapper.content).toJSONString(), ReadAckRequest.class);
  38. wrapper.content = request;
  39. String userId = request.userId;
  40. if(StringUtils.isBlank(userId)) {
  41. logger.error("Can not find avaiable user id for ReadAckRequest {}", json);
  42. return;
  43. }
  44. try {
  45. MessageType type = MessageType.parse(request.type);
  46. if (type == null) {
  47. logger.error("Can not parse MessageType in ReadAckRequest {}", json);
  48. throw new IMException("Unknown MessageType " + Integer.toString(request.type));
  49. }
  50. switch(type) {
  51. case Session:
  52. String remoteId = request.remoteId;
  53. if (StringUtils.isBlank(remoteId)) {
  54. logger.error("Can not find remote id for ReadAckRequest {}", json);
  55. throw new IMException("Non existing ReadAckRequest " + json);
  56. }
  57. inboxService.removeSessionMsg(userId, IMUtils.getSessionKey(userId, remoteId), request.msgId);
  58. sendSuccessResponse(userId, request);
  59. break;
  60. case Group:
  61. String groupId = request.groupId;
  62. if (StringUtils.isBlank(groupId)) {
  63. logger.error("Can not find group id for ReadAckRequest {}", json);
  64. throw new IMException("Non existing ReadAckRequest " + json);
  65. }
  66. inboxService.removeGroupMsg(userId, groupId, request.msgId);
  67. sendSuccessResponse(userId, request);
  68. break;
  69. default:
  70. logger.error("Message type {} is not handled in ReadAckCallback. ReadAckRequest: {}", type.getValue(), json);
  71. throw new IMException("Unhandled MessageType " + type.getValue() + " for ReadAckCallback");
  72. }
  73. } catch (Exception e) {
  74. logger.error("Exception when handling ReadAckRequest.", e);
  75. sendFailResponse(userId, request, e.getMessage());
  76. }
  77. } catch (Exception e) {
  78. throw new IMConsumerException(e, json);
  79. }
  80. }
  81. private void sendSuccessResponse(String userId, ReadAckRequest request) {
  82. ReadAckResponse response = new ReadAckResponse();
  83. response.rId = request.rId;
  84. response.userId = request.userId;
  85. response.remoteId = request.remoteId;
  86. response.groupId = request.groupId;
  87. response.type = request.type;
  88. response.status = 200;
  89. outboxService.put(userId, response.toWrapperJson());
  90. }
  91. private void sendFailResponse(String userId, ReadAckRequest request, String errMsg) {
  92. ReadAckResponse response = new ReadAckResponse();
  93. response.rId = request.rId;
  94. response.userId = request.userId;
  95. response.remoteId = request.remoteId;
  96. response.groupId = request.groupId;
  97. response.type = request.type;
  98. response.status = 500;
  99. response.errCode = errMsg;
  100. outboxService.put(userId, response.toWrapperJson());
  101. }
  102. }