19
19
import com .ibm .cloud .sdk .core .http .ResponseConverter ;
20
20
import com .ibm .cloud .sdk .core .util .CredentialUtils ;
21
21
import com .ibm .cloud .sdk .core .util .ResponseConverterUtils ;
22
-
23
22
import okhttp3 .Call ;
24
23
import okhttp3 .FormBody ;
25
24
import okhttp3 .Request ;
26
25
27
26
import java .io .IOException ;
27
+ import java .util .logging .Logger ;
28
28
29
29
/**
30
30
* Retrieves, stores, and refreshes IAM tokens.
@@ -35,6 +35,8 @@ public class IamTokenManager {
35
35
private String url ;
36
36
private IamToken tokenData ;
37
37
38
+ private static final Logger LOG = Logger .getLogger (IamTokenManager .class .getName ());
39
+ private static final String ERROR_MESSAGE = "Error getting IAM token from API" ;
38
40
private static final String DEFAULT_AUTHORIZATION = "Basic Yng6Yng=" ;
39
41
private static final String DEFAULT_IAM_URL = "https://iam.bluemix.net/identity/token" ;
40
42
private static final String GRANT_TYPE = "grant_type" ;
@@ -181,14 +183,32 @@ private boolean isRefreshTokenExpired() {
181
183
* @return object containing requested IAM token information
182
184
*/
183
185
private IamToken callIamApi (Request request ) {
184
- Call call = HttpClientSingleton .getInstance ().createHttpClient ().newCall (request );
185
- ResponseConverter <IamToken > converter = ResponseConverterUtils .getObject (IamToken .class );
186
+ final IamToken [] returnToken = new IamToken [1 ];
187
+
188
+ Thread iamApiCall = new Thread (new Runnable () {
189
+ @ Override
190
+ public void run () {
191
+ Call call = HttpClientSingleton .getInstance ().createHttpClient ().newCall (request );
192
+ ResponseConverter <IamToken > converter = ResponseConverterUtils .getObject (IamToken .class );
193
+
194
+ try {
195
+ okhttp3 .Response response = call .execute ();
196
+ returnToken [0 ] = converter .convert (response );
197
+ } catch (IOException e ) {
198
+ LOG .severe (ERROR_MESSAGE );
199
+ e .printStackTrace ();
200
+ throw new RuntimeException (e );
201
+ }
202
+ }
203
+ });
186
204
205
+ iamApiCall .start ();
187
206
try {
188
- okhttp3 . Response response = call . execute ();
189
- return converter . convert ( response );
190
- } catch ( IOException e ) {
191
- throw new RuntimeException ( e );
207
+ iamApiCall . join ();
208
+ } catch ( InterruptedException e ) {
209
+ LOG . severe ( ERROR_MESSAGE );
210
+ e . printStackTrace ( );
192
211
}
212
+ return returnToken [0 ];
193
213
}
194
214
}
0 commit comments