All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.taotao.boot.dingtalk.model.DingerDefinitionHandler Maven / Gradle / Ivy
/*
* Copyright (c) 2020-2030, Shuigedeng ([email protected] & https://blog.taotaocloud.top/).
*
* Licensed 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
*
* https://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 com.taotao.boot.dingtalk.model;
import com.taotao.boot.dingtalk.annatations.DingerImageText;
import com.taotao.boot.dingtalk.annatations.DingerLink;
import com.taotao.boot.dingtalk.annatations.DingerMarkdown;
import com.taotao.boot.dingtalk.annatations.DingerText;
import com.taotao.boot.dingtalk.annatations.DingerTokenId;
import com.taotao.boot.dingtalk.entity.DingerRequest;
import com.taotao.boot.dingtalk.entity.MsgType;
import com.taotao.boot.dingtalk.enums.AsyncExecuteType;
import com.taotao.boot.dingtalk.enums.DingerDefinitionType;
import com.taotao.boot.dingtalk.enums.DingerType;
import com.taotao.boot.dingtalk.enums.MessageMainType;
import com.taotao.boot.dingtalk.enums.MessageSubType;
import com.taotao.boot.dingtalk.exception.DingerException;
import com.taotao.boot.dingtalk.xml.BodyTag;
import com.taotao.boot.dingtalk.xml.ConfigurationTag;
import com.taotao.boot.dingtalk.xml.ContentTag;
import com.taotao.boot.dingtalk.xml.MessageTag;
import com.taotao.boot.dingtalk.xml.PhoneTag;
import com.taotao.boot.dingtalk.xml.PhonesTag;
import com.taotao.boot.dingtalk.xml.TokenId;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import static com.taotao.boot.dingtalk.enums.ExceptionEnum.DINGERDEFINITIONTYPE_ERROR;
/**
* DefinitionGenerator
*
* @author shuigedeng
* @version 2022.07
* @since 2022-07-06 15:22:45
*/
public class DingerDefinitionHandler {
/** 企业微信@所有人标识 */
public static final String WETALK_AT_ALL = "@all";
/**
* 处理注解-Text定义的Dinger消息
*
* @param dingerType Dinger类型 {@link DingerType}
* @param context Dinger定义源
* @return dingerDefinition {@link DingerDefinition}
*/
protected static DingerDefinition dingerTextHandler(
DingerType dingerType, DingerDefinitionGeneratorContext context) {
String keyName = context.getKeyName();
DingerText dinger = context.getSource();
DingerDefinition dingerDefinition = annotationDingerDefition(keyName, dinger.tokenId(), dinger.asyncExecute());
dingerDefinition.setDingerType(dingerType);
dingerDefinition.setMessageSubType(MessageSubType.TEXT);
DingerRequest request;
if (dinger.atAll()) {
request = DingerRequest.request(dinger.value(), true);
} else {
request = DingerRequest.request(dinger.value(), Arrays.asList(dinger.phones()));
}
MsgType msgType = dingerDefinition.messageSubType().msgType(dingerType, request);
dingerDefinition.setMessage(msgType);
return dingerDefinition;
}
/**
* 处理注解-Markdown定义的Dinger消息
*
* @param dingerType Dinger类型 {@link DingerType}
* @param context Dinger定义源
* @return dingerDefinition {@link DingerDefinition}
*/
protected static DingerDefinition dingerMarkdownHandler(
DingerType dingerType, DingerDefinitionGeneratorContext context) {
String keyName = context.getKeyName();
DingerMarkdown dinger = context.getSource();
DingerDefinition dingerDefinition = annotationDingerDefition(keyName, dinger.tokenId(), dinger.asyncExecute());
dingerDefinition.setDingerType(dingerType);
dingerDefinition.setMessageSubType(MessageSubType.MARKDOWN);
// markdown not support at all members
DingerRequest request = DingerRequest.request(dinger.value(), dinger.title(), Arrays.asList(dinger.phones()));
MsgType msgType = dingerDefinition.messageSubType().msgType(dingerType, request);
dingerDefinition.setMessage(msgType);
return dingerDefinition;
}
/**
* 处理注解-ImageText定义的Dinger消息
*
* @param dingerType Dinger类型 {@link DingerType}
* @param context Dinger定义源
* @return dingerDefinition {@link DingerDefinition}
*/
protected static DingerDefinition dingerImageTextHandler(
DingerType dingerType, DingerDefinitionGeneratorContext context) {
String keyName = context.getKeyName();
DingerImageText dinger = context.getSource();
DingerDefinition dingerDefinition = annotationDingerDefition(keyName, dinger.tokenId(), dinger.asyncExecute());
dingerDefinition.setDingerType(dingerType);
dingerDefinition.setMessageSubType(MessageSubType.IMAGETEXT);
MsgType msgType = dingerDefinition.messageSubType().msgType(dingerType, null);
dingerDefinition.setMessage(msgType);
return dingerDefinition;
}
/**
* 处理注解-Link定义的Dinger消息
*
* @param dingerType Dinger类型 {@link DingerType}
* @param context Dinger定义源
* @return dingerDefinition {@link DingerDefinition}
*/
protected static DingerDefinition dingerLinkHandler(
DingerType dingerType, DingerDefinitionGeneratorContext context) {
String keyName = context.getKeyName();
DingerLink dinger = context.getSource();
DingerDefinition dingerDefinition = annotationDingerDefition(keyName, dinger.tokenId(), dinger.asyncExecute());
dingerDefinition.setDingerType(dingerType);
dingerDefinition.setMessageSubType(MessageSubType.LINK);
MsgType msgType = dingerDefinition.messageSubType().msgType(dingerType, null);
dingerDefinition.setMessage(msgType);
return dingerDefinition;
}
/**
* 处理Xml定义的Dinger消息
*
* @param dingerDefinitionType Dinger消息体定义类型
* @param context Dinger定义源
* @return dingerDefinition {@link DingerDefinition}
*/
protected static DingerDefinition xmlHandler(
DingerDefinitionType dingerDefinitionType, DingerDefinitionGeneratorContext context) {
MessageMainType messageMainType = dingerDefinitionType.messageMainType();
if (messageMainType != MessageMainType.XML) {
throw new DingerException(
DINGERDEFINITIONTYPE_ERROR, dingerDefinitionType, MessageMainType.XML, messageMainType);
}
String keyName = context.getKeyName();
MessageTag messageTag = context.getSource();
DingerDefinition dingerDefinition = new DefaultDingerDefinition();
dingerDefinition.setDingerType(dingerDefinitionType.dingerType());
dingerDefinition.setMessageMainType(messageMainType);
dingerDefinition.setMessageSubType(dingerDefinitionType.messageSubType());
dingerDefinition.setDingerName(keyName);
// 处理DingerConfig逻辑
DingerConfig dingerConfig = new DingerConfig();
dingerDefinition.setDingerConfig(dingerConfig);
Optional configuration = Optional.ofNullable(messageTag.getConfiguration());
dingerConfig(dingerConfig, configuration);
DingerRequest request = null;
if (dingerDefinitionType.messageSubType().isSupport()) {
Optional body = Optional.ofNullable(messageTag.getBody());
// 处理@成员逻辑
Optional phonesTag = body.map(e -> e.getPhones());
Boolean atAll = phonesTag.map(e -> e.getAtAll()).orElse(false);
List phoneTags = phonesTag.map(e -> e.getPhones()).orElse(null);
List phones;
if (phoneTags != null) {
phones = phoneTags.stream().map(PhoneTag::getValue).toList();
} else {
phones = new ArrayList<>();
}
// 处理消息体逻辑
Optional contentTag = body.map(e -> e.getContent());
String content = contentTag.map(e -> e.getContent()).orElse("");
String title = contentTag.map(e -> e.getTitle()).orElse("Dinger Title");
if (atAll) {
request = DingerRequest.request(content, title, true);
} else {
request = DingerRequest.request(content, title, phones);
}
}
MsgType message = dingerDefinitionType.messageSubType().msgType(dingerDefinitionType.dingerType(), request);
dingerDefinition.setMessage(message);
return dingerDefinition;
}
/**
* 从注解内容提取dingerDefinition
*
* @param dingerName dingerName
* @param dingerTokenId dingerTokenId
* @param asyncExecuteType asyncExecuteType
* @return dingerDefinition {@link DingerDefinition}
*/
private static DingerDefinition annotationDingerDefition(
String dingerName, DingerTokenId dingerTokenId, AsyncExecuteType asyncExecuteType) {
DingerDefinition dingerDefinition = new DefaultDingerDefinition();
dingerDefinition.setMessageMainType(MessageMainType.ANNOTATION);
dingerDefinition.setDingerName(dingerName);
DingerConfig dingerConfig = dingerConfig(dingerTokenId);
dingerConfig.setAsyncExecute(asyncExecuteType == AsyncExecuteType.NONE ? null : asyncExecuteType.type());
dingerDefinition.setDingerConfig(dingerConfig);
return dingerDefinition;
}
/**
* dingerConfig从configuration中获取
*
* @param dingerConfig dingerConfig
* @param configuration xml中配置的configuration
*/
private static void dingerConfig(DingerConfig dingerConfig, Optional configuration) {
if (configuration.isPresent()) {
Optional tokenId = configuration.map(e -> e.getTokenId());
if (tokenId.isPresent()) {
dingerConfig.setTokenId(tokenId.map(TokenId::getValue).orElse(null));
dingerConfig.setDecryptKey(tokenId.map(TokenId::getDecryptKey).orElse(null));
dingerConfig.setSecret(tokenId.map(TokenId::getSecret).orElse(null));
}
dingerConfig.setAsyncExecute(configuration
.map(e -> (e.getAsync() == null) ? e.getAsyncExecute() : e.getAsync())
.orElse(false));
// do check dingtalk config by person
dingerConfig.check();
}
}
/**
* 从dingerTokenId解析DingerConfig
*
* @param dingerTokenId dingerTokenId
* @return dingerConfig
*/
private static DingerConfig dingerConfig(DingerTokenId dingerTokenId) {
DingerConfig dingerConfig = new DingerConfig();
dingerConfig.setTokenId(dingerTokenId.value());
dingerConfig.setSecret(dingerTokenId.secret());
dingerConfig.setDecryptKey(dingerTokenId.decryptKey());
dingerConfig.check();
return dingerConfig;
}
}