REQ-3444: 调用方控制是否截断参数

This commit is contained in:
yanglin 2024-12-17 16:18:21 +08:00
parent a564ea8898
commit f27881e394
6 changed files with 78 additions and 6 deletions

View File

@ -42,4 +42,9 @@ public class SendMessageRequestDto extends ExpansionInfo {
@NotNull(message = "params must not be null")
private Map<String, Object> params = new HashMap<>();
/**
* 变量过长时是否截断
*/
private VariableAbbreviation abbreviation = VariableAbbreviation.defaultAbbreviate();
}

View File

@ -0,0 +1,42 @@
package cn.axzo.msg.center.api.request;
import lombok.Getter;
import lombok.Setter;
/**
* @author yanglin
*/
@Setter
@Getter
public class VariableAbbreviation {
public static VariableAbbreviation defaultAbbreviate() {
return abbreviate(32);
}
public static VariableAbbreviation abbreviate(int maxLength) {
if (maxLength <= 0)
throw new IllegalArgumentException("maxLength must be greater than 0");
VariableAbbreviation abbreviation = new VariableAbbreviation();
abbreviation.setAbbreviate(true);
abbreviation.setMaxLength(maxLength);
return abbreviation;
}
public static VariableAbbreviation noAbbreviation() {
VariableAbbreviation abbreviation = new VariableAbbreviation();
abbreviation.setAbbreviate(false);
return abbreviation;
}
/**
* 是否截断
*/
private boolean abbreviate;
/**
* 最大保留长度
*/
private int maxLength;
}

View File

@ -1,5 +1,6 @@
package cn.axzo.msg.center.notices.manager.api.dto.request;
import cn.axzo.msg.center.api.request.VariableAbbreviation;
import cn.axzo.msg.center.domain.entity.MNSChannelMessageTemplate;
import cn.axzo.msg.center.domain.entity.MNSMessageTemplate;
import lombok.Data;
@ -62,4 +63,9 @@ public class MessageSendRequestDto {
* 原始请求
*/
private MnsRequestDto request;
/**
* 变量过长时是否截断
*/
private VariableAbbreviation abbreviation = VariableAbbreviation.abbreviate(32);
}

View File

@ -1,6 +1,7 @@
package cn.axzo.msg.center.notices.manager.api.dto.request;
import cn.axzo.msg.center.api.request.ExpansionInfo;
import cn.axzo.msg.center.api.request.VariableAbbreviation;
import com.alibaba.fastjson.JSON;
import lombok.AllArgsConstructor;
import lombok.Builder;
@ -50,6 +51,11 @@ public class MnsRequestDto extends ExpansionInfo {
private Object internalObj;
/**
* 变量过长时是否截断
*/
private VariableAbbreviation abbreviation = VariableAbbreviation.abbreviate(32);
public <T> T getInternalObj(Class<T> clazz) {
return clazz.isInstance(internalObj) ? clazz.cast(internalObj) : null;
}

View File

@ -1,5 +1,6 @@
package cn.axzo.msg.center.notices.manager;
import cn.axzo.msg.center.api.request.VariableAbbreviation;
import cn.axzo.msg.center.domain.entity.MNSMessageChannelLog;
import cn.axzo.msg.center.notices.common.domain.ServiceContext;
import cn.axzo.msg.center.notices.common.domain.ServiceContextHolder;
@ -122,7 +123,7 @@ public class SmsSendManagerComposite implements SmsSendManager, ApplicationConte
Map<String, Object> templateMap = request.getTemplateMap();
// 处理短信字符串变量参数超长问题
Map<String, Object> renderTemplateMap = handleSmsParamsSize(templateMap);
Map<String, Object> renderTemplateMap = handleSmsParamsSize(request, templateMap);
// 修改模板参数
request.setTemplateMap(renderTemplateMap);
@ -130,10 +131,12 @@ public class SmsSendManagerComposite implements SmsSendManager, ApplicationConte
/**
* 处理短信字符串变量参数超长问题
*
* @param request
* @param templateMap
* @return
*/
private Map<String, Object> handleSmsParamsSize(Map<String, Object> templateMap) {
private Map<String, Object> handleSmsParamsSize(MessageSendRequestDto request, Map<String, Object> templateMap) {
if (CollectionUtils.isEmpty(templateMap)){
return templateMap;
}
@ -145,7 +148,7 @@ public class SmsSendManagerComposite implements SmsSendManager, ApplicationConte
String key = entry.getKey();
if (Objects.nonNull(value)){
if (value instanceof String){
String handledStr = handleSmsVariableSize(String.valueOf(value));
String handledStr = handleSmsVariableSize(request, String.valueOf(value));
templateMap.put(key,handledStr);
}
}
@ -155,16 +158,25 @@ public class SmsSendManagerComposite implements SmsSendManager, ApplicationConte
/**
* 处理短信参数长度
*
* @param request
* @param str
* @return
*/
private String handleSmsVariableSize(String str) {
private String handleSmsVariableSize(MessageSendRequestDto request, String str) {
VariableAbbreviation abbreviation = request.getAbbreviation();
if (abbreviation == null) {
abbreviation = VariableAbbreviation.defaultAbbreviate();
}
if (!abbreviation.isAbbreviate()) {
return str;
}
if (str != null) {
int size = str.length();
if ( size <= 35 ) {
if ( size <= abbreviation.getMaxLength() + 3 ) {
return str;
}
return str.substring(0, 32) + "...";
return str.substring(0, abbreviation.getMaxLength()) + "...";
}
return "";
}

View File

@ -165,6 +165,7 @@ public class MessageServiceImpl implements MessageService, EnvironmentAware {
// 发送短信
try {
MessageSendRequestDto dto = new MessageSendRequestDto();
dto.setAbbreviation(request.getAbbreviation());
dto.setAppCode(request.getAppCode());
dto.setChannelCode(message.getChannelCode());
dto.setPhoneNo(message.getTargetAddress());