feat: 完善云信通短信通知平台

This commit is contained in:
椰子 2021-07-07 12:46:04 +08:00
parent 36c3287b06
commit aa1cbbdd93
10 changed files with 120 additions and 81 deletions

View File

@ -35,5 +35,10 @@
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -7,8 +7,8 @@ import com.cicdi.notify.Provider;
*/
public enum SmsProvider implements Provider {
aliyunSms("阿里云短信服务"),
telecom("电信139短信服务");
aliyun("阿里云短信服务"),
js139("云信通短信通知服务");
private final String name;

View File

@ -61,7 +61,7 @@ public class AliyunSmsNotifier extends AbstractNotifier<AliyunSmsTemplate> {
@Override
public Provider getProvider() {
return SmsProvider.aliyunSms;
return SmsProvider.aliyun;
}
@Override
@ -84,9 +84,7 @@ public class AliyunSmsNotifier extends AbstractNotifier<AliyunSmsTemplate> {
JSONObject json = JSON.parseObject(response.getData());
if (!"ok".equalsIgnoreCase(json.getString("Code"))) {
// TODO 自定义异常
// return new BusinessException(json.getString("Message"), json.getString("Code"));
throw new Exception("json.getString(\"Message\"), json.getString(\"Code\")");
throw new RuntimeException("json.getString(\"Message\"), json.getString(\"Code\")");
}
} catch (Exception e) {
e.printStackTrace();

View File

@ -3,6 +3,7 @@ package com.cicdi.notify.sms.aliyun;
import com.alibaba.fastjson.JSON;
import com.cicdi.notify.*;
import com.cicdi.notify.sms.SmsProvider;
import com.cicdi.notify.template.Template;
import com.cicdi.notify.template.TemplateManager;
import com.cicdi.notify.template.TemplateProperties;
import com.cicdi.notify.template.TemplateProvider;
@ -20,7 +21,7 @@ public class AliyunSmsNotifierProvider implements NotifierProvider, TemplateProv
@Override
public Provider getProvider() {
return SmsProvider.aliyunSms;
return SmsProvider.aliyun;
}
// public static final DefaultConfigMetadata templateConfig = new DefaultConfigMetadata("阿里云短信模版",
@ -46,9 +47,7 @@ public class AliyunSmsNotifierProvider implements NotifierProvider, TemplateProv
// }
@Override
public AliyunSmsTemplate createTemplate(TemplateProperties properties) {
// TODO 验证工具
//return ValidatorUtils.tryValidate(JSON.parseObject(properties.getTemplate(), AliyunSmsTemplate.class));
public Template createTemplate(TemplateProperties properties) {
return JSON.parseObject(properties.getTemplate(), AliyunSmsTemplate.class);
}

View File

@ -1,40 +1,46 @@
package com.cicdi.notify.sms.telecom;
import com.alibaba.fastjson.JSONObject;
import com.cicdi.notify.*;
import com.cicdi.notify.sms.SmsProvider;
import com.cicdi.notify.template.TemplateManager;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/**
* 云信通短信通知平台通知器
*
* @author xueye
*/
public class TelecomSmsNotifier extends AbstractNotifier<TelecomSmsTemplate> {
private String serviceUrl;
private String userId;
private String password;
private final String notifyApi = "http://www.js139.com.cn:8022/hysms/SendMsg";
private String notifierId;
private final TelecomSmsNotifierConfiguration configuration;
private final String notifierId;
private final CloseableHttpClient httpClient;
public TelecomSmsNotifier(NotifierProperties properties, TemplateManager templateManager) {
super(templateManager);
Map<String, Object> configuration = properties.getConfiguration();
this.serviceUrl = (String) Objects.requireNonNull(configuration.get("serviceUrl"), "serviceUrl不能为空");
this.userId = (String) Objects.requireNonNull(configuration.get("userId"), "userId不能为空");
this.password = (String) Objects.requireNonNull(configuration.get("password"), "password不能为空");
this.httpClient = HttpClientBuilder.create().build();
this.notifierId = properties.getId();
this.configuration = new JSONObject(properties.getConfiguration()).toJavaObject(TelecomSmsNotifierConfiguration.class);
}
@Override
public String getNotifierId() {
return null;
return notifierId;
}
@Override
@ -44,60 +50,57 @@ public class TelecomSmsNotifier extends AbstractNotifier<TelecomSmsTemplate> {
@Override
public Provider getProvider() {
return SmsProvider.telecom;
return SmsProvider.js139;
}
@Override
public void send(TelecomSmsTemplate template, Map<String, Object> context) {
try {
URL obj = new URL(serviceUrl);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
//添加请求头
con.setRequestMethod("POST");
// con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
String urlParameters = "userId=" +
userId +
"&password=" +
password +
"&content=" +
template.getText() +
"&mobile=" +
template.getMobile();
//发送Post请求
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + serviceUrl);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
URI sendMsgURI = new URIBuilder(notifyApi).addParameter("userId", configuration.getUserId())
.addParameter("password", configuration.getPassword())
.addParameter("content", template.getContent())
.addParameter("mobile", template.getMobile())
.build();
HttpPost httpPost = new HttpPost(sendMsgURI);
CloseableHttpResponse response = httpClient.execute(httpPost);
String responseString = EntityUtils.toString(response.getEntity());
Map<String, String> resultMap = handleResponse(responseString);
String resultCodeKey = "rspCode";
if (resultMap.containsKey(resultCodeKey)) {
if (!"0".equals(resultMap.get(resultCodeKey)) && !"DELIVRD".equalsIgnoreCase(resultMap.get(resultCodeKey))) {
throw new RuntimeException("短信发送失败:" + resultMap.get("rspDesc"));
}
}
in.close();
//打印结果
System.out.println(response.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void close() {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 处理HTTP请求响应信息
* <p>将URL参数转换成键值对</p>
*
* @param response 响应字符串
* @return 处理过的响应信息
*/
private Map<String, String> handleResponse(String response) {
Map<String, String> map = new HashMap<>();
String[] param = response.split("&");
for (String key : param) {
String[] pair = key.split("=");
if (pair.length == 2) {
map.put(pair[0], pair[1]);
}
}
return map;
}
}

View File

@ -0,0 +1,35 @@
package com.cicdi.notify.sms.telecom;
/**
* 云信通短信通知平台配置信息
*
* @author xueye
*/
public class TelecomSmsNotifierConfiguration {
/**
* 用户账户
*/
private String userId;
/**
* 用户密码32位MD5加密值
*/
private String password;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

View File

@ -3,7 +3,6 @@ package com.cicdi.notify.sms.telecom;
import com.alibaba.fastjson.JSON;
import com.cicdi.notify.*;
import com.cicdi.notify.sms.SmsProvider;
import com.cicdi.notify.sms.aliyun.AliyunSmsNotifier;
import com.cicdi.notify.template.Template;
import com.cicdi.notify.template.TemplateManager;
import com.cicdi.notify.template.TemplateProperties;
@ -27,7 +26,7 @@ public class TelecomSmsNotifierProvider implements NotifierProvider, TemplatePro
@Override
public Provider getProvider() {
return SmsProvider.telecom;
return SmsProvider.js139;
}
@Override

View File

@ -9,19 +9,20 @@ public class TelecomSmsTemplate implements Template {
/**
* 短信文本
*/
private String text;
private String content;
/**
* 手机号码
* <p>多个手机号使用半角逗号分隔</p>
*/
private String mobile;
public String getText() {
return text;
public String getContent() {
return content;
}
public void setText(String text) {
this.text = text;
public void setContent(String content) {
this.content = content;
}
public String getMobile() {

View File

@ -24,7 +24,7 @@ public class AliyunSmsTest {
public NotifierProperties getNotifyConfig(NotifyType notifyType, String configId) {
NotifierProperties notifierProperties = new NotifierProperties();
notifierProperties.setType(DefaultNotifyType.sms.getId());
notifierProperties.setProvider(SmsProvider.aliyunSms.getId());
notifierProperties.setProvider(SmsProvider.aliyun.getId());
notifierProperties.setId("12");
Map<String, Object> config = new HashMap<>();
@ -44,7 +44,7 @@ public class AliyunSmsTest {
protected TemplateProperties getProperties(NotifyType type, String id) {
TemplateProperties templateProperties = new TemplateProperties();
templateProperties.setType(DefaultNotifyType.sms.getId());
templateProperties.setProvider(SmsProvider.aliyunSms.getId());
templateProperties.setProvider(SmsProvider.aliyun.getId());
templateProperties.setTemplate("{\"signName\":\"Hello\",\"phoneNumber\":[\"xueye404@qq.com\"],\"code\":\"1322\"}");

View File

@ -23,11 +23,10 @@ public class TelecomSmsTest {
NotifyConfigManager notifyConfigManager = (notifyType, configId) -> {
NotifierProperties notifierProperties = new NotifierProperties();
notifierProperties.setType(DefaultNotifyType.sms.getId());
notifierProperties.setProvider(SmsProvider.telecom.getId());
notifierProperties.setProvider(SmsProvider.js139.getId());
notifierProperties.setId("12");
Map<String, Object> config = new HashMap<>();
config.put("serviceUrl", "http://www.js139.com.cn:8022/hysms/SendMsg");
config.put("userId", "HYjlzx");
config.put("password", "5CA45EA4944D2C32567E8DBBDDBD65DD");
@ -42,11 +41,11 @@ public class TelecomSmsTest {
protected TemplateProperties getProperties(NotifyType type, String id) {
TemplateProperties templateProperties = new TemplateProperties();
templateProperties.setType(DefaultNotifyType.sms.getId());
templateProperties.setProvider(SmsProvider.telecom.getId());
templateProperties.setProvider(SmsProvider.js139.getId());
TelecomSmsTemplate template = new TelecomSmsTemplate();
template.setText("Hello World!");
template.setMobile("18605120786");
template.setContent("Hello World!");
template.setMobile("13047689449,18605120786");
templateProperties.setTemplate(JSON.toJSONString(template));