New feature (#779)

* feat : supplement StringUtil

* fix : To solve RestTemplate Missing parameter (#775)

* fix : again appoint MediaType for request post (#770)
pull/781/head
pizihao 3 years ago committed by GitHub
parent 4666c90bc2
commit aec3d98bbf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -223,6 +223,56 @@ public class StringUtil {
return sb.toString(); 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 * Replace a portion of the string, replacing all found
* *

@ -18,83 +18,108 @@
package cn.hippo4j.common.toolkit; package cn.hippo4j.common.toolkit;
import org.junit.Test; import org.junit.Test;
import org.junit.Assert;
import java.util.Objects;
public class StringUtilTest { public class StringUtilTest {
@Test @Test
public void assertIsEmpty() { public void assertIsEmpty() {
String string = ""; String string = "";
Assert.isTrue(StringUtil.isEmpty(string)); Assert.assertTrue(StringUtil.isEmpty(string));
} }
@Test @Test
public void assertIsNotEmpty() { public void assertIsNotEmpty() {
String string = "string"; String string = "string";
Assert.isTrue(StringUtil.isNotEmpty(string)); Assert.assertTrue(StringUtil.isNotEmpty(string));
} }
@Test @Test
public void emptyToNull() { public void emptyToNull() {
String string = ""; String string = "";
Assert.isNull(StringUtil.emptyToNull(string)); Assert.assertNull(StringUtil.emptyToNull(string));
} }
@Test @Test
public void nullToEmpty() { public void nullToEmpty() {
String string = "null"; String string = "null";
Assert.notEmpty(StringUtil.nullToEmpty(string)); Assert.assertEquals("null", StringUtil.nullToEmpty(string));
} }
@Test @Test
public void isNullOrEmpty() { public void isNullOrEmpty() {
String string = "null"; String string = "null";
Assert.isTrue(!StringUtil.isNullOrEmpty(string)); Assert.assertFalse(StringUtil.isNullOrEmpty(string));
} }
@Test @Test
public void isBlank() { public void isBlank() {
String string = ""; String string = "";
Assert.isTrue(StringUtil.isBlank(string)); Assert.assertTrue(StringUtil.isBlank(string));
} }
@Test @Test
public void isNotBlank() { public void isNotBlank() {
String string = "null"; String string = "null";
Assert.isTrue(StringUtil.isNotBlank(string)); Assert.assertTrue(StringUtil.isNotBlank(string));
} }
@Test @Test
public void isAllNotEmpty() { public void isAllNotEmpty() {
String strings = "str"; String strings = "str";
Assert.isTrue(StringUtil.isAllNotEmpty(strings)); Assert.assertTrue(StringUtil.isAllNotEmpty(strings));
} }
@Test @Test
public void hasEmpty() { public void hasEmpty() {
String strings = ""; String strings = "";
Assert.isTrue(StringUtil.hasEmpty(strings)); Assert.assertTrue(StringUtil.hasEmpty(strings));
} }
@Test @Test
public void toUnderlineCase() { public void toUnderlineCase() {
String string = "str"; String string = "str";
String s = StringUtil.toUnderlineCase(string); String s = StringUtil.toUnderlineCase(string);
Assert.isTrue(Objects.equals(s, "str")); Assert.assertEquals("str", s);
} }
@Test @Test
public void toSymbolCase() { public void toSymbolCase() {
String string = "str"; String string = "str";
String s = StringUtil.toSymbolCase(string, StringUtil.UNDERLINE); String s = StringUtil.toSymbolCase(string, StringUtil.UNDERLINE);
Assert.isTrue(Objects.equals(s, "str")); Assert.assertEquals("str", s);
} }
@Test @Test
public void toCamelCase() { public void toCamelCase() {
String string = "str_str"; String string = "str_str";
String s = StringUtil.toCamelCase(string, StringUtil.UNDERLINE); 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());
} }
} }

@ -30,7 +30,6 @@ import cn.hippo4j.config.model.biz.adapter.ThreadPoolAdapterReqDTO;
import cn.hippo4j.config.model.biz.adapter.ThreadPoolAdapterRespDTO; import cn.hippo4j.config.model.biz.adapter.ThreadPoolAdapterRespDTO;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
@ -38,7 +37,6 @@ import java.util.*;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors; 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; 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 { public class ThreadPoolAdapterService {
/** /**
* Map<mark, Map<tenantItem, Map<threadPoolKey, List<ThreadPoolAdapterState>>>> * Map&lt;mark, Map&lt;tenantItem, Map&lt;threadPoolKey, List&lt;ThreadPoolAdapterState&gt;&gt;&gt;&gt;
*/ */
private static final Map<String, Map<String, Map<String, List<ThreadPoolAdapterState>>>> THREAD_POOL_ADAPTER_MAP = new ConcurrentHashMap<>(); private static final Map<String, Map<String, Map<String, List<ThreadPoolAdapterState>>>> THREAD_POOL_ADAPTER_MAP = new ConcurrentHashMap<>();
@ -98,18 +96,23 @@ public class ThreadPoolAdapterService {
List<String> addressList = actual.stream().map(ThreadPoolAdapterState::getClientAddress).collect(Collectors.toList()); List<String> addressList = actual.stream().map(ThreadPoolAdapterState::getClientAddress).collect(Collectors.toList());
List<ThreadPoolAdapterRespDTO> result = new ArrayList<>(addressList.size()); List<ThreadPoolAdapterRespDTO> result = new ArrayList<>(addressList.size());
addressList.forEach(each -> { addressList.forEach(each -> {
String urlString = new StringBuilder() StringBuilder builder = StringUtil.createBuilder("http://", each, "/adapter/thread-pool/info");
.append("http://")
.append(each)
.append("/adapter/thread-pool/info")
.toString();
Map<String, Object> param = new HashMap<>(); Map<String, Object> param = new HashMap<>();
param.put("mark", requestParameter.getMark()); param.put("mark", requestParameter.getMark());
param.put("threadPoolKey", requestParameter.getThreadPoolKey()); param.put("threadPoolKey", requestParameter.getThreadPoolKey());
List<String> 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 { try {
RestTemplate template = new RestTemplate(); 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)) { if (StringUtil.isNotBlank(resultStr)) {
Result<ThreadPoolAdapterRespDTO> restResult = JSONUtil.parseObject(resultStr, new TypeReference<Result<ThreadPoolAdapterRespDTO>>() { Result<ThreadPoolAdapterRespDTO> restResult = JSONUtil.parseObject(resultStr, new TypeReference<Result<ThreadPoolAdapterRespDTO>>() {
}); });
@ -131,22 +134,16 @@ public class ThreadPoolAdapterService {
return actual.keySet(); return actual.keySet();
} }
} }
return new HashSet(); return new HashSet<>();
} }
public static void remove(String identify) { public static void remove(String identify) {
synchronized (ThreadPoolAdapterService.class) { synchronized (ThreadPoolAdapterService.class) {
THREAD_POOL_ADAPTER_MAP.values().forEach(each -> each.forEach((key, val) -> { THREAD_POOL_ADAPTER_MAP.values().forEach(each -> each.forEach((key, val) ->
val.forEach((threadPoolKey, states) -> { val.forEach((threadPoolKey, states) ->
Iterator<ThreadPoolAdapterState> iterator = states.iterator(); states.removeIf(adapterState -> Objects.equals(adapterState.getIdentify(), identify))
while (iterator.hasNext()) { )
ThreadPoolAdapterState adapterState = iterator.next(); ));
if (Objects.equals(adapterState.getIdentify(), identify)) {
iterator.remove();
}
}
});
}));
} }
} }

@ -21,6 +21,7 @@ import cn.hippo4j.common.constant.ConfigModifyTypeConstants;
import cn.hippo4j.common.toolkit.BeanUtil; import cn.hippo4j.common.toolkit.BeanUtil;
import cn.hippo4j.common.toolkit.JSONUtil; import cn.hippo4j.common.toolkit.JSONUtil;
import cn.hippo4j.common.toolkit.UserContext; 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.Result;
import cn.hippo4j.common.web.base.Results; import cn.hippo4j.common.web.base.Results;
import cn.hippo4j.config.model.biz.adapter.ThreadPoolAdapterReqDTO; 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.service.ThreadPoolAdapterService;
import cn.hippo4j.config.verify.ConfigModificationVerifyServiceChoose; import cn.hippo4j.config.verify.ConfigModificationVerifyServiceChoose;
import lombok.AllArgsConstructor; 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.GetMapping;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
@ -51,8 +55,6 @@ public class ThreadPoolAdapterController {
private final ConfigModificationVerifyServiceChoose configModificationVerifyServiceChoose; private final ConfigModificationVerifyServiceChoose configModificationVerifyServiceChoose;
private final RestTemplate restTemplate = new RestTemplate();
@GetMapping(REGISTER_ADAPTER_BASE_PATH + "/query") @GetMapping(REGISTER_ADAPTER_BASE_PATH + "/query")
public Result<List<ThreadPoolAdapterRespDTO>> queryAdapterThreadPool(ThreadPoolAdapterReqDTO requestParameter) { public Result<List<ThreadPoolAdapterRespDTO>> queryAdapterThreadPool(ThreadPoolAdapterReqDTO requestParameter) {
List<ThreadPoolAdapterRespDTO> result = threadPoolAdapterService.query(requestParameter); List<ThreadPoolAdapterRespDTO> result = threadPoolAdapterService.query(requestParameter);
@ -69,12 +71,13 @@ public class ThreadPoolAdapterController {
public Result<Void> updateAdapterThreadPool(@RequestBody ThreadPoolAdapterReqDTO requestParameter) { public Result<Void> updateAdapterThreadPool(@RequestBody ThreadPoolAdapterReqDTO requestParameter) {
if (UserContext.getUserRole().equals("ROLE_ADMIN")) { if (UserContext.getUserRole().equals("ROLE_ADMIN")) {
for (String each : requestParameter.getClientAddressList()) { for (String each : requestParameter.getClientAddressList()) {
String urlString = new StringBuilder() String urlString = StringUtil.newBuilder("http://", each, "/adapter/thread-pool/update");
.append("http://") RestTemplate restTemplate = new RestTemplate();
.append(each) // again appoint MediaType
.append("/adapter/thread-pool/update") HttpHeaders requestHeaders = new HttpHeaders();
.toString(); requestHeaders.setContentType(MediaType.APPLICATION_JSON);
restTemplate.postForObject(urlString, JSONUtil.toJSONString(requestParameter), Object.class); HttpEntity<String> requestEntity = new HttpEntity<>(JSONUtil.toJSONString(requestParameter), requestHeaders);
restTemplate.postForObject(urlString, requestEntity, Object.class);
} }
} else { } else {
ConfigModifySaveReqDTO modifySaveReqDTO = BeanUtil.convert(requestParameter, ConfigModifySaveReqDTO.class); ConfigModifySaveReqDTO modifySaveReqDTO = BeanUtil.convert(requestParameter, ConfigModifySaveReqDTO.class);

@ -36,6 +36,9 @@ import cn.hippo4j.discovery.core.BaseInstanceRegistry;
import cn.hippo4j.discovery.core.Lease; import cn.hippo4j.discovery.core.Lease;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor; 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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
@ -62,8 +65,6 @@ public class ThreadPoolController {
private final ConfigModificationVerifyServiceChoose configModificationVerifyServiceChoose; private final ConfigModificationVerifyServiceChoose configModificationVerifyServiceChoose;
private final RestTemplate restTemplate = new RestTemplate();
@PostMapping("/query/page") @PostMapping("/query/page")
public Result<IPage<ThreadPoolRespDTO>> queryNameSpacePage(@RequestBody ThreadPoolQueryReqDTO reqDTO) { public Result<IPage<ThreadPoolRespDTO>> queryNameSpacePage(@RequestBody ThreadPoolQueryReqDTO reqDTO) {
return Results.success(threadPoolService.queryThreadPoolPage(reqDTO)); return Results.success(threadPoolService.queryThreadPoolPage(reqDTO));
@ -109,12 +110,8 @@ public class ThreadPoolController {
@GetMapping("/run/state/{tpId}") @GetMapping("/run/state/{tpId}")
public Result runState(@PathVariable("tpId") String tpId, public Result runState(@PathVariable("tpId") String tpId,
@RequestParam(value = "clientAddress") String clientAddress) { @RequestParam(value = "clientAddress") String clientAddress) {
String urlString = new StringBuilder() String urlString = StringUtil.newBuilder("http://", clientAddress, "/run/state/", tpId);
.append("http://") RestTemplate restTemplate = new RestTemplate();
.append(clientAddress)
.append("/run/state/")
.append(tpId)
.toString();
String data = restTemplate.getForObject(urlString, String.class, new HashMap<>()); String data = restTemplate.getForObject(urlString, String.class, new HashMap<>());
Result result = JSONUtil.parseObject(data, Result.class); Result result = JSONUtil.parseObject(data, Result.class);
return result; return result;
@ -123,12 +120,8 @@ public class ThreadPoolController {
@GetMapping("/run/thread/state/{tpId}") @GetMapping("/run/thread/state/{tpId}")
public Result runThreadState(@PathVariable("tpId") String tpId, public Result runThreadState(@PathVariable("tpId") String tpId,
@RequestParam(value = "clientAddress") String clientAddress) { @RequestParam(value = "clientAddress") String clientAddress) {
String urlString = new StringBuilder() String urlString = StringUtil.newBuilder("http://", clientAddress, "/run/thread/state/", tpId);
.append("http://") RestTemplate restTemplate = new RestTemplate();
.append(clientAddress)
.append("/run/thread/state/")
.append(tpId)
.toString();
String data = restTemplate.getForObject(urlString, String.class, new HashMap<>()); String data = restTemplate.getForObject(urlString, String.class, new HashMap<>());
Result result = JSONUtil.parseObject(data, Result.class); Result result = JSONUtil.parseObject(data, Result.class);
return result; return result;
@ -166,11 +159,8 @@ public class ThreadPoolController {
@GetMapping("/web/base/info") @GetMapping("/web/base/info")
public Result getPoolBaseState(@RequestParam(value = "clientAddress") String clientAddress) { public Result getPoolBaseState(@RequestParam(value = "clientAddress") String clientAddress) {
String urlString = new StringBuilder() String urlString = StringUtil.newBuilder("http://", clientAddress, "/web/base/info");
.append("http://") RestTemplate restTemplate = new RestTemplate();
.append(clientAddress)
.append("/web/base/info")
.toString();
String data = restTemplate.getForObject(urlString, String.class, new HashMap<>()); String data = restTemplate.getForObject(urlString, String.class, new HashMap<>());
Result result = JSONUtil.parseObject(data, Result.class); Result result = JSONUtil.parseObject(data, Result.class);
return result; return result;
@ -178,11 +168,8 @@ public class ThreadPoolController {
@GetMapping("/web/run/state") @GetMapping("/web/run/state")
public Result getPoolRunState(@RequestParam(value = "clientAddress") String clientAddress) { public Result getPoolRunState(@RequestParam(value = "clientAddress") String clientAddress) {
String urlString = new StringBuilder() String urlString = StringUtil.newBuilder("http://", clientAddress, "/web/run/state");
.append("http://") RestTemplate restTemplate = new RestTemplate();
.append(clientAddress)
.append("/web/run/state")
.toString();
String data = restTemplate.getForObject(urlString, String.class, new HashMap<>()); String data = restTemplate.getForObject(urlString, String.class, new HashMap<>());
Result result = JSONUtil.parseObject(data, Result.class); Result result = JSONUtil.parseObject(data, Result.class);
return result; return result;
@ -192,12 +179,13 @@ public class ThreadPoolController {
public Result<Void> updateWebThreadPool(@RequestBody WebThreadPoolReqDTO requestParam) { public Result<Void> updateWebThreadPool(@RequestBody WebThreadPoolReqDTO requestParam) {
if (UserContext.getUserRole().equals("ROLE_ADMIN")) { if (UserContext.getUserRole().equals("ROLE_ADMIN")) {
for (String each : requestParam.getClientAddressList()) { for (String each : requestParam.getClientAddressList()) {
String urlString = new StringBuilder() String urlString = StringUtil.newBuilder("http://", each, "/web/update/pool");
.append("http://") RestTemplate restTemplate = new RestTemplate();
.append(each) // again appoint MediaType
.append("/web/update/pool") HttpHeaders requestHeaders = new HttpHeaders();
.toString(); requestHeaders.setContentType(MediaType.APPLICATION_JSON);
restTemplate.postForObject(urlString, JSONUtil.toJSONString(requestParam), Object.class); HttpEntity<String> requestEntity = new HttpEntity<>(JSONUtil.toJSONString(requestParam), requestHeaders);
restTemplate.postForObject(urlString, requestEntity, Object.class);
} }
} else { } else {
ConfigModifySaveReqDTO modifySaveReqDTO = BeanUtil.convert(requestParam, ConfigModifySaveReqDTO.class); ConfigModifySaveReqDTO modifySaveReqDTO = BeanUtil.convert(requestParam, ConfigModifySaveReqDTO.class);
@ -238,5 +226,4 @@ public class ThreadPoolController {
}); });
return Results.success(returnThreadPool); return Results.success(returnThreadPool);
} }
} }

@ -26,7 +26,6 @@ import cn.hippo4j.message.platform.base.RobotMessageExecuteDTO;
import lombok.Data; import lombok.Data;
import lombok.experimental.Accessors; import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import static cn.hippo4j.message.platform.constant.WeChatAlarmConstants.*; import static cn.hippo4j.message.platform.constant.WeChatAlarmConstants.*;

Loading…
Cancel
Save