|
|
@@ -0,0 +1,162 @@
|
|
|
+package com.goafanti.message;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.Base64;
|
|
|
+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.HttpPost;
|
|
|
+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;
|
|
|
+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 com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.goafanti.common.utils.LoggerUtils;
|
|
|
+import com.goafanti.message.bo.JGMessage;
|
|
|
+import com.goafanti.message.bo.MessageAdapter;
|
|
|
+import com.goafanti.message.queue.MessageRedisQueue;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 极光推送
|
|
|
+ * @author Administrator
|
|
|
+ *
|
|
|
+ */
|
|
|
+public class JGMessageHelper implements InitializingBean, DisposableBean{
|
|
|
+ @Value(value = "${jiguang.appKey}")
|
|
|
+ private String appKey;
|
|
|
+ @Value(value = "${jiguang.masterSecret}")
|
|
|
+ private String masterSecret;
|
|
|
+ @Value(value = "${jiguang.pushUrl}")
|
|
|
+ private String pushUrl;
|
|
|
+ @Autowired
|
|
|
+ private MessageRedisQueue jedisQueue;
|
|
|
+ private static final Optional<Integer> UN_AUTH = Optional.of(401);//未验证
|
|
|
+ private static final Optional<Integer> NOT_SUITABLE = Optional.of(405);//不支持该请求方法
|
|
|
+ private static final Optional<Integer> EXCEED_THE_LIMIT = Optional.of(429);//超过频率限制
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(JGMessage.class);
|
|
|
+ public JSONObject send(MessageAdapter messageAdapter){
|
|
|
+ HttpUriRequest req = buildRequest(messageAdapter.getJgMessage());
|
|
|
+ CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
|
|
|
+ httpclient.start();
|
|
|
+ LoggerUtils.debug(logger, "SEND TO JGPUSH SERVER: [%s] - [%s]", req.getMethod(), req.getURI());
|
|
|
+ Future<HttpResponse> future = httpclient.execute(req, null);
|
|
|
+ try {
|
|
|
+ HttpResponse response = future.get();
|
|
|
+ Optional<Integer> rescode = getResCode(response);
|
|
|
+ if(rescode.equals(UN_AUTH)){
|
|
|
+ LoggerUtils.debug(logger, "Request Not Authorized");
|
|
|
+ }else if(rescode.equals(NOT_SUITABLE)){
|
|
|
+ LoggerUtils.debug(logger, "Request Not Allow");
|
|
|
+ }else if(rescode.equals(EXCEED_THE_LIMIT)){
|
|
|
+ LoggerUtils.debug(logger, "Request Too Frequent");
|
|
|
+ sendLater(messageAdapter);
|
|
|
+ }else{
|
|
|
+ HttpEntity entity = response.getEntity();
|
|
|
+ if (entity != null) {
|
|
|
+ try {
|
|
|
+ String resStr = EntityUtils.toString(entity, "UTF-8");
|
|
|
+ return (JSONObject) JSON.parse(resStr);
|
|
|
+ } catch (Exception e) {
|
|
|
+ LoggerUtils.debug(logger, e.getMessage(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ LoggerUtils.debug(logger, response);
|
|
|
+ } catch (InterruptedException | ExecutionException e) {
|
|
|
+ LoggerUtils.debug(logger, e.getMessage(), e);
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ httpclient.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return messageAdapter.getTryCount()>1?null:send(messageAdapter);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void sendLater(MessageAdapter messageAdapter) {
|
|
|
+ jedisQueue.pushFromTail(messageAdapter);
|
|
|
+ }
|
|
|
+
|
|
|
+ private HttpUriRequest buildRequest(JGMessage jgMessage){
|
|
|
+ String authorization = Base64.getEncoder().encodeToString((appKey+":"+pushUrl).getBytes());
|
|
|
+ HttpUriRequest req = new HttpPost(pushUrl);
|
|
|
+ req.addHeader("Authorization", "Basic "+authorization);
|
|
|
+ ((HttpPost) req).setEntity(new StringEntity(JSONObject.toJSONString(jgMessage), "UTF_8"));
|
|
|
+ ((HttpRequestBase) req).setConfig(RequestConfig.custom().setConnectTimeout(10000)
|
|
|
+ .setConnectionRequestTimeout(10000).setSocketTimeout(10000).build());
|
|
|
+ req.addHeader("Content-Type", "application/json");
|
|
|
+ return req;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Optional<Integer> getResCode(HttpResponse response) {
|
|
|
+ return Optional.ofNullable(response).map(res -> res.getStatusLine()).map(sl -> sl.getStatusCode());
|
|
|
+ }
|
|
|
+
|
|
|
+ public void sendAsync(MessageAdapter messageAdapter) {
|
|
|
+ HttpUriRequest req = buildRequest(messageAdapter.getJgMessage());
|
|
|
+ CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
|
|
|
+ httpclient.start();
|
|
|
+ LoggerUtils.debug(logger, "SEND TO JGPUSH SERVER: [%s] - [%s]", req.getMethod(), req.getURI());
|
|
|
+ httpclient.execute(req, new FutureCallback<HttpResponse>() {
|
|
|
+ @Override
|
|
|
+ public void failed(Exception ex) {
|
|
|
+ sendLater(messageAdapter.tryCount((messageAdapter.getTryCount()+1)));
|
|
|
+ close();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void completed(HttpResponse result) {
|
|
|
+ Optional<Integer> rescode = getResCode(result);
|
|
|
+ if(rescode.equals(UN_AUTH)){
|
|
|
+ LoggerUtils.debug(logger, "Request Not Authorized");
|
|
|
+ }else if(rescode.equals(NOT_SUITABLE)){
|
|
|
+ LoggerUtils.debug(logger, "Request Not Allow");
|
|
|
+ }else if(rescode.equals(EXCEED_THE_LIMIT)){
|
|
|
+ LoggerUtils.debug(logger, "Request Too Frequent");
|
|
|
+ sendLater(messageAdapter);
|
|
|
+ }
|
|
|
+ LoggerUtils.debug(logger, result);
|
|
|
+ close();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void cancelled() {
|
|
|
+ close();
|
|
|
+ LoggerUtils.debug(logger, "CANCELLED: [%s] - [%s]", req.getMethod(), req.getURI());
|
|
|
+ }
|
|
|
+
|
|
|
+ private void close() {
|
|
|
+ try {
|
|
|
+ httpclient.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void destroy() throws Exception {
|
|
|
+ LoggerUtils.debug(logger, "消息系统关闭");
|
|
|
+
|
|
|
+ }
|
|
|
+ @Override
|
|
|
+ public void afterPropertiesSet() throws Exception {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|