All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sophon.module.sms.integration.client.impl.AbstractSmsClient Maven / Gradle / Ivy

There is a newer version: 1.0.4
Show newest version
package org.sophon.module.sms.integration.client.impl;

import java.util.List;
import java.util.function.Supplier;

import org.sophon.commons.core.KeyValue;
import org.sophon.commons.exception.ErrorCode;
import org.sophon.commons.exception.ErrorCodeMapping;
import org.sophon.commons.exception.SophonException;
import org.sophon.module.sms.commons.constants.SmsErrorCodeConstants;
import org.sophon.module.sms.integration.client.SmsClient;
import org.sophon.module.sms.integration.client.dto.SmsInvokeResult;
import org.sophon.module.sms.integration.client.dto.SmsReceiveRespDTO;
import org.sophon.module.sms.integration.client.dto.SmsSendRespDTO;
import org.sophon.module.sms.integration.client.dto.SmsTemplateRespDTO;
import org.sophon.module.sms.integration.prop.SmsChannelProperties;

import lombok.extern.slf4j.Slf4j;

/**
 * 短信客户端模板方法
 * @author moushaokun
 * @since time: 2023-03-13 10:53
 */
@Slf4j
public abstract class AbstractSmsClient implements SmsClient {
    /**
     * 短信渠道配置
     */
    protected volatile SmsChannelProperties properties;
    /**
     * 错误码枚举类
     */
    protected final ErrorCodeMapping        codeMapping;

    public AbstractSmsClient(SmsChannelProperties properties, ErrorCodeMapping codeMapping) {
        this.properties = prepareProperties(properties);
        this.codeMapping = codeMapping;
    }

    /**
     * 初始化
     */
    public final void init() {
        doInit();
        log.info("[{}] 配置({}) 初始化完成", SmsErrorCodeConstants.MODULE_TAG, properties);
    }

    /**
     * 自定义初始化
     */
    protected abstract void doInit();

    public final void refresh(SmsChannelProperties properties) {
        // 判断是否更新
        if (properties.equals(this.properties)) {
            return;
        }
        log.info("[{}] 配置({})发生变化,重新初始化", SmsErrorCodeConstants.MODULE_TAG, properties);
        this.properties = prepareProperties(properties);
        // 初始化
        this.init();
    }

    /**
     * 在赋值给{@link this#properties}前,子类可根据需要预处理短信渠道配置
     *
     * @param properties 数据库中存储的短信渠道配置
     * @return 满足子类实现的短信渠道配置
     */
    protected SmsChannelProperties prepareProperties(SmsChannelProperties properties) {
        return properties;
    }

    @Override
    public final SmsInvokeResult sendSms(String bizNo, String mobile, String apiTemplateId, List> templateParams) {
        return executeWithWrappedException(
                () -> doSendSms(bizNo, mobile, apiTemplateId, templateParams),
                () -> String.format("发送短信异常,bizNo(%s) mobile(%s) apiTemplateId(%s) templateParams(%s)",  bizNo, mobile, apiTemplateId, templateParams),
                () -> SmsErrorCodeConstants.INVOKE_ERROR_UNKNOWN
        );
    }

    /**
     * 执行发送
     * @param bizNo 业务编号
     * @param mobile    手机号
     * @param apiTemplateId api模板id
     * @param templateParams api模板参数
     * @return 发送结果
     */
    protected abstract SmsInvokeResult doSendSms(String bizNo, String mobile, String apiTemplateId, List> templateParams);

    @Override
    public SmsInvokeResult> parseSmsReceiveStatus(String text){
        return executeWithWrappedException(
                () -> doParseSmsReceiveStatus(text),
                () -> String.format("text(%s) 解析发生异常", text),
                () -> SmsErrorCodeConstants.PARSE_ERROR
        );
    }

    /**
     * 执行解析
     * @param text  结果内容字符串
     * @return  结构化的结果内容
     */
    protected abstract SmsInvokeResult> doParseSmsReceiveStatus(String text);

    @Override
    public SmsInvokeResult getSmsTemplate(String apiTemplateId) {
        return executeWithWrappedException(
                () -> doGetSmsTemplate(apiTemplateId),
                () -> String.format("获得短信模板 apiTemplateId(%s) 发生异常", apiTemplateId),
                () -> SmsErrorCodeConstants.INVOKE_ERROR_UNKNOWN
        );
    }

    /**
     * 执行获取模板
     * @param apiTemplateId api模板ID
     * @return api模板信息
     */
    protected abstract SmsInvokeResult doGetSmsTemplate(String apiTemplateId);

    /**
     * 统一处理非sdk异常,sdk异常需要在具体实现中自行转换处理
     */
    protected  SmsInvokeResult executeWithWrappedException(Supplier> executor, Supplier errorMessage, Supplier otherwise){
        try {
            return executor.get();
        } catch (Throwable ex) {
            String errorMsg = errorMessage.get();
            log.error("[{}] {}", SmsErrorCodeConstants.MODULE_TAG, errorMsg, ex);
            SophonException SophonException;
            if(ex instanceof SophonException){
                SophonException = (SophonException) ex;
            } else {
                SophonException = new SophonException(otherwise.get(), ex, errorMsg);
            }
            return SmsInvokeResult.error(SophonException);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy