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.protocol.ws.api.event.WebSocketAbortedPayload; 020import org.apache.wicket.protocol.ws.api.event.WebSocketBinaryPayload; 021import org.apache.wicket.protocol.ws.api.event.WebSocketClosedPayload; 022import org.apache.wicket.protocol.ws.api.event.WebSocketConnectedPayload; 023import org.apache.wicket.protocol.ws.api.event.WebSocketErrorPayload; 024import org.apache.wicket.protocol.ws.api.event.WebSocketPayload; 025import org.apache.wicket.protocol.ws.api.event.WebSocketPushPayload; 026import org.apache.wicket.protocol.ws.api.event.WebSocketTextPayload; 027import org.apache.wicket.protocol.ws.api.message.AbortedMessage; 028import org.apache.wicket.protocol.ws.api.message.BinaryMessage; 029import org.apache.wicket.protocol.ws.api.message.ClosedMessage; 030import org.apache.wicket.protocol.ws.api.message.ConnectedMessage; 031import org.apache.wicket.protocol.ws.api.message.ErrorMessage; 032import org.apache.wicket.protocol.ws.api.message.IWebSocketPushMessage; 033import org.apache.wicket.protocol.ws.api.message.TextMessage; 034import org.apache.wicket.request.resource.IResource; 035 036/** 037 * An IResource that can be used as WebSocket endpoint 038 */ 039public abstract class WebSocketResource implements IResource 040{ 041 void onPayload(WebSocketPayload<?> payload) 042 { 043 WebSocketRequestHandler webSocketHandler = payload.getHandler(); 044 045 if (payload instanceof WebSocketTextPayload) 046 { 047 WebSocketTextPayload textPayload = (WebSocketTextPayload) payload; 048 TextMessage data = textPayload.getMessage(); 049 onMessage(webSocketHandler, data); 050 } 051 else if (payload instanceof WebSocketBinaryPayload) 052 { 053 WebSocketBinaryPayload binaryPayload = (WebSocketBinaryPayload) payload; 054 BinaryMessage binaryData = binaryPayload.getMessage(); 055 onMessage(webSocketHandler, binaryData); 056 } 057 else if (payload instanceof WebSocketConnectedPayload) 058 { 059 WebSocketConnectedPayload connectedPayload = (WebSocketConnectedPayload) payload; 060 ConnectedMessage message = connectedPayload.getMessage(); 061 onConnect(message); 062 } 063 else if (payload instanceof WebSocketClosedPayload) 064 { 065 WebSocketClosedPayload connectedPayload = (WebSocketClosedPayload) payload; 066 ClosedMessage message = connectedPayload.getMessage(); 067 onClose(message); 068 } 069 else if (payload instanceof WebSocketErrorPayload) 070 { 071 WebSocketErrorPayload errorPayload = (WebSocketErrorPayload) payload; 072 ErrorMessage message = errorPayload.getMessage(); 073 onError(webSocketHandler, message); 074 } 075 else if (payload instanceof WebSocketAbortedPayload) 076 { 077 WebSocketAbortedPayload abortedPayload = (WebSocketAbortedPayload) payload; 078 AbortedMessage message = abortedPayload.getMessage(); 079 onAbort(message); 080 } 081 else if (payload instanceof WebSocketPushPayload) 082 { 083 WebSocketPushPayload pushPayload = (WebSocketPushPayload) payload; 084 IWebSocketPushMessage message = pushPayload.getMessage(); 085 onPush(webSocketHandler, message); 086 } 087 } 088 089 /** 090 * A callback method called when there is a message pushed/broadcasted by the 091 * server, e.g. pushed by a backend service 092 * 093 * @param handler 094 * The request handler that can be used to send messages to the client 095 * @param message 096 * The message pushed/broadcasted by the server 097 */ 098 protected void onPush(WebSocketRequestHandler handler, IWebSocketPushMessage message) 099 { 100 } 101 102 /** 103 * A callback method called when a WebSocket client has connected to the endpoint 104 * handled by this WebSocketBehavior 105 * 106 * @param message 107 * the connect message with the info about the client 108 */ 109 protected void onConnect(ConnectedMessage message) 110 { 111 } 112 113 /** 114 * A callback method called when the server has aborted the connection 115 * 116 * @param message 117 * the aborted message with the info about the server 118 */ 119 protected void onAbort(AbortedMessage message) 120 { 121 } 122 123 /** 124 * A callback method called when a WebSocket client has closed the connection 125 * to the endpoint handled by this WebSocketBehavior 126 * 127 * @param message 128 * the close message with the info about the client 129 */ 130 protected void onClose(ClosedMessage message) 131 { 132 } 133 134 /** 135 * A callback method called when there is a communication error 136 * 137 * @param handler 138 * The request handler that can be used to send messages to the client 139 * @param message 140 * The error message that that brings information about the communication error 141 */ 142 protected void onError(WebSocketRequestHandler handler, ErrorMessage message) 143 { 144 } 145 146 /** 147 * A callback method called when there is a text message sent by the client 148 * 149 * @param handler 150 * The request handler that can be used to send messages back to the client 151 * @param message 152 * The text message sent by the client 153 */ 154 protected void onMessage(WebSocketRequestHandler handler, TextMessage message) 155 { 156 } 157 158 /** 159 * A callback method called when there is a binary message sent by the client 160 * 161 * @param handler 162 * The request handler that can be used to send messages back to the client 163 * @param binaryMessage 164 * The binary message sent by the client 165 */ 166 protected void onMessage(WebSocketRequestHandler handler, BinaryMessage binaryMessage) 167 { 168 } 169 170 @Override 171 public final void respond(Attributes attributes) 172 { 173 } 174}