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.UUID;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||||
import org.springframework.http.HttpEntity;
|
import org.springframework.http.HttpEntity;
|
||||||
import org.springframework.http.HttpHeaders;
|
|
||||||
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpMethod;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
@ -28,35 +28,27 @@ public class CordaClient {
|
|||||||
private final RestTemplate restTemplate;
|
private final RestTemplate restTemplate;
|
||||||
private final ObjectMapper jsonMapper;
|
private final ObjectMapper jsonMapper;
|
||||||
|
|
||||||
public CordaClient(RestTemplate restTemplate, ObjectMapper jsonMapper) {
|
public CordaClient(RestTemplateBuilder restTemplateBuilder , ObjectMapper jsonMapper) {
|
||||||
//System.out.println("Creating REST Service");
|
this.restTemplate = restTemplateBuilder.build();
|
||||||
// this.restTemplate = restTemplateBuilder
|
|
||||||
// .basicAuthentication("admin", "admin")
|
|
||||||
// .build();
|
|
||||||
this.restTemplate = restTemplate;
|
|
||||||
this.jsonMapper = jsonMapper;
|
this.jsonMapper = jsonMapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<VirtualNode> getVirtualNodeList() {
|
public List<VirtualNode> getVirtualNodeList() {
|
||||||
|
|
||||||
// Request authorization header
|
|
||||||
HttpHeaders headers = basicAuthorizationHeader();
|
|
||||||
|
|
||||||
// Request
|
|
||||||
final HttpEntity<String> request = new HttpEntity<>(headers);
|
|
||||||
|
|
||||||
ResponseEntity<VirtualNodeList> resp = this.restTemplate.exchange(
|
ResponseEntity<VirtualNodeList> resp = this.restTemplate.exchange(
|
||||||
"https://localhost:8888/api/v1/virtualnode",
|
"/virtualnode",
|
||||||
HttpMethod.GET,
|
HttpMethod.GET,
|
||||||
request,
|
null,
|
||||||
VirtualNodeList.class );
|
VirtualNodeList.class
|
||||||
|
);
|
||||||
|
|
||||||
if (resp.getStatusCode() != HttpStatus.OK) {
|
if (resp.getStatusCode() != HttpStatus.OK) {
|
||||||
throw new RuntimeException("CordaClient.getVirtualNodeList: unexpected request status "+resp.getStatusCode()) ;
|
throw new RuntimeException("CordaClient.getVirtualNodeList: unexpected request status "+resp.getStatusCode()) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
return requireNonNull(resp.getBody(), "CordaClient.getVirtualNodeList: empty getBody()")
|
return requireNonNull(
|
||||||
.virtualNodes();
|
resp.getBody(),
|
||||||
|
"CordaClient.getVirtualNodeList: empty getBody()"
|
||||||
|
).virtualNodes();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -107,12 +99,10 @@ public class CordaClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private ResponseBody cordaFlowPost(HoldingIdentity holdingIdentity, String requestBodyJson) {
|
private ResponseBody cordaFlowPost(HoldingIdentity holdingIdentity, String requestBodyJson) {
|
||||||
final HttpHeaders requestHeaders = basicAuthorizationHeader();
|
final HttpEntity<String> request = new HttpEntity<>(requestBodyJson);
|
||||||
|
|
||||||
final HttpEntity<String> request = new HttpEntity<>(requestBodyJson, requestHeaders);
|
|
||||||
|
|
||||||
final ResponseEntity<ResponseBody> responce = this.restTemplate.exchange(
|
final ResponseEntity<ResponseBody> responce = this.restTemplate.exchange(
|
||||||
"https://localhost:8888/api/v1/flow/" + holdingIdentity.shortHash(),
|
"/flow/" + holdingIdentity.shortHash(),
|
||||||
HttpMethod.POST,
|
HttpMethod.POST,
|
||||||
request,
|
request,
|
||||||
ResponseBody.class
|
ResponseBody.class
|
||||||
@ -138,20 +128,17 @@ public class CordaClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String cordaFlowPoll(ResponseBody startedFlow) throws InterruptedException {
|
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++) {
|
for (int retry = 0; retry < 6; retry++) {
|
||||||
// Give Corda cluster some time to process our request
|
// Give Corda cluster some time to process our request
|
||||||
TimeUnit.SECONDS.sleep(retry*retry +1); // 1 2 5 8 17 33 sec
|
TimeUnit.SECONDS.sleep(retry*retry +1); // 1 2 5 8 17 33 sec
|
||||||
|
|
||||||
final ResponseEntity<ResponseBody> responce = this.restTemplate.exchange(
|
final ResponseEntity<ResponseBody> responce = this.restTemplate.exchange(
|
||||||
"https://localhost:8888/api/v1/flow/"
|
"/flow/"
|
||||||
+ startedFlow.holdingIdentityShortHash()+"/"
|
+ startedFlow.holdingIdentityShortHash()+"/"
|
||||||
+ startedFlow.clientRequestId(),
|
+ startedFlow.clientRequestId(),
|
||||||
HttpMethod.GET,
|
HttpMethod.GET,
|
||||||
request,
|
null,
|
||||||
ResponseBody.class
|
ResponseBody.class
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -171,19 +158,9 @@ public class CordaClient {
|
|||||||
} else
|
} else
|
||||||
if (responseBody.flowError() != null) {
|
if (responseBody.flowError() != null) {
|
||||||
return "Flow execution error: " +responseBody.flowError();
|
return "Flow execution error: " +responseBody.flowError();
|
||||||
} else
|
|
||||||
if (!responseBody.isFlowRunning()) {
|
|
||||||
return "Unexpect ResponseBody status: " +responseBody.flowStatus();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return "CordaClient.cordaFlowPoll: retry limit";
|
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.http.config.RegistryBuilder;
|
||||||
import org.apache.hc.core5.ssl.SSLContexts;
|
import org.apache.hc.core5.ssl.SSLContexts;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
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.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||||
import org.springframework.web.client.RestTemplate;
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
public class RestTemplateConfiguration {
|
public class CordaClientConfiguration {
|
||||||
|
|
||||||
@Value("${trust.store}")
|
@Value("${trust.store}")
|
||||||
private Resource trustStore;
|
private Resource trustStore;
|
||||||
@ -34,17 +34,32 @@ public class RestTemplateConfiguration {
|
|||||||
@Value("${trust.store.password}")
|
@Value("${trust.store.password}")
|
||||||
private String trustStorePassword;
|
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
|
@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())
|
.loadTrustMaterial(trustStore.getURL(), trustStorePassword.toCharArray())
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
|
final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
|
||||||
final Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory> create()
|
sslContext,
|
||||||
|
NoopHostnameVerifier.INSTANCE);
|
||||||
|
|
||||||
|
final Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder
|
||||||
|
.<ConnectionSocketFactory> create()
|
||||||
.register("https", sslsf)
|
.register("https", sslsf)
|
||||||
.register("http", new PlainConnectionSocketFactory())
|
.register("http", new PlainConnectionSocketFactory())
|
||||||
.build();
|
.build();
|
||||||
@ -56,23 +71,15 @@ public class RestTemplateConfiguration {
|
|||||||
// connectionManager.setMaxTotal(100);
|
// connectionManager.setMaxTotal(100);
|
||||||
// connectionManager.setDefaultMaxPerRoute(6);
|
// connectionManager.setDefaultMaxPerRoute(6);
|
||||||
|
|
||||||
final CloseableHttpClient httpClient = HttpClients.custom()
|
final CloseableHttpClient httpClient = HttpClients
|
||||||
|
.custom()
|
||||||
.setConnectionManager(connectionManager)
|
.setConnectionManager(connectionManager)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
final HttpComponentsClientHttpRequestFactory requestFactory =
|
return new RestTemplateBuilder()
|
||||||
new HttpComponentsClientHttpRequestFactory(httpClient);
|
.requestFactory(() -> new HttpComponentsClientHttpRequestFactory(httpClient))
|
||||||
//requestFactory.setReadTimeout(readTimeout);
|
.rootUri(cordaHost+":"+cordaPort+"/api/v1")
|
||||||
//requestFactory.setConnectTimeout(connectTimeout);
|
.basicAuthentication(rootLoging, rootPassw);
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -2,4 +2,9 @@
|
|||||||
trust.store=classpath:keystore/truststore.p12
|
trust.store=classpath:keystore/truststore.p12
|
||||||
trust.store.password=test123
|
trust.store.password=test123
|
||||||
|
|
||||||
|
corda.host=https://localhost
|
||||||
|
corda.port=8888
|
||||||
|
corda.root.login=admin
|
||||||
|
corda.root.passw=admin
|
||||||
|
|
||||||
server.port=8081
|
server.port=8081
|
Loading…
Reference in New Issue
Block a user