SMSWebChineseRPCServiceImpl.java
7.46 KB
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
package com.diligrp.website.rpc.impl;
import java.util.Map;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.diligrp.website.domain.MobMessage;
import com.diligrp.website.rpc.SMSRPCService;
import com.diligrp.website.util.code.MD5Utils;
import com.google.common.collect.Maps;
/**
*
* <B>Description</B> 中国建网(sms.webchinese.cn) SMS短信发送接口实现 <br />
* <B>Copyright</B> Copyright (c) 2014 www.diligrp.com All rights reserved. <br />
* 本软件源代码版权归地利集团,未经许可不得任意复制与传播.<br />
* <B>Company</B> 地利集团
* @createTime 2014年6月4日 下午5:52:35
* @author Wang22
*/
public class SMSWebChineseRPCServiceImpl implements SMSRPCService {
private static final Logger LOG = LoggerFactory.getLogger(SMSWebChineseRPCServiceImpl.class);
private Map<String, String> SMSConfig;
private MD5Utils md5Utils = new MD5Utils();
private boolean smsSwitch;
public static void main(String[] args) {
// <project.sms.webchina.SMSKey>2cf3264488ee3beef224</project.sms.webchina.SMSKey>
// <project.sms.webchina.userName>itly</project.sms.webchina.userName>
// <project.sms.webchina.postURL>http://gbk.sms.webchinese.cn</project.sms.webchina.postURL>
// <entry key="SMSKey" value="${project.sms.webchina.SMSKey}" />
// <entry key="userName" value="${project.sms.webchina.userName}" />
// <entry key="postURL" value="${project.sms.webchina.postURL}" />
Map<String, String> smsCfg = Maps.newHashMap();
smsCfg.put("SMSKey", "2cf3264488ee3beef224");
smsCfg.put("userName", "itly");
smsCfg.put("postURL", "http://gbk.sms.webchinese.cn");
SMSWebChineseRPCServiceImpl sms = new SMSWebChineseRPCServiceImpl();
sms.setSMSConfig(smsCfg);
MobMessage mm = new MobMessage();
mm.setSmsMob("13693447275");
mm.setSmsText("text");
sms.sendSMS(mm);
}
@Override
public boolean sendSMS(MobMessage mm) {
// 如果短信发送关闭,直接返回true
if(!smsSwitch){
LOG.info("调用 sms.webchinese.cn 短信接口 ,手机号:" + StringUtils.join(mm.getSmsMob(), ",") + ",短信内容:" + mm.getSmsText());
return true;
}
for (int i = 0; i < 5; i++) {
SMSState state = postSMS(mm);
if (state != SMSState.EXCEPTION) {
return true;
}
LOG.info("调用接口异常,尝试重新调用,尝试第" + (i + 1) + "次");
}
return false;
}
/**
*
* 发送短信
* @param mm 发送短信对象
* @return
* @throws Exception
* @createTime 2014年7月17日 下午7:08:18
* @author Wang22
*/
private SMSState postSMS(MobMessage mm) {
HttpClient client = new HttpClient();
PostMethod post = new PostMethod(SMSConfig.get("postURL"));
post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=gbk");
String md5Key = md5Utils.getMD5ofStr(SMSConfig.get("SMSKey"));
String userName = SMSConfig.get("userName");
String smsMobs = StringUtils.join(mm.getSmsMob(), ",");
NameValuePair[] data = { new NameValuePair("Uid", userName),
new NameValuePair("KeyMD5", md5Key),
new NameValuePair("smsMob", smsMobs),
new NameValuePair("smsText", mm.getSmsText()) };
post.setRequestBody(data);
SMSState state = SMSState.FAIL;
try {
LOG.info("调用 sms.webchinese.cn 短信接口 ,手机号:" + smsMobs + ",短信内容:" + mm.getSmsText());
client.setTimeout(5000);
client.executeMethod(post);
int statusCode = post.getStatusCode();
if (statusCode == 200) {
String result = new String(post.getResponseBodyAsString().getBytes("gbk"));
state = getSMSState(userName, smsMobs, Integer.parseInt(result));
return state;
} else {
LOG.info("调用 sms.webchinese.cn 接口失败,可能由于接口放内部原因,返回码:" + statusCode);
}
throw new Exception();
} catch (Exception e) {
LOG.error("调用 sms.webchinese.cn 接口异常", e);
state = SMSState.EXCEPTION;
} finally {
post.releaseConnection();
}
return state;
}
/**
*
* 获取调用状态
* @param userName
* @param smsMobs
* @param state
* @param rstCode
* @return
* @createTime 2014年7月17日 下午7:25:31
* @author Wang22
*/
private SMSState getSMSState(String userName, String smsMobs, int rstCode) {
SMSState state = SMSState.FAIL;
switch (rstCode) {
case -1:
LOG.info("调用 sms.webchinese.cn 短信接口失败,原因:没有该用户账户" + userName);
break;
case -2:
LOG.info("调用 sms.webchinese.cn 短信接口失败,原因:接口密钥不正确");
break;
case -21:
LOG.info("调用 sms.webchinese.cn 短信接口失败,原因:MD5接口密钥加密不正确");
break;
case -3:
LOG.info("调用 sms.webchinese.cn 短信接口失败,原因:短信数量不足");
break;
case -11:
LOG.info("调用 sms.webchinese.cn 短信接口失败,原因:该用户被禁用");
break;
case -14:
LOG.info("调用 sms.webchinese.cn 短信接口失败,原因:短信内容出现非法字符");
break;
case -4:
LOG.info("调用 sms.webchinese.cn 短信接口失败,原因:手机号格式不正确 " + smsMobs );
break;
case -41:
LOG.info("调用 sms.webchinese.cn 短信接口失败,原因:手机号码为空");
break;
case -42:
LOG.info("调用 sms.webchinese.cn 短信接口失败,原因:短信内容为空");
break;
case -51:
LOG.info("调用 sms.webchinese.cn 短信接口失败,原因:短信签名格式不正确,接口签名格式为:【签名内容】");
break;
default:
LOG.info("调用 sms.webchinese.cn 短信接口 ,短信发送成功,手机号:" + smsMobs);
state = SMSState.SUCCESS;
break;
}
return state;
}
/**
* set value of SMSWebChineseRPCServiceImpl.sMSConfig
* @param sMSConfig the sMSConfig to set
* @createTime 2014年6月5日 下午1:40:06
* @author Wang22
*/
public void setSMSConfig(Map<String, String> sMSConfig) {
SMSConfig = sMSConfig;
}
/**
* set value of SMSWebChineseRPCServiceImpl.md5Utils
* @param md5Utils the md5Utils to set
* @createTime 2014年6月5日 下午1:40:06
* @author Wang22
*/
public void setMd5Utils(MD5Utils md5Utils) {
this.md5Utils = md5Utils;
}
public enum SMSState {
SUCCESS, FAIL, EXCEPTION
}
public boolean isSmsSwitch() {
return smsSwitch;
}
public void setSmsSwitch(boolean smsSwitch) {
this.smsSwitch = smsSwitch;
}
}