|
@@ -0,0 +1,182 @@
|
|
|
|
|
+package com.goafanti.easemob;
|
|
|
|
|
+
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
|
+import java.util.Optional;
|
|
|
|
|
+import java.util.concurrent.ExecutionException;
|
|
|
|
|
+import java.util.concurrent.Future;
|
|
|
|
|
+
|
|
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
|
|
+import org.apache.http.HttpResponse;
|
|
|
|
|
+import org.apache.http.client.config.RequestConfig;
|
|
|
|
|
+import org.apache.http.client.methods.HttpGet;
|
|
|
|
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
|
|
+import org.apache.http.client.methods.HttpUriRequest;
|
|
|
|
|
+import org.apache.http.client.methods.HttpRequestBase;
|
|
|
|
|
+import org.apache.http.concurrent.FutureCallback;
|
|
|
|
|
+import org.apache.http.entity.StringEntity;
|
|
|
|
|
+import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
|
|
|
|
|
+import org.apache.http.impl.nio.client.HttpAsyncClients;
|
|
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
|
+import org.springframework.beans.factory.DisposableBean;
|
|
|
|
|
+import org.springframework.beans.factory.InitializingBean;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
+import org.springframework.http.HttpMethod;
|
|
|
|
|
+
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
+import com.goafanti.easemob.bo.EasemobInfo;
|
|
|
|
|
+import com.goafanti.easemob.queue.EasemobRedisQueue;
|
|
|
|
|
+
|
|
|
|
|
+public class EasemobUtils implements InitializingBean, DisposableBean {
|
|
|
|
|
+ private static final String UTF_8 = "UTF-8";
|
|
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(EasemobUtils.class);
|
|
|
|
|
+ private static final Optional<Integer> UN_AUTH = Optional.of(401);
|
|
|
|
|
+
|
|
|
|
|
+ @Value(value = "${easemob.client.url}")
|
|
|
|
|
+ private String clientUrl;
|
|
|
|
|
+ @Value(value = "${easemob.client.id}")
|
|
|
|
|
+ private String clientId;
|
|
|
|
|
+ @Value(value = "${easemob.client.secret}")
|
|
|
|
|
+ private String clientSecret;
|
|
|
|
|
+
|
|
|
|
|
+ private String token;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private EasemobRedisQueue jedisQueue;
|
|
|
|
|
+
|
|
|
|
|
+ private void auth() {
|
|
|
|
|
+ JSONObject jo = new JSONObject();
|
|
|
|
|
+ jo.put("grant_type", "client_credentials");
|
|
|
|
|
+ jo.put("client_id", clientId);
|
|
|
|
|
+ jo.put("client_secret", clientSecret);
|
|
|
|
|
+ JSONObject res = send(
|
|
|
|
|
+ new EasemobInfo().uri("/token").data(jo.toJSONString()).method(HttpMethod.POST).withAuth(false));
|
|
|
|
|
+ if (res != null) {
|
|
|
|
|
+ token = res.getString("access_token");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private HttpUriRequest buildRequest(EasemobInfo info) {
|
|
|
|
|
+ HttpUriRequest req = null;
|
|
|
|
|
+ switch (info.getMethod()) {
|
|
|
|
|
+ case POST:
|
|
|
|
|
+ req = new HttpPost(clientUrl + info.getUri());
|
|
|
|
|
+ ((HttpPost) req).setEntity(new StringEntity(info.getData(), UTF_8));
|
|
|
|
|
+ break;
|
|
|
|
|
+ default:
|
|
|
|
|
+ req = new HttpGet(clientUrl + info.getUri());
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (info.isWithAuth()) {
|
|
|
|
|
+ req.addHeader("Authorization", "Bearer " + token);
|
|
|
|
|
+ }
|
|
|
|
|
+ ((HttpRequestBase) req).setConfig(RequestConfig.custom().setConnectTimeout(10000)
|
|
|
|
|
+ .setConnectionRequestTimeout(10000).setSocketTimeout(10000).build());
|
|
|
|
|
+ req.addHeader("Content-Type", "application/json");
|
|
|
|
|
+ return req;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public JSONObject send(EasemobInfo info) {
|
|
|
|
|
+ HttpUriRequest req = buildRequest(info);
|
|
|
|
|
+ CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
|
|
|
|
|
+ httpclient.start();
|
|
|
|
|
+ logger.debug(req.toString());
|
|
|
|
|
+ Future<HttpResponse> future = httpclient.execute(req, null);
|
|
|
|
|
+ try {
|
|
|
|
|
+ HttpResponse response = future.get();
|
|
|
|
|
+ if (info.isWithAuth() && isUnauth(response)) {
|
|
|
|
|
+ auth();
|
|
|
|
|
+ } else {
|
|
|
|
|
+ HttpEntity entity = response.getEntity();
|
|
|
|
|
+ if (entity != null) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ String resStr = EntityUtils.toString(entity, UTF_8);
|
|
|
|
|
+ logger.debug(resStr);
|
|
|
|
|
+ return (JSONObject) JSON.parse(resStr);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ logger.error(e.getMessage(), e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (InterruptedException | ExecutionException e) {
|
|
|
|
|
+ logger.error(e.getMessage(), e);
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ try {
|
|
|
|
|
+ httpclient.close();
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return info.getTryCount() > 1 ? null : send(info.tryCount(info.getTryCount() + 1));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void sendLater(EasemobInfo info) {
|
|
|
|
|
+ jedisQueue.pushFromTail(info);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void sendAsync(EasemobInfo info) {
|
|
|
|
|
+ HttpUriRequest req = buildRequest(info);
|
|
|
|
|
+ CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
|
|
|
|
|
+ httpclient.start();
|
|
|
|
|
+ logger.debug(req.toString());
|
|
|
|
|
+ httpclient.execute(req, new FutureCallback<HttpResponse>() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void failed(Exception ex) {
|
|
|
|
|
+ sendLater(info.tryCount(info.getTryCount() + 1));
|
|
|
|
|
+ close();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void completed(HttpResponse result) {
|
|
|
|
|
+ if (info.isWithAuth() && isUnauth(result)) {
|
|
|
|
|
+ auth();
|
|
|
|
|
+ } else {
|
|
|
|
|
+ HttpEntity entity = result.getEntity();
|
|
|
|
|
+ if (entity != null) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ logger.debug(EntityUtils.toString(entity, UTF_8));
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ logger.error(e.getMessage(), e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ try {
|
|
|
|
|
+ httpclient.close();
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ }
|
|
|
|
|
+ close();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void cancelled() {
|
|
|
|
|
+ close();
|
|
|
|
|
+ logger.debug(req.toString() + "cancelled!");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void close() {
|
|
|
|
|
+ try {
|
|
|
|
|
+ httpclient.close();
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private boolean isUnauth(HttpResponse response) {
|
|
|
|
|
+ return UN_AUTH
|
|
|
|
|
+ .equals(Optional.ofNullable(response).map(res -> res.getStatusLine()).map(sl -> sl.getStatusCode()));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void destroy() throws Exception {
|
|
|
|
|
+ logger.debug("消息系统关闭");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void afterPropertiesSet() throws Exception {
|
|
|
|
|
+ auth();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|