feat: 添加电信短信通知器

This commit is contained in:
椰子 2021-07-02 15:00:51 +08:00
parent 19de2dd4f9
commit 0b6c8d56b3
9 changed files with 373 additions and 0 deletions

View File

@ -8,6 +8,7 @@ package com.cicdi.notify;
public enum DefaultNotifyType implements NotifyType { public enum DefaultNotifyType implements NotifyType {
email("邮件"), email("邮件"),
sms("短信")
; ;
private final String name; private final String name;

43
notify-sms/pom.xml Normal file
View File

@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>notify</artifactId>
<groupId>com.cicdi</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>notify-sms</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.cicdi</groupId>
<artifactId>notify-core</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.73</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,28 @@
package com.cicdi.notify.sms;
import com.cicdi.notify.Provider;
/**
* @author xueye
*/
public enum SmsProvider implements Provider {
aliyunSms("阿里云短信服务"),
telecom("电信139短信服务");
private final String name;
SmsProvider(String name) {
this.name = name;
}
@Override
public String getId() {
return name();
}
@Override
public String getName() {
return name;
}
}

View File

@ -0,0 +1,51 @@
package com.cicdi.notify.sms;
import com.cicdi.notify.template.Template;
import java.util.List;
import java.util.Map;
/**
* @author xueye
*/
public class SmsTemplate implements Template {
/**
* 短信文本
*/
private String text;
/**
* 收件人
*/
private List<String> sendTo;
public String getTextSms(Map<String, Object> context) {
// TODO 对模板参数进行处理
// return ExpressionUtils.analytical(text, context, "spel");
return text;
}
public String[] getSendTo(Map<String, Object> context) {
// TODO 对收件人参数进行处理
// return sendTo.stream()
// .map(str -> ExpressionUtils.analytical(str, context, "spel")).toArray(String[]::new);
return sendTo.toArray(new String[0]);
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public List<String> getSendTo() {
return sendTo;
}
public void setSendTo(List<String> sendTo) {
this.sendTo = sendTo;
}
}

View File

@ -0,0 +1,103 @@
package com.cicdi.notify.sms.telecom;
import com.cicdi.notify.*;
import com.cicdi.notify.sms.SmsProvider;
import com.cicdi.notify.template.TemplateManager;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
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 String notifierId;
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.notifierId = properties.getId();
}
@Override
public String getNotifierId() {
return null;
}
@Override
public NotifyType getType() {
return DefaultNotifyType.sms;
}
@Override
public Provider getProvider() {
return SmsProvider.telecom;
}
@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);
}
in.close();
//打印结果
System.out.println(response.toString());
} catch (Exception e) {
}
}
@Override
public void close() {
}
}

View File

@ -0,0 +1,42 @@
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;
import com.cicdi.notify.template.TemplateProvider;
/**
* @author xueye
*/
public class TelecomSmsNotifierProvider implements NotifierProvider, TemplateProvider {
private final TemplateManager templateManager;
public TelecomSmsNotifierProvider(TemplateManager templateManager) {
this.templateManager = templateManager;
}
@Override
public NotifyType getType() {
return DefaultNotifyType.sms;
}
@Override
public Provider getProvider() {
return SmsProvider.telecom;
}
@Override
public Template createTemplate(TemplateProperties properties) {
return JSON.parseObject(properties.getTemplate(), TelecomSmsTemplate.class);
}
@Override
public Notifier<? extends Template> createNotifier(NotifierProperties properties) {
return new TelecomSmsNotifier(properties, templateManager);
}
}

View File

@ -0,0 +1,34 @@
package com.cicdi.notify.sms.telecom;
import com.cicdi.notify.template.Template;
/**
* @author xueye
*/
public class TelecomSmsTemplate implements Template {
/**
* 短信文本
*/
private String text;
/**
* 手机号码
*/
private String mobile;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
}

View File

@ -0,0 +1,70 @@
package com.cicdi.notify.sms;
import com.cicdi.notify.*;
import com.cicdi.notify.sms.aliyun.AliyunSmsNotifierProvider;
import com.cicdi.notify.sms.telecom.TelecomSmsNotifierProvider;
import com.cicdi.notify.template.AbstractTemplateManager;
import com.cicdi.notify.template.Template;
import com.cicdi.notify.template.TemplateManager;
import com.cicdi.notify.template.TemplateProperties;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
/**
* @author xueye
*/
public class TelecomSmsTest {
@Test
public void test() {
// 通知器配置管理器
NotifyConfigManager notifyConfigManager = new NotifyConfigManager() {
@Override
public NotifierProperties getNotifyConfig(NotifyType notifyType, String configId) {
NotifierProperties notifierProperties = new NotifierProperties();
notifierProperties.setType(DefaultNotifyType.sms.getId());
notifierProperties.setProvider(SmsProvider.telecom.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");
notifierProperties.setConfiguration(config);
return notifierProperties;
}
};
// 模板管理器
TemplateManager templateManager = new AbstractTemplateManager() {
@Override
protected TemplateProperties getProperties(NotifyType type, String id) {
TemplateProperties templateProperties = new TemplateProperties();
templateProperties.setType(DefaultNotifyType.sms.getId());
templateProperties.setProvider(SmsProvider.telecom.getId());
templateProperties.setTemplate("{\"text\":\"Hello\",\"mobile\":\"18605120786\"}");
return templateProperties;
}
};
NotifierManager notifierManager = new DefaultNotifierManager(notifyConfigManager);
// register
AliyunSmsNotifierProvider aliyunSmsNotifierProvider = new AliyunSmsNotifierProvider(templateManager);
TelecomSmsNotifierProvider telecomSmsNotifierProvider = new TelecomSmsNotifierProvider(templateManager);
notifierManager.registerProvider(aliyunSmsNotifierProvider);
notifierManager.registerProvider(telecomSmsNotifierProvider);
templateManager.register(aliyunSmsNotifierProvider);
templateManager.register(telecomSmsNotifierProvider);
Notifier<Template> notifier = notifierManager.getNotifier(DefaultNotifyType.sms, "123");
notifier.send(templateManager.getTemplate(DefaultNotifyType.sms, "124"), new HashMap<>());
}
}

View File

@ -11,6 +11,7 @@
<modules> <modules>
<module>notify-core</module> <module>notify-core</module>
<module>notify-email</module> <module>notify-email</module>
<module>notify-sms</module>
</modules> </modules>
<properties> <properties>