44
55package com .sap .cloud .sdk .cloudplatform .connectivity ;
66
7- import static com .sap .cloud .security .xsuaa .client .OAuth2TokenServiceConstants .*;
7+ import static com .sap .cloud .security .xsuaa .client .OAuth2TokenServiceConstants .ACCESS_TOKEN ;
8+ import static com .sap .cloud .security .xsuaa .client .OAuth2TokenServiceConstants .ASSERTION ;
9+ import static com .sap .cloud .security .xsuaa .client .OAuth2TokenServiceConstants .CLIENT_SECRET ;
10+ import static com .sap .cloud .security .xsuaa .client .OAuth2TokenServiceConstants .EXPIRES_IN ;
11+ import static com .sap .cloud .security .xsuaa .client .OAuth2TokenServiceConstants .PASSWORD ;
12+ import static com .sap .cloud .security .xsuaa .client .OAuth2TokenServiceConstants .REFRESH_TOKEN ;
13+ import static com .sap .cloud .security .xsuaa .client .OAuth2TokenServiceConstants .TOKEN_TYPE ;
814
915import java .io .IOException ;
1016import java .net .URI ;
@@ -110,19 +116,14 @@ private String executeRequest(
110116 throws OAuth2ServiceException
111117 {
112118 final HttpPost httpPost = createHttpPost (tokenUri , createRequestHeaders (headers ), parameters );
113- log
114- .debug (
115- "Requesting access token from url {} with headers {} and {} retries left" ,
116- tokenUri ,
117- headers ,
118- attemptsLeft );
119+ log .debug ("Requesting access token with {} retries left" , attemptsLeft );
119120 try {
120121 return httpClient .execute (httpPost , response -> {
121122 final int statusCode = response .getCode ();
122123 final String body = EntityUtils .toString (response .getEntity (), StandardCharsets .UTF_8 );
123- log .debug ("Received statusCode {} from {}" , statusCode , tokenUri );
124+ log .debug ("Received statusCode {} from host {}" , statusCode , tokenUri . getHost () );
124125 if ( HttpStatus .SC_OK == statusCode ) {
125- log .debug ("Successfully retrieved access token from {} with params {}." , tokenUri , parameters );
126+ log .debug ("Successfully retrieved access token from host {}." , tokenUri . getHost () );
126127 return body ;
127128 } else if ( attemptsLeft > 0 && config .getRetryStatusCodes ().contains (statusCode ) ) {
128129 log .warn ("Request failed with status {} but is retryable. Retrying..." , statusCode );
@@ -143,12 +144,15 @@ private String executeRequest(
143144 if ( e instanceof final OAuth2ServiceException oAuth2Exception ) {
144145 throw oAuth2Exception ;
145146 } else {
146- throw OAuth2ServiceException
147- .builder ("Error requesting access token!" )
148- .withUri (tokenUri )
149- .withRequestHeaders (getHeadersAsStringArray (httpPost .getHeaders ()))
150- .withResponseBody (e .getMessage ())
151- .build ();
147+ final var exception =
148+ OAuth2ServiceException
149+ .builder ("Error requesting access token!" )
150+ .withUri (tokenUri )
151+ .withRequestHeaders (getHeadersAsStringArray (httpPost .getHeaders ()))
152+ .withResponseBody (e .getMessage ())
153+ .build ();
154+ exception .initCause (e );
155+ throw exception ;
152156 }
153157 }
154158 }
@@ -189,7 +193,9 @@ private HttpPost createHttpPost( final URI uri, final HttpHeaders headers, final
189193 httpPost .addHeader (org .apache .hc .core5 .http .HttpHeaders .USER_AGENT , HttpClientUtil .getUserAgent ());
190194 }
191195 catch ( final Exception e ) {
192- throw new OAuth2ServiceException ("Unexpected error parsing URI: " + e .getMessage ());
196+ final var exception = new OAuth2ServiceException ("Unexpected error parsing URI: " + e .getMessage ());
197+ exception .initCause (e );
198+ throw exception ;
193199 }
194200 logRequest (headers , parameters );
195201 return httpPost ;
@@ -213,8 +219,11 @@ private Long convertExpiresInToLong( final String expiresIn )
213219 return Long .parseLong (expiresIn );
214220 }
215221 catch ( final NumberFormatException e ) {
216- throw new OAuth2ServiceException (
217- String .format ("Cannot convert expires_in from response (%s) to long" , expiresIn ));
222+ final var exception =
223+ new OAuth2ServiceException (
224+ String .format ("Cannot convert expires_in from response (%s) to long" , expiresIn ));
225+ exception .initCause (e );
226+ throw exception ;
218227 }
219228 }
220229
@@ -310,8 +319,11 @@ static CloseableHttpClient createHttpClient( @Nullable final ClientIdentity clie
310319 return createHttpClientWithKeyStore (identityKeyStore );
311320 }
312321 catch ( final Exception e ) {
313- throw new HttpClientException (
314- "Failed to create HTTPS HttpClient5 with certificate authentication: " + e .getMessage ());
322+ final var exception =
323+ new HttpClientException (
324+ "Failed to create HTTPS HttpClient5 with certificate authentication: " + e .getMessage ());
325+ exception .initCause (e );
326+ throw exception ;
315327 }
316328 }
317329
@@ -335,7 +347,10 @@ private static CloseableHttpClient createHttpClientWithKeyStore( @Nonnull final
335347 return HttpClientBuilder .create ().useSystemProperties ().setConnectionManager (connectionManager ).build ();
336348 }
337349 catch ( final Exception e ) {
338- throw new HttpClientException ("Failed to create HTTPS HttpClient5 with KeyStore: " + e .getMessage ());
350+ final var exception =
351+ new HttpClientException ("Failed to create HTTPS HttpClient5 with KeyStore: " + e .getMessage ());
352+ exception .initCause (e );
353+ throw exception ;
339354 }
340355 }
341356}
0 commit comments