Skip to content

Commit

Permalink
GlobalInterceptor and NetworkInterceptor support Order
Browse files Browse the repository at this point in the history
  • Loading branch information
chentianming11 committed Mar 30, 2022
1 parent 50f84ac commit f119d4c
Show file tree
Hide file tree
Showing 13 changed files with 199 additions and 138 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ gitee项目地址:[https://gitee.com/lianjiatech/retrofit-spring-boot-starter]
<dependency>
<groupId>com.github.lianjiatech</groupId>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>2.2.21</version>
<version>2.2.22</version>
</dependency>
```

Expand All @@ -67,7 +67,7 @@ gitee项目地址:[https://gitee.com/lianjiatech/retrofit-spring-boot-starter]
<dependency>
<groupId>com.github.lianjiatech</groupId>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>2.2.21</version>
<version>2.2.22</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
Expand Down
8 changes: 4 additions & 4 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
```xml
<dependency>
<groupId>com.github.lianjiatech</groupId>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>2.2.21</version>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>2.2.22</version>
</dependency>
```

Expand All @@ -52,8 +52,8 @@ This project depends on Retrofit-2.9.0, okhttp-3.14.9, and okio-1.17.5 versions.
```xml
<dependency>
<groupId>com.github.lianjiatech</groupId>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>2.2.21</version>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>2.2.22</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.lianjiatech</groupId>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>2.2.21</version>
<version>2.2.22</version>

<name>retrofit-spring-boot-starter</name>
<description>retrofit-spring-boot-starter</description>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
package com.github.lianjiatech.retrofit.spring.boot.config;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.lianjiatech.retrofit.spring.boot.core.*;
import com.github.lianjiatech.retrofit.spring.boot.degrade.BaseResourceNameParser;
import com.github.lianjiatech.retrofit.spring.boot.degrade.RetrofitDegradeRuleInitializer;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.BaseGlobalInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.NetworkInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.ServiceInstanceChooserInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.retry.BaseRetryInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.util.ApplicationContextUtils;
import okhttp3.ConnectionPool;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
Expand All @@ -27,15 +19,27 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.lianjiatech.retrofit.spring.boot.core.AutoConfiguredRetrofitScannerRegistrar;
import com.github.lianjiatech.retrofit.spring.boot.core.NoValidServiceInstanceChooser;
import com.github.lianjiatech.retrofit.spring.boot.core.PrototypeInterceptorBdfProcessor;
import com.github.lianjiatech.retrofit.spring.boot.core.RetrofitFactoryBean;
import com.github.lianjiatech.retrofit.spring.boot.core.ServiceInstanceChooser;
import com.github.lianjiatech.retrofit.spring.boot.degrade.BaseResourceNameParser;
import com.github.lianjiatech.retrofit.spring.boot.degrade.RetrofitDegradeRuleInitializer;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.GlobalAndNetworkInterceptorFinder;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.ServiceInstanceChooserInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.retry.BaseRetryInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.util.ApplicationContextUtils;

import okhttp3.ConnectionPool;
import retrofit2.CallAdapter;
import retrofit2.Converter;
import retrofit2.converter.jackson.JacksonConverterFactory;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

/**
* @author 陈添明
*/
Expand All @@ -60,10 +64,16 @@ public static PrototypeInterceptorBdfProcessor prototypeInterceptorBdfProcessor(
}
}

@Bean
public GlobalAndNetworkInterceptorFinder globalAndNetworkInterceptorFinder() {
return new GlobalAndNetworkInterceptorFinder();
}

@Bean
@ConditionalOnMissingBean
public RetrofitConfigBean retrofitConfigBean() throws IllegalAccessException, InstantiationException {
RetrofitConfigBean retrofitConfigBean = new RetrofitConfigBean(retrofitProperties);
RetrofitConfigBean retrofitConfigBean =
new RetrofitConfigBean(retrofitProperties, globalAndNetworkInterceptorFinder());
// Initialize the connection pool
Map<String, ConnectionPool> poolRegistry = new ConcurrentHashMap<>(4);
Map<String, PoolConfig> pool = retrofitProperties.getPool();
Expand All @@ -85,10 +95,6 @@ public RetrofitConfigBean retrofitConfigBean() throws IllegalAccessException, In
Class<? extends Converter.Factory>[] globalConverterFactories = retrofitProperties.getGlobalConverterFactories();
retrofitConfigBean.setGlobalConverterFactoryClasses(globalConverterFactories);

// globalInterceptors
Collection<BaseGlobalInterceptor> globalInterceptors = ApplicationContextUtils.getBeans(applicationContext, BaseGlobalInterceptor.class);
retrofitConfigBean.setGlobalInterceptors(globalInterceptors);

// retryInterceptor
RetryProperty retry = retrofitProperties.getRetry();
Class<? extends BaseRetryInterceptor> retryInterceptor = retry.getRetryInterceptor();
Expand All @@ -99,10 +105,6 @@ public RetrofitConfigBean retrofitConfigBean() throws IllegalAccessException, In
BeanUtils.copyProperties(retry, retryInterceptorInstance);
retrofitConfigBean.setRetryInterceptor(retryInterceptorInstance);

// add networkInterceptor
Collection<NetworkInterceptor> networkInterceptors = ApplicationContextUtils.getBeans(applicationContext, NetworkInterceptor.class);
retrofitConfigBean.setNetworkInterceptors(networkInterceptors);

// add ServiceInstanceChooserInterceptor
ServiceInstanceChooser serviceInstanceChooser;
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package com.github.lianjiatech.retrofit.spring.boot.config;

import java.util.List;
import java.util.Map;

import com.github.lianjiatech.retrofit.spring.boot.degrade.BaseResourceNameParser;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.BaseGlobalInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.GlobalAndNetworkInterceptorFinder;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.GlobalInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.NetworkInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.ServiceInstanceChooserInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.retry.BaseRetryInterceptor;

import okhttp3.ConnectionPool;
import retrofit2.CallAdapter;
import retrofit2.Converter;

import java.util.Collection;
import java.util.Map;

/**
* @author 陈添明
*/
Expand All @@ -21,9 +23,9 @@ public class RetrofitConfigBean {

private Map<String, ConnectionPool> poolRegistry;

private Collection<BaseGlobalInterceptor> globalInterceptors;
private final List<GlobalInterceptor> globalInterceptors;

private Collection<NetworkInterceptor> networkInterceptors;
private final List<NetworkInterceptor> networkInterceptors;

private BaseRetryInterceptor retryInterceptor;

Expand All @@ -40,8 +42,11 @@ public RetrofitProperties getRetrofitProperties() {
}


public RetrofitConfigBean(RetrofitProperties retrofitProperties) {
public RetrofitConfigBean(RetrofitProperties retrofitProperties,
GlobalAndNetworkInterceptorFinder globalAndNetworkInterceptorFinder) {
this.retrofitProperties = retrofitProperties;
this.globalInterceptors = globalAndNetworkInterceptorFinder.getGlobalInterceptors();
this.networkInterceptors = globalAndNetworkInterceptorFinder.getNetworkInterceptors();
}


Expand All @@ -53,14 +58,10 @@ public void setPoolRegistry(Map<String, ConnectionPool> poolRegistry) {
this.poolRegistry = poolRegistry;
}

public Collection<BaseGlobalInterceptor> getGlobalInterceptors() {
public List<GlobalInterceptor> getGlobalInterceptors() {
return globalInterceptors;
}

public void setGlobalInterceptors(Collection<BaseGlobalInterceptor> globalInterceptors) {
this.globalInterceptors = globalInterceptors;
}

public BaseRetryInterceptor getRetryInterceptor() {
return retryInterceptor;
}
Expand All @@ -69,14 +70,10 @@ public void setRetryInterceptor(BaseRetryInterceptor retryInterceptor) {
this.retryInterceptor = retryInterceptor;
}

public Collection<NetworkInterceptor> getNetworkInterceptors() {
public List<NetworkInterceptor> getNetworkInterceptors() {
return networkInterceptors;
}

public void setNetworkInterceptors(Collection<NetworkInterceptor> networkInterceptors) {
this.networkInterceptors = networkInterceptors;
}

public ServiceInstanceChooserInterceptor getServiceInstanceChooserInterceptor() {
return serviceInstanceChooserInterceptor;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package com.github.lianjiatech.retrofit.spring.boot.core;

import com.github.lianjiatech.retrofit.spring.boot.annotation.*;
import com.github.lianjiatech.retrofit.spring.boot.config.DegradeProperty;
import com.github.lianjiatech.retrofit.spring.boot.config.LogProperty;
import com.github.lianjiatech.retrofit.spring.boot.config.RetrofitConfigBean;
import com.github.lianjiatech.retrofit.spring.boot.config.RetrofitProperties;
import com.github.lianjiatech.retrofit.spring.boot.degrade.*;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.*;
import com.github.lianjiatech.retrofit.spring.boot.util.ApplicationContextUtils;
import com.github.lianjiatech.retrofit.spring.boot.util.BeanExtendUtils;
import com.github.lianjiatech.retrofit.spring.boot.util.RetrofitUtils;
import okhttp3.ConnectionPool;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
Expand All @@ -25,15 +26,43 @@
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.github.lianjiatech.retrofit.spring.boot.annotation.Intercept;
import com.github.lianjiatech.retrofit.spring.boot.annotation.InterceptMark;
import com.github.lianjiatech.retrofit.spring.boot.annotation.Intercepts;
import com.github.lianjiatech.retrofit.spring.boot.annotation.OkHttpClientBuilder;
import com.github.lianjiatech.retrofit.spring.boot.annotation.RetrofitClient;
import com.github.lianjiatech.retrofit.spring.boot.config.DegradeProperty;
import com.github.lianjiatech.retrofit.spring.boot.config.LogProperty;
import com.github.lianjiatech.retrofit.spring.boot.config.RetrofitConfigBean;
import com.github.lianjiatech.retrofit.spring.boot.config.RetrofitProperties;
import com.github.lianjiatech.retrofit.spring.boot.degrade.BaseResourceNameParser;
import com.github.lianjiatech.retrofit.spring.boot.degrade.Degrade;
import com.github.lianjiatech.retrofit.spring.boot.degrade.DegradeStrategy;
import com.github.lianjiatech.retrofit.spring.boot.degrade.DegradeType;
import com.github.lianjiatech.retrofit.spring.boot.degrade.FallbackFactory;
import com.github.lianjiatech.retrofit.spring.boot.degrade.RetrofitDegradeRule;
import com.github.lianjiatech.retrofit.spring.boot.degrade.RetrofitDegradeRuleInitializer;
import com.github.lianjiatech.retrofit.spring.boot.degrade.SentinelDegradeInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.BaseLoggingInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.BasePathMatchInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.ErrorDecoderInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.GlobalInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.LogLevel;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.LogStrategy;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.NetworkInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.ServiceInstanceChooserInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.util.ApplicationContextUtils;
import com.github.lianjiatech.retrofit.spring.boot.util.BeanExtendUtils;
import com.github.lianjiatech.retrofit.spring.boot.util.RetrofitUtils;

import okhttp3.ConnectionPool;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import retrofit2.CallAdapter;
import retrofit2.Converter;
import retrofit2.Retrofit;

import java.lang.annotation.Annotation;
import java.lang.reflect.*;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
* @author 陈添明
*/
Expand Down Expand Up @@ -279,7 +308,7 @@ private synchronized OkHttpClient getOkHttpClient(Class<?> retrofitClientInterfa
// Add the interceptor defined by the annotation on the interface
List<Interceptor> interceptors = new ArrayList<>(findInterceptorByAnnotation(retrofitClientInterfaceClass));
// add global interceptor
Collection<BaseGlobalInterceptor> globalInterceptors = retrofitConfigBean.getGlobalInterceptors();
List<GlobalInterceptor> globalInterceptors = retrofitConfigBean.getGlobalInterceptors();
if (!CollectionUtils.isEmpty(globalInterceptors)) {
interceptors.addAll(globalInterceptors);
}
Expand Down Expand Up @@ -310,7 +339,7 @@ private synchronized OkHttpClient getOkHttpClient(Class<?> retrofitClientInterfa
okHttpClientBuilder.addNetworkInterceptor(loggingInterceptor);
}

Collection<NetworkInterceptor> networkInterceptors = retrofitConfigBean.getNetworkInterceptors();
List<NetworkInterceptor> networkInterceptors = retrofitConfigBean.getNetworkInterceptors();
if (!CollectionUtils.isEmpty(networkInterceptors)) {
for (NetworkInterceptor networkInterceptor : networkInterceptors) {
okHttpClientBuilder.addNetworkInterceptor(networkInterceptor);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.github.lianjiatech.retrofit.spring.boot.interceptor;

import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

/**
* @author 陈添明
* @since 2022/3/30 11:01 下午
*/
public class GlobalAndNetworkInterceptorFinder {

@Autowired(required = false)
private List<GlobalInterceptor> globalInterceptors;

@Autowired(required = false)
private List<NetworkInterceptor> networkInterceptors;

public List<GlobalInterceptor> getGlobalInterceptors() {
if (globalInterceptors == null) {
return Collections.emptyList();
}
return globalInterceptors;
}

public List<NetworkInterceptor> getNetworkInterceptors() {
if (networkInterceptors == null) {
return Collections.emptyList();
}
return networkInterceptors;
}
}
Loading

0 comments on commit f119d4c

Please sign in to comment.