feat: 完善云信通短信通知平台
This commit is contained in:
parent
36c3287b06
commit
aa1cbbdd93
@ -35,5 +35,10 @@
|
|||||||
<version>4.5.2</version>
|
<version>4.5.2</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.httpcomponents</groupId>
|
||||||
|
<artifactId>httpclient</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
@ -7,8 +7,8 @@ import com.cicdi.notify.Provider;
|
|||||||
*/
|
*/
|
||||||
public enum SmsProvider implements Provider {
|
public enum SmsProvider implements Provider {
|
||||||
|
|
||||||
aliyunSms("阿里云短信服务"),
|
aliyun("阿里云短信服务"),
|
||||||
telecom("电信139短信服务");
|
js139("云信通短信通知服务");
|
||||||
|
|
||||||
private final String name;
|
private final String name;
|
||||||
|
|
||||||
|
@ -61,7 +61,7 @@ public class AliyunSmsNotifier extends AbstractNotifier<AliyunSmsTemplate> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Provider getProvider() {
|
public Provider getProvider() {
|
||||||
return SmsProvider.aliyunSms;
|
return SmsProvider.aliyun;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -84,9 +84,7 @@ public class AliyunSmsNotifier extends AbstractNotifier<AliyunSmsTemplate> {
|
|||||||
|
|
||||||
JSONObject json = JSON.parseObject(response.getData());
|
JSONObject json = JSON.parseObject(response.getData());
|
||||||
if (!"ok".equalsIgnoreCase(json.getString("Code"))) {
|
if (!"ok".equalsIgnoreCase(json.getString("Code"))) {
|
||||||
// TODO 自定义异常
|
throw new RuntimeException("json.getString(\"Message\"), json.getString(\"Code\")");
|
||||||
// return new BusinessException(json.getString("Message"), json.getString("Code"));
|
|
||||||
throw new Exception("json.getString(\"Message\"), json.getString(\"Code\")");
|
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
@ -3,6 +3,7 @@ package com.cicdi.notify.sms.aliyun;
|
|||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.cicdi.notify.*;
|
import com.cicdi.notify.*;
|
||||||
import com.cicdi.notify.sms.SmsProvider;
|
import com.cicdi.notify.sms.SmsProvider;
|
||||||
|
import com.cicdi.notify.template.Template;
|
||||||
import com.cicdi.notify.template.TemplateManager;
|
import com.cicdi.notify.template.TemplateManager;
|
||||||
import com.cicdi.notify.template.TemplateProperties;
|
import com.cicdi.notify.template.TemplateProperties;
|
||||||
import com.cicdi.notify.template.TemplateProvider;
|
import com.cicdi.notify.template.TemplateProvider;
|
||||||
@ -20,7 +21,7 @@ public class AliyunSmsNotifierProvider implements NotifierProvider, TemplateProv
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Provider getProvider() {
|
public Provider getProvider() {
|
||||||
return SmsProvider.aliyunSms;
|
return SmsProvider.aliyun;
|
||||||
}
|
}
|
||||||
|
|
||||||
// public static final DefaultConfigMetadata templateConfig = new DefaultConfigMetadata("阿里云短信模版",
|
// public static final DefaultConfigMetadata templateConfig = new DefaultConfigMetadata("阿里云短信模版",
|
||||||
@ -46,9 +47,7 @@ public class AliyunSmsNotifierProvider implements NotifierProvider, TemplateProv
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public AliyunSmsTemplate createTemplate(TemplateProperties properties) {
|
public Template createTemplate(TemplateProperties properties) {
|
||||||
// TODO 验证工具
|
|
||||||
//return ValidatorUtils.tryValidate(JSON.parseObject(properties.getTemplate(), AliyunSmsTemplate.class));
|
|
||||||
return JSON.parseObject(properties.getTemplate(), AliyunSmsTemplate.class);
|
return JSON.parseObject(properties.getTemplate(), AliyunSmsTemplate.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,40 +1,46 @@
|
|||||||
package com.cicdi.notify.sms.telecom;
|
package com.cicdi.notify.sms.telecom;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.cicdi.notify.*;
|
import com.cicdi.notify.*;
|
||||||
import com.cicdi.notify.sms.SmsProvider;
|
import com.cicdi.notify.sms.SmsProvider;
|
||||||
import com.cicdi.notify.template.TemplateManager;
|
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.IOException;
|
||||||
import java.io.DataOutputStream;
|
import java.net.URI;
|
||||||
import java.io.InputStreamReader;
|
import java.util.HashMap;
|
||||||
import java.net.HttpURLConnection;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* 云信通短信通知平台通知器
|
||||||
|
*
|
||||||
* @author xueye
|
* @author xueye
|
||||||
*/
|
*/
|
||||||
public class TelecomSmsNotifier extends AbstractNotifier<TelecomSmsTemplate> {
|
public class TelecomSmsNotifier extends AbstractNotifier<TelecomSmsTemplate> {
|
||||||
|
|
||||||
private String serviceUrl;
|
private final String notifyApi = "http://www.js139.com.cn:8022/hysms/SendMsg";
|
||||||
private String userId;
|
|
||||||
private String password;
|
|
||||||
|
|
||||||
private String notifierId;
|
private final TelecomSmsNotifierConfiguration configuration;
|
||||||
|
|
||||||
|
private final String notifierId;
|
||||||
|
|
||||||
|
private final CloseableHttpClient httpClient;
|
||||||
|
|
||||||
public TelecomSmsNotifier(NotifierProperties properties, TemplateManager templateManager) {
|
public TelecomSmsNotifier(NotifierProperties properties, TemplateManager templateManager) {
|
||||||
super(templateManager);
|
super(templateManager);
|
||||||
Map<String, Object> configuration = properties.getConfiguration();
|
this.httpClient = HttpClientBuilder.create().build();
|
||||||
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.notifierId = properties.getId();
|
this.notifierId = properties.getId();
|
||||||
|
this.configuration = new JSONObject(properties.getConfiguration()).toJavaObject(TelecomSmsNotifierConfiguration.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getNotifierId() {
|
public String getNotifierId() {
|
||||||
return null;
|
return notifierId;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -44,60 +50,57 @@ public class TelecomSmsNotifier extends AbstractNotifier<TelecomSmsTemplate> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Provider getProvider() {
|
public Provider getProvider() {
|
||||||
return SmsProvider.telecom;
|
return SmsProvider.js139;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void send(TelecomSmsTemplate template, Map<String, Object> context) {
|
public void send(TelecomSmsTemplate template, Map<String, Object> context) {
|
||||||
try {
|
try {
|
||||||
URL obj = new URL(serviceUrl);
|
URI sendMsgURI = new URIBuilder(notifyApi).addParameter("userId", configuration.getUserId())
|
||||||
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
|
.addParameter("password", configuration.getPassword())
|
||||||
|
.addParameter("content", template.getContent())
|
||||||
//添加请求头
|
.addParameter("mobile", template.getMobile())
|
||||||
con.setRequestMethod("POST");
|
.build();
|
||||||
// con.setRequestProperty("User-Agent", USER_AGENT);
|
HttpPost httpPost = new HttpPost(sendMsgURI);
|
||||||
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
|
CloseableHttpResponse response = httpClient.execute(httpPost);
|
||||||
|
String responseString = EntityUtils.toString(response.getEntity());
|
||||||
String urlParameters = "userId=" +
|
Map<String, String> resultMap = handleResponse(responseString);
|
||||||
userId +
|
String resultCodeKey = "rspCode";
|
||||||
"&password=" +
|
if (resultMap.containsKey(resultCodeKey)) {
|
||||||
password +
|
if (!"0".equals(resultMap.get(resultCodeKey)) && !"DELIVRD".equalsIgnoreCase(resultMap.get(resultCodeKey))) {
|
||||||
"&content=" +
|
throw new RuntimeException("短信发送失败:" + resultMap.get("rspDesc"));
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
in.close();
|
|
||||||
|
|
||||||
//打印结果
|
|
||||||
System.out.println(response.toString());
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close() {
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -3,7 +3,6 @@ package com.cicdi.notify.sms.telecom;
|
|||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.cicdi.notify.*;
|
import com.cicdi.notify.*;
|
||||||
import com.cicdi.notify.sms.SmsProvider;
|
import com.cicdi.notify.sms.SmsProvider;
|
||||||
import com.cicdi.notify.sms.aliyun.AliyunSmsNotifier;
|
|
||||||
import com.cicdi.notify.template.Template;
|
import com.cicdi.notify.template.Template;
|
||||||
import com.cicdi.notify.template.TemplateManager;
|
import com.cicdi.notify.template.TemplateManager;
|
||||||
import com.cicdi.notify.template.TemplateProperties;
|
import com.cicdi.notify.template.TemplateProperties;
|
||||||
@ -27,7 +26,7 @@ public class TelecomSmsNotifierProvider implements NotifierProvider, TemplatePro
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Provider getProvider() {
|
public Provider getProvider() {
|
||||||
return SmsProvider.telecom;
|
return SmsProvider.js139;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -9,19 +9,20 @@ public class TelecomSmsTemplate implements Template {
|
|||||||
/**
|
/**
|
||||||
* 短信文本
|
* 短信文本
|
||||||
*/
|
*/
|
||||||
private String text;
|
private String content;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 手机号码
|
* 手机号码
|
||||||
|
* <p>多个手机号使用半角逗号分隔</p>
|
||||||
*/
|
*/
|
||||||
private String mobile;
|
private String mobile;
|
||||||
|
|
||||||
public String getText() {
|
public String getContent() {
|
||||||
return text;
|
return content;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setText(String text) {
|
public void setContent(String content) {
|
||||||
this.text = text;
|
this.content = content;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getMobile() {
|
public String getMobile() {
|
||||||
|
@ -24,7 +24,7 @@ public class AliyunSmsTest {
|
|||||||
public NotifierProperties getNotifyConfig(NotifyType notifyType, String configId) {
|
public NotifierProperties getNotifyConfig(NotifyType notifyType, String configId) {
|
||||||
NotifierProperties notifierProperties = new NotifierProperties();
|
NotifierProperties notifierProperties = new NotifierProperties();
|
||||||
notifierProperties.setType(DefaultNotifyType.sms.getId());
|
notifierProperties.setType(DefaultNotifyType.sms.getId());
|
||||||
notifierProperties.setProvider(SmsProvider.aliyunSms.getId());
|
notifierProperties.setProvider(SmsProvider.aliyun.getId());
|
||||||
notifierProperties.setId("12");
|
notifierProperties.setId("12");
|
||||||
|
|
||||||
Map<String, Object> config = new HashMap<>();
|
Map<String, Object> config = new HashMap<>();
|
||||||
@ -44,7 +44,7 @@ public class AliyunSmsTest {
|
|||||||
protected TemplateProperties getProperties(NotifyType type, String id) {
|
protected TemplateProperties getProperties(NotifyType type, String id) {
|
||||||
TemplateProperties templateProperties = new TemplateProperties();
|
TemplateProperties templateProperties = new TemplateProperties();
|
||||||
templateProperties.setType(DefaultNotifyType.sms.getId());
|
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\"}");
|
templateProperties.setTemplate("{\"signName\":\"Hello\",\"phoneNumber\":[\"xueye404@qq.com\"],\"code\":\"1322\"}");
|
||||||
|
|
||||||
|
@ -23,11 +23,10 @@ public class TelecomSmsTest {
|
|||||||
NotifyConfigManager notifyConfigManager = (notifyType, configId) -> {
|
NotifyConfigManager notifyConfigManager = (notifyType, configId) -> {
|
||||||
NotifierProperties notifierProperties = new NotifierProperties();
|
NotifierProperties notifierProperties = new NotifierProperties();
|
||||||
notifierProperties.setType(DefaultNotifyType.sms.getId());
|
notifierProperties.setType(DefaultNotifyType.sms.getId());
|
||||||
notifierProperties.setProvider(SmsProvider.telecom.getId());
|
notifierProperties.setProvider(SmsProvider.js139.getId());
|
||||||
notifierProperties.setId("12");
|
notifierProperties.setId("12");
|
||||||
|
|
||||||
Map<String, Object> config = new HashMap<>();
|
Map<String, Object> config = new HashMap<>();
|
||||||
config.put("serviceUrl", "http://www.js139.com.cn:8022/hysms/SendMsg");
|
|
||||||
config.put("userId", "HYjlzx");
|
config.put("userId", "HYjlzx");
|
||||||
config.put("password", "5CA45EA4944D2C32567E8DBBDDBD65DD");
|
config.put("password", "5CA45EA4944D2C32567E8DBBDDBD65DD");
|
||||||
|
|
||||||
@ -42,11 +41,11 @@ public class TelecomSmsTest {
|
|||||||
protected TemplateProperties getProperties(NotifyType type, String id) {
|
protected TemplateProperties getProperties(NotifyType type, String id) {
|
||||||
TemplateProperties templateProperties = new TemplateProperties();
|
TemplateProperties templateProperties = new TemplateProperties();
|
||||||
templateProperties.setType(DefaultNotifyType.sms.getId());
|
templateProperties.setType(DefaultNotifyType.sms.getId());
|
||||||
templateProperties.setProvider(SmsProvider.telecom.getId());
|
templateProperties.setProvider(SmsProvider.js139.getId());
|
||||||
|
|
||||||
TelecomSmsTemplate template = new TelecomSmsTemplate();
|
TelecomSmsTemplate template = new TelecomSmsTemplate();
|
||||||
template.setText("Hello World!");
|
template.setContent("Hello World!");
|
||||||
template.setMobile("18605120786");
|
template.setMobile("13047689449,18605120786");
|
||||||
|
|
||||||
templateProperties.setTemplate(JSON.toJSONString(template));
|
templateProperties.setTemplate(JSON.toJSONString(template));
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user