Skip to content

Commit

Permalink
[bug]add utf-8 charset in decode method of CustomErrorDecoder
Browse files Browse the repository at this point in the history
  • Loading branch information
Abdollahi authored and Abdollahi committed Feb 13, 2024
1 parent 4a51b78 commit df7e3d7
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private HttpHeaders getMaskedHeaders(HttpHeaders headers) {
List<String> headerValues = entry.getValue();
List<String> 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));
Expand Down Expand Up @@ -135,5 +135,4 @@ private String toJson(Object object) {
return "error creating json. " + exception.getMessage();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down Expand Up @@ -131,7 +130,8 @@ public List<RequestInterceptor> requestInterceptors(

@Override
@Bean("customServer-feignContract")
public Contract feignContractWithCustomSpringConversion(ConversionService feignConversionService, List<AnnotatedParameterProcessor> processors) {
public Contract feignContractWithCustomSpringConversion(
ConversionService feignConversionService, List<AnnotatedParameterProcessor> processors) {
return super.feignContractWithCustomSpringConversion(feignConversionService, processors);
}

Expand All @@ -144,26 +144,29 @@ public FormattingConversionService feignConversionService(List<FeignFormatterReg

@Override
@Bean("customServer-feignEncoder")
public Encoder feignEncoder(@Qualifier("customServer-jacksonHttpMessageConverter") HttpMessageConverter<Object> httpMessageConverter) {
public Encoder feignEncoder(
@Qualifier("customServer-jacksonHttpMessageConverter") HttpMessageConverter<Object> httpMessageConverter) {
return super.feignEncoder(httpMessageConverter);
}

@Override
@Bean("customServer-feignDecoder")
public Decoder feignDecoder(@Qualifier("customServer-jacksonHttpMessageConverter") HttpMessageConverter<Object> httpMessageConverter) {
public Decoder feignDecoder(
@Qualifier("customServer-jacksonHttpMessageConverter") HttpMessageConverter<Object> httpMessageConverter) {
return super.feignDecoder(httpMessageConverter);
}

@Override
@Bean("customServer-jacksonHttpMessageConverter")
public HttpMessageConverter<Object> httpMessageConverter(@Qualifier("customServer-objectMapper")
ObjectMapper objectMapper) {
public HttpMessageConverter<Object> 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);
Expand All @@ -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);
}

Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, Class<? extends Exception>> exceptionMap = customErrorDecoderConfig.getExceptionMap();
if (status >= 400 && status < 500) {
return extractBadRequestErrorException(responseBody, exceptionMap);
Expand Down

0 comments on commit df7e3d7

Please sign in to comment.