001/* 002 * Copyright (c) 2013 - 2016 TDP Ltd All Rights Reserved. 003 * TDP Ltd grants permission, free of charge, to any person obtaining copies 004 * of this software and its associated documentation files (the "Software"), 005 * to deal in the Software without restriction, including to use, copy, adapt, 006 * publish, distribute, display, perform, sublicense, and sell copies of the 007 * Software, subject to the following condition: You must include the above 008 * copyright notice and this permission notice in all full or partial copies 009 * of the Software. 010* 011 * TDP LTD PROVIDES THE SOFTWARE "AS IS," WITHOUT ANY EXPRESS OR IMPLIED WARRANTY, 012 * INCLUDING WITHOUT THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 013 * PARTICULAR PURPOSE, AND NON-INFRINGMENT. TDP LTD, THE AUTHORS OF THE SOFTWARE, 014 * AND THE OWNERS OF COPYRIGHT IN THE SOFTWARE ARE NOT LIABLE FOR ANY CLAIM, DAMAGES, 015 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING 016 * FROM, OUT OF, OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 017 * THE SOFTWARE. 018 */ 019 020package cz.tdp.kshield.notification; 021 022import org.eclipse.jetty.websocket.api.RemoteEndpoint; 023import org.eclipse.jetty.websocket.api.Session; 024import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose; 025import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect; 026import org.eclipse.jetty.websocket.api.annotations.OnWebSocketError; 027import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage; 028 029/** 030 * Implementation of WebSocket Endpoint for Jetty Server. You must provide access to KShieldAuthManager instance 031 */ 032public abstract class AbstractJettyKShieldEndpoint extends AbstractWebSocketEndpoint 033{ 034 private Session session; 035 private RemoteEndpoint remote; 036 037 @OnWebSocketClose 038 public void onWebSocketClose(int statusCode, String reason) { 039 onCloseImpl(); 040 041 this.session = null; 042 this.remote = null; 043 } 044 045 @OnWebSocketConnect 046 public void onWebSocketConnect(Session newSession) { 047 onOpenImpl(); 048 049 this.session = newSession; 050 this.remote = this.session.getRemote(); 051 } 052 053 @OnWebSocketError 054 public void onWebSocketError(Throwable cause) { 055 onErrorImpl(cause); 056 } 057 058 @OnWebSocketMessage 059 public void onWebSocketText(String message) { 060 if (this.session != null && this.session.isOpen() && this.remote != null) { 061 send(onMessageImpl(message)); 062 } 063 } 064 065 @Override 066 protected void send(String message) { 067 if (remote != null) { 068 remote.sendStringByFuture(message); 069 } 070 } 071 072 @Override 073 protected void reset() { 074 session = null; 075 remote = null; 076 077 super.reset(); 078 } 079}