a7086308d5bf0557c8abd5b7ce84bc85573a96c7.svn-base 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package cn.com.goldenwater.dcproj.service.impl;
  2. import cn.com.goldenwater.dcproj.model.AgoraStudioDesc;
  3. import cn.com.goldenwater.dcproj.param.AgoraStudioDescParam;
  4. import cn.com.goldenwater.dcproj.service.AgoraChannelService;
  5. import cn.com.goldenwater.dcproj.service.AgoraStudioDescService;
  6. import com.alibaba.fastjson.JSON;
  7. import com.github.pagehelper.PageHelper;
  8. import com.github.pagehelper.PageInfo;
  9. import net.sf.json.JSONObject;
  10. import org.apache.commons.collections.MapUtils;
  11. import org.apache.commons.lang3.StringUtils;
  12. import org.apache.http.HttpResponse;
  13. import org.apache.http.client.methods.HttpGet;
  14. import org.apache.http.impl.client.CloseableHttpClient;
  15. import org.apache.http.impl.client.HttpClients;
  16. import org.apache.http.util.EntityUtils;
  17. import org.slf4j.Logger;
  18. import org.slf4j.LoggerFactory;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.stereotype.Service;
  21. import org.springframework.transaction.annotation.Transactional;
  22. import java.io.IOException;
  23. import java.util.*;
  24. /**
  25. * @author lhc
  26. * @date 2021-7-4
  27. */
  28. @Service
  29. @Transactional
  30. public class AgoraChannelServiceImpl implements AgoraChannelService {
  31. private Logger logger = LoggerFactory.getLogger(getClass());
  32. private static final String APP_ID = "6f592a3023ef4960a2d5342a6fc456e7";
  33. /**
  34. * 客户 ID
  35. */
  36. private static final String CUSTOMER_KEY = "77864af0d5e449ae9b8271a930aba2f4";
  37. /**
  38. * 客户密钥
  39. */
  40. private static final String CUSTOMER_SECRET = "216c9520cc974121806034bad52f533c";
  41. @Autowired
  42. private AgoraStudioDescService agoraStudioDescService;
  43. @Override
  44. public PageInfo<AgoraStudioDesc> pageList(AgoraStudioDescParam agoraStudioDescParam) {
  45. PageHelper.startPage(agoraStudioDescParam);
  46. List<AgoraStudioDesc> list = this.agoraStudioDescService.findList(agoraStudioDescParam);
  47. updateState(list);
  48. return new PageInfo<>(list);
  49. }
  50. private void updateState(List<AgoraStudioDesc> list) {
  51. Set<String> onlineChannelList = getOnlineChannel();
  52. if (null == onlineChannelList || onlineChannelList.size() == 0) {
  53. return;
  54. }
  55. list.forEach(agoraStudioDesc -> {
  56. if (onlineChannelList.contains(agoraStudioDesc.getChannelName())) {
  57. agoraStudioDesc.setState("1");
  58. }
  59. });
  60. }
  61. private Set<String> getOnlineChannel() {
  62. Set<String> rt = new HashSet<>();
  63. // 拼接客户 ID 和客户密钥并使用 base64 编码
  64. String plainCredentials = CUSTOMER_KEY + ":" + CUSTOMER_SECRET;
  65. String base64Credentials = new String(Base64.getEncoder().encode(plainCredentials.getBytes()));
  66. // 创建 authorization header
  67. String authorizationHeader = "Basic " + base64Credentials;
  68. try (CloseableHttpClient httpCilent = HttpClients.createDefault()) {
  69. HttpGet httpGet = new HttpGet("https://api.agora.io/dev/v1/channel/" + APP_ID);
  70. httpGet.setHeader("Accept", "application/json");
  71. httpGet.setHeader("Authorization", authorizationHeader);
  72. HttpResponse httpResponse = httpCilent.execute(httpGet);
  73. String result = EntityUtils.toString(httpResponse.getEntity());
  74. // "key": "{\"success\":true,\"data\":{\"channels\":[],\"total_size\":0}}"
  75. String channels = Optional.ofNullable(result)
  76. .map(m -> jsonToMap(m).get("data"))
  77. .map(m -> MapUtils.getString(jsonToMap(m), "channels")).orElse(null);
  78. if (StringUtils.isBlank(channels)) {
  79. return null;
  80. }
  81. List<String> channelList = JSON.parseArray(channels, String.class);
  82. return new HashSet<>(channelList);
  83. } catch (IOException e) {
  84. e.printStackTrace();
  85. }
  86. return null;
  87. }
  88. /**
  89. * json string 转换为 map 对象
  90. *
  91. * @param jsonObj
  92. * @return
  93. */
  94. public Map<Object, Object> jsonToMap(Object jsonObj) {
  95. JSONObject jsonObject = JSONObject.fromObject(jsonObj);
  96. return (Map<Object, Object>) jsonObject;
  97. }
  98. }