com.taotao.boot.dingtalk.model.DingerMessageHandler 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.common.utils.log.LogUtils;
import com.taotao.boot.dingtalk.annatations.Dinger;
import com.taotao.boot.dingtalk.entity.DingerResponse;
import com.taotao.boot.dingtalk.entity.MsgType;
import com.taotao.boot.dingtalk.entity.MultiDingerConfig;
import com.taotao.boot.dingtalk.enums.DingerType;
import com.taotao.boot.dingtalk.enums.MultiDingerConfigContainer;
import com.taotao.boot.dingtalk.multi.MultiDingerProperty;
import com.taotao.boot.dingtalk.properties.DingtalkProperties;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import static com.taotao.boot.dingtalk.constant.DingerConstant.SPOT_SEPERATOR;
/**
* DingerMessageHandler
*
* @author shuigedeng
* @version 2022.07
* @since 2022-07-06 15:23:02
*/
public class DingerMessageHandler extends MultiDingerProperty
implements ParamHandler, MessageTransfer, ResultHandler {
protected DingerRobot dingerRobot;
protected DingtalkProperties dingtalkProperties;
@Override
public Map paramsHandler(Method method, DingerDefinition dingerDefinition, Object[] values) {
Map params = new HashMap<>();
int valueLength = values.length;
if (valueLength == 0) {
return params;
}
String[] keys = dingerDefinition.methodParams();
int[] genericIndex = dingerDefinition.genericIndex();
if (genericIndex.length > 0) {
for (int i : genericIndex) {
params.put(keys[i], values[i]);
}
return params;
}
int keyLength = keys.length;
if (keyLength == valueLength) {
for (int i = 0; i < valueLength; i++) {
params.put(keys[i], values[i]);
}
return params;
}
Parameter[] parameters = method.getParameters();
for (int i = 0; i < Objects.requireNonNull(parameters).length; i++) {
Parameter parameter = parameters[i];
String paramName = parameter.getName();
com.taotao.boot.dingtalk.annatations.Parameter[] panno =
parameter.getDeclaredAnnotationsByType(com.taotao.boot.dingtalk.annatations.Parameter.class);
if (panno != null && panno.length > 0) {
paramName = panno[0].value();
}
params.put(paramName, values[i]);
}
return params;
}
@Override
public MsgType transfer(DingerDefinition dingerDefinition, Map params) {
MsgType message = copyProperties(dingerDefinition.message());
message.transfer(params);
return message;
}
@Override
public Object resultHandler(Class> resultType, DingerResponse dingerResponse) {
String name = resultType.getName();
if (String.class.getName().equals(name)) {
return Optional.ofNullable(dingerResponse).map(e -> e.getData()).orElse(null);
} else if (DingerResponse.class.getName().equals(name)) {
return dingerResponse;
}
return null;
}
/**
* copyProperties
*
* @param src src
* @param T extends Message
* @return msg
*/
private T copyProperties(MsgType src) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(src);
ByteArrayInputStream byteIn = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream in = new ObjectInputStream(byteIn);
@SuppressWarnings("unchecked")
T dest = (T) in.readObject();
return dest;
} catch (Exception e) {
LogUtils.debug("copy properties error:", e);
return null;
}
}
/**
* 获取方法执行的Dinger
*
* @param method 代理方法
* @return 返回Dinger
*/
DingerType dingerType(Method method) {
Class> dingerClass = method.getDeclaringClass();
if (dingerClass.isAnnotationPresent(Dinger.class)) {
return dingerClass.getAnnotation(Dinger.class).value();
}
return dingtalkProperties.getDefaultDinger();
}
/**
* 获取Dinger定义
*
*
* 优先级: local(dinger为空使用默认 {@link DingerMessageHandler#dingerType}) > multi > default({@link DingerMessageHandler#dingerType})
*
*
* @param useDinger 代理方法使用的Dinger
* @param dingerClassName 代理类全限定名
* @param keyName 代理方法全限定名
* @return dingerDefinition {@link DingerDefinition}
*/
DingerDefinition dingerDefinition(DingerType useDinger, String dingerClassName, String keyName) {
DingerDefinition dingerDefinition;
DingerConfig localDinger = DingerHelper.getLocalDinger();
// 优先使用用户设定 dingerConfig
if (localDinger == null) {
String dingerName = useDinger + SPOT_SEPERATOR + keyName;
dingerDefinition = AbstractDingerDefinitionResolver.Container.INSTANCE.get(dingerName);
if (dingerDefinition == null) {
return null;
}
DingerConfig dingerMethodDefaultDingerConfig = dingerDefinition.dingerConfig();
// 判断是否是multiDinger
if (multiDinger()) {
MultiDingerConfig multiDingerConfig =
MultiDingerConfigContainer.INSTANCE.get(useDinger, dingerClassName);
DingerConfig dingerConfig = null;
if (multiDingerConfig != null) {
// 拿到MultiDingerConfig中当前应该使用的DingerConfig
dingerConfig = multiDingerConfig
.getAlgorithmHandler()
.dingerConfig(multiDingerConfig.getDingerConfigs(), dingerMethodDefaultDingerConfig);
}
// use default dingerConfig
if (dingerConfig == null) {
dingerConfig = dingerMethodDefaultDingerConfig;
}
DingerHelper.assignDinger(dingerConfig);
} else {
DingerHelper.assignDinger(dingerMethodDefaultDingerConfig);
}
} else {
DingerType dingerType = localDinger.getDingerType();
if (dingerType == null) {
dingerType = useDinger;
}
keyName = dingerType + SPOT_SEPERATOR + keyName;
dingerDefinition = AbstractDingerDefinitionResolver.Container.INSTANCE.get(keyName);
if (dingerDefinition == null) {
return null;
}
}
return dingerDefinition;
}
}