|
|
@@ -10,8 +10,8 @@ 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.client.methods.HttpUriRequest;
|
|
|
import org.apache.http.concurrent.FutureCallback;
|
|
|
import org.apache.http.entity.StringEntity;
|
|
|
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
|
|
|
@@ -27,13 +27,15 @@ import org.springframework.http.HttpMethod;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.goafanti.common.utils.LoggerUtils;
|
|
|
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);
|
|
|
+ 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);
|
|
|
+ private static final Optional<Integer> REACH_LIMIT = Optional.of(429);
|
|
|
|
|
|
@Value(value = "${easemob.client.url}")
|
|
|
private String clientUrl;
|
|
|
@@ -83,26 +85,29 @@ public class EasemobUtils implements InitializingBean, DisposableBean {
|
|
|
HttpUriRequest req = buildRequest(info);
|
|
|
CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
|
|
|
httpclient.start();
|
|
|
- logger.debug(req.toString());
|
|
|
+ LoggerUtils.debug(logger, "SEND: [%s] - [%s]", info.getMethod(), info.getUri());
|
|
|
Future<HttpResponse> future = httpclient.execute(req, null);
|
|
|
try {
|
|
|
HttpResponse response = future.get();
|
|
|
- if (info.isWithAuth() && isUnauth(response)) {
|
|
|
+ Optional<Integer> rescode = getResCode(response);
|
|
|
+ if (info.isWithAuth() && isUnauth(rescode)) {
|
|
|
auth();
|
|
|
+ } else if (isReachLimit(rescode)) {
|
|
|
+ LoggerUtils.debug(logger, "Reach Easemob API limitation!");
|
|
|
} 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);
|
|
|
+ LoggerUtils.debug(logger, e.getMessage(), e);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ LoggerUtils.debug(logger, response);
|
|
|
} catch (InterruptedException | ExecutionException e) {
|
|
|
- logger.error(e.getMessage(), e);
|
|
|
+ LoggerUtils.debug(logger, e.getMessage(), e);
|
|
|
} finally {
|
|
|
try {
|
|
|
httpclient.close();
|
|
|
@@ -120,7 +125,7 @@ public class EasemobUtils implements InitializingBean, DisposableBean {
|
|
|
HttpUriRequest req = buildRequest(info);
|
|
|
CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
|
|
|
httpclient.start();
|
|
|
- logger.debug(req.toString());
|
|
|
+ LoggerUtils.debug(logger, "SEND: [%s] - [%s]", info.getMethod(), info.getUri());
|
|
|
httpclient.execute(req, new FutureCallback<HttpResponse>() {
|
|
|
@Override
|
|
|
public void failed(Exception ex) {
|
|
|
@@ -130,29 +135,21 @@ public class EasemobUtils implements InitializingBean, DisposableBean {
|
|
|
|
|
|
@Override
|
|
|
public void completed(HttpResponse result) {
|
|
|
- if (info.isWithAuth() && isUnauth(result)) {
|
|
|
+ Optional<Integer> rescode = getResCode(result);
|
|
|
+ if (info.isWithAuth() && isUnauth(rescode)) {
|
|
|
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) {
|
|
|
+ } else if (isReachLimit(rescode)) {
|
|
|
+ LoggerUtils.debug(logger, "Reach Easemob API limitation!");
|
|
|
+ sendLater(info.tryCount(info.getTryCount() + 1));
|
|
|
}
|
|
|
+ LoggerUtils.debug(logger, result);
|
|
|
close();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void cancelled() {
|
|
|
close();
|
|
|
- logger.debug(req.toString() + "cancelled!");
|
|
|
+ LoggerUtils.debug(logger, "CANCELLED: [%s] - [%s]", req.getMethod(), req.getURI());
|
|
|
}
|
|
|
|
|
|
private void close() {
|
|
|
@@ -164,14 +161,33 @@ public class EasemobUtils implements InitializingBean, DisposableBean {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- private boolean isUnauth(HttpResponse response) {
|
|
|
- return UN_AUTH
|
|
|
- .equals(Optional.ofNullable(response).map(res -> res.getStatusLine()).map(sl -> sl.getStatusCode()));
|
|
|
+ public void sendMessage(String from, String to, String msg, Object... value) {
|
|
|
+ JSONObject message = new JSONObject();
|
|
|
+ message.put("type", "txt");
|
|
|
+ message.put("msg", String.format(msg, value));
|
|
|
+ JSONObject jo = new JSONObject();
|
|
|
+ jo.put("target_type", "users");
|
|
|
+ jo.put("target", new String[] { to });
|
|
|
+ jo.put("msg", message);
|
|
|
+ jo.put("from", from);
|
|
|
+ sendAsync(new EasemobInfo().uri("/messages").data(jo.toJSONString()).method(HttpMethod.POST));
|
|
|
+ }
|
|
|
+
|
|
|
+ private Optional<Integer> getResCode(HttpResponse response) {
|
|
|
+ return Optional.ofNullable(response).map(res -> res.getStatusLine()).map(sl -> sl.getStatusCode());
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean isUnauth(Optional<Integer> responseStatus) {
|
|
|
+ return UN_AUTH.equals(responseStatus);
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean isReachLimit(Optional<Integer> responseStatus) {
|
|
|
+ return REACH_LIMIT.equals(responseStatus);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void destroy() throws Exception {
|
|
|
- logger.debug("消息系统关闭");
|
|
|
+ LoggerUtils.debug(logger, "消息系统关闭");
|
|
|
}
|
|
|
|
|
|
@Override
|