Condition 'null != interceptors' is always 'true' (#357)

Co-authored-by: xingducai <836959451@qq.com>
pull/361/head
Haotian Zhang 2 years ago committed by GitHub
parent 61fa55d661
commit b5e850cd3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -10,7 +10,7 @@
- [Feature:add restTemplate Report Polaris](https://github.com/Tencent/spring-cloud-tencent/pull/304) - [Feature:add restTemplate Report Polaris](https://github.com/Tencent/spring-cloud-tencent/pull/304)
- [Update GitHub Actions workflow](https://github.com/Tencent/spring-cloud-tencent/pull/305) - [Update GitHub Actions workflow](https://github.com/Tencent/spring-cloud-tencent/pull/305)
- [fix: use non-blocking call instead of blocking call](https://github.com/Tencent/spring-cloud-tencent/pull/309) - [fix: use non-blocking call instead of blocking call](https://github.com/Tencent/spring-cloud-tencent/pull/309)
- [fix:solve the chaos code problem on rejectTips](https://github.com/Tencent/spring-cloud-tencent/pull/283) - [solve the chaos code problem on rejectTips in 2021.0](https://github.com/Tencent/spring-cloud-tencent/pull/315)
- [Fix config file format misspell](https://github.com/Tencent/spring-cloud-tencent/pull/321) - [Fix config file format misspell](https://github.com/Tencent/spring-cloud-tencent/pull/321)
- [Feature: Add instance metadata spi for registration](https://github.com/Tencent/spring-cloud-tencent/pull/324) - [Feature: Add instance metadata spi for registration](https://github.com/Tencent/spring-cloud-tencent/pull/324)
- [Improve: Improve code style & Upgrade spring boot version with 2.6.9](https://github.com/Tencent/spring-cloud-tencent/pull/322) - [Improve: Improve code style & Upgrade spring boot version with 2.6.9](https://github.com/Tencent/spring-cloud-tencent/pull/322)
@ -19,4 +19,5 @@
- [feat:Add GitHub action of codecov.yml and update junit of metadata.](https://github.com/Tencent/spring-cloud-tencent/pull/344) - [feat:Add GitHub action of codecov.yml and update junit of metadata.](https://github.com/Tencent/spring-cloud-tencent/pull/344)
- [Feature: optimize static metadata manager](https://github.com/Tencent/spring-cloud-tencent/pull/346) - [Feature: optimize static metadata manager](https://github.com/Tencent/spring-cloud-tencent/pull/346)
- [fix: shutdown thread pool before the container closes](https://github.com/Tencent/spring-cloud-tencent/pull/355) - [fix: shutdown thread pool before the container closes](https://github.com/Tencent/spring-cloud-tencent/pull/355)
- [rm code: Condition 'null != interceptors' is always 'true' ](https://github.com/Tencent/spring-cloud-tencent/pull/357)
- [docs:update logo in README.](https://github.com/Tencent/spring-cloud-tencent/pull/360) - [docs:update logo in README.](https://github.com/Tencent/spring-cloud-tencent/pull/360)

@ -68,8 +68,10 @@ public class MetadataTransferAutoConfiguration {
DecodeTransferMetadataServletFilter decodeTransferMetadataServletFilter) { DecodeTransferMetadataServletFilter decodeTransferMetadataServletFilter) {
FilterRegistrationBean<DecodeTransferMetadataServletFilter> filterRegistrationBean = new FilterRegistrationBean<>( FilterRegistrationBean<DecodeTransferMetadataServletFilter> filterRegistrationBean = new FilterRegistrationBean<>(
decodeTransferMetadataServletFilter); decodeTransferMetadataServletFilter);
filterRegistrationBean.setDispatcherTypes(ASYNC, ERROR, FORWARD, INCLUDE, REQUEST); filterRegistrationBean.setDispatcherTypes(ASYNC, ERROR, FORWARD, INCLUDE,
filterRegistrationBean.setOrder(MetadataConstant.OrderConstant.WEB_FILTER_ORDER); REQUEST);
filterRegistrationBean
.setOrder(MetadataConstant.OrderConstant.WEB_FILTER_ORDER);
return filterRegistrationBean; return filterRegistrationBean;
} }
@ -140,29 +142,34 @@ public class MetadataTransferAutoConfiguration {
BeanPostProcessor encodeTransferMetadataRestTemplatePostProcessor( BeanPostProcessor encodeTransferMetadataRestTemplatePostProcessor(
EncodeTransferMedataRestTemplateInterceptor encodeTransferMedataRestTemplateInterceptor) { EncodeTransferMedataRestTemplateInterceptor encodeTransferMedataRestTemplateInterceptor) {
// Coping with multiple bean injection scenarios // Coping with multiple bean injection scenarios
Map<String, RestTemplate> beans = this.context.getBeansOfType(RestTemplate.class); Map<String, RestTemplate> beans = this.context
.getBeansOfType(RestTemplate.class);
// If the restTemplate has been created when the // If the restTemplate has been created when the
// MetadataRestTemplatePostProcessor Bean // MetadataRestTemplatePostProcessor Bean
// is initialized, then manually set the interceptor. // is initialized, then manually set the interceptor.
if (!CollectionUtils.isEmpty(beans)) { if (!CollectionUtils.isEmpty(beans)) {
for (RestTemplate restTemplate : beans.values()) { for (RestTemplate restTemplate : beans.values()) {
List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors(); List<ClientHttpRequestInterceptor> interceptors = restTemplate
.getInterceptors();
// Avoid setting interceptor repeatedly. // Avoid setting interceptor repeatedly.
if (null != interceptors && !interceptors.contains(encodeTransferMedataRestTemplateInterceptor)) { if (!interceptors.contains(encodeTransferMedataRestTemplateInterceptor)) {
interceptors.add(encodeTransferMedataRestTemplateInterceptor); interceptors.add(encodeTransferMedataRestTemplateInterceptor);
restTemplate.setInterceptors(interceptors); restTemplate.setInterceptors(interceptors);
} }
} }
} }
return new EncodeTransferMetadataRestTemplatePostProcessor(encodeTransferMedataRestTemplateInterceptor); return new EncodeTransferMetadataRestTemplatePostProcessor(
encodeTransferMedataRestTemplateInterceptor);
} }
@Override @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.context = applicationContext; this.context = applicationContext;
} }
public static class EncodeTransferMetadataRestTemplatePostProcessor implements BeanPostProcessor { public static class EncodeTransferMetadataRestTemplatePostProcessor
implements BeanPostProcessor {
private EncodeTransferMedataRestTemplateInterceptor encodeTransferMedataRestTemplateInterceptor; private EncodeTransferMedataRestTemplateInterceptor encodeTransferMedataRestTemplateInterceptor;
@ -180,9 +187,10 @@ public class MetadataTransferAutoConfiguration {
public Object postProcessAfterInitialization(Object bean, String beanName) { public Object postProcessAfterInitialization(Object bean, String beanName) {
if (bean instanceof RestTemplate) { if (bean instanceof RestTemplate) {
RestTemplate restTemplate = (RestTemplate) bean; RestTemplate restTemplate = (RestTemplate) bean;
List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors(); List<ClientHttpRequestInterceptor> interceptors = restTemplate
.getInterceptors();
// Avoid setting interceptor repeatedly. // Avoid setting interceptor repeatedly.
if (null != interceptors && !interceptors.contains(encodeTransferMedataRestTemplateInterceptor)) { if (!interceptors.contains(encodeTransferMedataRestTemplateInterceptor)) {
interceptors.add(this.encodeTransferMedataRestTemplateInterceptor); interceptors.add(this.encodeTransferMedataRestTemplateInterceptor);
restTemplate.setInterceptors(interceptors); restTemplate.setInterceptors(interceptors);
} }

Loading…
Cancel
Save