From a9e92c5db0c88a19ea8a27f60fd55ae7e0bf76ee Mon Sep 17 00:00:00 2001 From: huangfeng Date: Tue, 28 May 2024 13:37:08 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E9=92=89=E9=92=89?= =?UTF-8?q?=E5=8F=91=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 6 ++ .../com/xydl/cac/util/DingTalkPushUtil.java | 53 +++++++++++ .../java/com/xydl/cac/util/JSONProcessor.java | 87 +++++++++++++++++++ src/main/java/com/xydl/cac/util/JSONUtil.java | 60 +++++++++++++ 4 files changed, 206 insertions(+) create mode 100644 src/main/java/com/xydl/cac/util/DingTalkPushUtil.java create mode 100644 src/main/java/com/xydl/cac/util/JSONProcessor.java create mode 100644 src/main/java/com/xydl/cac/util/JSONUtil.java diff --git a/pom.xml b/pom.xml index 5f8836f..7c3deeb 100644 --- a/pom.xml +++ b/pom.xml @@ -142,6 +142,12 @@ querydsl-apt ${querydsl.version} + + + com.aliyun + alibaba-dingtalk-service-sdk + 2.0.0 + diff --git a/src/main/java/com/xydl/cac/util/DingTalkPushUtil.java b/src/main/java/com/xydl/cac/util/DingTalkPushUtil.java new file mode 100644 index 0000000..1992438 --- /dev/null +++ b/src/main/java/com/xydl/cac/util/DingTalkPushUtil.java @@ -0,0 +1,53 @@ +package com.xydl.cac.util; + +import com.dingtalk.api.DefaultDingTalkClient; +import com.dingtalk.api.DingTalkClient; +import com.dingtalk.api.request.OapiRobotSendRequest; +import com.dingtalk.api.response.OapiRobotSendResponse; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.codec.binary.Base64; + +import javax.crypto.Mac; +import javax.crypto.spec.SecretKeySpec; +import java.net.URLEncoder; + +/** + * 钉钉推送工具类 + */ +@Slf4j +public class DingTalkPushUtil { + + public static final String CUSTOM_ROBOT_TOKEN = "b0571a2261a96ac2346c3c913e25783e0d7994bf1bff7e64e5a5e171eeccf1f8"; + public static final String SECRET = "SEC0757cffdf63deea00a57f718f4540e5d97ffe887509d199403c06d9d00c0a158"; + public static final String KEY_WORD = "运维告警"; + + + public static void pushText(String content) { + try { + Long timestamp = System.currentTimeMillis(); + String stringToSign = timestamp + "\n" + SECRET; + Mac mac = Mac.getInstance("HmacSHA256"); + mac.init(new SecretKeySpec(SECRET.getBytes("UTF-8"), "HmacSHA256")); + byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8")); + String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8"); + + //sign字段和timestamp字段必须拼接到请求URL上,否则会出现 310000 的错误信息 + DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/robot/send?sign=" + sign + "×tamp=" + timestamp); + OapiRobotSendRequest req = new OapiRobotSendRequest(); + /** + * 发送文本消息 + */ + //定义文本内容 + OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text(); + text.setContent(KEY_WORD + content); + //设置消息类型 + req.setMsgtype("text"); + req.setText(text); + OapiRobotSendResponse rsp = client.execute(req, CUSTOM_ROBOT_TOKEN); + } catch (Exception e) { + log.error("钉钉发送失败", e); + } + } + +} + diff --git a/src/main/java/com/xydl/cac/util/JSONProcessor.java b/src/main/java/com/xydl/cac/util/JSONProcessor.java new file mode 100644 index 0000000..3fd6546 --- /dev/null +++ b/src/main/java/com/xydl/cac/util/JSONProcessor.java @@ -0,0 +1,87 @@ +package com.xydl.cac.util; + +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.MapperFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException; + +import java.io.File; +import java.io.IOException; +import java.util.Map; + +public class JSONProcessor { + + + public Map fromJSON2Map(String json) throws JsonParseException, JsonMappingException, IOException { + ObjectMapper mapper = new ObjectMapper(); + @SuppressWarnings("unchecked") + Map productMap = mapper.readValue(json, Map.class); + return productMap; + } + + public Map fromJSON2Map(Object obj) throws JsonParseException, JsonMappingException, IOException { + ObjectMapper mapper = new ObjectMapper(); + @SuppressWarnings("unchecked") + Map productMap = mapper.convertValue(obj, Map.class); + return productMap; + } + + public String buildJSONFromJSONObject(Object obj) { + String jsonInString = null; + ObjectMapper mapper = new ObjectMapper(); + try { + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true); + mapper.setSerializationInclusion(Include.NON_NULL); + jsonInString = mapper.writeValueAsString(obj); + } catch (Exception e) { +// LOG.error("JSON transform failed. ", e); + } + return jsonInString; + } + + public String buildJSONFromJSONObject(Object obj, boolean prettyPrinter) { + String jsonInString = null; + ObjectMapper mapper = new ObjectMapper(); + try { + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true); + mapper.setSerializationInclusion(Include.NON_NULL); + if (prettyPrinter) { + jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj); + } else { + jsonInString = mapper.writeValueAsString(obj); + } + } catch (Exception e) { +// LOG.error("JSON transform failed. ", e); + } + return jsonInString; + } + + public T buildJSONObjectFromJSON(String json, Class clazz) throws Exception { + ObjectMapper mapper = new ObjectMapper(); + try { + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + return mapper.readValue(json, clazz); + } catch (UnrecognizedPropertyException e) { + throw new Exception(e.getPropertyName(), e); + } catch (Exception e) { + throw new Exception("JSON Object transform failed. ", e); + } + } + + public T buildJSONObjectFromJSON(File file, Class clazz) throws Exception { + ObjectMapper mapper = new ObjectMapper(); + try { + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + return mapper.readValue(file, clazz); + } catch (UnrecognizedPropertyException e) { + throw new Exception(e.getPropertyName(), e); + } catch (Exception e) { + throw new Exception("JSON Object transform failed. ", e); + } + } +} diff --git a/src/main/java/com/xydl/cac/util/JSONUtil.java b/src/main/java/com/xydl/cac/util/JSONUtil.java new file mode 100644 index 0000000..205aaa8 --- /dev/null +++ b/src/main/java/com/xydl/cac/util/JSONUtil.java @@ -0,0 +1,60 @@ +package com.xydl.cac.util; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.dataformat.xml.XmlMapper; + +import java.io.File; +import java.io.IOException; +import java.util.Map; + + +public class JSONUtil { + + private static JSONProcessor jSONProcessor = new JSONProcessor(); + + public static String object2Json(Object obj) { + return jSONProcessor.buildJSONFromJSONObject(obj); + } + + public static String object2Json(Object obj, boolean prettyPrinter) { + return jSONProcessor.buildJSONFromJSONObject(obj, prettyPrinter); + } + + public static T json2Object(String json, Class clazz) throws Exception { + return jSONProcessor.buildJSONObjectFromJSON(json, clazz); + } + + public static T file2Object(File file, Class clazz) throws Exception { + return jSONProcessor.buildJSONObjectFromJSON(file, clazz); + } + + public static Map object2Map(Object obj) { + try { + return jSONProcessor.fromJSON2Map(obj); + } catch (IOException e) { + return null; + } + } + + public static String object2Xml(Object obj) throws JsonProcessingException { + XmlMapper xmlMapper = XmlMapper.builder() + .enable(SerializationFeature.INDENT_OUTPUT) + .build(); + return xmlMapper.writeValueAsString(obj); + } + + public static T xml2Object(String xml, Class clazz) throws JsonProcessingException { + XmlMapper xmlMapper = XmlMapper.builder() + .build(); + return xmlMapper.readValue(xml, clazz); + } + + public static JsonNode xml2JsonNode(String xml) throws JsonProcessingException { + XmlMapper xmlMapper = XmlMapper.builder() + .build(); + return xmlMapper.readTree(xml); + } + +}