k94314517
2024-05-21 6a394fff5528c02f981abc0e9ee2d7211b694b70
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package cn.emay.sdk.client;
 
import java.io.File;
import java.util.Date;
 
import cn.emay.sdk.common.CommonConstants;
import cn.emay.sdk.core.dto.sms.common.ResultModel;
import cn.emay.sdk.core.dto.sms.request.BalanceRequest;
import cn.emay.sdk.core.dto.sms.request.MoRequest;
import cn.emay.sdk.core.dto.sms.request.ReportRequest;
import cn.emay.sdk.core.dto.sms.request.RetrieveReportRequest;
import cn.emay.sdk.core.dto.sms.request.SmsBatchOnlyRequest;
import cn.emay.sdk.core.dto.sms.request.SmsBatchRequest;
import cn.emay.sdk.core.dto.sms.request.SmsPersonalityAllRequest;
import cn.emay.sdk.core.dto.sms.request.SmsPersonalityRequest;
import cn.emay.sdk.core.dto.sms.request.SmsSingleRequest;
import cn.emay.sdk.core.dto.sms.response.BalanceResponse;
import cn.emay.sdk.core.dto.sms.response.MoResponse;
import cn.emay.sdk.core.dto.sms.response.ReportResponse;
import cn.emay.sdk.core.dto.sms.response.RetrieveReportResponse;
import cn.emay.sdk.core.dto.sms.response.SmsResponse;
import cn.emay.sdk.core.service.SDKService;
import cn.emay.sdk.core.service.security.SDKSecurityServiceImpl;
import cn.emay.sdk.task.ContrastHostTask;
import cn.emay.sdk.util.DateUtil;
import cn.emay.sdk.util.HostUtil;
import cn.emay.sdk.util.Md5;
import cn.emay.sdk.util.StringUtil;
import cn.emay.sdk.util.exception.SDKParamsException;
import cn.emay.sdk.util.http.common.EmayHttpResultCode;
 
public class SmsSDKClient {
 
    /**
     * 亿美服务帐号
     */
    private String appId;
    /**
     * 亿美服务密码
     */
    private String secretkey;
 
    private SDKService service;
    /**
     * 亿美http请求地址
     */
    private String host;
    /**
     * SDK缓存目录
     */
    private File file;
 
    /**
     * @param ip
     *            亿美网关IP
     * @param port
     *            亿美网关端口
     * @param appId
     *            亿美服务帐号
     * @param secretkey
     *            亿美服务密码
     * @throws SDKParamsException
     */
    public SmsSDKClient(String ip, int port, String appId, String secretkey) throws SDKParamsException {
        if (StringUtil.isEmpty(appId) || StringUtil.isEmpty(secretkey) || StringUtil.isEmpty(ip) || port <= 0) {
            throw new SDKParamsException("SDK params error");
        }
        this.appId = appId;
        this.secretkey = secretkey;
        if (!ip.toLowerCase().startsWith("http://")) {
            ip = "http://" + ip;
        }
        host = ip + ":" + port;
        CommonConstants.isBest = true;
        CommonConstants.bestUrl = host;
        service = new SDKSecurityServiceImpl();
    }
 
    /**
     * @param appId
     *            亿美服务帐号
     * @param secretkey
     *            亿美服务密码
     * @throws SDKParamsException
     */
    public SmsSDKClient(String appId, String secretkey) throws SDKParamsException {
        this(appId, secretkey, "");
    }
 
    /**
     * 
     * @param appId
     *            亿美服务帐号
     * @param secretkey
     *            亿美服务密码
     * @param filePath
     *            缓存数据目录(用于存储请求地址)
     * @throws SDKParamsException
     */
    public SmsSDKClient(String appId, String secretkey, String filePath) throws SDKParamsException {
        if (StringUtil.isEmpty(appId) || StringUtil.isEmpty(secretkey)) {
            throw new SDKParamsException("SDK params error");
        }
        this.appId = appId;
        this.secretkey = secretkey;
        file = HostUtil.creatCacheFile(filePath);
        String url = HostUtil.getFileUrl(file, appId);
        if (StringUtil.isEmpty(url)) {
            HostUtil.getSDKInter();
            if (CommonConstants.interList == null || CommonConstants.interList.isEmpty()) {
                throw new SDKParamsException("SDK Request interface address exception");
            }
            new Thread(new ContrastHostTask(file, appId, secretkey)).start();
            CommonConstants.bestUrl = CommonConstants.interList.get(0);
        } else {
            CommonConstants.isBest = true;
            CommonConstants.bestUrl = url;
        }
        service = new SDKSecurityServiceImpl();
    }
 
    /**
     * 状态报告重新获取
     * 
     * @param reportRequest
     * @return
     */
    public ResultModel<RetrieveReportResponse> retrieveReport(RetrieveReportRequest reportRequest) {
        String timestamp = DateUtil.toString(new Date(), "yyyyMMddHHmmss");
        String sign = Md5.md5((appId + secretkey + timestamp).getBytes());
        String url = (host == null || host.equals("")) ? CommonConstants.bestUrl : host;
        return service.retrieveReport(appId, timestamp, sign, url, reportRequest);
    }
 
    /**
     * 发送单条短信
     * 
     * @param request
     * @return
     */
 
    public ResultModel<SmsResponse> sendSingleSms(SmsSingleRequest request) {
        String url = (host == null || host.equals("")) ? CommonConstants.bestUrl : host;
        return service.sendSingleSms(appId, secretkey, url, request);
    }
 
    /**
     * 发送批次短信【非自定义smsid】
     * 
     * @param request
     * @return
     */
    public ResultModel<SmsResponse[]> sendBatchOnlySms(SmsBatchOnlyRequest request) {
        String url = (host == null || host.equals("")) ? CommonConstants.bestUrl : host;
        return service.sendBatchOnlySms(appId, secretkey, url, request);
    }
 
    /**
     * 发送批次短信【自定义smsid】
     * 
     * @param request
     * @return
     * @throws SDKParamsException
     */
    public ResultModel<SmsResponse[]> sendBatchSms(SmsBatchRequest request) throws SDKParamsException {
        String url = (host == null || host.equals("")) ? CommonConstants.bestUrl : host;
        return service.sendBatchSms(appId, secretkey, url, request);
    }
 
    /**
     * 发送个性短信
     * 
     * @param request
     * @return
     */
 
    public ResultModel<SmsResponse[]> sendPersonalitySms(SmsPersonalityRequest request) throws SDKParamsException {
        String url = (host == null || host.equals("")) ? CommonConstants.bestUrl : host;
        return service.sendPersonalitySms(appId, secretkey, url, request);
    }
 
    /**
     * 发送个性短信【全属性个性】
     * 
     * @param request
     * @return
     */
    public ResultModel<SmsResponse[]> sendPersonalityAllSMS(SmsPersonalityAllRequest request) throws SDKParamsException {
        String url = (host == null || host.equals("")) ? CommonConstants.bestUrl : host;
        return service.sendPersonalityAllSms(appId, secretkey, url, request);
    }
 
    /**
     * 获取余额
     * 
     * @param request
     * @return
     */
    public ResultModel<BalanceResponse> getBalance(BalanceRequest request) {
        String url = (host == null || host.equals("")) ? CommonConstants.bestUrl : host;
        return service.getBalance(appId, secretkey, url, request);
    }
 
    /**
     * 获取状态报告
     * 
     * @param request
     * @return
     */
 
    public ResultModel<ReportResponse[]> getReport(ReportRequest reportRequest) {
        String url = "";
        if (CommonConstants.isBest) {
            url = CommonConstants.bestUrl;
        } else {
            return new ResultModel<ReportResponse[]>(EmayHttpResultCode.SUCCESS.getCode(), new ReportResponse[0]);
        }
        return service.getReport(appId, secretkey, url, reportRequest);
    }
 
    /**
     * 获取上行短信
     * 
     * @param request
     * @return
     */
    public ResultModel<MoResponse[]> getMo(MoRequest request) {
        String url = "";
        if (CommonConstants.isBest) {
            url = CommonConstants.bestUrl;
        } else {
            return new ResultModel<MoResponse[]>(EmayHttpResultCode.SUCCESS.getCode(), new MoResponse[0]);
        }
        return service.getMo(appId, secretkey, url, request);
    }
 
}