This is an automated email from the ASF dual-hosted git repository. xiaoyu pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push: new 3b5f075449 [type:improve] Optimize code for shenyu-alert (#5865) 3b5f075449 is described below commit 3b5f075449035de41a54315ec586f4d9ae54d6f2 Author: po-168 <po...@foxmail.com> AuthorDate: Thu Dec 26 14:43:51 2024 +0800 [type:improve] Optimize code for shenyu-alert (#5865) * [type:improve] Optimize code for shenyu-alert(#5865) --------- Co-authored-by: aias00 <liuhon...@apache.org> Co-authored-by: xiaoyu <xia...@apache.org> --- .../org/apache/shenyu/alert/config/HeaderRequestInterceptor.java | 8 +++++--- .../org/apache/shenyu/alert/exception/AlertNoticeException.java | 3 +++ .../main/java/org/apache/shenyu/alert/model/AlertReceiverDTO.java | 4 +++- .../apache/shenyu/alert/strategy/AbstractAlertNotifyHandler.java | 3 ++- .../apache/shenyu/alert/strategy/EmailAlertNotifyStrategy.java | 3 ++- 5 files changed, 15 insertions(+), 6 deletions(-) diff --git a/shenyu-alert/src/main/java/org/apache/shenyu/alert/config/HeaderRequestInterceptor.java b/shenyu-alert/src/main/java/org/apache/shenyu/alert/config/HeaderRequestInterceptor.java index e291495bc6..2a025039f2 100644 --- a/shenyu-alert/src/main/java/org/apache/shenyu/alert/config/HeaderRequestInterceptor.java +++ b/shenyu-alert/src/main/java/org/apache/shenyu/alert/config/HeaderRequestInterceptor.java @@ -25,6 +25,7 @@ import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.http.client.ClientHttpResponse; import java.io.IOException; +import java.util.Objects; /** * Rest Template interceptor adds request header information. @@ -35,11 +36,12 @@ public class HeaderRequestInterceptor implements ClientHttpRequestInterceptor { public ClientHttpResponse intercept(final HttpRequest request, final byte[] body, final ClientHttpRequestExecution execution) throws IOException { // Send json by default - if (request.getHeaders().getContentType() == null) { - request.getHeaders().setContentType(MediaType.APPLICATION_JSON); + HttpHeaders headers = request.getHeaders(); + if (Objects.isNull(headers.getContentType())) { + headers.setContentType(MediaType.APPLICATION_JSON); } // Use short links - request.getHeaders().add(HttpHeaders.CONNECTION, "close"); + headers.add(HttpHeaders.CONNECTION, "close"); return execution.execute(request, body); } } diff --git a/shenyu-alert/src/main/java/org/apache/shenyu/alert/exception/AlertNoticeException.java b/shenyu-alert/src/main/java/org/apache/shenyu/alert/exception/AlertNoticeException.java index a0ff3f56f6..6c73106b45 100644 --- a/shenyu-alert/src/main/java/org/apache/shenyu/alert/exception/AlertNoticeException.java +++ b/shenyu-alert/src/main/java/org/apache/shenyu/alert/exception/AlertNoticeException.java @@ -21,6 +21,9 @@ package org.apache.shenyu.alert.exception; * alert notice send failed. */ public class AlertNoticeException extends RuntimeException { + + private static final long serialVersionUID = 1904557336581968426L; + public AlertNoticeException(final String message) { super(message); } diff --git a/shenyu-alert/src/main/java/org/apache/shenyu/alert/model/AlertReceiverDTO.java b/shenyu-alert/src/main/java/org/apache/shenyu/alert/model/AlertReceiverDTO.java index 4400ef6154..1a483832bc 100644 --- a/shenyu-alert/src/main/java/org/apache/shenyu/alert/model/AlertReceiverDTO.java +++ b/shenyu-alert/src/main/java/org/apache/shenyu/alert/model/AlertReceiverDTO.java @@ -26,7 +26,9 @@ import java.util.Map; * AlertReceiver. */ public class AlertReceiverDTO implements Serializable { - + + private static final long serialVersionUID = 1790514185328089645L; + /** * primary key id. */ diff --git a/shenyu-alert/src/main/java/org/apache/shenyu/alert/strategy/AbstractAlertNotifyHandler.java b/shenyu-alert/src/main/java/org/apache/shenyu/alert/strategy/AbstractAlertNotifyHandler.java index e22079bc33..a2a3016340 100644 --- a/shenyu-alert/src/main/java/org/apache/shenyu/alert/strategy/AbstractAlertNotifyHandler.java +++ b/shenyu-alert/src/main/java/org/apache/shenyu/alert/strategy/AbstractAlertNotifyHandler.java @@ -27,6 +27,7 @@ import jakarta.annotation.Resource; import java.time.Instant; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +import java.util.Objects; import java.util.TimeZone; /** @@ -67,7 +68,7 @@ abstract class AbstractAlertNotifyHandler implements AlertNotifyHandler { protected abstract String templateName(); private static String removeBlankLine(final String value) { - if (value == null) { + if (Objects.isNull(value)) { return null; } return value.replaceAll("(?m)^\\s*$(\\n|\\r\\n)", ""); diff --git a/shenyu-alert/src/main/java/org/apache/shenyu/alert/strategy/EmailAlertNotifyStrategy.java b/shenyu-alert/src/main/java/org/apache/shenyu/alert/strategy/EmailAlertNotifyStrategy.java index e1c810963b..658e847f51 100644 --- a/shenyu-alert/src/main/java/org/apache/shenyu/alert/strategy/EmailAlertNotifyStrategy.java +++ b/shenyu-alert/src/main/java/org/apache/shenyu/alert/strategy/EmailAlertNotifyStrategy.java @@ -31,6 +31,7 @@ import org.thymeleaf.context.Context; import jakarta.mail.internet.MimeMessage; import java.text.SimpleDateFormat; import java.util.Date; +import java.util.Objects; /** * email alert notice. @@ -80,7 +81,7 @@ final class EmailAlertNotifyStrategy implements AlertNotifyHandler { context.setVariable("content", alert.getContent()); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date alertTime = alert.getDateCreated(); - if (alertTime == null) { + if (Objects.isNull(alert)) { alertTime = new Date(); } String alarmTime = simpleDateFormat.format(alertTime);