Changeset 1596 for trunk/grails-app/services/dbnp
- Timestamp:
- Mar 8, 2011, 11:20:31 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/services/dbnp/modules/ModuleCommunicationService.groovy
r1577 r1596 81 81 * @param restUrl Full URL for the method to call 82 82 * @return JSON JSON object of the parsed text 83 * @deprecated Use callModuleMethod instead 83 84 */ 84 85 def callModuleRestMethodJSON( consumer, restUrl ) throws Exception { 86 def parts = restUrl.split( /\?/ ); 87 def url = ""; 88 def query = ""; 89 90 if( parts.size() > 1 ) { 91 url = parts[ 0 ] 92 query = parts[ 1 ] 93 } else if( parts.size() > 0 ) { 94 url = parts[ 0 ]; 95 } 96 97 return callModuleMethod( consumer, url, query ); 98 } 99 100 /** 101 * Calls a rest method on a module 102 * 103 * @param consumer Consumer of that specific module 104 * @param restUrl Full URL for the method to call, without query string 105 * @param args Query string for the url to call (e.q. token=abc&field=xyz) 106 * @param requestMethod GET or POST - HTTP request method to use 107 * @return JSON JSON object of the parsed text 108 */ 109 def callModuleMethod( String consumer, String restUrl, String args = null, String requestMethod = "GET" ) { 85 110 if (!authenticationService.isLoggedIn()) { 86 111 // should not happen because we can only get here when a user is … … 90 115 91 116 // Check whether the url is present in cache 92 def cacheData = retrieveFromCache( restUrl );117 def cacheData = retrieveFromCache( restUrl, args ); 93 118 if( cacheData && cacheData[ "success" ] ) 94 119 return cacheData[ "contents" ]; … … 104 129 105 130 // Append the sessionToken to the URL 106 def url = restUrl 107 if( restUrl.indexOf( '?' ) > 0 ) { 108 // The url itself also has parameters 109 url += '&sessionToken=' + sessionToken 110 } else { 111 // The url itself doesn't have parameters 112 url += '?sessionToken=' + sessionToken 113 } 131 if( !args ) 132 args = "" 133 134 args += "&sessionToken=" + sessionToken 114 135 115 136 // Perform a call to the url 116 137 def restResponse 117 138 try { 118 def textResponse = url.toURL().getText() 119 log.trace "GSCF call to " + consumer + " URL: " + url 139 log.trace "GSCF call (" + requestMethod + ") to " + consumer + " URL: " + restUrl + " (args: " + args + ")" 140 141 def textResponse 142 switch( requestMethod.toUpperCase() ) { 143 case "GET": 144 def url = restUrl + "?" + args; 145 def connection = url.toURL().openConnection(); 146 147 textResponse = url.toURL().getText() 148 149 break 150 case "POST": 151 def connection = restUrl.toURL().openConnection() 152 connection.setRequestMethod( "POST" ); 153 connection.doOutput = true 154 155 def writer = new OutputStreamWriter( connection.outputStream ) 156 writer.write( args ); 157 writer.flush() 158 writer.close() 159 160 connection.connect(); 161 162 textResponse = connection.content.text 163 164 break 165 default: 166 throw new Exception( "Unknown request method given. Use GET or POST" ) 167 } 168 120 169 log.trace "GSCF response: " + textResponse 121 170 restResponse = JSON.parse( textResponse ) 122 171 } catch (Exception e) { 123 storeErrorInCache( restUrl, e.getMessage() );124 throw new Exception( "An error occurred while fetching " + url + ".", e )172 storeErrorInCache( restUrl, e.getMessage(), args ); 173 throw new Exception( "An error occurred while fetching " + restUrl + ".", e ) 125 174 } finally { 126 175 // Dispose of the ephemeral session token 127 //authenticationService.logOffRemotely(consumer, sessionToken)176 authenticationService.logOffRemotely(consumer, sessionToken) 128 177 } 129 178 130 179 // Store the response in cache 131 storeInCache( restUrl, restResponse );180 storeInCache( restUrl, restResponse, args ); 132 181 133 182 return restResponse 134 } 135 183 184 } 185 136 186 /** 137 187 * Checks whether a specific url exists in cache … … 139 189 * @return true if the url is present in cache 140 190 */ 141 def existsInCache( url ) {142 return retrieveFromCache( url ) == null;191 def existsInCache( url, args = null ) { 192 return retrieveFromCache( url, args ) == null; 143 193 } 144 194 … … 148 198 * @return JSON object with the contents of the URL or null if the url doesn't exist in cache 149 199 */ 150 def retrieveFromCache( url ) {200 def retrieveFromCache( url, args = null ) { 151 201 def user = authenticationService.getLoggedInUser(); 152 202 def userId = user ? user.id : -1; 203 204 url = cacheUrl( url, args ) 153 205 154 206 if( cache[ userId ] && cache[ userId ][ url ] && ( System.currentTimeMillis() - cache[ userId ][ url ][ "timestamp" ] ) < numberOfSecondsInCache * 1000 ) { … … 164 216 * @param contents Contents of the URL 165 217 */ 166 def storeInCache( url, contents ) {218 def storeInCache( url, contents, args = null ) { 167 219 def user = authenticationService.getLoggedInUser(); 168 220 def userId = user ? user.id : -1; … … 171 223 cache[ userId ] = [:] 172 224 173 cache[ userId ][ url] = [225 cache[ userId ][ cacheUrl( url, args ) ] = [ 174 226 "timestamp": System.currentTimeMillis(), 175 227 "success": true, … … 183 235 * @param contents Contents of the URL 184 236 */ 185 def storeErrorInCache( url, error ) {237 def storeErrorInCache( url, error, args = null ) { 186 238 def user = authenticationService.getLoggedInUser(); 187 239 def userId = user ? user.id : -1; … … 190 242 cache[ userId ] = [:] 191 243 192 cache[ userId ][ url] = [244 cache[ userId ][ cacheUrl( url, args ) ] = [ 193 245 "timestamp": System.currentTimeMillis(), 194 246 "success": false, … … 196 248 ]; 197 249 } 250 251 /** 252 * Url used to save data in cache 253 */ 254 def cacheUrl( url, args = null ) { 255 if( args ) { 256 // Remove sessionToken from args 257 args = args; 258 def sessionFound = ( args =~ /sessionToken=[^&]/ ); 259 args = sessionFound.replaceAll( "sessionToken=" ); 260 261 url += '?' + args 262 } 263 264 return url; 265 } 198 266 199 267 }
Note: See TracChangeset
for help on using the changeset viewer.