-
Notifications
You must be signed in to change notification settings - Fork 0
/
PaymentAddressServer.groovy
108 lines (83 loc) · 3.61 KB
/
PaymentAddressServer.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
* Created by whoisjeremylam on 12/05/14.
*
* Thanks to https://bitbucket.org/jsumners/restlet-2.1-demo for a sane example using Restlet
*/
import groovy.json.JsonSlurper
import org.restlet.Component
import org.restlet.data.Protocol
import org.restlet.data.Status
import org.restlet.resource.Put
import org.restlet.resource.ServerResource
import org.restlet.Application
import org.restlet.Restlet
import org.restlet.routing.Router
import org.json.JSONException
import org.json.JSONObject
import org.restlet.ext.json.JsonRepresentation
@Grab(group='log4j', module='log4j', version='1.2.17')
@Grab(group='org.restlet.jse', module='org.restlet', version = '2.2.0')
@Grab(group='org.restlet.jse', module='org.restlet.ext.json', version = '2.2.0')
@Grab(group='org.restlet.jse', module='org.restlet.lib.org.json', version = '2.0') //org.restlet.jse:org.restlet.lib.org.json:2.0
class PaymentAddressServer {
public static class PaymentAddressServerResource extends ServerResource {
@Override
public void doInit() {
}
@Put
public String put(String value) {
def GeneratePaymentAddress addressGenerator = new GeneratePaymentAddress()
def JsonRepresentation jsonRepresentation
def JSONObject jsonObject = new JSONObject()
try {
def slurper = new JsonSlurper()
def result = slurper.parseText(value)
def counterpartyAddress = result.counterpartyAddress
def nativeRefundAddress = result.nativeRefundAddress
if (nativeRefundAddress == null || counterpartyAddress == null) {
jsonObject.put("counterpartyPaymentAddress", '')
jsonObject.put("nativePaymentAddress", '')
jsonObject.put("returnCode", -1)
}
else {
def generatedAddresses = addressGenerator.generateAddress("${counterpartyAddress}", "${nativeRefundAddress}", "udf1", "udf2", "udf3", "udf4", "udf5")
jsonObject.put("nativePaymentAddress", generatedAddresses[0])
jsonObject.put("counterpartyPaymentAddress", generatedAddresses[1])
jsonObject.put("returnCode", 0)
}
jsonRepresentation = new JsonRepresentation(jsonObject)
} catch(JSONException jsonEx) {
System.out.println(jsonEx.toString())
}
response = this.getResponse()
response.setStatus(Status.SUCCESS_CREATED)
response.setEntity(jsonRepresentation)
}
}
public static class PaymentAddressServerApplication extends Application {
/**
* Creates a root Restlet that will receive all incoming calls.
*/
@Override
public synchronized Restlet createInboundRoot() {
// Create a router Restlet that routes each call to a new instance of HelloWorldResource.
Router router = new Router(getContext())
router.attach("/address", PaymentAddressServerResource.class)
return router
}
}
static init() {
}
public static void main(String[] args) throws Exception {
def serverApp = new PaymentAddressServerApplication()
init()
// Create a new Component.
Component component = new Component()
// Add a new HTTP server listening on port 8182.
component.getServers().add(Protocol.HTTP, 8182)
// Attach the sample application.
component.getDefaultHost().attach("/vennd", serverApp)
// Start the component.
component.start()
}
}