Skip to content

Commit 69502e8

Browse files
authored
Merge pull request #3 from xelloss00x/main
[feature] 1.0.1.1 优化OPPO获取AuthToken的机制
2 parents d9b32d3 + d64c247 commit 69502e8

File tree

6 files changed

+82
-21
lines changed

6 files changed

+82
-21
lines changed

CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
* OPPO、XM icon和图片上传接口实现
66

7-
[1.0.1.0] - 2021-01-04
7+
[1.0.1.0] - 2021-01-26
88

99
* 优化了配置文件读取方式,支持读取jar包内配置文件
10-
* 提供厂商接口单线程执行的配置
10+
* 提供厂商接口单线程执行的配置
11+
12+
[1.0.1.1] - 2021-01-27
13+
14+
* 优化OPPO获取AuthToken的机制

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.getui.push</groupId>
88
<artifactId>getui-3rd-push-utils</artifactId>
9-
<version>1.0.1.0</version>
9+
<version>1.0.1.1</version>
1010
<packaging>jar</packaging>
1111

1212
<name>${project.groupId}:${project.artifactId}</name>

src/main/java/com/getui/gtps/manufacturer/Result.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ public class Result implements Serializable {
1818
public final static int timeout = 2;
1919
public final static int noInstance = 3;
2020
public final static int authFail = 4;
21+
public final static int invalidAuthToken = 5;
2122
public final static String SUCCESS = "success";
2223
public final static String FAIL = "fail";
2324
public final static String TIMEOUT = "timeout";
2425
public final static String NO_INSTANCE = "has no manufacturer instance";
2526
public final static String AUTH_FAIL = "auth fail";
27+
public final static String InvalidAuthToken = "Invalid Auth Token";
2628

2729
private final int code;
2830
private final String message;
@@ -70,6 +72,9 @@ public static Result authFail() {
7072
return new Result(authFail, AUTH_FAIL, null);
7173
}
7274

75+
public static Result invalidAuthToken() {
76+
return new Result(invalidAuthToken, InvalidAuthToken, null);
77+
}
7378

7479
@Override
7580
public String toString() {

src/main/java/com/getui/gtps/manufacturer/oppo/OppoConstants.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,19 @@ String getPath() {
2828
return this.path;
2929
}
3030
}
31+
32+
enum ReturnCode {
33+
Success(0),
34+
InvalidAuthCode(11);
35+
36+
private final int code;
37+
38+
ReturnCode(int code) {
39+
this.code = code;
40+
}
41+
42+
int getCode() {
43+
return this.code;
44+
}
45+
}
3146
}

src/main/java/com/getui/gtps/manufacturer/oppo/OppoService.java

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.HashMap;
2121
import java.util.Map;
2222
import java.util.Optional;
23+
import java.util.concurrent.atomic.AtomicLong;
2324

2425
import static com.getui.gtps.config.GtSDKConstants.CommandPreValue.BySha1;
2526
import static com.getui.gtps.manufacturer.constant.ManufacturerConstants.MANUFACTURER_NAME_OPPO;
@@ -37,6 +38,8 @@ public class OppoService extends BaseManufacturer {
3738

3839
public final static String name = MANUFACTURER_NAME_OPPO;
3940

41+
private final AtomicLong authLock = new AtomicLong(System.currentTimeMillis());
42+
4043
public OppoService(String appId, String appKey, String appSecret, String masterSecret) {
4144
super(appId, appKey, appSecret, masterSecret);
4245
}
@@ -48,10 +51,7 @@ public String getName() {
4851

4952
@Override
5053
protected void auth() throws AuthFailedException {
51-
if (!this.needAuth()) {
52-
return;
53-
}
54-
synchronized (this) {
54+
synchronized (authLock) {
5555
// 可能并发操作已鉴权
5656
if (!this.needAuth()) {
5757
return;
@@ -69,16 +69,19 @@ protected void auth() throws AuthFailedException {
6969
LOGGER.info("OppoService auth result: {}", result.toString());
7070
if (result.success()) {
7171
JsonNode jsonNode = new ObjectMapper().readTree(result.getContent());
72-
if (jsonNode.get("code").intValue() == 0) {
72+
if (OppoConstants.ReturnCode.Success.getCode() == jsonNode.get("code").intValue()) {
7373
String authToken = jsonNode.get("data").get("auth_token").textValue();
74-
this.cacheMap.put(AUTH_TOKEN, CacheServiceFactory.getCacheService(CaffeineCacheService.class).set(name, authToken, 24 * 3600));
74+
// OPPO说明:鉴权令牌有效期是24小时,在有效期内多次申请均返回相同的令牌,令牌超过有效期后申请将返回新令牌。令牌过期失效后有10分钟过渡期,过渡期间新旧两个auth_token均可使用,超过过渡期后只有新令牌可使用。
75+
// TTL说明:为了防止24小时临界点调用接口时,op可能返回旧的token被我们换成24小时,这里+5分钟进行缓存
76+
this.cacheMap.put(AUTH_TOKEN, CacheServiceFactory.getCacheService(CaffeineCacheService.class).set(name, authToken, 24 * 3600 + 5 * 60));
77+
authLock.compareAndSet(authLock.get(), System.currentTimeMillis());
7578
}
7679
}
7780
} catch (JsonProcessingException ex) {
7881
e = ex;
7982
}
80-
} while (needAuth() && i++ < 2);
81-
if (needAuth()) {
83+
} while (isEmptyToken(getAuthTokenFromCache()) && i++ < 2);
84+
if (isEmptyToken(getAuthTokenFromCache())) {
8285
throw new AuthFailedException(this.getName(), e);
8386
}
8487
}
@@ -91,21 +94,44 @@ private static String getSign(String appKey, long timestamp, String masterSecret
9194

9295
@Override
9396
protected boolean needAuth() {
94-
Object cache = this.cacheMap.get(AUTH_TOKEN);
95-
return cache == null || CacheServiceFactory.getCacheService(CaffeineCacheService.class).get(cache, name) == null;
97+
return isEmptyToken(getAuthTokenFromCache()) || System.currentTimeMillis() - authLock.get() >= 1000;
9698
}
9799

98100
@Override
99101
protected String getAuthToken() {
100-
return (String) CacheServiceFactory.getCacheService(CaffeineCacheService.class).get(this.cacheMap.get(AUTH_TOKEN), name);
102+
String token = getAuthTokenFromCache();
103+
if (isEmptyToken(token)) {
104+
auth();
105+
token = getAuthTokenFromCache();
106+
}
107+
return token;
108+
}
109+
110+
private String getAuthTokenFromCache() {
111+
Object cache = this.cacheMap.get(AUTH_TOKEN);
112+
String token = null;
113+
if (cache != null) {
114+
token = (String) CacheServiceFactory.getCacheService(CaffeineCacheService.class).get(cache, name);
115+
}
116+
return token;
117+
}
118+
119+
private boolean isEmptyToken(String token) {
120+
return token == null || token.length() == 0;
101121
}
102122

103123
@Override
104124
public Result uploadIcon(File file) throws AuthFailedException {
105-
auth();
106125
String cacheKey = BySha1.equals(CommonConfig.sameFileJudgePattern) ? FileUtils.sha1(file) : file.getName();
107126
Optional<Result> cacheResult = getCacheResult(ICON_URL, cacheKey);
108-
return cacheResult.orElseGet(() -> uploadIcon(file, cacheKey));
127+
return cacheResult.orElseGet(() -> {
128+
Result result = uploadIcon(file, cacheKey);
129+
if (Result.invalidAuthToken().getCode() == result.getCode()) {
130+
auth();
131+
result = uploadIcon(file, cacheKey);
132+
}
133+
return result;
134+
});
109135
}
110136

111137
private Result uploadIcon(File file, String cacheKey) {
@@ -117,10 +143,13 @@ private Result uploadIcon(File file, String cacheKey) {
117143
if (httpResponse.success()) {
118144
try {
119145
JsonNode jsonNode = new ObjectMapper().readTree(httpResponse.getContent());
120-
if (0 == jsonNode.get("code").intValue()) {
146+
int returnCode = jsonNode.get("code").intValue();
147+
if (OppoConstants.ReturnCode.Success.getCode() == returnCode) {
121148
String url = jsonNode.get("data").get("small_picture_id").textValue();
122149
this.cacheMap.put(ICON_URL, CacheServiceFactory.getCacheService(CaffeineCacheService.class).set(cacheKey, url));
123150
return Result.success(url);
151+
} else if (OppoConstants.ReturnCode.InvalidAuthCode.getCode() == returnCode) {
152+
return Result.invalidAuthToken();
124153
} else {
125154
return Result.fail(jsonNode.get("message").textValue());
126155
}
@@ -133,10 +162,16 @@ private Result uploadIcon(File file, String cacheKey) {
133162

134163
@Override
135164
public Result uploadPic(File file) {
136-
auth();
137165
String cacheKey = BySha1.equals(CommonConfig.sameFileJudgePattern) ? FileUtils.sha1(file) : file.getName();
138166
Optional<Result> cacheResult = getCacheResult(PIC_URL, cacheKey);
139-
return cacheResult.orElseGet(() -> uploadPic(file, cacheKey));
167+
return cacheResult.orElseGet(() -> {
168+
Result result = uploadPic(file, cacheKey);
169+
if (Result.invalidAuthToken().getCode() == result.getCode()) {
170+
auth();
171+
result = uploadPic(file, cacheKey);
172+
}
173+
return result;
174+
});
140175
}
141176

142177
private Result uploadPic(File file, String cacheKey) {
@@ -148,10 +183,13 @@ private Result uploadPic(File file, String cacheKey) {
148183
if (httpResponse.success()) {
149184
try {
150185
JsonNode jsonNode = new ObjectMapper().readTree(httpResponse.getContent());
151-
if (0 == jsonNode.get("code").intValue()) {
186+
int returnCode = jsonNode.get("code").intValue();
187+
if (OppoConstants.ReturnCode.Success.getCode() == returnCode) {
152188
String url = jsonNode.get("data").get("big_picture_id").textValue();
153189
this.cacheMap.put(PIC_URL, CacheServiceFactory.getCacheService(CaffeineCacheService.class).set(cacheKey, url));
154190
return Result.success(url);
191+
} else if (OppoConstants.ReturnCode.InvalidAuthCode.getCode() == returnCode) {
192+
return Result.invalidAuthToken();
155193
} else {
156194
return Result.fail(jsonNode.get("message").textValue());
157195
}

src/test/com/getui/gtps/TestUtils.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import static org.junit.Assert.*;
1818

1919
/**
20-
* @author zhourh@getui.com
2120
* Date: 2021-01-06
2221
*/
2322
public class TestUtils {

0 commit comments

Comments
 (0)