root/trunk/grails-app/services/dbnp/modules/ModuleCommunicationService.groovy @ 1455

Revision 1455, 3.1 KB (checked in by s.h.sikkema@…, 3 years ago)

Using moduleCommunicationService now to fetch measurement data from module

  • Property svn:keywords set to Rev Author Date
Line 
1/**
2 * SynchronizationService Service
3 *
4 * Description of my service
5 *
6 * @author  your email (+name?)
7 * @since       2010mmdd
8 * @package     ???
9 *
10 * Revision information:
11 * $Rev$
12 * $Author$
13 * $Date$
14 */
15package dbnp.modules
16
17import dbnp.studycapturing.*
18import grails.converters.*
19import javax.servlet.http.HttpServletResponse
20
21class ModuleCommunicationService implements Serializable {
22    boolean transactional = false
23        def authenticationService
24        def moduleNotificationService
25       
26    /**
27     * Sends a notification to assay modules that some part of a study has changed.
28     *
29     * Only modules that have the notify flag set to true will be notified. They will be notified on the URL
30     *
31     * [moduleUrl]/rest/notifyStudyChange?studyToken=abc
32     *
33     * Errors that occur when calling this URL are ignored. The module itself is responsible of
34     * maintaining a synchronized state.
35     *
36     * @param study
37     * @return
38     */
39        def invalidateStudy( Study study ) {
40                moduleNotificationService.invalidateStudy( study );
41    }
42       
43        /**
44         * Checks whether a specific method on a module is reachable and returns a SC_OK response code.
45         *
46         * This method will return false if a method returns an error (including 403 and 401 errors) or
47         * a redirect
48         *
49         * @param moduleUrl             URL of the module
50         * @param path                  Path of the rest method on that module. If omitted, the module reachablility itself is tested
51         * @return                              True if the module is reachable, false otherwise
52         */
53        def isModuleReachable(moduleUrl, path = "") {
54                def connection = ( moduleUrl + path ).toURL().openConnection()
55                try {
56                        return connection.responseCode == HttpServletResponse.SC_OK
57                } catch(e) {
58                        return false
59                }
60        }
61       
62        /**
63         * Calls a rest method on a module
64         *
65         * @param consumer      Consumer of that specific module
66         * @param restUrl       Full URL for the method to call
67         * @return                      JSON    JSON object of the parsed text
68         */
69        def callModuleRestMethodJSON( consumer, restUrl ) throws Exception {
70                // create a random session token that will be used to allow to module to
71                // sync with gscf prior to presenting the measurement data
72                def sessionToken = UUID.randomUUID().toString()
73
74                if (!authenticationService.isLoggedIn()) {
75                        // should not happen because we can only get here when a user is
76                        // logged in...
77                        throw new Exception('User is not logged in.')
78                }
79
80                // put the session token to work
81                authenticationService.logInRemotely( consumer, sessionToken, authenticationService.getLoggedInUser() )
82
83                // Append the sessionToken to the URL
84                def url = restUrl
85                if( restUrl.indexOf( '?' ) > 0 ) {
86                        // The url itself also has parameters
87                        url += '&sessionToken=' + sessionToken
88                } else {
89                        // The url itself doesn't have parameters
90                        url += '?sessionToken=' + sessionToken
91                }
92               
93                // Perform a call to the url
94                def restResponse
95                try {
96                        def textResponse = url.toURL().getText()
97                        restResponse = JSON.parse( textResponse )
98                } catch (Exception e) {
99                        throw new Exception( "An error occurred while fetching " + url + ".", e )
100                } finally {
101                        // Dispose of the ephemeral session token
102                        authenticationService.logOffRemotely(consumer, sessionToken)
103                }
104               
105                return restResponse
106        }
107}
Note: See TracBrowser for help on using the browser.