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.util.tester;
018
019import org.apache.wicket.Application;
020import org.apache.wicket.Page;
021import org.apache.wicket.protocol.http.WebApplication;
022import org.apache.wicket.protocol.ws.WebSocketSettings;
023import org.apache.wicket.protocol.ws.api.IWebSocketProcessor;
024import org.apache.wicket.protocol.ws.api.WebSocketPushBroadcaster;
025import org.apache.wicket.protocol.ws.api.message.ConnectedMessage;
026import org.apache.wicket.protocol.ws.api.message.IWebSocketPushMessage;
027import org.apache.wicket.protocol.ws.api.registry.IKey;
028import org.apache.wicket.util.lang.Args;
029import org.apache.wicket.util.tester.WicketTester;
030
031/**
032 * A helper class to test WebSocket related operations.
033 * 
034 * @since 6.0
035 */
036public class WebSocketTester
037{
038        private final IWebSocketProcessor socketProcessor;
039
040        /**
041         * Constructor.
042         * Prepares a WebSocketConnection that will be used to send messages from the client (the test case)
043         * to the server.
044         *
045         * @param page
046         *      the page that may have registered {@link org.apache.wicket.protocol.ws.api.WebSocketBehavior}
047         */
048        public WebSocketTester(final WicketTester wicketTester, final Page page)
049        {
050                Args.notNull(wicketTester, "wicketTester");
051                Args.notNull(page, "page");
052
053                WebApplication webApplication = wicketTester.getApplication();
054                if (webApplication.getWicketFilter().getFilterPath() == null)
055                {
056                        webApplication.getWicketFilter().setFilterPath("");
057                }
058
059                WebSocketSettings webSocketSettings = WebSocketSettings.Holder.get(webApplication);
060                webSocketSettings.setWebSocketPushMessageExecutor(new WebSocketSettings.SameThreadExecutor());
061
062                socketProcessor = new TestWebSocketProcessor(wicketTester, page)
063                {
064                        @Override
065                        protected void onOutMessage(String message)
066                        {
067                                WebSocketTester.this.onOutMessage(message);
068                        }
069
070                        @Override
071                        protected void onOutMessage(byte[] message, int offset, int length)
072                        {
073                                WebSocketTester.this.onOutMessage(message, offset, length);
074                        }
075                };
076                socketProcessor.onOpen(null);
077        }
078
079        /**
080         * Constructor.
081         *
082         * Prepares a WebSockConnection that will be used to send messages from the client (the test case)
083         * to the server.
084         *
085         * @param resourceName
086         *      the name of the shared WebSocketResource that will handle the web socket messages
087         */
088        public WebSocketTester(final WicketTester wicketTester, final String resourceName)
089        {
090                Args.notNull(wicketTester, "wicketTester");
091                Args.notNull(resourceName, "resourceName");
092
093                WebApplication webApplication = wicketTester.getApplication();
094                if (webApplication.getWicketFilter().getFilterPath() == null)
095                {
096                        webApplication.getWicketFilter().setFilterPath("");
097                }
098
099                WebSocketSettings webSocketSettings = WebSocketSettings.Holder.get(webApplication);
100                webSocketSettings.setWebSocketPushMessageExecutor(new WebSocketSettings.SameThreadExecutor());
101
102                socketProcessor = new TestWebSocketProcessor(wicketTester, resourceName)
103                {
104                        @Override
105                        protected void onOutMessage(String message)
106                        {
107                                WebSocketTester.this.onOutMessage(message);
108                        }
109
110                        @Override
111                        protected void onOutMessage(byte[] message, int offset, int length)
112                        {
113                                WebSocketTester.this.onOutMessage(message, offset, length);
114                        }
115                };
116                socketProcessor.onOpen(null);
117        }
118
119        /**
120         * Sends a text message from the client (a test case) to the server
121         * @param message
122         *      the text message to send to the server
123         */
124        public void sendMessage(final String message)
125        {
126                socketProcessor.onMessage(message);
127        }
128
129
130        /**
131         * Sends a binary message from the client (a test case) to the server
132         *
133         * @param message
134         *      the binary message to send to the server
135         * @param offset
136         *      the offset of the binary message to start to read from
137         * @param length
138         *      the length of bytes to read from the binary message
139         */
140        public void sendMessage(final byte[] message, final int offset, final int length)
141        {
142                socketProcessor.onMessage(message, offset, length);
143        }
144
145        /**
146         * Broadcasts/pushes a message to specific web socket connection
147         *
148         * @param application
149         *          The application where the web socket connection is registered
150         * @param sessionId
151         *          The id of the http session with which the web socket connection is registered
152         * @param key
153         *          The key with which the web socket connection is registered
154         * @param message
155         *          The message to broadcast/push
156         */
157        public void broadcast(Application application, String sessionId, IKey key, IWebSocketPushMessage message)
158        {
159                WebSocketSettings webSocketSettings = WebSocketSettings.Holder.get(application);
160                WebSocketPushBroadcaster broadcaster = new WebSocketPushBroadcaster(webSocketSettings.getConnectionRegistry());
161                ConnectedMessage wsMessage = new ConnectedMessage(application, sessionId, key);
162                broadcaster.broadcast(wsMessage, message);
163        }
164
165        /**
166         * Broadcasts/pushes a message to all active web socket connections
167         *
168         * @param application
169         *          The application where the web socket connection is registered
170         * @param message
171         *          The message to broadcast/push
172         */
173        public void broadcastAll(Application application, IWebSocketPushMessage message)
174        {
175                WebSocketSettings webSocketSettings = WebSocketSettings.Holder.get(application);
176                WebSocketPushBroadcaster broadcaster = new WebSocketPushBroadcaster(webSocketSettings.getConnectionRegistry());
177                broadcaster.broadcastAll(application, message);
178        }
179        
180        public void destroy()
181        {
182                socketProcessor.onClose(0, "Closed by WebSocketTester");
183        }
184
185        /**
186         * A callback method which may be overritten to receive messages pushed by the server
187         *
188         * @param message
189         *      the pushed text message from the server
190         */
191        protected void onOutMessage(String message)
192        {
193        }
194
195        /**
196         * A callback method which may be overritten to receive messages pushed by the server
197         *
198         * @param message
199         *      the pushed binary message from the server
200         * @param offset
201         *      the offset of the binary message to start to read from
202         * @param length
203         *      the length of bytes to read from the binary message
204         */
205        protected void onOutMessage(byte[] message, int offset, int length)
206        {
207        }
208}