001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.protocol.ws.api;
018
019import org.apache.wicket.Component;
020import org.apache.wicket.event.IEvent;
021import org.apache.wicket.protocol.ws.api.event.WebSocketAbortedPayload;
022import org.apache.wicket.protocol.ws.api.event.WebSocketBinaryPayload;
023import org.apache.wicket.protocol.ws.api.event.WebSocketClosedPayload;
024import org.apache.wicket.protocol.ws.api.event.WebSocketConnectedPayload;
025import org.apache.wicket.protocol.ws.api.event.WebSocketErrorPayload;
026import org.apache.wicket.protocol.ws.api.event.WebSocketPayload;
027import org.apache.wicket.protocol.ws.api.event.WebSocketPushPayload;
028import org.apache.wicket.protocol.ws.api.event.WebSocketTextPayload;
029import org.apache.wicket.protocol.ws.api.message.AbortedMessage;
030import org.apache.wicket.protocol.ws.api.message.BinaryMessage;
031import org.apache.wicket.protocol.ws.api.message.ClosedMessage;
032import org.apache.wicket.protocol.ws.api.message.ConnectedMessage;
033import org.apache.wicket.protocol.ws.api.message.ErrorMessage;
034import org.apache.wicket.protocol.ws.api.message.IWebSocketPushMessage;
035import org.apache.wicket.protocol.ws.api.message.TextMessage;
036
037/**
038 * A behavior that provides optional callbacks for the WebSocket
039 * messages (connect, message, close)
040 *
041 * @since 6.0
042 */
043public abstract class WebSocketBehavior extends BaseWebSocketBehavior
044{
045        public WebSocketBehavior()
046        {
047        }
048
049        @Override
050        public void onEvent(Component component, IEvent<?> event)
051        {
052                super.onEvent(component, event);
053
054                Object payload = event.getPayload();
055                if (payload instanceof WebSocketPayload)
056                {
057                        WebSocketPayload<?> wsPayload = (WebSocketPayload<?>) payload;
058                        WebSocketRequestHandler webSocketHandler = wsPayload.getHandler();
059
060                        if (payload instanceof WebSocketTextPayload)
061                        {
062                                WebSocketTextPayload textPayload = (WebSocketTextPayload) payload;
063                                TextMessage data = textPayload.getMessage();
064                                onMessage(webSocketHandler, data);
065                        }
066                        else if (wsPayload instanceof WebSocketBinaryPayload)
067                        {
068                                WebSocketBinaryPayload binaryPayload = (WebSocketBinaryPayload) wsPayload;
069                                BinaryMessage binaryData = binaryPayload.getMessage();
070                                onMessage(webSocketHandler, binaryData);
071                        }
072                        else if (wsPayload instanceof WebSocketConnectedPayload)
073                        {
074                                WebSocketConnectedPayload connectedPayload = (WebSocketConnectedPayload) wsPayload;
075                                ConnectedMessage message = connectedPayload.getMessage();
076                                onConnect(message);
077                        }
078                        else if (wsPayload instanceof WebSocketClosedPayload)
079                        {
080                                WebSocketClosedPayload closedPayload = (WebSocketClosedPayload) wsPayload;
081                                ClosedMessage message = closedPayload.getMessage();
082                                onClose(message);
083                        }
084                        else if (wsPayload instanceof WebSocketErrorPayload)
085                        {
086                                WebSocketErrorPayload errorPayload = (WebSocketErrorPayload) wsPayload;
087                                ErrorMessage message = errorPayload.getMessage();
088                                onError(webSocketHandler, message);
089                        }
090                        else if (wsPayload instanceof WebSocketAbortedPayload)
091                        {
092                                WebSocketAbortedPayload abortedPayload = (WebSocketAbortedPayload) wsPayload;
093                                AbortedMessage message = abortedPayload.getMessage();
094                                onAbort(message);
095                        }
096                        else if (wsPayload instanceof WebSocketPushPayload)
097                        {
098                                WebSocketPushPayload pushPayload = (WebSocketPushPayload) wsPayload;
099                                IWebSocketPushMessage message = pushPayload.getMessage();
100                                onPush(webSocketHandler, message);
101                        }
102                }
103        }
104
105        /**
106         * A callback method called when there is a message pushed/broadcasted by the
107         * server, e.g. pushed by a backend service
108         *
109         * @param handler
110         *          The request handler that can be used to send messages to the client
111         * @param message
112         *          The message pushed/broadcasted by the server
113         */
114        protected void onPush(WebSocketRequestHandler handler, IWebSocketPushMessage message)
115        {
116        }
117
118        /**
119         * A callback method called when a WebSocket client has connected to the endpoint
120         * handled by this WebSocketBehavior
121         *
122         * @param message
123         *          the connect message with the info about the client
124         */
125        protected void onConnect(ConnectedMessage message)
126        {
127        }
128
129        /**
130         * A callback method called when a WebSocket client has closed the connection
131         * to the endpoint handled by this WebSocketBehavior
132         *
133         * @param message
134         *          the close message with the info about the client
135         */
136        protected void onClose(ClosedMessage message)
137        {
138        }
139
140        /**
141         * A callback method called when there is a communication error
142         *
143         * @param handler
144         *          The request handler that can be used to send messages to the client
145         * @param message
146         *          The error message that that brings information about the communication error
147         */
148        protected void onError(WebSocketRequestHandler handler, ErrorMessage message)
149        {
150        }
151
152        /**
153         * A callback method called when the server has aborted the connection
154         *
155         * @param message
156         *          the aborted message with the info about the client
157         */
158        protected void onAbort(AbortedMessage message) {
159        }
160
161        /**
162         * A callback method called when there is a text message sent by the client
163         *
164         * @param handler
165         *          The request handler that can be used to send messages back to the client
166         * @param message
167         *          The text message sent by the client
168         */
169        protected void onMessage(WebSocketRequestHandler handler, TextMessage message)
170        {
171        }
172
173        /**
174         * A callback method called when there is a binary message sent by the client
175         *
176         * @param handler
177         *          The request handler that can be used to send messages back to the client
178         * @param binaryMessage
179         *          The binary message sent by the client
180         */
181        protected void onMessage(WebSocketRequestHandler handler, BinaryMessage binaryMessage)
182        {
183        }
184}