From 94a8ade8eddb1ea9bdc1d21c9aee43b2d39786d2 Mon Sep 17 00:00:00 2001
From: cdp <1>
Date: Tue, 14 May 2019 16:45:19 +0800
Subject: [PATCH 1/3] Add qiye_weixin send feature.
---
escheduler-alert/pom.xml | 11 ++
.../cn/escheduler/alert/utils/Constants.java | 12 ++
.../cn/escheduler/alert/utils/FuncUtils.java | 18 ++
.../alert/utils/QiyeWeixinUtils.java | 167 ++++++++++++++++++
.../src/main/resources/alert.properties | 7 +
.../alert/utils/QiyeWeixinUtilsTest.java | 110 ++++++++++++
6 files changed, 325 insertions(+)
create mode 100644 escheduler-alert/src/main/java/cn/escheduler/alert/utils/FuncUtils.java
create mode 100644 escheduler-alert/src/main/java/cn/escheduler/alert/utils/QiyeWeixinUtils.java
create mode 100644 escheduler-alert/src/test/java/cn/escheduler/alert/utils/QiyeWeixinUtilsTest.java
diff --git a/escheduler-alert/pom.xml b/escheduler-alert/pom.xml
index f066bc4b72..cb7d2db4bc 100644
--- a/escheduler-alert/pom.xml
+++ b/escheduler-alert/pom.xml
@@ -102,6 +102,17 @@
escheduler-dao
+
+ org.apache.httpcomponents
+ httpcore
+ 4.4.6
+
+
+ org.apache.httpcomponents
+ httpclient
+ 4.5.5
+
+
diff --git a/escheduler-alert/src/main/java/cn/escheduler/alert/utils/Constants.java b/escheduler-alert/src/main/java/cn/escheduler/alert/utils/Constants.java
index 07d1866a5d..cc2530826f 100644
--- a/escheduler-alert/src/main/java/cn/escheduler/alert/utils/Constants.java
+++ b/escheduler-alert/src/main/java/cn/escheduler/alert/utils/Constants.java
@@ -126,4 +126,16 @@ public class Constants {
public static final String TH_END = "";
public static final int ALERT_SCAN_INTERVEL = 5000;
+
+ public static final String QIYE_WEIXIN_CORP_ID = "qiye.weixin.corp.id";
+
+ public static final String QIYE_WEIXIN_SECRET = "qiye.weixin.secret";
+
+ public static final String QIYE_WEIXIN_TOKEN_URL = "qiye.weixin.token.url";
+
+ public static final String QIYE_WEIXIN_PUSH_URL = "qiye.weixin.push.url";
+
+ public static final String QIYE_WEIXIN_TEAM_SEND_MSG = "qiye.weixin.team.send.msg";
+
+ public static final String QIYE_WEIXIN_USER_SEND_MSG = "qiye.weixin.user.send.msg";
}
diff --git a/escheduler-alert/src/main/java/cn/escheduler/alert/utils/FuncUtils.java b/escheduler-alert/src/main/java/cn/escheduler/alert/utils/FuncUtils.java
new file mode 100644
index 0000000000..c973e1de9b
--- /dev/null
+++ b/escheduler-alert/src/main/java/cn/escheduler/alert/utils/FuncUtils.java
@@ -0,0 +1,18 @@
+package cn.escheduler.alert.utils;
+
+public class FuncUtils {
+
+ static public String mkString(Iterable list, String split) {
+ StringBuilder sb = new StringBuilder();
+ boolean first = true;
+ for (String item : list) {
+ if (first)
+ first = false;
+ else
+ sb.append(split);
+ sb.append(item);
+ }
+ return sb.toString();
+ }
+
+}
diff --git a/escheduler-alert/src/main/java/cn/escheduler/alert/utils/QiyeWeixinUtils.java b/escheduler-alert/src/main/java/cn/escheduler/alert/utils/QiyeWeixinUtils.java
new file mode 100644
index 0000000000..5af6baa4fc
--- /dev/null
+++ b/escheduler-alert/src/main/java/cn/escheduler/alert/utils/QiyeWeixinUtils.java
@@ -0,0 +1,167 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package cn.escheduler.alert.utils;
+
+import com.alibaba.fastjson.JSON;
+
+import com.google.common.reflect.TypeToken;
+import org.apache.http.HttpEntity;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.util.EntityUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Map;
+
+import static cn.escheduler.alert.utils.PropertyUtils.getString;
+
+/**
+ * qiye weixin utils
+ */
+public class QiyeWeixinUtils {
+
+ public static final Logger logger = LoggerFactory.getLogger(QiyeWeixinUtils.class);
+
+ private static final String qiyeWeixinCorpId = getString(Constants.QIYE_WEIXIN_CORP_ID);
+
+ private static final String qiyeWeixinSecret = getString(Constants.QIYE_WEIXIN_SECRET);
+
+ private static final String qiyeWeixinTokenUrl = getString(Constants.QIYE_WEIXIN_TOKEN_URL);
+ private String qiyeWeixinTokenUrlReplace = qiyeWeixinTokenUrl
+ .replaceAll("\\$weixinCorpId", qiyeWeixinCorpId)
+ .replaceAll("\\$weixinSecret", qiyeWeixinSecret);
+
+ private static final String qiyeWeixinPushUrl = getString(Constants.QIYE_WEIXIN_PUSH_URL);
+
+ private static final String qiyeWeixinTeamSendMsg = getString(Constants.QIYE_WEIXIN_TEAM_SEND_MSG);
+
+ private static final String qiyeWeixinUserSendMsg = getString(Constants.QIYE_WEIXIN_USER_SEND_MSG);
+
+ /**
+ * get winxin token info
+ * @return token string info
+ * @throws IOException
+ */
+ public String getToken() throws IOException {
+ String resp;
+
+ CloseableHttpClient httpClient = HttpClients.createDefault();
+ HttpGet httpGet = new HttpGet(qiyeWeixinTokenUrlReplace);
+ CloseableHttpResponse response = httpClient.execute(httpGet);
+ try {
+ HttpEntity entity = response.getEntity();
+ resp = EntityUtils.toString(entity, "utf-8");
+ EntityUtils.consume(entity);
+ } finally {
+ response.close();
+ }
+
+ Map map = JSON.parseObject(resp,
+ new TypeToken