format constatns UTF-8 (#313)

* format constatns UTF-8

* add update log
pull/316/head
weihubeats 2 years ago committed by GitHub
parent 99d22321ff
commit 6a870b9935
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -17,3 +17,4 @@
- [fix:refactor to use text/html resolve chaos problem on rejectTips](https://github.com/Tencent/spring-cloud-tencent/pull/285) - [fix:refactor to use text/html resolve chaos problem on rejectTips](https://github.com/Tencent/spring-cloud-tencent/pull/285)
- [UT: add metadata-transfer unit test](https://github.com/Tencent/spring-cloud-tencent/pull/294) - [UT: add metadata-transfer unit test](https://github.com/Tencent/spring-cloud-tencent/pull/294)
- [Feature:add restTemplate Report Polaris](https://github.com/Tencent/spring-cloud-tencent/pull/272) - [Feature:add restTemplate Report Polaris](https://github.com/Tencent/spring-cloud-tencent/pull/272)
- [Use jdk constants instead of magic variables](https://github.com/Tencent/spring-cloud-tencent/pull/313)

@ -20,6 +20,7 @@ package com.tencent.cloud.metadata.core;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -86,7 +87,7 @@ public class DecodeTransferMetadataReactiveFilter implements WebFilter, Ordered
.getFirst(MetadataConstant.HeaderName.CUSTOM_METADATA); .getFirst(MetadataConstant.HeaderName.CUSTOM_METADATA);
try { try {
if (StringUtils.hasText(customMetadataStr)) { if (StringUtils.hasText(customMetadataStr)) {
customMetadataStr = URLDecoder.decode(customMetadataStr, "UTF-8"); customMetadataStr = URLDecoder.decode(customMetadataStr, StandardCharsets.UTF_8.name());
} }
} }
catch (UnsupportedEncodingException e) { catch (UnsupportedEncodingException e) {

@ -21,6 +21,7 @@ package com.tencent.cloud.metadata.core;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -78,7 +79,7 @@ public class DecodeTransferMetadataServletFilter extends OncePerRequestFilter {
.getHeader(MetadataConstant.HeaderName.CUSTOM_METADATA); .getHeader(MetadataConstant.HeaderName.CUSTOM_METADATA);
try { try {
if (StringUtils.hasText(customMetadataStr)) { if (StringUtils.hasText(customMetadataStr)) {
customMetadataStr = URLDecoder.decode(customMetadataStr, "UTF-8"); customMetadataStr = URLDecoder.decode(customMetadataStr, StandardCharsets.UTF_8.name());
} }
} }
catch (UnsupportedEncodingException e) { catch (UnsupportedEncodingException e) {

@ -20,6 +20,7 @@ package com.tencent.cloud.metadata.core;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Map; import java.util.Map;
import com.tencent.cloud.common.constant.MetadataConstant; import com.tencent.cloud.common.constant.MetadataConstant;
@ -63,7 +64,7 @@ public class EncodeTransferMedataFeignInterceptor implements RequestInterceptor,
requestTemplate.removeHeader(CUSTOM_METADATA); requestTemplate.removeHeader(CUSTOM_METADATA);
try { try {
requestTemplate.header(CUSTOM_METADATA, requestTemplate.header(CUSTOM_METADATA,
URLEncoder.encode(encodedTransitiveMetadata, "UTF-8")); URLEncoder.encode(encodedTransitiveMetadata, StandardCharsets.UTF_8.name()));
} }
catch (UnsupportedEncodingException e) { catch (UnsupportedEncodingException e) {
LOG.error("Set header failed.", e); LOG.error("Set header failed.", e);

@ -21,6 +21,7 @@ package com.tencent.cloud.metadata.core;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Map; import java.util.Map;
import com.tencent.cloud.common.constant.MetadataConstant; import com.tencent.cloud.common.constant.MetadataConstant;
@ -60,7 +61,7 @@ public class EncodeTransferMedataRestTemplateInterceptor
String encodedTransitiveMetadata = JacksonUtils.serialize2Json(customMetadata); String encodedTransitiveMetadata = JacksonUtils.serialize2Json(customMetadata);
try { try {
httpRequest.getHeaders().set(MetadataConstant.HeaderName.CUSTOM_METADATA, httpRequest.getHeaders().set(MetadataConstant.HeaderName.CUSTOM_METADATA,
URLEncoder.encode(encodedTransitiveMetadata, "UTF-8")); URLEncoder.encode(encodedTransitiveMetadata, StandardCharsets.UTF_8.name()));
} }
catch (UnsupportedEncodingException e) { catch (UnsupportedEncodingException e) {
httpRequest.getHeaders().set(MetadataConstant.HeaderName.CUSTOM_METADATA, httpRequest.getHeaders().set(MetadataConstant.HeaderName.CUSTOM_METADATA,

@ -20,6 +20,7 @@ package com.tencent.cloud.metadata.core;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Map; import java.util.Map;
import com.tencent.cloud.common.constant.MetadataConstant; import com.tencent.cloud.common.constant.MetadataConstant;
@ -70,7 +71,7 @@ public class EncodeTransferMedataScgFilter implements GlobalFilter, Ordered {
String metadataStr = JacksonUtils.serialize2Json(customMetadata); String metadataStr = JacksonUtils.serialize2Json(customMetadata);
try { try {
builder.header(MetadataConstant.HeaderName.CUSTOM_METADATA, builder.header(MetadataConstant.HeaderName.CUSTOM_METADATA,
URLEncoder.encode(metadataStr, "UTF-8")); URLEncoder.encode(metadataStr, StandardCharsets.UTF_8.name()));
} }
catch (UnsupportedEncodingException e) { catch (UnsupportedEncodingException e) {
builder.header(MetadataConstant.HeaderName.CUSTOM_METADATA, metadataStr); builder.header(MetadataConstant.HeaderName.CUSTOM_METADATA, metadataStr);

@ -20,6 +20,7 @@ package com.tencent.cloud.metadata.core;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Map; import java.util.Map;
import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.ZuulFilter;
@ -71,7 +72,7 @@ public class EncodeTransferMetadataZuulFilter extends ZuulFilter {
try { try {
requestContext.addZuulRequestHeader( requestContext.addZuulRequestHeader(
MetadataConstant.HeaderName.CUSTOM_METADATA, MetadataConstant.HeaderName.CUSTOM_METADATA,
URLEncoder.encode(metadataStr, "UTF-8")); URLEncoder.encode(metadataStr, StandardCharsets.UTF_8.name()));
} }
catch (UnsupportedEncodingException e) { catch (UnsupportedEncodingException e) {
requestContext.addZuulRequestHeader( requestContext.addZuulRequestHeader(

@ -20,6 +20,7 @@ package com.tencent.cloud.metadata.core.filter;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Map; import java.util.Map;
import com.tencent.cloud.common.constant.MetadataConstant; import com.tencent.cloud.common.constant.MetadataConstant;
@ -64,7 +65,7 @@ public class EncodeTransferMedataScgFilterTest {
MockServerWebExchange exchange = MockServerWebExchange.from(builder); MockServerWebExchange exchange = MockServerWebExchange.from(builder);
filter.filter(exchange, chain); filter.filter(exchange, chain);
String metadataStr = exchange.getRequest().getHeaders().getFirst(MetadataConstant.HeaderName.CUSTOM_METADATA); String metadataStr = exchange.getRequest().getHeaders().getFirst(MetadataConstant.HeaderName.CUSTOM_METADATA);
String decode = URLDecoder.decode(metadataStr, "UTF-8"); String decode = URLDecoder.decode(metadataStr, StandardCharsets.UTF_8.name());
Map<String, String> transitiveMap = JacksonUtils.deserialize2Map(decode); Map<String, String> transitiveMap = JacksonUtils.deserialize2Map(decode);
Assertions.assertThat(transitiveMap.size()).isEqualTo(1); Assertions.assertThat(transitiveMap.size()).isEqualTo(1);
Assert.assertEquals(transitiveMap.get("b"), "2"); Assert.assertEquals(transitiveMap.get("b"), "2");

@ -20,6 +20,7 @@ package com.tencent.cloud.metadata.core.filter;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Map; import java.util.Map;
import com.netflix.zuul.context.RequestContext; import com.netflix.zuul.context.RequestContext;
@ -70,7 +71,7 @@ public class EncodeTransferMetadataZuulFilterTest {
final RequestContext ctx = RequestContext.getCurrentContext(); final RequestContext ctx = RequestContext.getCurrentContext();
Map<String, String> zuulRequestHeaders = ctx.getZuulRequestHeaders(); Map<String, String> zuulRequestHeaders = ctx.getZuulRequestHeaders();
String metaData = zuulRequestHeaders.get(MetadataConstant.HeaderName.CUSTOM_METADATA.toLowerCase()); String metaData = zuulRequestHeaders.get(MetadataConstant.HeaderName.CUSTOM_METADATA.toLowerCase());
String decode = URLDecoder.decode(metaData, "UTF-8"); String decode = URLDecoder.decode(metaData, StandardCharsets.UTF_8.name());
Map<String, String> transitiveMap = JacksonUtils.deserialize2Map(decode); Map<String, String> transitiveMap = JacksonUtils.deserialize2Map(decode);
Assertions.assertThat(transitiveMap.size()).isEqualTo(1); Assertions.assertThat(transitiveMap.size()).isEqualTo(1);
Assert.assertEquals(transitiveMap.get("b"), "2"); Assert.assertEquals(transitiveMap.get("b"), "2");

@ -19,6 +19,7 @@ package com.tencent.cloud.polaris.gateway.example.callee;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import com.tencent.cloud.common.constant.MetadataConstant; import com.tencent.cloud.common.constant.MetadataConstant;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -64,8 +65,8 @@ public class GatewayCalleeController {
public String echoHeader( public String echoHeader(
@RequestHeader(MetadataConstant.HeaderName.CUSTOM_METADATA) String metadataStr) @RequestHeader(MetadataConstant.HeaderName.CUSTOM_METADATA) String metadataStr)
throws UnsupportedEncodingException { throws UnsupportedEncodingException {
LOG.info(URLDecoder.decode(metadataStr, "UTF-8")); LOG.info(URLDecoder.decode(metadataStr, StandardCharsets.UTF_8.name()));
return URLDecoder.decode(metadataStr, "UTF-8"); return URLDecoder.decode(metadataStr, StandardCharsets.UTF_8.name());
} }
} }

@ -19,6 +19,7 @@ package com.tencent.cloud.polaris.gateway.example.callee;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import com.tencent.cloud.common.constant.MetadataConstant; import com.tencent.cloud.common.constant.MetadataConstant;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -64,8 +65,8 @@ public class GatewayCalleeController {
public String echoHeader( public String echoHeader(
@RequestHeader(MetadataConstant.HeaderName.CUSTOM_METADATA) String metadataStr) @RequestHeader(MetadataConstant.HeaderName.CUSTOM_METADATA) String metadataStr)
throws UnsupportedEncodingException { throws UnsupportedEncodingException {
LOG.info(URLDecoder.decode(metadataStr, "UTF-8")); LOG.info(URLDecoder.decode(metadataStr, StandardCharsets.UTF_8.name()));
return URLDecoder.decode(metadataStr, "UTF-8"); return URLDecoder.decode(metadataStr, StandardCharsets.UTF_8.name());
} }
} }

Loading…
Cancel
Save