|
|
@@ -0,0 +1,117 @@
|
|
|
+package org.jetlinks.community.notify.webhook;
|
|
|
+
|
|
|
+import cn.hutool.http.HttpRequest;
|
|
|
+import cn.hutool.http.HttpResponse;
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
+import cn.hutool.http.Method;
|
|
|
+import lombok.Getter;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.hswebframework.web.exception.BusinessException;
|
|
|
+import org.jetlinks.community.notify.*;
|
|
|
+import org.jetlinks.community.notify.template.TemplateManager;
|
|
|
+import org.jetlinks.core.Values;
|
|
|
+import reactor.core.publisher.Mono;
|
|
|
+
|
|
|
+import javax.annotation.Nonnull;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author lifang
|
|
|
+ * @version 1.0.0
|
|
|
+ * @ClassName WebHookNotifier.java
|
|
|
+ * @Description TODO
|
|
|
+ * @createTime 2021年08月30日 09:35:00
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+public class WebHookNotifier extends AbstractNotifier<WebHookTemplate> {
|
|
|
+ private String method;
|
|
|
+ private String url;
|
|
|
+ private Long connectionTimeout;
|
|
|
+
|
|
|
+ private Long readTimeout;
|
|
|
+
|
|
|
+ @Getter
|
|
|
+ private String notifierId;
|
|
|
+
|
|
|
+
|
|
|
+ private HttpRequest request;
|
|
|
+ public WebHookNotifier(NotifierProperties profile, TemplateManager templateManager) {
|
|
|
+ super(templateManager);
|
|
|
+ Map<String, Object> config = profile.getConfiguration();
|
|
|
+ this.method = (String) Objects.requireNonNull(config.get("method"), "请求方法不能为空");
|
|
|
+ this.url=(String) Objects.requireNonNull(config.get("url"), "url不能为空");
|
|
|
+ this.connectionTimeout=(Long) Objects.requireNonNull(config.get("connectionTimeout"), "连接超时时长不能为空");
|
|
|
+ this.readTimeout=(Long) Objects.requireNonNull(config.get("readTimeout"), "请求超时时长不能为空");
|
|
|
+ switch (method){
|
|
|
+ case "post":
|
|
|
+ request = HttpUtil.createPost(url);
|
|
|
+ break;
|
|
|
+ case "get":
|
|
|
+ request = HttpUtil.createGet(url);
|
|
|
+ break;
|
|
|
+ default: throw new BusinessException(String.format("http不支持该方法{%s}",method));
|
|
|
+ }
|
|
|
+ request.setReadTimeout(Long.valueOf(TimeUnit.SECONDS.toMillis(readTimeout)).intValue());
|
|
|
+ request.setConnectionTimeout(Long.valueOf(TimeUnit.SECONDS.toMillis(connectionTimeout)).intValue());
|
|
|
+ this.notifierId = profile.getId();
|
|
|
+ }
|
|
|
+
|
|
|
+ public WebHookNotifier(TemplateManager templateManager) {
|
|
|
+ super(templateManager);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String getNotifierId() {
|
|
|
+ return notifierId;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Nonnull
|
|
|
+ @Override
|
|
|
+ public NotifyType getType() {
|
|
|
+ return DefaultNotifyType.webHook;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Nonnull
|
|
|
+ @Override
|
|
|
+ public Provider getProvider() {
|
|
|
+ return WebHookProvider.webHook;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Nonnull
|
|
|
+ @Override
|
|
|
+ public Mono<Void> send(@Nonnull WebHookTemplate template, @Nonnull Values context) {
|
|
|
+ return Mono.defer(()->{
|
|
|
+ try {
|
|
|
+ Method method = request.getMethod();
|
|
|
+ switch (method){
|
|
|
+ case GET:
|
|
|
+ Map<String, String> headers = new HashMap<>();
|
|
|
+ context.getAllValues().forEach((k,v)->headers.put(k,String.valueOf(v)));
|
|
|
+ request.addHeaders(headers);
|
|
|
+ break;
|
|
|
+ case PUT:
|
|
|
+ request.body(context.toString());
|
|
|
+ break;
|
|
|
+ default:break;
|
|
|
+ }
|
|
|
+ HttpResponse response = request.execute();
|
|
|
+ if (200!=response.getStatus()) {
|
|
|
+ return Mono.error(new BusinessException(response.body(), response.getStatus()));
|
|
|
+ }
|
|
|
+ }catch (Exception e){
|
|
|
+ return Mono.error(e);
|
|
|
+ }
|
|
|
+ return Mono.empty();
|
|
|
+ }
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ @Nonnull
|
|
|
+ @Override
|
|
|
+ public Mono<Void> close() {
|
|
|
+ return Mono.fromRunnable(()->request.getConnection().disconnect());
|
|
|
+ }
|
|
|
+}
|