From aec3d98bbf6f3565a6e173df67ea5cb46a172c05 Mon Sep 17 00:00:00 2001 From: pizihao <48643103+pizihao@users.noreply.github.com> Date: Sun, 9 Oct 2022 16:10:28 +0800 Subject: [PATCH] New feature (#779) * feat : supplement StringUtil * fix : To solve RestTemplate Missing parameter (#775) * fix : again appoint MediaType for request post (#770) --- .../cn/hippo4j/common/toolkit/StringUtil.java | 50 +++++++++++++++++ .../common/toolkit/StringUtilTest.java | 53 ++++++++++++++----- .../service/ThreadPoolAdapterService.java | 41 +++++++------- .../ThreadPoolAdapterController.java | 19 ++++--- .../controller/ThreadPoolController.java | 49 +++++++---------- .../platform/WeChatSendMessageHandler.java | 1 - 6 files changed, 137 insertions(+), 76 deletions(-) diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/StringUtil.java b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/StringUtil.java index 553d2acb..e81c1999 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/StringUtil.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/StringUtil.java @@ -223,6 +223,56 @@ public class StringUtil { return sb.toString(); } + /** + * combination CharSequence, get a String + * + * @param charSequences CharSequence, if null or empty, get {@link StringUtil#EMPTY} + * @return String + */ + public static String newBuilder(CharSequence... charSequences) { + if (charSequences == null || charSequences.length == 0) { + return StringUtil.EMPTY; + } + return createBuilder(charSequences).toString(); + } + + /** + * combination CharSequence, get a StringBuilder + * + * @param charSequences CharSequence + * @return StringBuilder + */ + public static StringBuilder createBuilder(CharSequence... charSequences) { + StringBuilder builder = new StringBuilder(); + if (charSequences == null || charSequences.length == 0) { + return builder; + } + for (CharSequence sequence : charSequences) { + builder.append(sequence); + } + return builder; + } + + /** + * combination CharSequence, to StringBuilder + * + * @param builder StringBuilder, if null create a new + * @param charSequences CharSequence + * @return StringBuilder + */ + public static StringBuilder appends(StringBuilder builder, CharSequence... charSequences) { + if (builder == null) { + return createBuilder(charSequences); + } + if (charSequences == null || charSequences.length == 0) { + return builder; + } + for (CharSequence sequence : charSequences) { + builder.append(sequence); + } + return builder; + } + /** * Replace a portion of the string, replacing all found * diff --git a/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/StringUtilTest.java b/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/StringUtilTest.java index d2f9173c..9d9c1f02 100644 --- a/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/StringUtilTest.java +++ b/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/StringUtilTest.java @@ -18,83 +18,108 @@ package cn.hippo4j.common.toolkit; import org.junit.Test; - -import java.util.Objects; +import org.junit.Assert; public class StringUtilTest { @Test public void assertIsEmpty() { String string = ""; - Assert.isTrue(StringUtil.isEmpty(string)); + Assert.assertTrue(StringUtil.isEmpty(string)); } @Test public void assertIsNotEmpty() { String string = "string"; - Assert.isTrue(StringUtil.isNotEmpty(string)); + Assert.assertTrue(StringUtil.isNotEmpty(string)); } @Test public void emptyToNull() { String string = ""; - Assert.isNull(StringUtil.emptyToNull(string)); + Assert.assertNull(StringUtil.emptyToNull(string)); } @Test public void nullToEmpty() { String string = "null"; - Assert.notEmpty(StringUtil.nullToEmpty(string)); + Assert.assertEquals("null", StringUtil.nullToEmpty(string)); } @Test public void isNullOrEmpty() { String string = "null"; - Assert.isTrue(!StringUtil.isNullOrEmpty(string)); + Assert.assertFalse(StringUtil.isNullOrEmpty(string)); } @Test public void isBlank() { String string = ""; - Assert.isTrue(StringUtil.isBlank(string)); + Assert.assertTrue(StringUtil.isBlank(string)); } @Test public void isNotBlank() { String string = "null"; - Assert.isTrue(StringUtil.isNotBlank(string)); + Assert.assertTrue(StringUtil.isNotBlank(string)); } @Test public void isAllNotEmpty() { String strings = "str"; - Assert.isTrue(StringUtil.isAllNotEmpty(strings)); + Assert.assertTrue(StringUtil.isAllNotEmpty(strings)); } @Test public void hasEmpty() { String strings = ""; - Assert.isTrue(StringUtil.hasEmpty(strings)); + Assert.assertTrue(StringUtil.hasEmpty(strings)); } @Test public void toUnderlineCase() { String string = "str"; String s = StringUtil.toUnderlineCase(string); - Assert.isTrue(Objects.equals(s, "str")); + Assert.assertEquals("str", s); } @Test public void toSymbolCase() { String string = "str"; String s = StringUtil.toSymbolCase(string, StringUtil.UNDERLINE); - Assert.isTrue(Objects.equals(s, "str")); + Assert.assertEquals("str", s); } @Test public void toCamelCase() { String string = "str_str"; String s = StringUtil.toCamelCase(string, StringUtil.UNDERLINE); - Assert.isTrue(Objects.equals(s, "strStr")); + Assert.assertEquals("strStr", s); + } + + @Test + public void newBuilder() { + String s1 = StringUtil.newBuilder(null); + Assert.assertEquals("", s1); + String s2 = StringUtil.newBuilder("H", "ippo", "4j"); + Assert.assertEquals("Hippo4j", s2); + } + + @Test + public void createBuilder() { + StringBuilder s1 = StringUtil.createBuilder(null); + Assert.assertEquals("", s1.toString()); + StringBuilder s2 = StringUtil.createBuilder("H", "ippo", "4j"); + Assert.assertEquals("Hippo4j", s2.toString()); + } + + @Test + public void appends() { + StringBuilder sb1 = StringUtil.appends(null, "H", "ippo", "4j"); + Assert.assertEquals("Hippo4j", sb1.toString()); + StringBuilder sb2 = StringUtil.appends(StringUtil.createBuilder("To "), null); + Assert.assertEquals("To ", sb2.toString()); + StringBuilder sb3 = StringUtil.appends(StringUtil.createBuilder("To "), "H", "ippo", "4j"); + Assert.assertEquals("To Hippo4j", sb3.toString()); } } diff --git a/hippo4j-config/src/main/java/cn/hippo4j/config/service/ThreadPoolAdapterService.java b/hippo4j-config/src/main/java/cn/hippo4j/config/service/ThreadPoolAdapterService.java index d94cc2e6..2a595939 100644 --- a/hippo4j-config/src/main/java/cn/hippo4j/config/service/ThreadPoolAdapterService.java +++ b/hippo4j-config/src/main/java/cn/hippo4j/config/service/ThreadPoolAdapterService.java @@ -30,7 +30,6 @@ import cn.hippo4j.config.model.biz.adapter.ThreadPoolAdapterReqDTO; import cn.hippo4j.config.model.biz.adapter.ThreadPoolAdapterRespDTO; import com.fasterxml.jackson.core.type.TypeReference; import lombok.extern.slf4j.Slf4j; -import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; @@ -38,7 +37,6 @@ import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; -import static cn.hippo4j.common.constant.Constants.HTTP_EXECUTE_TIMEOUT; import static cn.hippo4j.common.constant.Constants.IDENTIFY_SLICER_SYMBOL; /** @@ -49,7 +47,7 @@ import static cn.hippo4j.common.constant.Constants.IDENTIFY_SLICER_SYMBOL; public class ThreadPoolAdapterService { /** - * Map>>> + * Map<mark, Map<tenantItem, Map<threadPoolKey, List<ThreadPoolAdapterState>>>> */ private static final Map>>> THREAD_POOL_ADAPTER_MAP = new ConcurrentHashMap<>(); @@ -98,18 +96,23 @@ public class ThreadPoolAdapterService { List addressList = actual.stream().map(ThreadPoolAdapterState::getClientAddress).collect(Collectors.toList()); List result = new ArrayList<>(addressList.size()); addressList.forEach(each -> { - String urlString = new StringBuilder() - .append("http://") - .append(each) - .append("/adapter/thread-pool/info") - .toString(); + StringBuilder builder = StringUtil.createBuilder("http://", each, "/adapter/thread-pool/info"); Map param = new HashMap<>(); param.put("mark", requestParameter.getMark()); param.put("threadPoolKey", requestParameter.getThreadPoolKey()); + List paramKey = new ArrayList<>(param.keySet()); + for (int i = 0; i < paramKey.size(); i++) { + if (i == 0) { + builder.append("?"); + } else { + builder.append("&"); + } + String s = paramKey.get(i); + builder.append(StringUtil.newBuilder(s, "={", s, "}")); + } try { - RestTemplate template = new RestTemplate(); - String resultStr = template.getForObject(urlString, String.class, param); + String resultStr = template.getForObject(builder.toString(), String.class, param); if (StringUtil.isNotBlank(resultStr)) { Result restResult = JSONUtil.parseObject(resultStr, new TypeReference>() { }); @@ -131,22 +134,16 @@ public class ThreadPoolAdapterService { return actual.keySet(); } } - return new HashSet(); + return new HashSet<>(); } public static void remove(String identify) { synchronized (ThreadPoolAdapterService.class) { - THREAD_POOL_ADAPTER_MAP.values().forEach(each -> each.forEach((key, val) -> { - val.forEach((threadPoolKey, states) -> { - Iterator iterator = states.iterator(); - while (iterator.hasNext()) { - ThreadPoolAdapterState adapterState = iterator.next(); - if (Objects.equals(adapterState.getIdentify(), identify)) { - iterator.remove(); - } - } - }); - })); + THREAD_POOL_ADAPTER_MAP.values().forEach(each -> each.forEach((key, val) -> + val.forEach((threadPoolKey, states) -> + states.removeIf(adapterState -> Objects.equals(adapterState.getIdentify(), identify)) + ) + )); } } diff --git a/hippo4j-console/src/main/java/cn/hippo4j/console/controller/ThreadPoolAdapterController.java b/hippo4j-console/src/main/java/cn/hippo4j/console/controller/ThreadPoolAdapterController.java index a30b6d07..30d19620 100644 --- a/hippo4j-console/src/main/java/cn/hippo4j/console/controller/ThreadPoolAdapterController.java +++ b/hippo4j-console/src/main/java/cn/hippo4j/console/controller/ThreadPoolAdapterController.java @@ -21,6 +21,7 @@ import cn.hippo4j.common.constant.ConfigModifyTypeConstants; import cn.hippo4j.common.toolkit.BeanUtil; import cn.hippo4j.common.toolkit.JSONUtil; import cn.hippo4j.common.toolkit.UserContext; +import cn.hippo4j.common.toolkit.StringUtil; import cn.hippo4j.common.web.base.Result; import cn.hippo4j.common.web.base.Results; import cn.hippo4j.config.model.biz.adapter.ThreadPoolAdapterReqDTO; @@ -29,6 +30,9 @@ import cn.hippo4j.config.model.biz.threadpool.ConfigModifySaveReqDTO; import cn.hippo4j.config.service.ThreadPoolAdapterService; import cn.hippo4j.config.verify.ConfigModificationVerifyServiceChoose; import lombok.AllArgsConstructor; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; @@ -51,8 +55,6 @@ public class ThreadPoolAdapterController { private final ConfigModificationVerifyServiceChoose configModificationVerifyServiceChoose; - private final RestTemplate restTemplate = new RestTemplate(); - @GetMapping(REGISTER_ADAPTER_BASE_PATH + "/query") public Result> queryAdapterThreadPool(ThreadPoolAdapterReqDTO requestParameter) { List result = threadPoolAdapterService.query(requestParameter); @@ -69,12 +71,13 @@ public class ThreadPoolAdapterController { public Result updateAdapterThreadPool(@RequestBody ThreadPoolAdapterReqDTO requestParameter) { if (UserContext.getUserRole().equals("ROLE_ADMIN")) { for (String each : requestParameter.getClientAddressList()) { - String urlString = new StringBuilder() - .append("http://") - .append(each) - .append("/adapter/thread-pool/update") - .toString(); - restTemplate.postForObject(urlString, JSONUtil.toJSONString(requestParameter), Object.class); + String urlString = StringUtil.newBuilder("http://", each, "/adapter/thread-pool/update"); + RestTemplate restTemplate = new RestTemplate(); + // again appoint MediaType + HttpHeaders requestHeaders = new HttpHeaders(); + requestHeaders.setContentType(MediaType.APPLICATION_JSON); + HttpEntity requestEntity = new HttpEntity<>(JSONUtil.toJSONString(requestParameter), requestHeaders); + restTemplate.postForObject(urlString, requestEntity, Object.class); } } else { ConfigModifySaveReqDTO modifySaveReqDTO = BeanUtil.convert(requestParameter, ConfigModifySaveReqDTO.class); diff --git a/hippo4j-console/src/main/java/cn/hippo4j/console/controller/ThreadPoolController.java b/hippo4j-console/src/main/java/cn/hippo4j/console/controller/ThreadPoolController.java index 09909cf8..1dac5089 100644 --- a/hippo4j-console/src/main/java/cn/hippo4j/console/controller/ThreadPoolController.java +++ b/hippo4j-console/src/main/java/cn/hippo4j/console/controller/ThreadPoolController.java @@ -36,6 +36,9 @@ import cn.hippo4j.discovery.core.BaseInstanceRegistry; import cn.hippo4j.discovery.core.Lease; import com.baomidou.mybatisplus.core.metadata.IPage; import lombok.AllArgsConstructor; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import org.springframework.web.client.RestTemplate; @@ -62,8 +65,6 @@ public class ThreadPoolController { private final ConfigModificationVerifyServiceChoose configModificationVerifyServiceChoose; - private final RestTemplate restTemplate = new RestTemplate(); - @PostMapping("/query/page") public Result> queryNameSpacePage(@RequestBody ThreadPoolQueryReqDTO reqDTO) { return Results.success(threadPoolService.queryThreadPoolPage(reqDTO)); @@ -109,12 +110,8 @@ public class ThreadPoolController { @GetMapping("/run/state/{tpId}") public Result runState(@PathVariable("tpId") String tpId, @RequestParam(value = "clientAddress") String clientAddress) { - String urlString = new StringBuilder() - .append("http://") - .append(clientAddress) - .append("/run/state/") - .append(tpId) - .toString(); + String urlString = StringUtil.newBuilder("http://", clientAddress, "/run/state/", tpId); + RestTemplate restTemplate = new RestTemplate(); String data = restTemplate.getForObject(urlString, String.class, new HashMap<>()); Result result = JSONUtil.parseObject(data, Result.class); return result; @@ -123,12 +120,8 @@ public class ThreadPoolController { @GetMapping("/run/thread/state/{tpId}") public Result runThreadState(@PathVariable("tpId") String tpId, @RequestParam(value = "clientAddress") String clientAddress) { - String urlString = new StringBuilder() - .append("http://") - .append(clientAddress) - .append("/run/thread/state/") - .append(tpId) - .toString(); + String urlString = StringUtil.newBuilder("http://", clientAddress, "/run/thread/state/", tpId); + RestTemplate restTemplate = new RestTemplate(); String data = restTemplate.getForObject(urlString, String.class, new HashMap<>()); Result result = JSONUtil.parseObject(data, Result.class); return result; @@ -166,11 +159,8 @@ public class ThreadPoolController { @GetMapping("/web/base/info") public Result getPoolBaseState(@RequestParam(value = "clientAddress") String clientAddress) { - String urlString = new StringBuilder() - .append("http://") - .append(clientAddress) - .append("/web/base/info") - .toString(); + String urlString = StringUtil.newBuilder("http://", clientAddress, "/web/base/info"); + RestTemplate restTemplate = new RestTemplate(); String data = restTemplate.getForObject(urlString, String.class, new HashMap<>()); Result result = JSONUtil.parseObject(data, Result.class); return result; @@ -178,11 +168,8 @@ public class ThreadPoolController { @GetMapping("/web/run/state") public Result getPoolRunState(@RequestParam(value = "clientAddress") String clientAddress) { - String urlString = new StringBuilder() - .append("http://") - .append(clientAddress) - .append("/web/run/state") - .toString(); + String urlString = StringUtil.newBuilder("http://", clientAddress, "/web/run/state"); + RestTemplate restTemplate = new RestTemplate(); String data = restTemplate.getForObject(urlString, String.class, new HashMap<>()); Result result = JSONUtil.parseObject(data, Result.class); return result; @@ -192,12 +179,13 @@ public class ThreadPoolController { public Result updateWebThreadPool(@RequestBody WebThreadPoolReqDTO requestParam) { if (UserContext.getUserRole().equals("ROLE_ADMIN")) { for (String each : requestParam.getClientAddressList()) { - String urlString = new StringBuilder() - .append("http://") - .append(each) - .append("/web/update/pool") - .toString(); - restTemplate.postForObject(urlString, JSONUtil.toJSONString(requestParam), Object.class); + String urlString = StringUtil.newBuilder("http://", each, "/web/update/pool"); + RestTemplate restTemplate = new RestTemplate(); + // again appoint MediaType + HttpHeaders requestHeaders = new HttpHeaders(); + requestHeaders.setContentType(MediaType.APPLICATION_JSON); + HttpEntity requestEntity = new HttpEntity<>(JSONUtil.toJSONString(requestParam), requestHeaders); + restTemplate.postForObject(urlString, requestEntity, Object.class); } } else { ConfigModifySaveReqDTO modifySaveReqDTO = BeanUtil.convert(requestParam, ConfigModifySaveReqDTO.class); @@ -238,5 +226,4 @@ public class ThreadPoolController { }); return Results.success(returnThreadPool); } - } diff --git a/hippo4j-message/src/main/java/cn/hippo4j/message/platform/WeChatSendMessageHandler.java b/hippo4j-message/src/main/java/cn/hippo4j/message/platform/WeChatSendMessageHandler.java index 38bc9111..65e3b228 100644 --- a/hippo4j-message/src/main/java/cn/hippo4j/message/platform/WeChatSendMessageHandler.java +++ b/hippo4j-message/src/main/java/cn/hippo4j/message/platform/WeChatSendMessageHandler.java @@ -26,7 +26,6 @@ import cn.hippo4j.message.platform.base.RobotMessageExecuteDTO; import lombok.Data; import lombok.experimental.Accessors; import lombok.extern.slf4j.Slf4j; -import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; import static cn.hippo4j.message.platform.constant.WeChatAlarmConstants.*;