1 | /** |
---|
2 | * ApiService Service |
---|
3 | * |
---|
4 | * Description of my service |
---|
5 | * |
---|
6 | * @author your email (+name?) |
---|
7 | * @since 2010mmdd |
---|
8 | * @package ??? |
---|
9 | * |
---|
10 | * Revision information: |
---|
11 | * $Rev: 1430 $ |
---|
12 | * $Author: work@osx.eu $ |
---|
13 | * $Date: 2011-01-21 21:05:36 +0100 (Fri, 21 Jan 2011) $ |
---|
14 | */ |
---|
15 | package api |
---|
16 | |
---|
17 | import java.security.MessageDigest |
---|
18 | import dbnp.studycapturing.Assay |
---|
19 | import dbnp.authentication.SecUser |
---|
20 | |
---|
21 | class ApiService implements Serializable { |
---|
22 | // the shared secret used to validate api calls |
---|
23 | static final String API_SECRET = "th!s_sH0uld^Pr0bab7y_m0v3_t%_th3_uSeR_d0Ma!n_ins7ead!" |
---|
24 | static transactional = false |
---|
25 | def moduleCommunicationService |
---|
26 | |
---|
27 | /** |
---|
28 | * validate a client request by checking the validation checksum |
---|
29 | * @param deviceID |
---|
30 | * @param validation |
---|
31 | * @return |
---|
32 | */ |
---|
33 | def validateRequest(String deviceID, String validation) { |
---|
34 | return true |
---|
35 | |
---|
36 | // get token for this device ID |
---|
37 | Token token = Token.findByDeviceID(deviceID) |
---|
38 | |
---|
39 | // increase sequence |
---|
40 | if (token) { |
---|
41 | token.sequence = token.sequence+1 |
---|
42 | token.save() |
---|
43 | |
---|
44 | // generate the validation checksum |
---|
45 | MessageDigest digest = MessageDigest.getInstance("MD5") |
---|
46 | String validationSum = new BigInteger(1,digest.digest("${token.deviceToken}${token.sequence}${API_SECRET}".getBytes())).toString(16).padLeft(32,"0") |
---|
47 | |
---|
48 | // check if the validation confirms |
---|
49 | return (validation == validationSum) |
---|
50 | } else { |
---|
51 | // no such token, re-authenticate |
---|
52 | return false |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | /** |
---|
57 | * flatten domain data to relevant data to return in an api |
---|
58 | * call and not to expose domain internals |
---|
59 | * |
---|
60 | * @param elements |
---|
61 | * @return |
---|
62 | */ |
---|
63 | def flattenDomainData(List elements) { |
---|
64 | def items = [] |
---|
65 | |
---|
66 | // iterate through elements |
---|
67 | elements.each { |
---|
68 | def fields = it.giveFields() |
---|
69 | def item = [:] |
---|
70 | |
---|
71 | // add token |
---|
72 | if (it.respondsTo('getToken')) { |
---|
73 | item['token'] = it.getToken() |
---|
74 | } else { |
---|
75 | item['id'] = it.id |
---|
76 | } |
---|
77 | |
---|
78 | // add subject field values |
---|
79 | fields.each { field -> |
---|
80 | def value = it.getFieldValue( field.name ) |
---|
81 | |
---|
82 | if (value.hasProperty('name')) { |
---|
83 | item[ field.name ] = value.name |
---|
84 | } else { |
---|
85 | item[ field.name ] = value |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | items[ items.size() ] = item |
---|
90 | } |
---|
91 | |
---|
92 | return items |
---|
93 | } |
---|
94 | |
---|
95 | def getMeasurements(Assay assay, SecUser user) { |
---|
96 | def serviceURL = "${assay.module.url}/rest/getMeasurements" |
---|
97 | def serviceArguments = "assayToken=${assay.assayUUID}" |
---|
98 | |
---|
99 | // call module method |
---|
100 | def json = moduleCommunicationService.callModuleMethod( |
---|
101 | assay.module.url, |
---|
102 | serviceURL, |
---|
103 | serviceArguments, |
---|
104 | "POST", |
---|
105 | user |
---|
106 | ); |
---|
107 | |
---|
108 | return json |
---|
109 | } |
---|
110 | |
---|
111 | def getMeasurementData(Assay assay, SecUser user) { |
---|
112 | def serviceURL = "${assay.module.url}/rest/getMeasurementData" |
---|
113 | def serviceArguments = "assayToken=${assay.assayUUID}&verbose=true" |
---|
114 | |
---|
115 | // call module method |
---|
116 | def json = moduleCommunicationService.callModuleMethod( |
---|
117 | assay.module.url, |
---|
118 | serviceURL, |
---|
119 | serviceArguments, |
---|
120 | "POST", |
---|
121 | user |
---|
122 | ); |
---|
123 | |
---|
124 | return json |
---|
125 | } |
---|
126 | |
---|
127 | def getMeasurementMetaData(Assay assay, SecUser user) { |
---|
128 | def serviceURL = "${assay.module.url}/rest/getMeasurementMetaData" |
---|
129 | def serviceArguments = "assayToken=${assay.assayUUID}" |
---|
130 | |
---|
131 | // call module method |
---|
132 | def json = moduleCommunicationService.callModuleMethod( |
---|
133 | assay.module.url, |
---|
134 | serviceURL, |
---|
135 | serviceArguments, |
---|
136 | "POST", |
---|
137 | user |
---|
138 | ); |
---|
139 | |
---|
140 | return json |
---|
141 | } |
---|
142 | } |
---|