@ Override public Token retrieveToken ( ) { log . debug ( "DefaultTokenProvider retrieveToken()" ) ; try { SSLContext sslContext ; /* * If SSL cert checking for endpoints has been explicitly disabled, * register a new scheme for HTTPS that won't cause self-signed * certs to error out. */ if ( SDKGlobalConfiguration . isCertCheckingDisabled ( ) ) { if ( log . isWarnEnabled ( ) ) { log . warn ( "SSL Certificate checking for endpoints has been " + "explicitly disabled." ) ; } sslContext = SSLContext . getInstance ( "TLS" ) ; sslContext . init ( null , new TrustManager [ ] { new TrustingX509TrustManager ( ) } , null ) ; } else { sslContext = SSLContexts . createDefault ( ) ; } SSLConnectionSocketFactory sslsf = new SdkTLSSocketFactory ( sslContext , new DefaultHostnameVerifier ( ) ) ; HttpClientBuilder builder = HttpClientBuilder . create ( ) ; if ( httpClientSettings != null ) { DefaultTokenManager . addProxyConfig ( builder , httpClientSettings ) ; } HttpClient client = builder . setSSLSocketFactory ( sslsf ) . build ( ) ; HttpPost post = new HttpPost ( iamEndpoint ) ; post . setHeader ( "Authorization" , BASIC_AUTH ) ; post . setHeader ( "Content-Type" , CONTENT_TYPE ) ; post . setHeader ( "Accept" , ACCEPT ) ; List < NameValuePair > urlParameters = new ArrayList < NameValuePair > ( ) ; urlParameters . add ( new BasicNameValuePair ( "grant_type" , GRANT_TYPE ) ) ; urlParameters . add ( new BasicNameValuePair ( "response_type" , RESPONSE_TYPE ) ) ; urlParameters . add ( new BasicNameValuePair ( "apikey" , apiKey ) ) ; post . setEntity ( new UrlEncodedFormEntity ( urlParameters ) ) ; final HttpResponse response = client . execute ( post ) ; if ( response . getStatusLine ( ) . getStatusCode ( ) != 200 ) { log . info ( "Response code= " + response . getStatusLine ( ) . getStatusCode ( ) + ", Reason= " + response . getStatusLine ( ) . getReasonPhrase ( ) + ".Throwing OAuthServiceException" ) ; OAuthServiceException exception = new OAuthServiceException ( "Token retrieval from IAM service failed" ) ; exception . setStatusCode ( response . getStatusLine ( ) . getStatusCode ( ) ) ; exception . setStatusMessage ( response . getStatusLine ( ) . getReasonPhrase ( ) ) ; throw exception ; } final HttpEntity entity = response . getEntity ( ) ; final String resultStr = EntityUtils . toString ( entity ) ; final ObjectMapper mapper = new ObjectMapper ( ) ; final Token token = mapper . readValue ( resultStr , Token . class ) ; return token ; } catch ( UnsupportedEncodingException e ) { e . printStackTrace ( ) ; } catch ( ClientProtocolException e ) { e . printStackTrace ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } catch ( NoSuchAlgorithmException e ) { e . printStackTrace ( ) ; } catch ( KeyManagementException e ) { e . printStackTrace ( ) ; } return null ; }
Retrieve the token using the Apache httpclient in a synchronous manner