From df7e3d77c77160c52e0ba0232c7cf1a0eb0d5d17 Mon Sep 17 00:00:00 2001 From: Abdollahi Date: Tue, 13 Feb 2024 18:06:43 +0330 Subject: [PATCH] [bug]add utf-8 charset in decode method of CustomErrorDecoder --- .../util/HttpLoggingInterceptorUtil.java | 3 +- .../config/feign/CustomServerFeignConfig.java | 38 +++++++++---------- .../AbstractFeignConfiguration.java | 7 ++-- .../impl/feign/CustomErrorDecoder.java | 8 ++-- 4 files changed, 27 insertions(+), 29 deletions(-) diff --git a/tosan-httpclient-spring-boot-resttemplate-starter/src/main/java/com/tosan/client/http/resttemplate/starter/util/HttpLoggingInterceptorUtil.java b/tosan-httpclient-spring-boot-resttemplate-starter/src/main/java/com/tosan/client/http/resttemplate/starter/util/HttpLoggingInterceptorUtil.java index a25e1f9..c465f4f 100644 --- a/tosan-httpclient-spring-boot-resttemplate-starter/src/main/java/com/tosan/client/http/resttemplate/starter/util/HttpLoggingInterceptorUtil.java +++ b/tosan-httpclient-spring-boot-resttemplate-starter/src/main/java/com/tosan/client/http/resttemplate/starter/util/HttpLoggingInterceptorUtil.java @@ -94,7 +94,7 @@ private HttpHeaders getMaskedHeaders(HttpHeaders headers) { List headerValues = entry.getValue(); List maskedHeaderValues = new ArrayList<>(); headerValues.forEach(headerValue -> { - if (headerValue != null && headerValue.length() > 0) { + if (headerValue != null && !headerValue.isEmpty()) { JsonReplaceResultDto jsonReplaceResultDto = replaceHelperDecider.checkJsonAndReplace(headerValue); if (!jsonReplaceResultDto.isJson()) { maskedHeaderValues.add(replaceHelperDecider.replace(headerName, headerValue)); @@ -135,5 +135,4 @@ private String toJson(Object object) { return "error creating json. " + exception.getMessage(); } } - } diff --git a/tosan-httpclient-spring-boot-sample/src/main/java/com/tosan/client/http/sample/server/api/config/feign/CustomServerFeignConfig.java b/tosan-httpclient-spring-boot-sample/src/main/java/com/tosan/client/http/sample/server/api/config/feign/CustomServerFeignConfig.java index 94c412b..3b95d73 100644 --- a/tosan-httpclient-spring-boot-sample/src/main/java/com/tosan/client/http/sample/server/api/config/feign/CustomServerFeignConfig.java +++ b/tosan-httpclient-spring-boot-sample/src/main/java/com/tosan/client/http/sample/server/api/config/feign/CustomServerFeignConfig.java @@ -54,16 +54,15 @@ public ObjectMapper objectMapper() { } @Bean("customServer-replace-helper") - public JsonReplaceHelperDecider replaceHelperDecider(JacksonReplaceHelper jacksonReplaceHelper, - RegexReplaceHelper regexReplaceHelper, - @Qualifier("customServer-secured-parameters") - SecureParametersConfig secureParametersConfig) { + public JsonReplaceHelperDecider replaceHelperDecider( + JacksonReplaceHelper jacksonReplaceHelper, RegexReplaceHelper regexReplaceHelper, + @Qualifier("customServer-secured-parameters") SecureParametersConfig secureParametersConfig) { return super.replaceHelperDecider(jacksonReplaceHelper, regexReplaceHelper, secureParametersConfig); } @Bean("customServer-httpFeignClientLogger") - public Logger httpFeignClientLogger(@Qualifier("customServer-replace-helper") - JsonReplaceHelperDecider replaceHelperDecider) { + public Logger httpFeignClientLogger( + @Qualifier("customServer-replace-helper") JsonReplaceHelperDecider replaceHelperDecider) { return super.httpFeignClientLogger(replaceHelperDecider, "custom-server"); } @@ -131,7 +130,8 @@ public List requestInterceptors( @Override @Bean("customServer-feignContract") - public Contract feignContractWithCustomSpringConversion(ConversionService feignConversionService, List processors) { + public Contract feignContractWithCustomSpringConversion( + ConversionService feignConversionService, List processors) { return super.feignContractWithCustomSpringConversion(feignConversionService, processors); } @@ -144,26 +144,29 @@ public FormattingConversionService feignConversionService(List httpMessageConverter) { + public Encoder feignEncoder( + @Qualifier("customServer-jacksonHttpMessageConverter") HttpMessageConverter httpMessageConverter) { return super.feignEncoder(httpMessageConverter); } @Override @Bean("customServer-feignDecoder") - public Decoder feignDecoder(@Qualifier("customServer-jacksonHttpMessageConverter") HttpMessageConverter httpMessageConverter) { + public Decoder feignDecoder( + @Qualifier("customServer-jacksonHttpMessageConverter") HttpMessageConverter httpMessageConverter) { return super.feignDecoder(httpMessageConverter); } @Override @Bean("customServer-jacksonHttpMessageConverter") - public HttpMessageConverter httpMessageConverter(@Qualifier("customServer-objectMapper") - ObjectMapper objectMapper) { + public HttpMessageConverter httpMessageConverter( + @Qualifier("customServer-objectMapper") ObjectMapper objectMapper) { return super.httpMessageConverter(objectMapper); } @Override @Bean("customServer-feignErrorDecoderConfig") - public CustomErrorDecoderConfig customErrorDecoderConfig(@Qualifier("customServer-objectMapper") ObjectMapper objectMapper) { + public CustomErrorDecoderConfig customErrorDecoderConfig( + @Qualifier("customServer-objectMapper") ObjectMapper objectMapper) { CustomErrorDecoderConfig customErrorDecoderConfig = new CustomErrorDecoderConfig(); customErrorDecoderConfig.getScanPackageList().add("com.tosan.client.http.sample.server.api.exception"); customErrorDecoderConfig.setExceptionExtractType(ExceptionExtractType.EXCEPTION_IDENTIFIER_FIELDS); @@ -175,7 +178,8 @@ public CustomErrorDecoderConfig customErrorDecoderConfig(@Qualifier("customServe @Override @Bean("customServer-feignErrorDecoder") - public CustomErrorDecoder customErrorDecoder(@Qualifier("customServer-feignErrorDecoderConfig") CustomErrorDecoderConfig customErrorDecoderConfig) { + public CustomErrorDecoder customErrorDecoder( + @Qualifier("customServer-feignErrorDecoderConfig") CustomErrorDecoderConfig customErrorDecoderConfig) { return super.customErrorDecoder(customErrorDecoderConfig); } @@ -187,18 +191,14 @@ public HttpClientBuilder apacheHttpClientBuilder() { @Override @Bean("customServer-retryer") - @ConditionalOnMissingBean( - name = {"customServer-retryer"} - ) + @ConditionalOnMissingBean(name = {"customServer-retryer"}) public Retryer retryer() { return super.retryer(); } @Override @Bean("customServer-feignLoggerLevel") - @ConditionalOnMissingBean( - name = {"customServer-feignLoggerLevel"} - ) + @ConditionalOnMissingBean(name = {"customServer-feignLoggerLevel"}) public Logger.Level feignLoggerLevel() { return super.feignLoggerLevel(); } diff --git a/tosan-httpclient-spring-boot-starter/src/main/java/com/tosan/client/http/starter/configuration/AbstractFeignConfiguration.java b/tosan-httpclient-spring-boot-starter/src/main/java/com/tosan/client/http/starter/configuration/AbstractFeignConfiguration.java index 3ba48f5..068c7df 100644 --- a/tosan-httpclient-spring-boot-starter/src/main/java/com/tosan/client/http/starter/configuration/AbstractFeignConfiguration.java +++ b/tosan-httpclient-spring-boot-starter/src/main/java/com/tosan/client/http/starter/configuration/AbstractFeignConfiguration.java @@ -84,9 +84,10 @@ public Logger httpFeignClientLogger(JsonReplaceHelperDecider replaceHelperDecide return new HttpFeignClientLogger(serverName, replaceHelperDecider); } - public ConfigurableApacheHttpClientFactory apacheHttpClientFactory(HttpClientBuilder builder, - PoolingHttpClientConnectionManagerBuilder connectionManagerBuilder, - HttpClientProperties customServerClientConfig) { + public ConfigurableApacheHttpClientFactory apacheHttpClientFactory( + HttpClientBuilder builder, + PoolingHttpClientConnectionManagerBuilder connectionManagerBuilder, + HttpClientProperties customServerClientConfig) { return new ConfigurableApacheHttpClientFactory(builder, connectionManagerBuilder, customServerClientConfig); } diff --git a/tosan-httpclient-spring-boot-starter/src/main/java/com/tosan/client/http/starter/impl/feign/CustomErrorDecoder.java b/tosan-httpclient-spring-boot-starter/src/main/java/com/tosan/client/http/starter/impl/feign/CustomErrorDecoder.java index 796c0f5..3c066a3 100644 --- a/tosan-httpclient-spring-boot-starter/src/main/java/com/tosan/client/http/starter/impl/feign/CustomErrorDecoder.java +++ b/tosan-httpclient-spring-boot-starter/src/main/java/com/tosan/client/http/starter/impl/feign/CustomErrorDecoder.java @@ -14,9 +14,9 @@ import org.springframework.util.CollectionUtils; import org.springframework.util.StreamUtils; -import java.io.ByteArrayOutputStream; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Modifier; +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -44,11 +44,9 @@ public ObjectMapper getObjectMapper() { public Exception decode(String methodKey, Response response) { try { Response.Body body = response.body(); - ByteArrayOutputStream output = new ByteArrayOutputStream(); - StreamUtils.copy(body.asInputStream(), output); - String responseBody = output.toString(); + String responseBody = StreamUtils.copyToString(body.asInputStream(), StandardCharsets.UTF_8); int status = response.status(); - LOGGER.info("ServerErrorResponse :\n ResponseStatus:{}\n ResponseBody:{}", status, responseBody); + LOGGER.info("ServerErrorResponse:\n ResponseStatus: {}\n ResponseBody: {}", status, responseBody); Map> exceptionMap = customErrorDecoderConfig.getExceptionMap(); if (status >= 400 && status < 500) { return extractBadRequestErrorException(responseBody, exceptionMap);