From 7403cf671adb471f4295efdf064fcf3309c806ab Mon Sep 17 00:00:00 2001 From: djmil Date: Sat, 2 Sep 2023 20:33:46 +0200 Subject: [PATCH] SpringBoot: CordaClientConfiguration via RestTemplateBuilder --- .../cordaclient/CordaClient.java | 55 ++++++------------- ...ion.java => CordaClientConfiguration.java} | 55 +++++++++++-------- .../src/main/resources/application.properties | 5 ++ 3 files changed, 52 insertions(+), 63 deletions(-) rename backend/src/main/java/djmil/cordacheckers/cordaclient/{RestTemplateConfiguration.java => CordaClientConfiguration.java} (64%) diff --git a/backend/src/main/java/djmil/cordacheckers/cordaclient/CordaClient.java b/backend/src/main/java/djmil/cordacheckers/cordaclient/CordaClient.java index 5dd7102..5460d3c 100644 --- a/backend/src/main/java/djmil/cordacheckers/cordaclient/CordaClient.java +++ b/backend/src/main/java/djmil/cordacheckers/cordaclient/CordaClient.java @@ -6,8 +6,8 @@ import java.util.List; import java.util.UUID; import java.util.concurrent.TimeUnit; +import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.http.HttpEntity; -import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -28,35 +28,27 @@ public class CordaClient { private final RestTemplate restTemplate; private final ObjectMapper jsonMapper; - public CordaClient(RestTemplate restTemplate, ObjectMapper jsonMapper) { - //System.out.println("Creating REST Service"); - // this.restTemplate = restTemplateBuilder - // .basicAuthentication("admin", "admin") - // .build(); - this.restTemplate = restTemplate; + public CordaClient(RestTemplateBuilder restTemplateBuilder , ObjectMapper jsonMapper) { + this.restTemplate = restTemplateBuilder.build(); this.jsonMapper = jsonMapper; } public List getVirtualNodeList() { - - // Request authorization header - HttpHeaders headers = basicAuthorizationHeader(); - - // Request - final HttpEntity request = new HttpEntity<>(headers); - ResponseEntity resp = this.restTemplate.exchange( - "https://localhost:8888/api/v1/virtualnode", + "/virtualnode", HttpMethod.GET, - request, - VirtualNodeList.class ); + null, + VirtualNodeList.class + ); if (resp.getStatusCode() != HttpStatus.OK) { throw new RuntimeException("CordaClient.getVirtualNodeList: unexpected request status "+resp.getStatusCode()) ; } - return requireNonNull(resp.getBody(), "CordaClient.getVirtualNodeList: empty getBody()") - .virtualNodes(); + return requireNonNull( + resp.getBody(), + "CordaClient.getVirtualNodeList: empty getBody()" + ).virtualNodes(); } /** @@ -107,12 +99,10 @@ public class CordaClient { } private ResponseBody cordaFlowPost(HoldingIdentity holdingIdentity, String requestBodyJson) { - final HttpHeaders requestHeaders = basicAuthorizationHeader(); - - final HttpEntity request = new HttpEntity<>(requestBodyJson, requestHeaders); + final HttpEntity request = new HttpEntity<>(requestBodyJson); final ResponseEntity responce = this.restTemplate.exchange( - "https://localhost:8888/api/v1/flow/" + holdingIdentity.shortHash(), + "/flow/" + holdingIdentity.shortHash(), HttpMethod.POST, request, ResponseBody.class @@ -138,20 +128,17 @@ public class CordaClient { } private String cordaFlowPoll(ResponseBody startedFlow) throws InterruptedException { - final HttpHeaders requestHeaders = basicAuthorizationHeader(); - - final HttpEntity request = new HttpEntity<>(requestHeaders); for (int retry = 0; retry < 6; retry++) { // Give Corda cluster some time to process our request TimeUnit.SECONDS.sleep(retry*retry +1); // 1 2 5 8 17 33 sec final ResponseEntity responce = this.restTemplate.exchange( - "https://localhost:8888/api/v1/flow/" + "/flow/" + startedFlow.holdingIdentityShortHash()+"/" + startedFlow.clientRequestId(), HttpMethod.GET, - request, + null, ResponseBody.class ); @@ -171,19 +158,9 @@ public class CordaClient { } else if (responseBody.flowError() != null) { return "Flow execution error: " +responseBody.flowError(); - } else - if (!responseBody.isFlowRunning()) { - return "Unexpect ResponseBody status: " +responseBody.flowStatus(); - } + } } return "CordaClient.cordaFlowPoll: retry limit"; } - - private HttpHeaders basicAuthorizationHeader() { - HttpHeaders headers = new HttpHeaders(); - headers.setBasicAuth("admin", "admin"); - - return headers; - } } diff --git a/backend/src/main/java/djmil/cordacheckers/cordaclient/RestTemplateConfiguration.java b/backend/src/main/java/djmil/cordacheckers/cordaclient/CordaClientConfiguration.java similarity index 64% rename from backend/src/main/java/djmil/cordacheckers/cordaclient/RestTemplateConfiguration.java rename to backend/src/main/java/djmil/cordacheckers/cordaclient/CordaClientConfiguration.java index c7858cd..e053f0f 100644 --- a/backend/src/main/java/djmil/cordacheckers/cordaclient/RestTemplateConfiguration.java +++ b/backend/src/main/java/djmil/cordacheckers/cordaclient/CordaClientConfiguration.java @@ -19,14 +19,14 @@ import org.apache.hc.core5.http.config.Registry; import org.apache.hc.core5.http.config.RegistryBuilder; import org.apache.hc.core5.ssl.SSLContexts; import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.Resource; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; -import org.springframework.web.client.RestTemplate; @Configuration -public class RestTemplateConfiguration { +public class CordaClientConfiguration { @Value("${trust.store}") private Resource trustStore; @@ -34,17 +34,32 @@ public class RestTemplateConfiguration { @Value("${trust.store.password}") private String trustStorePassword; - @Bean - RestTemplate restTemplate() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException { - - System.out.println("Setting UP"); + @Value("${corda.host}") + private String cordaHost; - final SSLContext sslContext = SSLContexts.custom() + @Value("${corda.port}") + private String cordaPort; + + @Value("${corda.root.login}") + private String rootLoging; + + @Value("${corda.root.passw}") + private String rootPassw; + + @Bean + RestTemplateBuilder restTemplateBuilder() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException { + + final SSLContext sslContext = SSLContexts + .custom() .loadTrustMaterial(trustStore.getURL(), trustStorePassword.toCharArray()) .build(); - final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE); - final Registry socketFactoryRegistry = RegistryBuilder. create() + final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( + sslContext, + NoopHostnameVerifier.INSTANCE); + + final Registry socketFactoryRegistry = RegistryBuilder + . create() .register("https", sslsf) .register("http", new PlainConnectionSocketFactory()) .build(); @@ -56,23 +71,15 @@ public class RestTemplateConfiguration { // connectionManager.setMaxTotal(100); // connectionManager.setDefaultMaxPerRoute(6); - final CloseableHttpClient httpClient = HttpClients.custom() + final CloseableHttpClient httpClient = HttpClients + .custom() .setConnectionManager(connectionManager) .build(); - final HttpComponentsClientHttpRequestFactory requestFactory = - new HttpComponentsClientHttpRequestFactory(httpClient); - //requestFactory.setReadTimeout(readTimeout); - //requestFactory.setConnectTimeout(connectTimeout); - - return new RestTemplate(requestFactory); - - // String BASE_URI_TEMPLATE = "http://localhost:8080"; - - // DefaultUriBuilderFactory uriBuilderFactory = new DefaultUriBuilderFactory(BASE_URI_TEMPLATE); - - // RestTemplate restTemplate = new RestTemplate(); - // restTemplate.setUriTemplateHandler(uriBuilderFactory); - // restTemplate.getForObject("/test", String.class); // like you may have used earlier + return new RestTemplateBuilder() + .requestFactory(() -> new HttpComponentsClientHttpRequestFactory(httpClient)) + .rootUri(cordaHost+":"+cordaPort+"/api/v1") + .basicAuthentication(rootLoging, rootPassw); } + } diff --git a/backend/src/main/resources/application.properties b/backend/src/main/resources/application.properties index d9b31e9..068a875 100644 --- a/backend/src/main/resources/application.properties +++ b/backend/src/main/resources/application.properties @@ -2,4 +2,9 @@ trust.store=classpath:keystore/truststore.p12 trust.store.password=test123 +corda.host=https://localhost +corda.port=8888 +corda.root.login=admin +corda.root.passw=admin + server.port=8081 \ No newline at end of file