This is an automated email from the ASF dual-hosted git repository.

gongchao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hertzbeat.git


The following commit(s) were added to refs/heads/master by this push:
     new 53787cc21 [bugfix] auto config timezone for Jackson (#2122)
53787cc21 is described below

commit 53787cc21588805b3834a4c33239b8b958d2d062
Author: Calvin <[email protected]>
AuthorDate: Sun Jul 7 14:52:13 2024 +0800

    [bugfix] auto config timezone for Jackson (#2122)
    
    Co-authored-by: tomsun28 <[email protected]>
---
 .../apache/hertzbeat/common/util/TimeZoneUtil.java | 56 ++++++++++++++++++++++
 e2e/data/monitor-ftp.json                          |  8 ++--
 e2e/data/monitor-http.json                         | 15 ------
 e2e/data/monitor-ping.json                         |  4 +-
 e2e/data/monitor-port.json                         |  6 +--
 e2e/data/monitor-sitemap.json                      |  8 ++--
 e2e/data/monitor-ssl.json                          |  4 +-
 e2e/data/monitor-udp.json                          |  6 +--
 e2e/data/monitor-website.json                      |  8 ++--
 e2e/testsuite.yaml                                 |  4 +-
 .../component/listener/TimeZoneListener.java       | 47 ++++++++++++++++++
 .../manager/config/CommonCommandLineRunner.java    | 23 ++++-----
 .../hertzbeat/manager/config/JacksonConfig.java    | 45 +++++++++++++++++
 .../impl/SystemGeneralConfigServiceImpl.java       | 26 +++-------
 14 files changed, 188 insertions(+), 72 deletions(-)

diff --git 
a/common/src/main/java/org/apache/hertzbeat/common/util/TimeZoneUtil.java 
b/common/src/main/java/org/apache/hertzbeat/common/util/TimeZoneUtil.java
new file mode 100644
index 000000000..6a9e157cd
--- /dev/null
+++ b/common/src/main/java/org/apache/hertzbeat/common/util/TimeZoneUtil.java
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hertzbeat.common.util;
+
+import java.util.Locale;
+import java.util.TimeZone;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hertzbeat.common.constants.CommonConstants;
+
+/**
+ * timezone util
+ */
+public class TimeZoneUtil {
+    private static final Integer LANG_REGION_LENGTH = 2;
+
+    public static void setTimeZoneAndLocale(String timeZoneId, String locale) {
+        setTimeZone(timeZoneId);
+        setLocale(locale);
+    }
+
+    public static void setTimeZone(String timeZoneId) {
+        if (StringUtils.isBlank(timeZoneId)) {
+            return;
+        }
+
+        TimeZone.setDefault(TimeZone.getTimeZone(timeZoneId));
+    }
+
+    public static void setLocale(String locale) {
+        if (StringUtils.isBlank(locale)) {
+            return;
+        }
+
+        String[] arr = locale.split(CommonConstants.LOCALE_SEPARATOR);
+        if (arr.length == LANG_REGION_LENGTH) {
+            String language = arr[0];
+            String country = arr[1];
+            Locale.setDefault(new Locale(language, country));
+        }
+    }
+}
diff --git a/e2e/data/monitor-ftp.json b/e2e/data/monitor-ftp.json
index 3be432eea..7ebfc5ee7 100644
--- a/e2e/data/monitor-ftp.json
+++ b/e2e/data/monitor-ftp.json
@@ -12,22 +12,22 @@
       {
         "field": "host",
         "type": 1,
-        "value": "127.0.0.1"
+        "paramValue": "127.0.0.1"
       },
       {
         "field": "port",
         "type": 0,
-        "value": 21
+        "paramValue": 21
       },
       {
         "field": "direction",
         "type": 1,
-        "value": "127.0.0.1"
+        "paramValue": "127.0.0.1"
       },
       {
         "field": "timeout",
         "type": 0,
-        "value": 1000
+        "paramValue": 1000
       },
       {
         "field": "username",
diff --git a/e2e/data/monitor-http.json b/e2e/data/monitor-http.json
index 841610abc..c53fcb7ef 100644
--- a/e2e/data/monitor-http.json
+++ b/e2e/data/monitor-http.json
@@ -10,81 +10,66 @@
   },
   "params": [
     {
-      "display": true,
       "field": "host",
       "type": 1,
       "paramValue": "127.0.0.1"
     },
     {
-      "display": true,
       "field": "port",
       "type": 0,
       "paramValue": 80
     },
     {
-      "display": true,
       "field": "httpMethod",
       "type": 1,
       "paramValue": "GET"
     },
     {
-      "display": true,
       "field": "uri",
       "type": 1
     },
     {
-      "display": true,
       "field": "ssl",
       "type": 1,
       "paramValue": false
     },
     {
-      "display": true,
       "field": "headers",
       "type": 3
     },
     {
-      "display": true,
       "field": "params",
       "type": 3
     },
     {
-      "display": true,
       "field": "timeout",
       "type": 0
     },
     {
-      "display": true,
       "field": "contentType",
       "type": 1
     },
     {
-      "display": false,
       "field": "payload",
       "type": 1
     },
     {
-      "display": true,
       "field": "authType",
       "type": 1
     },
     {
-      "display": true,
       "field": "username",
       "type": 1
     },
     {
-      "display": true,
       "field": "password",
       "type": 1
     },
     {
-      "display": true,
       "field": "keyword",
       "type": 1
     },
     {
-      "display": true,
       "field": "successCode",
       "type": 4,
       "paramValue": "200, 201"
diff --git a/e2e/data/monitor-ping.json b/e2e/data/monitor-ping.json
index b592b9b5f..4bc332a22 100644
--- a/e2e/data/monitor-ping.json
+++ b/e2e/data/monitor-ping.json
@@ -12,12 +12,12 @@
       {
         "field": "host",
         "type": 1,
-        "value": "127.0.0.1"
+        "paramValue": "127.0.0.1"
       },
       {
         "field": "timeout",
         "type": 0,
-        "value": 6000
+        "paramValue": 6000
       }
     ]
 }
diff --git a/e2e/data/monitor-port.json b/e2e/data/monitor-port.json
index a41a26d2d..4c2afc621 100644
--- a/e2e/data/monitor-port.json
+++ b/e2e/data/monitor-port.json
@@ -12,17 +12,17 @@
       {
         "field": "host",
         "type": 1,
-        "value": "127.0.0.1"
+        "paramValue": "127.0.0.1"
       },
       {
         "field": "port",
         "type": 0,
-        "value": 80
+        "paramValue": 80
       },
       {
         "field": "timeout",
         "type": 0,
-        "value": 6000
+        "paramValue": 6000
       }
     ]
 }
diff --git a/e2e/data/monitor-sitemap.json b/e2e/data/monitor-sitemap.json
index 9b630fa83..2be2d1bf6 100644
--- a/e2e/data/monitor-sitemap.json
+++ b/e2e/data/monitor-sitemap.json
@@ -12,22 +12,22 @@
       {
         "field": "host",
         "type": 1,
-        "value": "127.0.0.1"
+        "paramValue": "127.0.0.1"
       },
       {
         "field": "port",
         "type": 0,
-        "value": 80
+        "paramValue": 80
       },
       {
         "field": "sitemap",
         "type": 1,
-        "value": "sitemap.xml"
+        "paramValue": "sitemap.xml"
       },
       {
         "field": "ssl",
         "type": 1,
-        "value": false
+        "paramValue": false
       }
     ]
 }
diff --git a/e2e/data/monitor-ssl.json b/e2e/data/monitor-ssl.json
index 6e36c181d..6ebbc7a15 100644
--- a/e2e/data/monitor-ssl.json
+++ b/e2e/data/monitor-ssl.json
@@ -12,12 +12,12 @@
       {
         "field": "host",
         "type": 1,
-        "value": "127.0.0.1"
+        "paramValue": "127.0.0.1"
       },
       {
         "field": "port",
         "type": 0,
-        "value": 443
+        "paramValue": 443
       },
       {
         "field": "uri",
diff --git a/e2e/data/monitor-udp.json b/e2e/data/monitor-udp.json
index cb076c18c..26c41c706 100644
--- a/e2e/data/monitor-udp.json
+++ b/e2e/data/monitor-udp.json
@@ -12,17 +12,17 @@
       {
         "field": "host",
         "type": 1,
-        "value": "127.0.0.1"
+        "paramValue": "127.0.0.1"
       },
       {
         "field": "port",
         "type": 0,
-        "value": 81
+        "paramValue": 81
       },
       {
         "field": "timeout",
         "type": 0,
-        "value": 6000
+        "paramValue": 6000
       },
       {
         "field": "content",
diff --git a/e2e/data/monitor-website.json b/e2e/data/monitor-website.json
index db4b520e6..804d837ff 100644
--- a/e2e/data/monitor-website.json
+++ b/e2e/data/monitor-website.json
@@ -12,21 +12,21 @@
       {
         "field": "host",
         "type": 1,
-        "value": "127.0.0.1"
+        "paramValue": "127.0.0.1"
       },
       {
         "field": "port",
         "type": 0,
-        "value": 80
+        "paramValue": 80
       },
       {
         "field": "uri",
-        "type": 1
+        "paramValue": 1
       },
       {
         "field": "ssl",
         "type": 1,
-        "value": false
+        "paramValue": false
       },
       {
         "field": "timeout",
diff --git a/e2e/testsuite.yaml b/e2e/testsuite.yaml
index 4435fc5f1..c78962f80 100644
--- a/e2e/testsuite.yaml
+++ b/e2e/testsuite.yaml
@@ -222,7 +222,7 @@ items:
       ]
 - name: listTags
   request:
-    api: /api/tag
+    api: /api/tag?pageIndex=0&pageSize=8&type=1
     header:
       Authorization: Bearer {{.login.data.token}}
 - name: updateTag
@@ -236,7 +236,7 @@ items:
       {
         "id": {{(index .listTags.data.content 0).id | int64}},
         "name": "{{randAlpha 3}}",
-        "value": "{{randAlpha 3}}",
+        "tagValue": "{{randAlpha 3}}",
         "color": "#ff4081",
         "type": 1,
         "creator": "admin",
diff --git 
a/manager/src/main/java/org/apache/hertzbeat/manager/component/listener/TimeZoneListener.java
 
b/manager/src/main/java/org/apache/hertzbeat/manager/component/listener/TimeZoneListener.java
new file mode 100644
index 000000000..2801ab7c8
--- /dev/null
+++ 
b/manager/src/main/java/org/apache/hertzbeat/manager/component/listener/TimeZoneListener.java
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hertzbeat.manager.component.listener;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import jakarta.annotation.Resource;
+import java.text.SimpleDateFormat;
+import java.util.TimeZone;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.hertzbeat.common.support.event.SystemConfigChangeEvent;
+import org.springframework.context.event.EventListener;
+import org.springframework.stereotype.Component;
+
+/**
+ * Listener for config Jackson timezone
+ */
+@Slf4j
+@Component
+public class TimeZoneListener {
+    @Resource
+    private ObjectMapper objectMapper;
+
+    @EventListener(SystemConfigChangeEvent.class)
+    public void onEvent(SystemConfigChangeEvent event) {
+        log.info("{} receive system config change event: {}.", 
this.getClass().getName(), event.getSource());
+
+        final SimpleDateFormat simpleDateFormat = new 
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
+        simpleDateFormat.setTimeZone(TimeZone.getDefault());
+        objectMapper.setTimeZone(TimeZone.getDefault())
+                .setDateFormat(simpleDateFormat);
+    }
+}
diff --git 
a/manager/src/main/java/org/apache/hertzbeat/manager/config/CommonCommandLineRunner.java
 
b/manager/src/main/java/org/apache/hertzbeat/manager/config/CommonCommandLineRunner.java
index 371718878..71fc96023 100644
--- 
a/manager/src/main/java/org/apache/hertzbeat/manager/config/CommonCommandLineRunner.java
+++ 
b/manager/src/main/java/org/apache/hertzbeat/manager/config/CommonCommandLineRunner.java
@@ -20,11 +20,13 @@ package org.apache.hertzbeat.manager.config;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.usthe.sureness.util.JsonWebTokenUtil;
 import jakarta.annotation.Resource;
+import java.text.SimpleDateFormat;
 import java.util.Locale;
 import java.util.Random;
 import java.util.TimeZone;
 import org.apache.hertzbeat.common.constants.CommonConstants;
 import org.apache.hertzbeat.common.entity.manager.GeneralConfig;
+import org.apache.hertzbeat.common.util.TimeZoneUtil;
 import org.apache.hertzbeat.manager.dao.GeneralConfigDao;
 import org.apache.hertzbeat.manager.pojo.dto.SystemConfig;
 import org.apache.hertzbeat.manager.pojo.dto.SystemSecret;
@@ -46,9 +48,7 @@ import org.springframework.util.StringUtils;
 @Component
 @Order(value = Ordered.HIGHEST_PRECEDENCE + 2)
 public class CommonCommandLineRunner implements CommandLineRunner {
-    
-    private static final Integer LANG_REGION_LENGTH = 2;
-    
+
     private static final String DEFAULT_JWT_SECRET = 
"CyaFv0bwq2Eik0jdrKUtsA6bx3sDJeFV643R " 
             + "LnfKefTjsIfJLBa2YkhEqEGtcHDTNe4CU6+9 "
             + "8tVt4bisXQ13rbN0oxhUZR73M6EByXIO+SV5 "
@@ -80,17 +80,12 @@ public class CommonCommandLineRunner implements 
CommandLineRunner {
         // for system config
         SystemConfig systemConfig = systemGeneralConfigService.getConfig();
         if (systemConfig != null) {
-            if (systemConfig.getTimeZoneId() != null) {
-                
TimeZone.setDefault(TimeZone.getTimeZone(systemConfig.getTimeZoneId()));
-            }
-            if (systemConfig.getLocale() != null) {
-                String[] arr = 
systemConfig.getLocale().split(CommonConstants.LOCALE_SEPARATOR);
-                if (arr.length == LANG_REGION_LENGTH) {
-                    String language = arr[0];
-                    String country = arr[1];
-                    Locale.setDefault(new Locale(language, country));   
-                }
-            }
+            TimeZoneUtil.setTimeZoneAndLocale(systemConfig.getTimeZoneId(), 
systemConfig.getLocale());
+
+            final SimpleDateFormat simpleDateFormat = new 
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
+            simpleDateFormat.setTimeZone(TimeZone.getDefault());
+            objectMapper.setTimeZone(TimeZone.getDefault())
+                    .setDateFormat(simpleDateFormat);
         } else {
             // init system config data
             systemConfig = 
SystemConfig.builder().timeZoneId(TimeZone.getDefault().getID())
diff --git 
a/manager/src/main/java/org/apache/hertzbeat/manager/config/JacksonConfig.java 
b/manager/src/main/java/org/apache/hertzbeat/manager/config/JacksonConfig.java
new file mode 100644
index 000000000..820f4e35c
--- /dev/null
+++ 
b/manager/src/main/java/org/apache/hertzbeat/manager/config/JacksonConfig.java
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hertzbeat.manager.config;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
+import java.text.SimpleDateFormat;
+import java.util.TimeZone;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * jackson config
+ */
+@Slf4j
+@Configuration
+public class JacksonConfig {
+    @Bean
+    public ObjectMapper objectMapper() {
+        JavaTimeModule javaTimeModule = new JavaTimeModule();
+        final SimpleDateFormat simpleDateFormat = new 
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
+        simpleDateFormat.setTimeZone(TimeZone.getDefault());
+
+        return new ObjectMapper()
+                .registerModule(javaTimeModule)
+                .setTimeZone(TimeZone.getDefault())
+                .setDateFormat(simpleDateFormat);
+    }
+}
diff --git 
a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/SystemGeneralConfigServiceImpl.java
 
b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/SystemGeneralConfigServiceImpl.java
index fc650a237..9a87eec4a 100644
--- 
a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/SystemGeneralConfigServiceImpl.java
+++ 
b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/SystemGeneralConfigServiceImpl.java
@@ -21,10 +21,9 @@ import com.fasterxml.jackson.core.type.TypeReference;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import jakarta.annotation.Resource;
 import java.lang.reflect.Type;
-import java.util.Locale;
-import java.util.TimeZone;
-import org.apache.hertzbeat.common.constants.CommonConstants;
+import java.util.Objects;
 import org.apache.hertzbeat.common.support.event.SystemConfigChangeEvent;
+import org.apache.hertzbeat.common.util.TimeZoneUtil;
 import org.apache.hertzbeat.manager.dao.GeneralConfigDao;
 import org.apache.hertzbeat.manager.pojo.dto.SystemConfig;
 import org.springframework.context.ApplicationContext;
@@ -35,9 +34,6 @@ import org.springframework.stereotype.Service;
  */
 @Service
 public class SystemGeneralConfigServiceImpl extends 
AbstractGeneralConfigServiceImpl<SystemConfig> {
-    
-    private static final Integer LANG_REGION_LENGTH = 2;
-    
     @Resource
     private ApplicationContext applicationContext;
     
@@ -54,20 +50,12 @@ public class SystemGeneralConfigServiceImpl extends 
AbstractGeneralConfigService
     
     @Override
     public void handler(SystemConfig systemConfig) {
-        if (systemConfig != null) {
-            if (systemConfig.getTimeZoneId() != null) {
-                
TimeZone.setDefault(TimeZone.getTimeZone(systemConfig.getTimeZoneId()));
-            }
-            if (systemConfig.getLocale() != null) {
-                String[] arr = 
systemConfig.getLocale().split(CommonConstants.LOCALE_SEPARATOR);
-                if (arr.length == LANG_REGION_LENGTH) {
-                    String language = arr[0];
-                    String country = arr[1];
-                    Locale.setDefault(new Locale(language, country));
-                }
-            }
-            applicationContext.publishEvent(new 
SystemConfigChangeEvent(applicationContext));
+        if (Objects.isNull(systemConfig)) {
+            return;
         }
+
+        TimeZoneUtil.setTimeZoneAndLocale(systemConfig.getTimeZoneId(), 
systemConfig.getLocale());
+        applicationContext.publishEvent(new 
SystemConfigChangeEvent(applicationContext));
     }
     
     @Override


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to