Changeset 2211


Ignore:
Timestamp:
Apr 5, 2012, 6:23:14 PM (11 years ago)
Author:
work@…
Message:
  • cleaned up api controller, moved re-used logic (as far as possible) into service to perform validation checks and takes a closure as an argument:

def executeApiCall(params,response,itemName,item,block) { ... }

Location:
trunk/grails-app
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/grails-app/controllers/api/ApiController.groovy

    r2210 r2211  
    155155        println "api::getSubjectsForStudy: ${params}"
    156156
    157         String deviceID     = (params.containsKey('deviceID')) ? params.deviceID : ''
    158         String validation   = (params.containsKey('validation')) ? params.validation : ''
     157        // fetch study
    159158        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,{
    173163            def subjects = apiService.flattenDomainData( study.subjects )
    174164
     
    188178                render result as JSON
    189179            }
    190         }
     180        })
    191181    }
    192182
     
    201191        println "api::getAssaysForStudy: ${params}"
    202192
    203         String deviceID     = (params.containsKey('deviceID')) ? params.deviceID : ''
    204         String validation   = (params.containsKey('validation')) ? params.validation : ''
     193        // fetch study
    205194        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,{
    219199            def assays = apiService.flattenDomainData( study.assays )
    220200
     
    234214                render result as JSON
    235215            }
    236         }
     216        })
    237217    }
    238218
     
    247227        println "api::getSamplesForAssay: ${params}"
    248228
    249         String deviceID     = (params.containsKey('deviceID')) ? params.deviceID : ''
    250         String validation   = (params.containsKey('validation')) ? params.validation : ''
     229        // fetch assay
    251230        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,{
    265235            def samples = apiService.flattenDomainData( assay.samples )
    266236
     
    280250                render result as JSON
    281251            }
    282         }
     252        })
    283253    }
    284254
     
    293263        println "api:getMeasurementDataForAssay: ${params}"
    294264
     265        // fetch assay
     266        String assayToken   = (params.containsKey('assayToken')) ? params.assayToken : ''
     267        def assay           = Assay.findByAssayUUID(assayToken)
     268
     269        // fetch user based on deviceID
    295270        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,{
    311275            // define sample measurement data matrix
    312276            def matrix = [:]
    313             def measurementData     = apiService.getMeasurementData(assay, user)
    314             def measurementMetaData = apiService.getMeasurementData(assay, user)
     277            def measurementData = apiService.getMeasurementData(assay, user)
     278            //def measurementMetaData = apiService.getMeasurementData(assay, user)
    315279
    316280            // iterate through measurementData and build data matrix
     
    339303                response.sendError(500, "module '${assay.module}' does not properly implement getMeasurementData REST specification (${e.getMessage()})")
    340304            }
    341         }
     305        })
    342306    }
    343307
     
    348312        println "api:debugModuleDataForAssay: ${params}"
    349313
     314        // fetch assay
     315        String assayToken   = (params.containsKey('assayToken')) ? params.assayToken : ''
     316        def assay           = Assay.findByAssayUUID(assayToken)
     317
     318        // fetch user based on deviceID
    350319        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,{
    366324            def serviceURL = "${assay.module.url}/rest/getMeasurementData"
    367325            def serviceArguments = "assayToken=${assay.assayUUID}&verbose=false"
     
    393351                render result as JSON
    394352            }
    395         }
     353        })
    396354    }
    397355}
  • trunk/grails-app/services/api/ApiService.groovy

    r2210 r2211  
    2020import grails.converters.JSON
    2121import org.dbnp.gdt.TemplateEntity
    22 
    23 class ApiService implements Serializable {
     22import org.springframework.context.ApplicationContextAware
     23import org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib
     24import org.springframework.context.ApplicationContext
     25
     26class ApiService implements Serializable, ApplicationContextAware {
    2427    // inject the module communication service
    2528    def moduleCommunicationService
     
    5255    ]
    5356
     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
    5465    /**
    5566     * validate a client request by checking the validation checksum
     
    145156
    146157    /**
     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    /**
    147196     * get the measurement tokens from the remote module
    148197     *
Note: See TracChangeset for help on using the changeset viewer.