diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d4daaa64..1036cdbbe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,3 +23,4 @@ - [fix: shutdown thread pool before the container closes](https://github.com/Tencent/spring-cloud-tencent/pull/354) - [rm code: Condition 'null != interceptors' is always 'true' ](https://github.com/Tencent/spring-cloud-tencent/pull/356) - [docs:update logo in README.](https://github.com/Tencent/spring-cloud-tencent/pull/359) +- [Use jdk constants instead of magic variables](https://github.com/Tencent/spring-cloud-tencent/pull/362) diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/DecodeTransferMetadataReactiveFilter.java b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/DecodeTransferMetadataReactiveFilter.java index 225e3cc5d..a81b75e7f 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/DecodeTransferMetadataReactiveFilter.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/DecodeTransferMetadataReactiveFilter.java @@ -20,6 +20,7 @@ package com.tencent.cloud.metadata.core; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; @@ -84,7 +85,7 @@ public class DecodeTransferMetadataReactiveFilter implements WebFilter, Ordered .getFirst(MetadataConstant.HeaderName.CUSTOM_METADATA); try { if (StringUtils.hasText(customMetadataStr)) { - customMetadataStr = URLDecoder.decode(customMetadataStr, "UTF-8"); + customMetadataStr = URLDecoder.decode(customMetadataStr, StandardCharsets.UTF_8.name()); } } catch (UnsupportedEncodingException e) { diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/DecodeTransferMetadataServletFilter.java b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/DecodeTransferMetadataServletFilter.java index 8ca9e2bc2..5f7692bc7 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/DecodeTransferMetadataServletFilter.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/DecodeTransferMetadataServletFilter.java @@ -21,6 +21,7 @@ package com.tencent.cloud.metadata.core; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; @@ -76,7 +77,7 @@ public class DecodeTransferMetadataServletFilter extends OncePerRequestFilter { String customMetadataStr = httpServletRequest.getHeader(MetadataConstant.HeaderName.CUSTOM_METADATA); try { if (StringUtils.hasText(customMetadataStr)) { - customMetadataStr = URLDecoder.decode(customMetadataStr, "UTF-8"); + customMetadataStr = URLDecoder.decode(customMetadataStr, StandardCharsets.UTF_8.name()); } } catch (UnsupportedEncodingException e) { diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMedataFeignInterceptor.java b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMedataFeignInterceptor.java index 0bf97bf0f..b361ee81b 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMedataFeignInterceptor.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMedataFeignInterceptor.java @@ -20,6 +20,7 @@ package com.tencent.cloud.metadata.core; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.Map; import com.tencent.cloud.common.constant.MetadataConstant; @@ -61,7 +62,8 @@ public class EncodeTransferMedataFeignInterceptor implements RequestInterceptor, String encodedTransitiveMetadata = JacksonUtils.serialize2Json(customMetadata); requestTemplate.removeHeader(CUSTOM_METADATA); try { - requestTemplate.header(CUSTOM_METADATA, URLEncoder.encode(encodedTransitiveMetadata, "UTF-8")); + requestTemplate.header(CUSTOM_METADATA, + URLEncoder.encode(encodedTransitiveMetadata, StandardCharsets.UTF_8.name())); } catch (UnsupportedEncodingException e) { LOG.error("Set header failed.", e); diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMedataRestTemplateInterceptor.java b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMedataRestTemplateInterceptor.java index 37f94eb2c..ce263754b 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMedataRestTemplateInterceptor.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMedataRestTemplateInterceptor.java @@ -21,6 +21,7 @@ package com.tencent.cloud.metadata.core; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.Map; import com.tencent.cloud.common.constant.MetadataConstant; @@ -59,7 +60,7 @@ public class EncodeTransferMedataRestTemplateInterceptor implements ClientHttpRe String encodedTransitiveMetadata = JacksonUtils.serialize2Json(customMetadata); try { httpRequest.getHeaders().set(MetadataConstant.HeaderName.CUSTOM_METADATA, - URLEncoder.encode(encodedTransitiveMetadata, "UTF-8")); + URLEncoder.encode(encodedTransitiveMetadata, StandardCharsets.UTF_8.name())); } catch (UnsupportedEncodingException e) { httpRequest.getHeaders().set(MetadataConstant.HeaderName.CUSTOM_METADATA, encodedTransitiveMetadata); diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMedataScgFilter.java b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMedataScgFilter.java index 81b4fba4b..b08a1072d 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMedataScgFilter.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMedataScgFilter.java @@ -20,6 +20,7 @@ package com.tencent.cloud.metadata.core; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.Map; import com.tencent.cloud.common.constant.MetadataConstant; @@ -67,7 +68,8 @@ public class EncodeTransferMedataScgFilter implements GlobalFilter, Ordered { if (!CollectionUtils.isEmpty(customMetadata)) { String metadataStr = JacksonUtils.serialize2Json(customMetadata); try { - builder.header(MetadataConstant.HeaderName.CUSTOM_METADATA, URLEncoder.encode(metadataStr, "UTF-8")); + builder.header(MetadataConstant.HeaderName.CUSTOM_METADATA, + URLEncoder.encode(metadataStr, StandardCharsets.UTF_8.name())); } catch (UnsupportedEncodingException e) { builder.header(MetadataConstant.HeaderName.CUSTOM_METADATA, metadataStr); diff --git a/spring-cloud-tencent-examples/polaris-gateway-example/gateway-callee-service/src/main/java/com/tencent/cloud/polaris/gateway/example/callee/GatewayCalleeController.java b/spring-cloud-tencent-examples/polaris-gateway-example/gateway-callee-service/src/main/java/com/tencent/cloud/polaris/gateway/example/callee/GatewayCalleeController.java index e270cdae0..eae571921 100644 --- a/spring-cloud-tencent-examples/polaris-gateway-example/gateway-callee-service/src/main/java/com/tencent/cloud/polaris/gateway/example/callee/GatewayCalleeController.java +++ b/spring-cloud-tencent-examples/polaris-gateway-example/gateway-callee-service/src/main/java/com/tencent/cloud/polaris/gateway/example/callee/GatewayCalleeController.java @@ -19,6 +19,7 @@ package com.tencent.cloud.polaris.gateway.example.callee; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; import com.tencent.cloud.common.constant.MetadataConstant; import org.slf4j.Logger; @@ -63,8 +64,8 @@ public class GatewayCalleeController { @RequestMapping("/echo") public String echoHeader(@RequestHeader(MetadataConstant.HeaderName.CUSTOM_METADATA) String metadataStr) throws UnsupportedEncodingException { - LOG.info(URLDecoder.decode(metadataStr, "UTF-8")); - return URLDecoder.decode(metadataStr, "UTF-8"); + LOG.info(URLDecoder.decode(metadataStr, StandardCharsets.UTF_8.name())); + return URLDecoder.decode(metadataStr, StandardCharsets.UTF_8.name()); } }