Initial commit
This commit is contained in:
@@ -0,0 +1,180 @@
|
||||
package com.ubt.jimu.base.http.interceptor;
|
||||
|
||||
import android.os.SystemClock;
|
||||
import com.google.gson.Gson;
|
||||
import com.ubt.jimu.JimuApplication;
|
||||
import com.ubt.jimu.base.cache.Cache;
|
||||
import com.ubt.jimu.base.http.ApiConstants;
|
||||
import com.ubt.jimu.user.model.SpUserHolder;
|
||||
import com.ubt.jimu.utils.DeviceUtils;
|
||||
import com.ubt.jimu.utils.LogUtils;
|
||||
import com.ubt.jimu.utils.Md5Utils;
|
||||
import com.ubtech.utils.XLog;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import okhttp3.FormBody;
|
||||
import okhttp3.HttpUrl;
|
||||
import okhttp3.Interceptor;
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.Response;
|
||||
import okio.Buffer;
|
||||
|
||||
/* loaded from: classes.dex */
|
||||
public class CommonParamsInterceptor implements Interceptor {
|
||||
private static final String APP_KEY = "a0a1dfa9a7d74d999f28b6b8ceaa0de3";
|
||||
public static final String CHARACTER = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
private static final String SIGN_PART_SEPARATOR = " ";
|
||||
private static final String TAG = "okhttp-intercept";
|
||||
private static Random sRandom = new Random();
|
||||
Map<String, String> headersMap;
|
||||
Map<String, String> postMap;
|
||||
Map<String, String> queryMap;
|
||||
|
||||
public static class Builder {
|
||||
CommonParamsInterceptor interceptor = new CommonParamsInterceptor();
|
||||
|
||||
public Builder addHeaders(Map<String, String> map) {
|
||||
this.interceptor.headersMap.putAll(map);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder addPostParams(String str, String str2) {
|
||||
this.interceptor.postMap.put(str, str2);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder addQueryParams(String str, String str2) {
|
||||
this.interceptor.queryMap.put(str, str2);
|
||||
return this;
|
||||
}
|
||||
|
||||
public CommonParamsInterceptor build() {
|
||||
return this.interceptor;
|
||||
}
|
||||
|
||||
public Builder addPostParams(Map<String, String> map) {
|
||||
if (map != null && map.size() > 0) {
|
||||
this.interceptor.postMap.putAll(map);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder addQueryParams(Map<String, String> map) {
|
||||
this.interceptor.queryMap.putAll(map);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
private Request addQueryParams2Url(HttpUrl.Builder builder, Request.Builder builder2, Map<String, String> map) {
|
||||
if (map.size() <= 0) {
|
||||
return builder2.build();
|
||||
}
|
||||
for (Map.Entry<String, String> entry : map.entrySet()) {
|
||||
builder.addQueryParameter(entry.getKey(), entry.getValue());
|
||||
}
|
||||
builder2.url(builder.build());
|
||||
return builder2.build();
|
||||
}
|
||||
|
||||
private String bodyToString(RequestBody requestBody) {
|
||||
try {
|
||||
Buffer buffer = new Buffer();
|
||||
if (requestBody == null) {
|
||||
return "";
|
||||
}
|
||||
requestBody.writeTo(buffer);
|
||||
return buffer.readUtf8();
|
||||
} catch (IOException unused) {
|
||||
return "did not work";
|
||||
}
|
||||
}
|
||||
|
||||
private static long getServerTimestamp() {
|
||||
long j = Cache.getInstance().differ;
|
||||
return j <= 0 ? System.currentTimeMillis() : j + SystemClock.elapsedRealtime();
|
||||
}
|
||||
|
||||
private static String randomAlphanumeric(int i) {
|
||||
StringBuffer stringBuffer = new StringBuffer();
|
||||
for (int i2 = 0; i2 < i; i2++) {
|
||||
stringBuffer.append(CHARACTER.charAt(sRandom.nextInt(62)));
|
||||
}
|
||||
return stringBuffer.toString();
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
private static String sign() {
|
||||
long currentTimeMillis = System.currentTimeMillis() / 1000;
|
||||
return Md5Utils.a(currentTimeMillis + APP_KEY) + SIGN_PART_SEPARATOR + currentTimeMillis;
|
||||
}
|
||||
|
||||
public static String sign2() {
|
||||
long serverTimestamp = getServerTimestamp() / 1000;
|
||||
String randomAlphanumeric = randomAlphanumeric(10);
|
||||
return Md5Utils.a(serverTimestamp + APP_KEY + randomAlphanumeric + DeviceUtils.a(JimuApplication.l())) + SIGN_PART_SEPARATOR + serverTimestamp + SIGN_PART_SEPARATOR + randomAlphanumeric + SIGN_PART_SEPARATOR + "v2";
|
||||
}
|
||||
|
||||
@Override // okhttp3.Interceptor
|
||||
public Response intercept(Interceptor.Chain chain) throws IOException {
|
||||
Request request = chain.request();
|
||||
Request.Builder newBuilder = request.newBuilder();
|
||||
if (this.headersMap.size() > 0) {
|
||||
for (Map.Entry<String, String> entry : this.headersMap.entrySet()) {
|
||||
newBuilder.addHeader(entry.getKey(), entry.getValue());
|
||||
XLog.a(TAG, "intercept Header:%s, %s", entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
newBuilder.addHeader("language", JimuApplication.l().g());
|
||||
LogUtils.b("header:language " + JimuApplication.l().g());
|
||||
newBuilder.addHeader(ApiConstants.AREA, JimuApplication.l().c());
|
||||
LogUtils.b("header:area " + JimuApplication.l().c());
|
||||
newBuilder.addHeader(ApiConstants.AUTHORIZATION, SpUserHolder.b() > 0 ? SpUserHolder.c() : Cache.getInstance().getUserToken());
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("header=authorization :");
|
||||
sb.append(SpUserHolder.b() > 0 ? SpUserHolder.c() : Cache.getInstance().getUserToken());
|
||||
sb.append(" 本地token:");
|
||||
sb.append(Cache.getInstance().getUserToken());
|
||||
LogUtils.c(sb.toString());
|
||||
newBuilder.addHeader(ApiConstants.KEY_SIGN, sign2());
|
||||
LogUtils.c("header=X-UBT-Sign :" + sign2());
|
||||
newBuilder.addHeader(ApiConstants.KEY_DEVICE_ID, DeviceUtils.a(JimuApplication.l()));
|
||||
LogUtils.c("header=X-UBT-DeviceId :" + DeviceUtils.a(JimuApplication.l()));
|
||||
Request addQueryParams2Url = addQueryParams2Url(request.url().newBuilder(), newBuilder, this.queryMap);
|
||||
if (addQueryParams2Url.method().equals("POST") || addQueryParams2Url.method().equals("PUT")) {
|
||||
HashMap hashMap = new HashMap();
|
||||
if (addQueryParams2Url.body() instanceof FormBody) {
|
||||
FormBody formBody = (FormBody) addQueryParams2Url.body();
|
||||
if (formBody.size() > 0) {
|
||||
int size = formBody.size();
|
||||
for (int i = 0; i < size; i++) {
|
||||
hashMap.put(formBody.name(i), formBody.value(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
hashMap.putAll(ApiConstants.getBasicParams());
|
||||
if (this.postMap.size() > 0) {
|
||||
hashMap.putAll(this.postMap);
|
||||
}
|
||||
String json = new Gson().toJson(hashMap);
|
||||
MediaType parse = MediaType.parse("application/json; charset=utf-8");
|
||||
if (addQueryParams2Url.method().equals("PUT")) {
|
||||
newBuilder.put(RequestBody.create(parse, json));
|
||||
} else {
|
||||
newBuilder.post(RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), bodyToString(addQueryParams2Url.body()))).build();
|
||||
}
|
||||
} else if (addQueryParams2Url.method().equals("GET")) {
|
||||
addQueryParams2Url = newBuilder.url(addQueryParams2Url.url().newBuilder().addQueryParameter("language", JimuApplication.l().g()).build()).build();
|
||||
}
|
||||
return chain.proceed(addQueryParams2Url);
|
||||
}
|
||||
|
||||
private CommonParamsInterceptor() {
|
||||
this.queryMap = new HashMap();
|
||||
this.postMap = new HashMap();
|
||||
this.headersMap = new HashMap();
|
||||
}
|
||||
}
|
@@ -0,0 +1,151 @@
|
||||
package com.ubt.jimu.base.http.interceptor;
|
||||
|
||||
import android.os.SystemClock;
|
||||
import android.text.TextUtils;
|
||||
import com.google.gson.Gson;
|
||||
import com.ubt.jimu.JimuApplication;
|
||||
import com.ubt.jimu.base.cache.Cache;
|
||||
import com.ubt.jimu.base.db.user.UserDbHandler;
|
||||
import com.ubt.jimu.base.entities.User;
|
||||
import com.ubt.jimu.base.event.MessageEvent;
|
||||
import com.ubt.jimu.base.http.ApiClient;
|
||||
import com.ubt.jimu.base.http.ApiConstants;
|
||||
import com.ubt.jimu.base.http.ApiResultException;
|
||||
import com.ubt.jimu.base.http.response.Timestamp;
|
||||
import com.ubt.jimu.base.http.service.UserService;
|
||||
import com.ubt.jimu.user.repository.UserRepository;
|
||||
import com.ubt.jimu.utils.JsonHelper;
|
||||
import com.ubt.jimu.utils.ShortcutHelper;
|
||||
import com.ubtech.utils.StringUtils;
|
||||
import com.ubtech.utils.XLog;
|
||||
import com.ubtrobot.log.ALog;
|
||||
import io.reactivex.Observer;
|
||||
import io.reactivex.disposables.Disposable;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import okhttp3.Interceptor;
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.ResponseBody;
|
||||
import okio.Buffer;
|
||||
import okio.BufferedSource;
|
||||
import org.greenrobot.eventbus.EventBus;
|
||||
|
||||
/* loaded from: classes.dex */
|
||||
public class ResponseBodyInterceptor implements Interceptor {
|
||||
private static final Charset UTF8 = Charset.forName("UTF-8");
|
||||
private final int SIGNATURE_FAILED = 1003;
|
||||
private volatile boolean requesting = false;
|
||||
private AtomicInteger retryNum = new AtomicInteger(0);
|
||||
private long future = 0;
|
||||
|
||||
private void getServerTimestamp() {
|
||||
((UserService) ApiClient.getService(UserService.class)).getServerTimestamp().observeOn(Schedulers.b()).observeOn(Schedulers.b()).subscribe(new Observer<Timestamp>() { // from class: com.ubt.jimu.base.http.interceptor.ResponseBodyInterceptor.1
|
||||
private void setLocalTime() {
|
||||
long elapsedRealtime = SystemClock.elapsedRealtime();
|
||||
Cache.getInstance().differ = System.currentTimeMillis() - elapsedRealtime;
|
||||
if (ResponseBodyInterceptor.this.retryNum.get() < 10) {
|
||||
ResponseBodyInterceptor.this.retryNum.incrementAndGet();
|
||||
}
|
||||
ResponseBodyInterceptor responseBodyInterceptor = ResponseBodyInterceptor.this;
|
||||
responseBodyInterceptor.future = (elapsedRealtime + 1000) << responseBodyInterceptor.retryNum.get();
|
||||
ResponseBodyInterceptor.this.requesting = false;
|
||||
}
|
||||
|
||||
@Override // io.reactivex.Observer
|
||||
public void onComplete() {
|
||||
}
|
||||
|
||||
@Override // io.reactivex.Observer
|
||||
public void onError(Throwable th) {
|
||||
setLocalTime();
|
||||
}
|
||||
|
||||
@Override // io.reactivex.Observer
|
||||
public void onSubscribe(Disposable disposable) {
|
||||
}
|
||||
|
||||
@Override // io.reactivex.Observer
|
||||
public void onNext(Timestamp timestamp) {
|
||||
if (timestamp == null || timestamp.getTimestamp() == 0) {
|
||||
setLocalTime();
|
||||
return;
|
||||
}
|
||||
Cache.getInstance().differ = (timestamp.getTimestamp() * 1000) - SystemClock.elapsedRealtime();
|
||||
ResponseBodyInterceptor.this.retryNum.set(0);
|
||||
ResponseBodyInterceptor.this.future = 0L;
|
||||
ResponseBodyInterceptor.this.requesting = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void logout() {
|
||||
try {
|
||||
Cache.getInstance().clearCacheUser();
|
||||
UserDbHandler.clearUser();
|
||||
ShortcutHelper.a(JimuApplication.l());
|
||||
EventBus.b().b(new MessageEvent(1));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private Response tryAgain(Response response) {
|
||||
if (!this.requesting) {
|
||||
if (this.future > SystemClock.elapsedRealtime()) {
|
||||
return response;
|
||||
}
|
||||
getServerTimestamp();
|
||||
this.requesting = true;
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
@Override // okhttp3.Interceptor
|
||||
public Response intercept(Interceptor.Chain chain) throws IOException {
|
||||
ApiResultException apiResultException;
|
||||
Request request = chain.request();
|
||||
try {
|
||||
Response proceed = chain.proceed(request);
|
||||
ResponseBody body = proceed.body();
|
||||
BufferedSource source = body.source();
|
||||
source.request(Long.MAX_VALUE);
|
||||
Buffer buffer = source.buffer();
|
||||
Charset charset = UTF8;
|
||||
MediaType contentType = body.contentType();
|
||||
if (contentType != null) {
|
||||
charset = contentType.charset(UTF8);
|
||||
}
|
||||
String readString = buffer.clone().readString(charset);
|
||||
ALog.a("ResponseBodyInterceptor").d(StringUtils.a(readString));
|
||||
if (proceed.code() != 401) {
|
||||
if (proceed.code() != 200 && (apiResultException = (ApiResultException) JsonHelper.b(readString, ApiResultException.class)) != null && apiResultException.getCode() == 1003) {
|
||||
return tryAgain(proceed);
|
||||
}
|
||||
if (proceed.code() == 200) {
|
||||
return proceed;
|
||||
}
|
||||
throw ((ApiResultException) new Gson().fromJson(readString, ApiResultException.class));
|
||||
}
|
||||
if (!UserRepository.e()) {
|
||||
User user = UserDbHandler.getUser();
|
||||
XLog.a("ResponseBodyInterceptor", "user %s", user);
|
||||
String token = user != null ? user.getToken() : null;
|
||||
if (!TextUtils.isEmpty(token)) {
|
||||
String header = request.headers() != null ? request.header(ApiConstants.AUTHORIZATION) : null;
|
||||
XLog.a("ResponseBodyInterceptor", "[request]header %s \n[response]header %s", request.headers(), proceed.headers());
|
||||
XLog.a("ResponseBodyInterceptor", "last Token %s, cur token %s", header, token);
|
||||
if (token != null && token.equals(header)) {
|
||||
logout();
|
||||
}
|
||||
}
|
||||
}
|
||||
throw ((ApiResultException) new Gson().fromJson(readString, ApiResultException.class));
|
||||
} catch (Exception e) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user