1313import javax .annotation .Nonnull ;
1414import javax .annotation .Nullable ;
1515
16- import org .apache .http .HttpResponse ;
17- import org .apache .http .HttpStatus ;
18- import org .apache .http .StatusLine ;
19- import org .apache .http . client . methods . HttpGet ;
20- import org .apache .http . client . methods . HttpUriRequest ;
16+ import org .apache .hc . client5 . http .classic . methods . HttpGet ;
17+ import org .apache .hc . client5 . http .impl . classic . BasicHttpClientResponseHandler ;
18+ import org .apache .hc . core5 . http .ClassicHttpRequest ;
19+ import org .apache .hc . core5 . http . ClassicHttpResponse ;
20+ import org .apache .hc . core5 . http . HttpStatus ;
2121
2222import com .google .common .base .Strings ;
2323import com .sap .cloud .environment .servicebinding .api .DefaultServiceBindingAccessor ;
3232import io .vavr .control .Try ;
3333import lombok .AccessLevel ;
3434import lombok .Getter ;
35+ import lombok .RequiredArgsConstructor ;
3536import lombok .extern .slf4j .Slf4j ;
3637
3738@ Slf4j
@@ -126,53 +127,62 @@ String getConfigurationAsJson(
126127 serviceDestinationLoader .apply (strategy .behalf ()),
127128 () -> "Destination for Destination Service on behalf of " + strategy .behalf () + " not found." );
128129
129- final HttpUriRequest request = prepareRequest (servicePath , strategy );
130+ final ClassicHttpRequest request = prepareRequest (servicePath , strategy );
130131
131- final HttpResponse response ;
132132 try {
133- response = HttpClientAccessor .getHttpClient (serviceDestination ).execute (request );
133+ return ApacheHttpClient5Accessor
134+ .getHttpClient (serviceDestination )
135+ .execute (request , new DestinationHttpClientResponseHandler (request ));
134136 }
135137 catch ( final IOException e ) {
136138 throw new DestinationAccessException (e );
137139 }
138- return handleResponse (request , response );
139140 }
140141
141- @ Nonnull
142- private static String handleResponse ( final HttpUriRequest request , final HttpResponse response )
142+ @ RequiredArgsConstructor ( access = AccessLevel . PRIVATE )
143+ static class DestinationHttpClientResponseHandler extends BasicHttpClientResponseHandler
143144 {
144- final StatusLine status = response .getStatusLine ();
145- final int statusCode = status .getStatusCode ();
146- final String reasonPhrase = status .getReasonPhrase ();
145+ final ClassicHttpRequest request ;
147146
148- log .debug ("Destination service returned HTTP status {} ({})" , statusCode , reasonPhrase );
147+ @ Override
148+ public String handleResponse ( final ClassicHttpResponse response )
149+ {
150+ final int statusCode = response .getCode ();
151+ final String reasonPhrase = response .getReasonPhrase ();
149152
150- Try <String > maybeBody = Try .of (() -> HttpEntityUtil .getResponseBody (response ));
151- if ( maybeBody .isFailure () ) {
152- final var ex =
153- new DestinationAccessException ("Failed to read body from HTTP response" , maybeBody .getCause ());
154- maybeBody = Try .failure (ex );
155- }
153+ log .debug ("Destination service returned HTTP status {} ({})" , statusCode , reasonPhrase );
156154
157- if ( statusCode == HttpStatus .SC_OK ) {
158- final var ex = new DestinationAccessException ("Failed to get destinations: no body returned in response." );
159- maybeBody = maybeBody .filter (it -> !Strings .isNullOrEmpty (it ), () -> ex );
160- return maybeBody .get ();
161- }
155+ Try <String > maybeBody = Try .of (() -> handleEntity (response .getEntity ()));
156+ if ( maybeBody .isFailure () ) {
157+ final var ex =
158+ new DestinationAccessException ("Failed to read body from HTTP response" , maybeBody .getCause ());
159+ maybeBody = Try .failure (ex );
160+ }
162161
163- final String requestUri = request .getURI ().getPath ();
164- if ( statusCode == HttpStatus .SC_NOT_FOUND ) {
165- throw new DestinationNotFoundException (null , "Destination could not be found for path " + requestUri + "." );
162+ if ( statusCode == HttpStatus .SC_OK ) {
163+ final var ex =
164+ new DestinationAccessException ("Failed to get destinations: no body returned in response." );
165+ maybeBody = maybeBody .filter (it -> !Strings .isNullOrEmpty (it ), () -> ex );
166+ return maybeBody .get ();
167+ }
168+
169+ final String requestUri = request .getPath ();
170+ if ( statusCode == HttpStatus .SC_NOT_FOUND ) {
171+ throw new DestinationNotFoundException (
172+ null ,
173+ "Destination could not be found for path " + requestUri + "." );
174+ }
175+ final String message =
176+ "Failed to get destinations: destination service responded with HTTP status %s (%S) at '%s'."
177+ .formatted (statusCode , reasonPhrase , requestUri );
178+ final String messageWithBody =
179+ message + " Body: %s" .formatted (maybeBody .getOrElseGet (Throwable ::getMessage ));
180+ log .error (messageWithBody );
181+ throw new DestinationAccessException (message );
166182 }
167- final String message =
168- "Failed to get destinations: destination service responded with HTTP status %s (%S) at '%s'."
169- .formatted (statusCode , reasonPhrase , requestUri );
170- final String messageWithBody = message + " Body: %s" .formatted (maybeBody .getOrElseGet (Throwable ::getMessage ));
171- log .error (messageWithBody );
172- throw new DestinationAccessException (message );
173183 }
174184
175- private HttpUriRequest prepareRequest ( final String servicePath , final DestinationRetrievalStrategy strategy )
185+ private ClassicHttpRequest prepareRequest ( final String servicePath , final DestinationRetrievalStrategy strategy )
176186 {
177187 final URI requestUri ;
178188 try {
@@ -183,7 +193,7 @@ private HttpUriRequest prepareRequest( final String servicePath, final Destinati
183193 }
184194
185195 log .debug ("Querying Destination Service via URI {}." , requestUri );
186- final HttpUriRequest request = new HttpGet (requestUri );
196+ final ClassicHttpRequest request = new HttpGet (requestUri );
187197
188198 if ( !servicePath .startsWith (DestinationService .PATH_DEFAULT )
189199 && !servicePath .startsWith (DestinationService .PATH_V2 ) ) {
0 commit comments