INotify
This commit is contained in:
parent
178ad6a65e
commit
10afdb3280
45
src/main/java/com/ycwl/basic/notify/NotifyFactory.java
Normal file
45
src/main/java/com/ycwl/basic/notify/NotifyFactory.java
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
package com.ycwl.basic.notify;
|
||||||
|
|
||||||
|
import com.ycwl.basic.notify.adapters.INotifyAdapter;
|
||||||
|
import com.ycwl.basic.notify.enums.NotifyType;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class NotifyFactory {
|
||||||
|
public static INotifyAdapter get(NotifyType type) {
|
||||||
|
switch (type) {
|
||||||
|
case SERVER_CHAN:
|
||||||
|
return new com.ycwl.basic.notify.adapters.ServerChanNotifyAdapter();
|
||||||
|
default:
|
||||||
|
throw new RuntimeException("不支持的通知类型");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected static Map<String, INotifyAdapter> namedNotifier = new HashMap<>();
|
||||||
|
protected static INotifyAdapter defaultNotifier = null;
|
||||||
|
|
||||||
|
public static void register(String name, INotifyAdapter adapter) {
|
||||||
|
namedNotifier.put(name, adapter);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static INotifyAdapter to(String name) {
|
||||||
|
INotifyAdapter adapter = namedNotifier.get(name);
|
||||||
|
if (adapter == null) {
|
||||||
|
throw new RuntimeException("未定义的通知方式:"+name);
|
||||||
|
}
|
||||||
|
return adapter;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static INotifyAdapter to() {
|
||||||
|
if (defaultNotifier == null) {
|
||||||
|
throw new RuntimeException("未定义默认通知方式");
|
||||||
|
}
|
||||||
|
return defaultNotifier;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setDefault(String defaultStorage) {
|
||||||
|
NotifyFactory.defaultNotifier = to(defaultStorage);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
package com.ycwl.basic.notify.adapters;
|
||||||
|
|
||||||
|
import com.ycwl.basic.notify.entity.NotifyContent;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public interface INotifyAdapter {
|
||||||
|
void loadConfig(Map<String, String> _config);
|
||||||
|
|
||||||
|
void send(NotifyContent notifyContent);
|
||||||
|
}
|
@ -0,0 +1,76 @@
|
|||||||
|
package com.ycwl.basic.notify.adapters;
|
||||||
|
|
||||||
|
import com.ycwl.basic.notify.entity.NotifyContent;
|
||||||
|
import com.ycwl.basic.notify.entity.ServerChanConfig;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
public class ServerChanNotifyAdapter implements INotifyAdapter {
|
||||||
|
ServerChanConfig config;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void loadConfig(Map<String, String> _config) {
|
||||||
|
ServerChanConfig config = new ServerChanConfig();
|
||||||
|
config.setKey(_config.get("key"));
|
||||||
|
config.checkEverythingOK();
|
||||||
|
this.config = config;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void send(NotifyContent notifyContent) {
|
||||||
|
scSend(notifyContent.getTitle(), notifyContent.getContent(), config.getKey());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String scSend(String title, String content, String key) {
|
||||||
|
try {
|
||||||
|
String api;
|
||||||
|
|
||||||
|
// 判断 sendkey 是否以 "sctp" 开头,并提取数字部分拼接 URL
|
||||||
|
if (key.startsWith("sctp")) {
|
||||||
|
Pattern pattern = Pattern.compile("sctp(\\d+)t");
|
||||||
|
Matcher matcher = pattern.matcher(key);
|
||||||
|
if (matcher.find()) {
|
||||||
|
String num = matcher.group(1);
|
||||||
|
api = "https://" + num + ".push.ft07.com/send/" + key +".send";
|
||||||
|
} else {
|
||||||
|
throw new IllegalArgumentException("Invalid sendkey format for sctp");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
api = "https://sctapi.ftqq.com/" + key + ".send";
|
||||||
|
}
|
||||||
|
|
||||||
|
String body = "text=" + URLEncoder.encode(title, "UTF-8") + "&desp=" + URLEncoder.encode(content, "UTF-8");
|
||||||
|
URL apiUrl = new URL(api);
|
||||||
|
HttpURLConnection httpConnection = (HttpURLConnection) apiUrl.openConnection();
|
||||||
|
httpConnection.setRequestMethod("POST");
|
||||||
|
httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
||||||
|
httpConnection.setDoOutput(true);
|
||||||
|
DataOutputStream bodyWriter = new DataOutputStream(httpConnection.getOutputStream());
|
||||||
|
bodyWriter.writeBytes(body);
|
||||||
|
bodyWriter.flush();
|
||||||
|
bodyWriter.close();
|
||||||
|
int responseCode = httpConnection.getResponseCode();
|
||||||
|
BufferedReader reader = new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));
|
||||||
|
StringBuilder responseText = new StringBuilder();
|
||||||
|
|
||||||
|
String line;
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
responseText.append(line);
|
||||||
|
}
|
||||||
|
|
||||||
|
reader.close();
|
||||||
|
return responseText.toString();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
package com.ycwl.basic.notify.entity;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class NotifyContent {
|
||||||
|
private String title;
|
||||||
|
private String content;
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.ycwl.basic.notify.entity;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class ServerChanConfig {
|
||||||
|
private String key;
|
||||||
|
|
||||||
|
public void checkEverythingOK() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
14
src/main/java/com/ycwl/basic/notify/enums/NotifyType.java
Normal file
14
src/main/java/com/ycwl/basic/notify/enums/NotifyType.java
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package com.ycwl.basic.notify.enums;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
public enum NotifyType {
|
||||||
|
SERVER_CHAN("SERVER_CHAN");
|
||||||
|
|
||||||
|
private final String type;
|
||||||
|
|
||||||
|
NotifyType(String type) {
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
package com.ycwl.basic.notify.starter;
|
||||||
|
|
||||||
|
import com.ycwl.basic.notify.NotifyFactory;
|
||||||
|
import com.ycwl.basic.notify.adapters.INotifyAdapter;
|
||||||
|
import com.ycwl.basic.notify.starter.config.NotifyConfigItem;
|
||||||
|
import com.ycwl.basic.notify.starter.config.OverallNotifyConfig;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class NotifyAutoConfigurator {
|
||||||
|
private final OverallNotifyConfig config;
|
||||||
|
public NotifyAutoConfigurator(OverallNotifyConfig config) {
|
||||||
|
this.config = config;
|
||||||
|
if (config != null) {
|
||||||
|
if (config.getConfigs() != null) {
|
||||||
|
loadConfig();
|
||||||
|
}
|
||||||
|
if (StringUtils.isNotBlank(config.getDefaultUse())) {
|
||||||
|
NotifyFactory.setDefault(config.getDefaultUse());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void loadConfig() {
|
||||||
|
for (NotifyConfigItem item : config.getConfigs()) {
|
||||||
|
INotifyAdapter adapter = NotifyFactory.get(item.getType());
|
||||||
|
adapter.loadConfig(item.getConfig());
|
||||||
|
NotifyFactory.register(item.getName(), adapter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
package com.ycwl.basic.notify.starter.config;
|
||||||
|
|
||||||
|
import com.ycwl.basic.notify.enums.NotifyType;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class NotifyConfigItem {
|
||||||
|
private String name;
|
||||||
|
private NotifyType type;
|
||||||
|
private Map<String, String> config;
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.ycwl.basic.notify.starter.config;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@ConfigurationProperties(prefix = "notify")
|
||||||
|
@Data
|
||||||
|
public class OverallNotifyConfig {
|
||||||
|
private String defaultUse;
|
||||||
|
private List<NotifyConfigItem> configs;
|
||||||
|
}
|
@ -156,3 +156,11 @@ aliFace:
|
|||||||
accessKeyId: "LTAI5tMwrmxVcUEKoH5QzLHx"
|
accessKeyId: "LTAI5tMwrmxVcUEKoH5QzLHx"
|
||||||
accessKeySecret: "ZCIP8aKx1jwX1wkeYIPQEDZ8fPtN1c"
|
accessKeySecret: "ZCIP8aKx1jwX1wkeYIPQEDZ8fPtN1c"
|
||||||
region: "cn-shanghai"
|
region: "cn-shanghai"
|
||||||
|
|
||||||
|
notify:
|
||||||
|
defaultUse: "developer"
|
||||||
|
configs:
|
||||||
|
- name: "developer"
|
||||||
|
type: "SERVER_CHAN"
|
||||||
|
config:
|
||||||
|
key: sctp747tje1xjxbwn2fodgu1qezpn3
|
@ -157,3 +157,11 @@ aliFace:
|
|||||||
accessKeyId: "LTAI5tMwrmxVcUEKoH5QzLHx"
|
accessKeyId: "LTAI5tMwrmxVcUEKoH5QzLHx"
|
||||||
accessKeySecret: "ZCIP8aKx1jwX1wkeYIPQEDZ8fPtN1c"
|
accessKeySecret: "ZCIP8aKx1jwX1wkeYIPQEDZ8fPtN1c"
|
||||||
region: "cn-shanghai"
|
region: "cn-shanghai"
|
||||||
|
|
||||||
|
notify:
|
||||||
|
defaultUse: "developer"
|
||||||
|
configs:
|
||||||
|
- name: "developer"
|
||||||
|
type: "SERVER_CHAN"
|
||||||
|
config:
|
||||||
|
key: sctp747tje1xjxbwn2fodgu1qezpn3
|
Loading…
x
Reference in New Issue
Block a user