2121import org .apache .hc .client5 .http .classic .methods .HttpPost ;
2222import org .apache .hc .client5 .http .impl .auth .BasicScheme ;
2323import org .apache .hc .client5 .http .impl .classic .CloseableHttpClient ;
24- import org .apache .hc .client5 .http .impl .classic .CloseableHttpResponse ;
2524import org .apache .hc .client5 .http .impl .classic .HttpClientBuilder ;
2625import org .apache .hc .client5 .http .impl .classic .HttpClients ;
2726import org .apache .hc .client5 .http .impl .io .PoolingHttpClientConnectionManagerBuilder ;
3534import org .apache .hc .core5 .http .HttpHeaders ;
3635import org .apache .hc .core5 .http .HttpHost ;
3736import org .apache .hc .core5 .http .HttpStatus ;
38- import org .apache .hc .core5 .http .ParseException ;
3937import org .apache .hc .core5 .http .io .entity .EntityUtils ;
4038import org .apache .hc .core5 .http .io .entity .StringEntity ;
4139import org .apache .hc .core5 .ssl .SSLContexts ;
@@ -184,17 +182,13 @@ private <T> T read(ClassicHttpRequest request, JavaType type, int successCode)
184182 BasicScheme basicAuth = new BasicScheme ();
185183 basicAuth .initPreemptive (new UsernamePasswordCredentials (userName , password .toCharArray ()));
186184 localContext .resetAuthExchange (target , basicAuth );
187- CloseableHttpResponse response = client ().execute (request , localContext );
188- String json ;
189- try {
190- json = EntityUtils .toString (response .getEntity ());
191- } catch (ParseException e ) {
192- throw new HttpResponseParsingException ("Could not parse response" , e );
193- }
194- if (response .getCode () == successCode ) {
195- return objectMapper .readValue (json , type );
196- }
197- throw new HttpResponseException (response .getCode (), response .getReasonPhrase () + " " + json );
185+ return client ().execute (request , localContext , r -> {
186+ String content = EntityUtils .toString (r .getEntity ());
187+ if (r .getCode () == successCode ) {
188+ return objectMapper .readValue (content , type );
189+ }
190+ throw new HttpResponseException (r .getCode (), r .getReasonPhrase () + " " + content );
191+ });
198192 }
199193
200194 private CloseableHttpClient client () throws KeyManagementException , NoSuchAlgorithmException , KeyStoreException {
@@ -211,15 +205,6 @@ private CloseableHttpClient client() throws KeyManagementException, NoSuchAlgori
211205 return client ;
212206 }
213207
214- private static class HttpResponseParsingException extends IOException {
215-
216- private static final long serialVersionUID = 1L ;
217-
218- public HttpResponseParsingException (String message , Throwable cause ) {
219- super (message , cause );
220- }
221- }
222-
223208 public List <InstalledModule > getModules () throws IOException , GeneralSecurityException {
224209 return get (v1 (MODULES ),
225210 objectMapper .getTypeFactory ().constructCollectionType (List .class , InstalledModule .class ));
@@ -242,11 +227,11 @@ public Database createDatabase(Database database) throws IOException, GeneralSec
242227 .until (() -> executeCommand (uid , new Command ("PING" )).getResponse ().asBoolean ());
243228 return response ;
244229 }
245-
230+
246231 public List <Database > getDatabases () throws IOException , GeneralSecurityException {
247232 return get (v1 (BDBS ), objectMapper .getTypeFactory ().constructCollectionType (List .class , Database .class ));
248233 }
249-
234+
250235 public void deleteAllDatabases () throws IOException , GeneralSecurityException {
251236 getDatabases ().stream ().map (Database ::getUid ).forEach (this ::deleteDatabase );
252237 Awaitility .await ().until (() -> getDatabases ().isEmpty ());
0 commit comments