4
4
5
5
import androidx .annotation .VisibleForTesting ;
6
6
7
- import com .iterable .iterableapi .util .Future ;
8
-
7
+ import org .json .JSONException ;
9
8
import org .json .JSONObject ;
10
9
11
10
import java .io .UnsupportedEncodingException ;
12
11
import java .util .Timer ;
13
12
import java .util .TimerTask ;
14
- import java .util .concurrent .Callable ;
13
+ import java .util .concurrent .ExecutorService ;
14
+ import java .util .concurrent .Executors ;
15
15
16
16
public class IterableAuthManager {
17
17
private static final String TAG = "IterableAuth" ;
@@ -20,55 +20,53 @@ public class IterableAuthManager {
20
20
private final IterableApi api ;
21
21
private final IterableAuthHandler authHandler ;
22
22
private final long expiringAuthTokenRefreshPeriod ;
23
-
23
+ private final long scheduledRefreshPeriod = 10000 ;
24
24
@ VisibleForTesting
25
25
Timer timer ;
26
26
private boolean hasFailedPriorAuth ;
27
27
private boolean pendingAuth ;
28
28
private boolean requiresAuthRefresh ;
29
29
30
+ private final ExecutorService executor = Executors .newSingleThreadExecutor ();
31
+
30
32
IterableAuthManager (IterableApi api , IterableAuthHandler authHandler , long expiringAuthTokenRefreshPeriod ) {
31
33
this .api = api ;
32
34
this .authHandler = authHandler ;
33
35
this .expiringAuthTokenRefreshPeriod = expiringAuthTokenRefreshPeriod ;
34
36
}
35
37
36
38
public synchronized void requestNewAuthToken (boolean hasFailedPriorAuth ) {
39
+ requestNewAuthToken (hasFailedPriorAuth , null );
40
+ }
41
+
42
+ private void handleSuccessForAuthToken (String authToken , IterableHelper .SuccessHandler successCallback ) {
43
+ try {
44
+ JSONObject object = new JSONObject ();
45
+ object .put ("newAuthToken" , authToken );
46
+ successCallback .onSuccess (object );
47
+ } catch (JSONException e ) {
48
+ e .printStackTrace ();
49
+ }
50
+ }
51
+
52
+ public synchronized void requestNewAuthToken (
53
+ boolean hasFailedPriorAuth ,
54
+ final IterableHelper .SuccessHandler successCallback ) {
37
55
if (authHandler != null ) {
38
56
if (!pendingAuth ) {
39
57
if (!(this .hasFailedPriorAuth && hasFailedPriorAuth )) {
40
58
this .hasFailedPriorAuth = hasFailedPriorAuth ;
41
59
pendingAuth = true ;
42
- Future .runAsync (new Callable <String >() {
43
- @ Override
44
- public String call () throws Exception {
45
- return authHandler .onAuthTokenRequested ();
46
- }
47
- }).onSuccess (new Future .SuccessCallback <String >() {
60
+
61
+ executor .submit (new Runnable () {
48
62
@ Override
49
- public void onSuccess (String authToken ) {
50
- if (authToken != null ) {
51
- queueExpirationRefresh (authToken );
52
- } else {
53
- IterableLogger .w (TAG , "Auth token received as null. Calling the handler in 10 seconds" );
54
- //TODO: Make this time configurable and in sync with SDK initialization flow for auth null scenario
55
- scheduleAuthTokenRefresh (10000 );
56
- authHandler .onTokenRegistrationFailed (new Throwable ("Auth token null" ));
57
- return ;
63
+ public void run () {
64
+ try {
65
+ final String authToken = authHandler .onAuthTokenRequested ();
66
+ handleAuthTokenSuccess (authToken , successCallback );
67
+ } catch (final Exception e ) {
68
+ handleAuthTokenFailure (e );
58
69
}
59
- IterableApi .getInstance ().setAuthToken (authToken );
60
- pendingAuth = false ;
61
- reSyncAuth ();
62
- authHandler .onTokenRegistrationSuccessful (authToken );
63
- }
64
- })
65
- .onFailure (new Future .FailureCallback () {
66
- @ Override
67
- public void onFailure (Throwable throwable ) {
68
- IterableLogger .e (TAG , "Error while requesting Auth Token" , throwable );
69
- authHandler .onTokenRegistrationFailed (throwable );
70
- pendingAuth = false ;
71
- reSyncAuth ();
72
70
}
73
71
});
74
72
}
@@ -82,6 +80,32 @@ public void onFailure(Throwable throwable) {
82
80
}
83
81
}
84
82
83
+ private void handleAuthTokenSuccess (String authToken , IterableHelper .SuccessHandler successCallback ) {
84
+ if (authToken != null ) {
85
+ if (successCallback != null ) {
86
+ handleSuccessForAuthToken (authToken , successCallback );
87
+ }
88
+ queueExpirationRefresh (authToken );
89
+ } else {
90
+ IterableLogger .w (TAG , "Auth token received as null. Calling the handler in 10 seconds" );
91
+ //TODO: Make this time configurable and in sync with SDK initialization flow for auth null scenario
92
+ scheduleAuthTokenRefresh (scheduledRefreshPeriod );
93
+ authHandler .onTokenRegistrationFailed (new Throwable ("Auth token null" ));
94
+ return ;
95
+ }
96
+ IterableApi .getInstance ().setAuthToken (authToken );
97
+ pendingAuth = false ;
98
+ reSyncAuth ();
99
+ authHandler .onTokenRegistrationSuccessful (authToken );
100
+ }
101
+
102
+ private void handleAuthTokenFailure (Throwable throwable ) {
103
+ IterableLogger .e (TAG , "Error while requesting Auth Token" , throwable );
104
+ authHandler .onTokenRegistrationFailed (throwable );
105
+ pendingAuth = false ;
106
+ reSyncAuth ();
107
+ }
108
+
85
109
public void queueExpirationRefresh (String encodedJWT ) {
86
110
clearRefreshTimer ();
87
111
try {
@@ -96,7 +120,7 @@ public void queueExpirationRefresh(String encodedJWT) {
96
120
IterableLogger .e (TAG , "Error while parsing JWT for the expiration" , e );
97
121
authHandler .onTokenRegistrationFailed (new Throwable ("Auth token decode failure. Scheduling auth token refresh in 10 seconds..." ));
98
122
//TODO: Sync with configured time duration once feature is available.
99
- scheduleAuthTokenRefresh (10000 );
123
+ scheduleAuthTokenRefresh (scheduledRefreshPeriod );
100
124
}
101
125
}
102
126
0 commit comments