SpringBoot: CordaClient service

This commit is contained in:
djmil 2023-08-19 19:57:01 +02:00
parent a8985e6a2b
commit 55d086bbe2
9 changed files with 156 additions and 11 deletions

View File

@ -19,7 +19,7 @@ dependencies {
//implementation 'org.springframework.boot:spring-boot-starter-security' //implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-webflux' implementation 'org.springframework.boot:spring-boot-starter-webflux'
testImplementation 'org.apache.httpcomponents.client5:httpclient5:5.2.1' implementation 'org.apache.httpcomponents.client5:httpclient5:5.2.1'
testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.projectreactor:reactor-test' testImplementation 'io.projectreactor:reactor-test'
//testImplementation 'org.springframework.security:spring-security-test' //testImplementation 'org.springframework.security:spring-security-test'

View File

@ -1,16 +1,29 @@
package djmil.cordacheckers; package djmil.cordacheckers;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import djmil.cordacheckers.cordaclient.CordaClient;
import djmil.cordacheckers.cordaclient.pojo.virtualNodes;
@RestController @RestController
public class ApiController { public class ApiController {
@Autowired
CordaClient cordaclient;
@GetMapping("/api/badjokes") @GetMapping("/api/badjokes")
public ResponseEntity<Joke> badJokes() { public ResponseEntity<Joke> badJokes() {
Joke joke = new Joke("What do you call a fly without wings? A walk!");
List<virtualNodes> vNodes = cordaclient.getVirtualnode();
Joke joke = new Joke("What do you call a fly without wings? A walk! " + vNodes.get(1).holdingIdentity().x500Name());
return ResponseEntity.ok(joke); return ResponseEntity.ok(joke);
} }
} }

View File

@ -0,0 +1,50 @@
package djmil.cordacheckers.cordaclient;
import java.util.List;
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;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import djmil.cordacheckers.cordaclient.pojo.virtualNodes;
import djmil.cordacheckers.cordaclient.pojo.virtualnode;
@Service
public class CordaClient {
private final RestTemplate restTemplate;
public CordaClient(RestTemplate restTemplate) {
//System.out.println("Creating REST Service");
// this.restTemplate = restTemplateBuilder
// .basicAuthentication("admin", "admin")
// .build();
this.restTemplate = restTemplate;
}
public List<virtualNodes> getVirtualnode() {
// Request authorization header
HttpHeaders headers = new HttpHeaders();
headers.setBasicAuth("admin", "admin");
// Request
final HttpEntity<String> request = new HttpEntity<>(headers);
ResponseEntity<virtualnode> resp = this.restTemplate.exchange(
"https://localhost:8888/api/v1/virtualnode",
HttpMethod.GET,
request,
virtualnode.class );
// TODO: throw exeption instead
if (resp.getStatusCode() != HttpStatus.OK || !resp.hasBody()) {
return null;
}
return resp.getBody().virtualNodes();
}
}

View File

@ -0,0 +1,78 @@
package djmil.cordacheckers.cordaclient;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import javax.net.ssl.SSLContext;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.apache.hc.client5.http.socket.ConnectionSocketFactory;
import org.apache.hc.client5.http.socket.PlainConnectionSocketFactory;
import org.apache.hc.client5.http.ssl.NoopHostnameVerifier;
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
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.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 {
@Value("${trust.store}")
private Resource trustStore;
@Value("${trust.store.password}")
private String trustStorePassword;
@Bean
RestTemplate restTemplate() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException {
System.out.println("Setting UP");
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()
.register("https", sslsf)
.register("http", new PlainConnectionSocketFactory())
.build();
//final BasicHttpClientConnectionManager connectionManager =
// new BasicHttpClientConnectionManager(socketFactoryRegistry);
final PoolingHttpClientConnectionManager connectionManager =
new PoolingHttpClientConnectionManager(socketFactoryRegistry);
// connectionManager.setMaxTotal(100);
// connectionManager.setDefaultMaxPerRoute(6);
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
}
}

View File

@ -1,4 +1,4 @@
package djmil.cordacheckers.pojo; package djmil.cordacheckers.cordaclient.pojo;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

View File

@ -1,4 +1,4 @@
package djmil.cordacheckers.pojo; package djmil.cordacheckers.cordaclient.pojo;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

View File

@ -1,4 +1,4 @@
package djmil.cordacheckers.pojo; package djmil.cordacheckers.cordaclient.pojo;
import java.util.List; import java.util.List;

View File

@ -1,3 +1,5 @@
trust.store=classpath:keystore/truststore.p12 trust.store=classpath:keystore/truststore.p12
trust.store.password=test123 trust.store.password=test123
server.port=8081

View File

@ -27,8 +27,8 @@ import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import djmil.cordacheckers.pojo.holdingIdentity; import djmil.cordacheckers.cordaclient.pojo.holdingIdentity;
import djmil.cordacheckers.pojo.virtualnode; import djmil.cordacheckers.cordaclient.pojo.virtualnode;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -87,8 +87,10 @@ class CordacheckersApplicationTests {
assertThat(response.hasBody()); assertThat(response.hasBody());
virtualnode vNode = response.getBody(); virtualnode vNode = response.getBody();
assertThat(vNode != null); assertThat(vNode).isNotNull();
assertThat(vNode.virtualNodes().size() == 5); if (vNode != null) {
assertThat(vNode.virtualNodes().size() == 5);
}
holdingIdentity identity = vNode.virtualNodes().get(0).holdingIdentity(); holdingIdentity identity = vNode.virtualNodes().get(0).holdingIdentity();
assertThat(identity.x500Name().contains("NotaryRep1")); assertThat(identity.x500Name().contains("NotaryRep1"));