SpringBoot: CordaClientConfiguration
via RestTemplateBuilder
This commit is contained in:
parent
a0c91ee9ce
commit
7403cf671a
@ -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<VirtualNode> getVirtualNodeList() {
|
||||
|
||||
// Request authorization header
|
||||
HttpHeaders headers = basicAuthorizationHeader();
|
||||
|
||||
// Request
|
||||
final HttpEntity<String> request = new HttpEntity<>(headers);
|
||||
|
||||
ResponseEntity<VirtualNodeList> 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<String> request = new HttpEntity<>(requestBodyJson, requestHeaders);
|
||||
final HttpEntity<String> request = new HttpEntity<>(requestBodyJson);
|
||||
|
||||
final ResponseEntity<ResponseBody> 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<String> 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<ResponseBody> 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;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
||||
@Value("${corda.host}")
|
||||
private String cordaHost;
|
||||
|
||||
@Value("${corda.port}")
|
||||
private String cordaPort;
|
||||
|
||||
@Value("${corda.root.login}")
|
||||
private String rootLoging;
|
||||
|
||||
@Value("${corda.root.passw}")
|
||||
private String rootPassw;
|
||||
|
||||
@Bean
|
||||
RestTemplate restTemplate() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException {
|
||||
RestTemplateBuilder restTemplateBuilder() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException {
|
||||
|
||||
System.out.println("Setting UP");
|
||||
|
||||
final SSLContext sslContext = SSLContexts.custom()
|
||||
final SSLContext sslContext = SSLContexts
|
||||
.custom()
|
||||
.loadTrustMaterial(trustStore.getURL(), trustStorePassword.toCharArray())
|
||||
.build();
|
||||
|
||||
final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
|
||||
final Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory> create()
|
||||
final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
|
||||
sslContext,
|
||||
NoopHostnameVerifier.INSTANCE);
|
||||
|
||||
final Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder
|
||||
.<ConnectionSocketFactory> 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);
|
||||
}
|
||||
|
||||
}
|
@ -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
|
Loading…
Reference in New Issue
Block a user