Skip to content

Commit 5f35705

Browse files
committed
java spring server & runConfig
1 parent c6bca65 commit 5f35705

21 files changed

+508
-303
lines changed

Basic/cordapp-example/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ buildscript {
1414
log4j_version = constants.getProperty("log4jVersion")
1515
slf4j_version = constants.getProperty("slf4jVersion")
1616
corda_platform_version = constants.getProperty("platformVersion")
17+
//springboot
18+
spring_boot_version = '2.0.2.RELEASE'
19+
spring_boot_gradle_plugin_version = '2.0.2.RELEASE'
1720
}
1821

1922
repositories {
Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
plugins {
2-
id 'org.jetbrains.kotlin.jvm'
3-
id 'io.spring.dependency-management' version '1.0.7.RELEASE'
4-
id 'net.corda.plugins.quasar-utils'
5-
id 'net.corda.plugins.cordformation'
1+
repositories {
2+
mavenLocal()
3+
jcenter()
4+
mavenCentral()
5+
maven { url 'https://jitpack.io' }
6+
maven { url 'https://ci-artifactory.corda.r3cev.com/artifactory/corda-releases' }
67
}
78

8-
ext {
9-
spring_boot_version = '1.5.7.RELEASE'
10-
spring_version = '4.3.11.RELEASE'
11-
}
9+
apply plugin: 'net.corda.plugins.cordformation'
10+
apply plugin: 'net.corda.plugins.quasar-utils'
1211

1312
sourceSets {
1413
main {
@@ -18,16 +17,13 @@ sourceSets {
1817
}
1918
}
2019

21-
// See https://docs.spring.io/dependency-management-plugin/docs/current/reference/html
22-
dependencyManagement {
23-
dependencies {
24-
dependency "org.apache.logging.log4j:log4j-slf4j-impl:$log4j_version"
25-
}
26-
}
20+
2721

2822
dependencies {
29-
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
30-
testCompile "org.jetbrains.kotlin:kotlin-test:$kotlin_version"
23+
// CorDapp dependencies.
24+
compile project(":contracts-java")
25+
compile project(":workflows-java")
26+
3127
testCompile "junit:junit:$junit_version"
3228

3329
// Corda dependencies.
@@ -42,37 +38,26 @@ dependencies {
4238
}
4339
compile "org.springframework.boot:spring-boot-starter-log4j2:$spring_boot_version"
4440
compile "org.apache.logging.log4j:log4j-web:$log4j_version"
45-
46-
cordapp project(":contracts-java")
47-
cordapp project(":workflows-java")
48-
cordapp project(":contracts-kotlin")
49-
cordapp project(":workflows-kotlin")
5041
}
5142

52-
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) {
53-
kotlinOptions {
54-
languageVersion = "1.2"
55-
apiVersion = "1.2"
56-
jvmTarget = "1.8"
57-
javaParameters = true // Useful for reflection.
58-
}
59-
}
43+
44+
6045

6146

6247
task runPartyAServer(type: JavaExec, dependsOn: jar) {
6348
classpath = sourceSets.main.runtimeClasspath
64-
main = 'com.example.server.ServerKt'
49+
main = 'com.example.server.Server'
6550
args '--server.port=50005', '--config.rpc.host=localhost', '--config.rpc.port=10005', '--config.rpc.username=user1', '--config.rpc.password=test'
6651
}
6752

6853
task runPartyBServer(type: JavaExec, dependsOn: jar) {
6954
classpath = sourceSets.main.runtimeClasspath
70-
main = 'com.example.server.ServerKt'
55+
main = 'com.example.server.Server'
7156
args '--server.port=50006', '--config.rpc.host=localhost', '--config.rpc.port=10009', '--config.rpc.username=user1', '--config.rpc.password=test'
7257
}
7358

7459
task runPartyCServer(type: JavaExec, dependsOn: jar) {
7560
classpath = sourceSets.main.runtimeClasspath
76-
main = 'com.example.server.ServerKt'
61+
main = 'com.example.server.Server'
7762
args '--server.port=50007', '--config.rpc.host=localhost', '--config.rpc.port=10013', '--config.rpc.username=user1', '--config.rpc.password=test'
7863
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.example.server;
2+
3+
public interface CONSTANTS {
4+
String CORDA_USER_NAME = "config.rpc.username";
5+
String CORDA_USER_PASSWORD = "config.rpc.password";
6+
String CORDA_NODE_HOST = "config.rpc.host";
7+
String CORDA_RPC_PORT = "config.rpc.port";
8+
}

Basic/cordapp-example/clients/src/main/java/com/example/server/JavaClientRpc.java

Lines changed: 0 additions & 71 deletions
This file was deleted.
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
package com.example.server;
2+
3+
import com.example.flow.ExampleFlow;
4+
import com.example.state.IOUState;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import net.corda.client.jackson.JacksonSupport;
7+
import net.corda.core.contracts.*;
8+
import net.corda.core.identity.CordaX500Name;
9+
import net.corda.core.identity.Party;
10+
import net.corda.core.messaging.CordaRPCOps;
11+
import net.corda.core.node.NodeInfo;
12+
import net.corda.core.transactions.SignedTransaction;
13+
14+
import java.time.LocalDateTime;
15+
import java.time.ZoneId;
16+
import java.util.*;
17+
import org.bouncycastle.asn1.x500.X500Name;
18+
import org.bouncycastle.asn1.x500.style.BCStyle;
19+
import org.slf4j.Logger;
20+
import org.slf4j.LoggerFactory;
21+
import org.springframework.context.annotation.Bean;
22+
import org.springframework.context.annotation.Configuration;
23+
import org.springframework.http.HttpStatus;
24+
import org.springframework.http.ResponseEntity;
25+
import org.springframework.web.bind.annotation.*;
26+
27+
import javax.servlet.http.HttpServletRequest;
28+
import java.util.stream.Collectors;
29+
import java.util.stream.Stream;
30+
31+
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
32+
import static org.springframework.http.MediaType.TEXT_PLAIN_VALUE;
33+
34+
/**
35+
* Define your API endpoints here.
36+
*/
37+
@RestController
38+
@RequestMapping("/api/example/") // The paths for HTTP requests are relative to this base path.
39+
public class MainController {
40+
private static final Logger logger = LoggerFactory.getLogger(RestController.class);
41+
private final CordaRPCOps proxy;
42+
private final CordaX500Name me;
43+
44+
public MainController(NodeRPCConnection rpc) {
45+
this.proxy = rpc.getProxy();
46+
this.me = proxy.nodeInfo().getLegalIdentities().get(0).getName();
47+
48+
}
49+
50+
/** Helpers for filtering the network map cache. */
51+
public String toDisplayString(X500Name name){
52+
return BCStyle.INSTANCE.toString(name);
53+
}
54+
55+
private boolean isNotary(NodeInfo nodeInfo) {
56+
return !proxy.notaryIdentities()
57+
.stream().filter(el -> nodeInfo.isLegalIdentity(el))
58+
.collect(Collectors.toList()).isEmpty();
59+
}
60+
61+
private boolean isMe(NodeInfo nodeInfo){
62+
return nodeInfo.getLegalIdentities().get(0).getName().equals(me);
63+
}
64+
65+
private boolean isNetworkMap(NodeInfo nodeInfo){
66+
return nodeInfo.getLegalIdentities().get(0).getName().getOrganisation().equals("Network Map Service");
67+
}
68+
69+
@Configuration
70+
class Plugin {
71+
@Bean
72+
public ObjectMapper registerModule() {
73+
return JacksonSupport.createNonRpcMapper();
74+
}
75+
}
76+
77+
@GetMapping(value = "/status", produces = TEXT_PLAIN_VALUE)
78+
private String status() {
79+
return "200";
80+
}
81+
82+
@GetMapping(value = "/servertime", produces = TEXT_PLAIN_VALUE)
83+
private String serverTime() {
84+
return (LocalDateTime.ofInstant(proxy.currentNodeTime(), ZoneId.of("UTC"))).toString();
85+
}
86+
87+
@GetMapping(value = "/addresses", produces = TEXT_PLAIN_VALUE)
88+
private String addresses() {
89+
return proxy.nodeInfo().getAddresses().toString();
90+
}
91+
92+
@GetMapping(value = "/identities", produces = TEXT_PLAIN_VALUE)
93+
private String identities() {
94+
return proxy.nodeInfo().getLegalIdentities().toString();
95+
}
96+
97+
@GetMapping(value = "/platformversion", produces = TEXT_PLAIN_VALUE)
98+
private String platformVersion() {
99+
return Integer.toString(proxy.nodeInfo().getPlatformVersion());
100+
}
101+
102+
@GetMapping(value = "/peers", produces = APPLICATION_JSON_VALUE)
103+
public HashMap<String, List<String>> getPeers() {
104+
HashMap<String, List<String>> myMap = new HashMap<>();
105+
106+
// Find all nodes that are not notaries, ourself, or the network map.
107+
Stream<NodeInfo> filteredNodes = proxy.networkMapSnapshot().stream()
108+
.filter(el -> !isNotary(el) && !isMe(el) && !isNetworkMap(el));
109+
// Get their names as strings
110+
List<String> nodeNames = filteredNodes.map(el -> el.getLegalIdentities().get(0).getName().toString())
111+
.collect(Collectors.toList());
112+
113+
myMap.put("peers", nodeNames);
114+
return myMap;
115+
}
116+
117+
@GetMapping(value = "/notaries", produces = TEXT_PLAIN_VALUE)
118+
private String notaries() {
119+
return proxy.notaryIdentities().toString();
120+
}
121+
122+
@GetMapping(value = "/flows", produces = TEXT_PLAIN_VALUE)
123+
private String flows() {
124+
return proxy.registeredFlows().toString();
125+
}
126+
127+
@GetMapping(value = "/states", produces = TEXT_PLAIN_VALUE)
128+
private String states() {
129+
return proxy.vaultQuery(ContractState.class).getStates().toString();
130+
}
131+
132+
@GetMapping(value = "/me",produces = APPLICATION_JSON_VALUE)
133+
private HashMap<String, String> whoami(){
134+
HashMap<String, String> myMap = new HashMap<>();
135+
myMap.put("me", me.toString());
136+
return myMap;
137+
}
138+
@GetMapping(value = "/ious",produces = APPLICATION_JSON_VALUE)
139+
public List<StateAndRef<IOUState>> getIOUs() {
140+
// Filter by state type: IOU.
141+
return proxy.vaultQuery(IOUState.class).getStates();
142+
}
143+
144+
@PostMapping (value = "create-iou" , produces = TEXT_PLAIN_VALUE , headers = "Content-Type=application/x-www-form-urlencoded" )
145+
public ResponseEntity<String> issueIOU(HttpServletRequest request) throws IllegalArgumentException {
146+
147+
int amount = Integer. valueOf(request.getParameter("iouValue"));
148+
String party = request.getParameter("partyName");
149+
// Get party objects for myself and the counterparty.
150+
151+
CordaX500Name partyX500Name = CordaX500Name.parse(party);
152+
Party otherParty = proxy.wellKnownPartyFromX500Name(partyX500Name);
153+
154+
// Create a new IOU state using the parameters given.
155+
try {
156+
// Start the IOUIssueFlow. We block and waits for the flow to return.
157+
SignedTransaction result = proxy.startTrackedFlowDynamic(ExampleFlow.Initiator.class, amount,otherParty).getReturnValue().get();
158+
// Return the response.
159+
return ResponseEntity
160+
.status(HttpStatus.CREATED)
161+
.body("Transaction id "+ result.getId() +" committed to ledger.\n " + result.getTx().getOutput(0));
162+
// For the purposes of this demo app, we do not differentiate by exception type.
163+
} catch (Exception e) {
164+
return ResponseEntity
165+
.status(HttpStatus.BAD_REQUEST)
166+
.body(e.getMessage());
167+
}
168+
}
169+
/**
170+
* Displays all IOU states that only this node has been involved in.
171+
*/
172+
@GetMapping(value = "my-ious",produces = APPLICATION_JSON_VALUE)
173+
public ResponseEntity<List<StateAndRef<IOUState>>> getMyIOUs() {
174+
List<StateAndRef<IOUState>> myious = proxy.vaultQuery(IOUState.class).getStates().stream().filter(
175+
it -> it.getState().getData().getLender().equals(proxy.nodeInfo().getLegalIdentities().get(0))).collect(Collectors.toList());
176+
return ResponseEntity.ok(myious);
177+
}
178+
179+
180+
}

0 commit comments

Comments
 (0)