All Downloads are FREE. Search and download functionalities are using the official Maven repository.

socket.events.grvt Maven / Gradle / Ivy

There is a newer version: 2.1.0-beta.1
Show newest version
/*******************************************************************************
 * © 2018 Disney | ABC Television Group
 *
 * Licensed under the Apache License, Version 2.0 (the "Apache License")
 * with the following modification; you may not use this file except in
 * compliance with the Apache License and the following modification to it:
 * Section 6. Trademarks. is deleted and replaced with:
 *
 * 6. Trademarks. This License does not grant permission to use the trade
 *     names, trademarks, service marks, or product names of the Licensor
 *     and its affiliates, except as required to comply with Section 4(c) of
 *     the License and to reproduce the content of the NOTICE file.
 *
 * You may obtain a copy of the Apache License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Apache License with the above modification is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the Apache License for the specific
 * language governing permissions and limitations under the Apache License.
 *******************************************************************************/
import com.disney.groovity.util.AsyncChannel
import com.disney.groovity.util.WebSocket

static web=[
	socket:'events',
	message:Map.class,
	auth : [ policy: { run('/policy/eventPolicy') } ],
	cors : [ policy: { run('/policy/eventPolicy') } ],
]

@Field AsyncChannel callbackChannel
@Field Map subscriptions

def open(){
	callbackChannel = accept{
		try{
			socket(it)
		}
		catch(Exception e){
			log(debug:"Closing event socket on error",thrown:e)
			socket.close()
		}
	}
	subscriptions = [:]
	offer(channel:'events-open'){ socket }
}

def close(){
	callbackChannel.close()
	subscriptions.values().each{
		it.close()
	}
	offer(channel:'events-close'){ socket }
}

def error(){
	offer(channel:'events-error'){ socket }
}

@CompileStatic handleMessage(Map message){
	def mId = message.get('id')
	def sub = message.get('subscribe')
	def socket = ((WebSocket) binding.getVariable('socket'))
	if(sub){
		//don't allow structured keys to get passed in as this could allow cross-user snooping
		sub = sub.toString()
		def broadcastChan = [ event: sub ]
		//register to receive
		if(!subscriptions.get(broadcastChan)){
			def cb = callbackChannel
			subscriptions.put(broadcastChan, (AsyncChannel) accept(channel: broadcastChan){ data ->
				cb.offer{
					[
						event: sub,
						data: data
					]
				}
			})
			def pr = socket.userPrincipal
			if(pr){
				//also subscribe to user-specific messages
				def userChan = [user:pr.name, event:sub]
				subscriptions.put(userChan, (AsyncChannel) accept(channel: userChan){ data ->
					cb.offer{
						[
							event: sub,
							data: data
						]
					}
				})
			}
		}
		if(mId!=null){
			//send ack
			callbackChannel.offer{
				[
					id: mId,
					data: [ subscribed: sub]
				]
			}
		}
	}
	else{
		def unsub = message.get('unsubscribe')
		if(unsub){
			unsub = unsub.toString()
			def broadcastChan = [ event: unsub ]
			def subs = subscriptions.remove(broadcastChan)
			if(subs){
				subs.close()
				def pr = socket.userPrincipal
				if(pr){
					def userUnsub = [user:pr.name,event:unsub]
					def uu = subscriptions.remove(userUnsub)
					if(uu){
						uu.close()
					}
				}
			}
			if(mId!=null){
				//send ack
				callbackChannel.offer{
					[
						id: mId,
						data: [ unsubscribed: unsub]
					]
				}
			}
		}
		else{
			def event = message.get('event')
			if(!event){
				throw new RuntimeException("Message does not specify an event ${message}")
			}
			offer(channel: [remote: event], 
				value: [ 
					data: message.get('data'), 
					session: socket.session, 
					callback: { data ->
						// wrap the callback in an envelope with the request ID
						callbackChannel.offer{
							[
								id: mId,
								data: data
							]
						}
					}
				] 
			)
		}
	}
}

handleMessage(message)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy