Changeset 1446
- Timestamp:
- Jan 27, 2011, 2:34:41 PM (10 years ago)
- Location:
- trunk/grails-app
- Files:
-
- 2 added
- 1 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/domain/dbnp/studycapturing/Study.groovy
r1440 r1446 4 4 import nl.grails.plugins.gdt.* 5 5 import java.util.UUID; 6 import dbnp.modules.*; 6 7 7 8 /** … … 16 17 static searchable = true 17 18 18 def synchronizationService19 def moduleNotificationService 19 20 20 21 SecUser owner // The owner of the study. A new study is automatically owned by its creator. … … 524 525 // Send messages to modules about changes in this study 525 526 def beforeInsert = { 526 synchronizationService.invalidateStudy( this );527 moduleNotificationService.invalidateStudy( this ); 527 528 } 528 529 def beforeUpdate = { 529 synchronizationService.invalidateStudy( this );530 moduleNotificationService.invalidateStudy( this ); 530 531 } 531 532 def beforeDelete = { 532 synchronizationService.invalidateStudy( this );533 moduleNotificationService.invalidateStudy( this ); 533 534 } 534 535 } -
trunk/grails-app/services/dbnp/modules/ModuleCommunicationService.groovy
r1445 r1446 13 13 * $Date$ 14 14 */ 15 package dbnp. studycapturing15 package dbnp.modules 16 16 17 17 import nl.grails.plugins.gdt.* 18 import com.sun.xml.internal.ws.api.streaming.XMLStreamReaderFactory.Default; 18 import dbnp.studycapturing.* 19 import grails.converters.* 19 20 20 class SynchronizationService implements Serializable {21 class ModuleCommunicationService implements Serializable { 21 22 boolean transactional = false 23 def authenticationService 24 def moduleNotificationService 22 25 23 26 /** … … 35 38 */ 36 39 def invalidateStudy( Study study ) { 37 if( !study ) 38 return 39 40 log.info( "Invalidate " + study.code ) 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() 41 73 42 def modules = AssayModule.findByNotify(true); 43 44 def urls = [] 45 modules.each { module -> 46 urls << module.url + '/rest/notifyStudyChange?studyToken=' + study.giveUUID() 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 47 91 } 48 92 49 Thread.start { 50 urls.each { url -> 51 try { 52 def connection = url.toURL().openConnection() 53 if( connection.responseCode == 200 ) { 54 log.info( "GSCF NOTIFY-CALL SUCCEEDED: ${url}" ) 55 } else { 56 log.info( "GSCF NOTIFY-CALL FAILED: ${url}: " + connection.responseCode ) 57 } 58 } catch( Exception ignore) { 59 log.info( "GSCF NOTIFY-CALL ERROR: ${url} - " + ignore.getMessage() ) 60 } 61 } 62 }; 63 } 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 } 64 107 }
Note: See TracChangeset
for help on using the changeset viewer.