diff --git a/backend/build.gradle b/backend/build.gradle index 08a0fb9..9144c67 100644 --- a/backend/build.gradle +++ b/backend/build.gradle @@ -19,7 +19,7 @@ dependencies { //implementation 'org.springframework.boot:spring-boot-starter-security' implementation 'org.springframework.boot:spring-boot-starter-web' 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 'io.projectreactor:reactor-test' //testImplementation 'org.springframework.security:spring-security-test' diff --git a/backend/src/main/java/djmil/cordacheckers/ApiController.java b/backend/src/main/java/djmil/cordacheckers/ApiController.java index 2bd7d02..ed989e1 100644 --- a/backend/src/main/java/djmil/cordacheckers/ApiController.java +++ b/backend/src/main/java/djmil/cordacheckers/ApiController.java @@ -1,16 +1,29 @@ package djmil.cordacheckers; +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; +import djmil.cordacheckers.cordaclient.CordaClient; +import djmil.cordacheckers.cordaclient.pojo.virtualNodes; + @RestController public class ApiController { + @Autowired + CordaClient cordaclient; + @GetMapping("/api/badjokes") public ResponseEntity badJokes() { - Joke joke = new Joke("What do you call a fly without wings? A walk!"); + + List 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); } - + } \ No newline at end of file diff --git a/backend/src/main/java/djmil/cordacheckers/cordaclient/CordaClient.java b/backend/src/main/java/djmil/cordacheckers/cordaclient/CordaClient.java new file mode 100644 index 0000000..e32574b --- /dev/null +++ b/backend/src/main/java/djmil/cordacheckers/cordaclient/CordaClient.java @@ -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 getVirtualnode() { + + // Request authorization header + HttpHeaders headers = new HttpHeaders(); + headers.setBasicAuth("admin", "admin"); + + // Request + final HttpEntity request = new HttpEntity<>(headers); + + ResponseEntity 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(); + } +} diff --git a/backend/src/main/java/djmil/cordacheckers/cordaclient/RestTemplateConfiguration.java b/backend/src/main/java/djmil/cordacheckers/cordaclient/RestTemplateConfiguration.java new file mode 100644 index 0000000..c7858cd --- /dev/null +++ b/backend/src/main/java/djmil/cordacheckers/cordaclient/RestTemplateConfiguration.java @@ -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 socketFactoryRegistry = RegistryBuilder. 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 + } +} diff --git a/backend/src/main/java/djmil/cordacheckers/pojo/holdingIdentity.java b/backend/src/main/java/djmil/cordacheckers/cordaclient/pojo/holdingIdentity.java similarity index 79% rename from backend/src/main/java/djmil/cordacheckers/pojo/holdingIdentity.java rename to backend/src/main/java/djmil/cordacheckers/cordaclient/pojo/holdingIdentity.java index 1a35303..3ecdce1 100644 --- a/backend/src/main/java/djmil/cordacheckers/pojo/holdingIdentity.java +++ b/backend/src/main/java/djmil/cordacheckers/cordaclient/pojo/holdingIdentity.java @@ -1,4 +1,4 @@ -package djmil.cordacheckers.pojo; +package djmil.cordacheckers.cordaclient.pojo; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; diff --git a/backend/src/main/java/djmil/cordacheckers/pojo/virtualNodes.java b/backend/src/main/java/djmil/cordacheckers/cordaclient/pojo/virtualNodes.java similarity index 78% rename from backend/src/main/java/djmil/cordacheckers/pojo/virtualNodes.java rename to backend/src/main/java/djmil/cordacheckers/cordaclient/pojo/virtualNodes.java index 207227a..39f9368 100644 --- a/backend/src/main/java/djmil/cordacheckers/pojo/virtualNodes.java +++ b/backend/src/main/java/djmil/cordacheckers/cordaclient/pojo/virtualNodes.java @@ -1,4 +1,4 @@ -package djmil.cordacheckers.pojo; +package djmil.cordacheckers.cordaclient.pojo; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; diff --git a/backend/src/main/java/djmil/cordacheckers/pojo/virtualnode.java b/backend/src/main/java/djmil/cordacheckers/cordaclient/pojo/virtualnode.java similarity index 80% rename from backend/src/main/java/djmil/cordacheckers/pojo/virtualnode.java rename to backend/src/main/java/djmil/cordacheckers/cordaclient/pojo/virtualnode.java index 9662886..6b56cb7 100644 --- a/backend/src/main/java/djmil/cordacheckers/pojo/virtualnode.java +++ b/backend/src/main/java/djmil/cordacheckers/cordaclient/pojo/virtualnode.java @@ -1,4 +1,4 @@ -package djmil.cordacheckers.pojo; +package djmil.cordacheckers.cordaclient.pojo; import java.util.List; diff --git a/backend/src/main/resources/application.properties b/backend/src/main/resources/application.properties index 585b43b..d9b31e9 100644 --- a/backend/src/main/resources/application.properties +++ b/backend/src/main/resources/application.properties @@ -1,3 +1,5 @@ trust.store=classpath:keystore/truststore.p12 -trust.store.password=test123 \ No newline at end of file +trust.store.password=test123 + +server.port=8081 \ No newline at end of file diff --git a/backend/src/test/java/djmil/cordacheckers/CordacheckersApplicationTests.java b/backend/src/test/java/djmil/cordacheckers/CordacheckersApplicationTests.java index ff59269..6a46fa7 100644 --- a/backend/src/test/java/djmil/cordacheckers/CordacheckersApplicationTests.java +++ b/backend/src/test/java/djmil/cordacheckers/CordacheckersApplicationTests.java @@ -27,8 +27,8 @@ import org.springframework.http.ResponseEntity; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; -import djmil.cordacheckers.pojo.holdingIdentity; -import djmil.cordacheckers.pojo.virtualnode; +import djmil.cordacheckers.cordaclient.pojo.holdingIdentity; +import djmil.cordacheckers.cordaclient.pojo.virtualnode; import static org.assertj.core.api.Assertions.assertThat; @@ -87,8 +87,10 @@ class CordacheckersApplicationTests { assertThat(response.hasBody()); virtualnode vNode = response.getBody(); - assertThat(vNode != null); - assertThat(vNode.virtualNodes().size() == 5); + assertThat(vNode).isNotNull(); + if (vNode != null) { + assertThat(vNode.virtualNodes().size() == 5); + } holdingIdentity identity = vNode.virtualNodes().get(0).holdingIdentity(); assertThat(identity.x500Name().contains("NotaryRep1"));