20
20
import java .util .HashMap ;
21
21
import java .util .Map ;
22
22
import java .util .Optional ;
23
+ import java .util .concurrent .atomic .AtomicLong ;
23
24
24
25
import static com .getui .gtps .config .GtSDKConstants .CommandPreValue .BySha1 ;
25
26
import static com .getui .gtps .manufacturer .constant .ManufacturerConstants .MANUFACTURER_NAME_OPPO ;
@@ -37,6 +38,8 @@ public class OppoService extends BaseManufacturer {
37
38
38
39
public final static String name = MANUFACTURER_NAME_OPPO ;
39
40
41
+ private final AtomicLong authLock = new AtomicLong (System .currentTimeMillis ());
42
+
40
43
public OppoService (String appId , String appKey , String appSecret , String masterSecret ) {
41
44
super (appId , appKey , appSecret , masterSecret );
42
45
}
@@ -48,10 +51,7 @@ public String getName() {
48
51
49
52
@ Override
50
53
protected void auth () throws AuthFailedException {
51
- if (!this .needAuth ()) {
52
- return ;
53
- }
54
- synchronized (this ) {
54
+ synchronized (authLock ) {
55
55
// 可能并发操作已鉴权
56
56
if (!this .needAuth ()) {
57
57
return ;
@@ -69,16 +69,19 @@ protected void auth() throws AuthFailedException {
69
69
LOGGER .info ("OppoService auth result: {}" , result .toString ());
70
70
if (result .success ()) {
71
71
JsonNode jsonNode = new ObjectMapper ().readTree (result .getContent ());
72
- if (jsonNode .get ("code" ).intValue () == 0 ) {
72
+ if (OppoConstants . ReturnCode . Success . getCode () == jsonNode .get ("code" ).intValue ()) {
73
73
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 ());
75
78
}
76
79
}
77
80
} catch (JsonProcessingException ex ) {
78
81
e = ex ;
79
82
}
80
- } while (needAuth ( ) && i ++ < 2 );
81
- if (needAuth ( )) {
83
+ } while (isEmptyToken ( getAuthTokenFromCache () ) && i ++ < 2 );
84
+ if (isEmptyToken ( getAuthTokenFromCache () )) {
82
85
throw new AuthFailedException (this .getName (), e );
83
86
}
84
87
}
@@ -91,21 +94,44 @@ private static String getSign(String appKey, long timestamp, String masterSecret
91
94
92
95
@ Override
93
96
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 ;
96
98
}
97
99
98
100
@ Override
99
101
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 ;
101
121
}
102
122
103
123
@ Override
104
124
public Result uploadIcon (File file ) throws AuthFailedException {
105
- auth ();
106
125
String cacheKey = BySha1 .equals (CommonConfig .sameFileJudgePattern ) ? FileUtils .sha1 (file ) : file .getName ();
107
126
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
+ });
109
135
}
110
136
111
137
private Result uploadIcon (File file , String cacheKey ) {
@@ -117,10 +143,13 @@ private Result uploadIcon(File file, String cacheKey) {
117
143
if (httpResponse .success ()) {
118
144
try {
119
145
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 ) {
121
148
String url = jsonNode .get ("data" ).get ("small_picture_id" ).textValue ();
122
149
this .cacheMap .put (ICON_URL , CacheServiceFactory .getCacheService (CaffeineCacheService .class ).set (cacheKey , url ));
123
150
return Result .success (url );
151
+ } else if (OppoConstants .ReturnCode .InvalidAuthCode .getCode () == returnCode ) {
152
+ return Result .invalidAuthToken ();
124
153
} else {
125
154
return Result .fail (jsonNode .get ("message" ).textValue ());
126
155
}
@@ -133,10 +162,16 @@ private Result uploadIcon(File file, String cacheKey) {
133
162
134
163
@ Override
135
164
public Result uploadPic (File file ) {
136
- auth ();
137
165
String cacheKey = BySha1 .equals (CommonConfig .sameFileJudgePattern ) ? FileUtils .sha1 (file ) : file .getName ();
138
166
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
+ });
140
175
}
141
176
142
177
private Result uploadPic (File file , String cacheKey ) {
@@ -148,10 +183,13 @@ private Result uploadPic(File file, String cacheKey) {
148
183
if (httpResponse .success ()) {
149
184
try {
150
185
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 ) {
152
188
String url = jsonNode .get ("data" ).get ("big_picture_id" ).textValue ();
153
189
this .cacheMap .put (PIC_URL , CacheServiceFactory .getCacheService (CaffeineCacheService .class ).set (cacheKey , url ));
154
190
return Result .success (url );
191
+ } else if (OppoConstants .ReturnCode .InvalidAuthCode .getCode () == returnCode ) {
192
+ return Result .invalidAuthToken ();
155
193
} else {
156
194
return Result .fail (jsonNode .get ("message" ).textValue ());
157
195
}
0 commit comments