Changeset 2211
- Timestamp:
- Apr 5, 2012, 6:23:14 PM (11 years ago)
- Location:
- trunk/grails-app
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/controllers/api/ApiController.groovy
r2210 r2211 155 155 println "api::getSubjectsForStudy: ${params}" 156 156 157 String deviceID = (params.containsKey('deviceID')) ? params.deviceID : '' 158 String validation = (params.containsKey('validation')) ? params.validation : '' 157 // fetch study 159 158 String studyToken = (params.containsKey('studyToken')) ? params.studyToken : '' 160 161 // fetch user and study 162 def user = Token.findByDeviceID(deviceID)?.user 163 def study = Study.findByStudyUUID(studyToken) 164 165 // check 166 if (!apiService.validateRequest(deviceID,validation)) { 167 response.sendError(401, 'Unauthorized') 168 } else if (!study) { 169 response.sendError(400, 'No such study') 170 } else if (!study.canRead(user)) { 171 response.sendError(401, 'Unauthorized') 172 } else { 159 def study = Study.findByStudyUUID(studyToken) 160 161 // wrap result in api call validator 162 apiService.executeApiCall(params,response,'study',study,{ 173 163 def subjects = apiService.flattenDomainData( study.subjects ) 174 164 … … 188 178 render result as JSON 189 179 } 190 } 180 }) 191 181 } 192 182 … … 201 191 println "api::getAssaysForStudy: ${params}" 202 192 203 String deviceID = (params.containsKey('deviceID')) ? params.deviceID : '' 204 String validation = (params.containsKey('validation')) ? params.validation : '' 193 // fetch study 205 194 String studyToken = (params.containsKey('studyToken')) ? params.studyToken : '' 206 207 // fetch user and study 208 def user = Token.findByDeviceID(deviceID)?.user 209 def study = Study.findByStudyUUID(studyToken) 210 211 // check 212 if (!apiService.validateRequest(deviceID,validation)) { 213 response.sendError(401, 'Unauthorized') 214 } else if (!study) { 215 response.sendError(400, 'No such study') 216 } else if (!study.canRead(user)) { 217 response.sendError(401, 'Unauthorized') 218 } else { 195 def study = Study.findByStudyUUID(studyToken) 196 197 // wrap result in api call validator 198 apiService.executeApiCall(params,response,'study',study,{ 219 199 def assays = apiService.flattenDomainData( study.assays ) 220 200 … … 234 214 render result as JSON 235 215 } 236 } 216 }) 237 217 } 238 218 … … 247 227 println "api::getSamplesForAssay: ${params}" 248 228 249 String deviceID = (params.containsKey('deviceID')) ? params.deviceID : '' 250 String validation = (params.containsKey('validation')) ? params.validation : '' 229 // fetch assay 251 230 String assayToken = (params.containsKey('assayToken')) ? params.assayToken : '' 252 253 // fetch user and study 254 def user = Token.findByDeviceID(deviceID)?.user 255 def assay = Assay.findByAssayUUID(assayToken) 256 257 // check 258 if (!apiService.validateRequest(deviceID,validation)) { 259 response.sendError(401, 'Unauthorized') 260 } else if (!assay) { 261 response.sendError(400, 'No such assay') 262 } else if (!assay.parent.canRead(user)) { 263 response.sendError(401, 'Unauthorized') 264 } else { 231 def assay = Assay.findByAssayUUID(assayToken) 232 233 // wrap result in api call validator 234 apiService.executeApiCall(params,response,'assay',assay,{ 265 235 def samples = apiService.flattenDomainData( assay.samples ) 266 236 … … 280 250 render result as JSON 281 251 } 282 } 252 }) 283 253 } 284 254 … … 293 263 println "api:getMeasurementDataForAssay: ${params}" 294 264 265 // fetch assay 266 String assayToken = (params.containsKey('assayToken')) ? params.assayToken : '' 267 def assay = Assay.findByAssayUUID(assayToken) 268 269 // fetch user based on deviceID 295 270 String deviceID = (params.containsKey('deviceID')) ? params.deviceID : '' 296 String validation = (params.containsKey('validation')) ? params.validation : '' 297 String assayToken = (params.containsKey('assayToken')) ? params.assayToken : '' 298 299 // fetch user and study 300 def user = Token.findByDeviceID(deviceID)?.user 301 def assay = Assay.findByAssayUUID(assayToken) 302 303 // check 304 if (!apiService.validateRequest(deviceID,validation)) { 305 response.sendError(401, 'Unauthorized') 306 } else if (!assay) { 307 response.sendError(400, 'No such assay') 308 } else if (!assay.parent.canRead(user)) { 309 response.sendError(401, 'Unauthorized') 310 } else { 271 def user = Token.findByDeviceID(deviceID)?.user 272 273 // wrap result in api call validator 274 apiService.executeApiCall(params,response,'assay',assay,{ 311 275 // define sample measurement data matrix 312 276 def matrix = [:] 313 def measurementData 314 def measurementMetaData = apiService.getMeasurementData(assay, user)277 def measurementData = apiService.getMeasurementData(assay, user) 278 //def measurementMetaData = apiService.getMeasurementData(assay, user) 315 279 316 280 // iterate through measurementData and build data matrix … … 339 303 response.sendError(500, "module '${assay.module}' does not properly implement getMeasurementData REST specification (${e.getMessage()})") 340 304 } 341 } 305 }) 342 306 } 343 307 … … 348 312 println "api:debugModuleDataForAssay: ${params}" 349 313 314 // fetch assay 315 String assayToken = (params.containsKey('assayToken')) ? params.assayToken : '' 316 def assay = Assay.findByAssayUUID(assayToken) 317 318 // fetch user based on deviceID 350 319 String deviceID = (params.containsKey('deviceID')) ? params.deviceID : '' 351 String validation = (params.containsKey('validation')) ? params.validation : '' 352 String assayToken = (params.containsKey('assayToken')) ? params.assayToken : '' 353 354 // fetch user and study 355 def user = Token.findByDeviceID(deviceID)?.user 356 def assay = Assay.findByAssayUUID(assayToken) 357 358 // check 359 if (!apiService.validateRequest(deviceID,validation)) { 360 response.sendError(401, 'Unauthorized') 361 } else if (!assay) { 362 response.sendError(400, 'No such assay') 363 } else if (!assay.parent.canRead(user)) { 364 response.sendError(401, 'Unauthorized') 365 } else { 320 def user = Token.findByDeviceID(deviceID)?.user 321 322 // wrap result in api call validator 323 apiService.executeApiCall(params,response,'assay',assay,{ 366 324 def serviceURL = "${assay.module.url}/rest/getMeasurementData" 367 325 def serviceArguments = "assayToken=${assay.assayUUID}&verbose=false" … … 393 351 render result as JSON 394 352 } 395 } 353 }) 396 354 } 397 355 } -
trunk/grails-app/services/api/ApiService.groovy
r2210 r2211 20 20 import grails.converters.JSON 21 21 import org.dbnp.gdt.TemplateEntity 22 23 class ApiService implements Serializable { 22 import org.springframework.context.ApplicationContextAware 23 import org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib 24 import org.springframework.context.ApplicationContext 25 26 class ApiService implements Serializable, ApplicationContextAware { 24 27 // inject the module communication service 25 28 def moduleCommunicationService … … 52 55 ] 53 56 57 private ApplicationTagLib g 58 59 void setApplicationContext(ApplicationContext applicationContext) { 60 g = applicationContext.getBean(ApplicationTagLib) 61 62 // now you have a reference to g that you can call render() on 63 } 64 54 65 /** 55 66 * validate a client request by checking the validation checksum … … 145 156 146 157 /** 158 * wrapper for performing api calls 159 * 160 * validates if the user may call this api 161 * 162 * @param params 163 * @param response 164 * @param itemName 165 * @param item 166 * @param block 167 */ 168 def executeApiCall(params,response,itemName,item,block) { 169 // get variables from parameters 170 String deviceID = (params.containsKey('deviceID')) ? params.deviceID : '' 171 String validation = (params.containsKey('validation')) ? params.validation : '' 172 173 // fetch user based on deviceID 174 def user = Token.findByDeviceID(deviceID)?.user 175 176 // check if api call may be performed 177 if (!validateRequest(deviceID,validation)) { 178 // validation md5sum does not match predicted hash 179 response.sendError(401, "Unauthorized") 180 } else if (!item) { 181 // no results 182 response.sendError(400, "No such ${itemName}") 183 } else if (item.respondsTo('canRead') && !item.canRead(user)) { 184 // the user cannot read this data 185 response.sendError(401, "Unauthorized") 186 } else if (item.hasProperty('parent') && item.parent.respondsTo('canRead') && !item.parent.canRead(user)) { 187 // the user cannot read this data 188 response.sendError(401, "Unauthorized") 189 } else { 190 // allowed api call, execute block / closure 191 block() 192 } 193 } 194 195 /** 147 196 * get the measurement tokens from the remote module 148 197 *
Note: See TracChangeset
for help on using the changeset viewer.