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.javax;
018
019import java.nio.ByteBuffer;
020
021import javax.websocket.EndpointConfig;
022import javax.websocket.MessageHandler;
023import javax.websocket.Session;
024
025import org.apache.wicket.protocol.http.WebApplication;
026import org.apache.wicket.protocol.ws.api.AbstractWebSocketProcessor;
027
028/**
029 * An {@link org.apache.wicket.protocol.ws.api.IWebSocketProcessor processor} that integrates with
030 * JSR 356 {@link Session web socket} implementation.
031 *
032 * @since 7.0.0
033 */
034public class JavaxWebSocketProcessor extends AbstractWebSocketProcessor
035{
036        /**
037         * Constructor.
038         *
039         * @param session
040         *            the WebSocket session
041         * @param application
042         *            the {@link org.apache.wicket.protocol.http.WebApplication}
043         * @param endpointConfig
044         *            the {@link javax.websocket.EndpointConfig}
045         */
046        public JavaxWebSocketProcessor(final Session session, final WebApplication application, EndpointConfig endpointConfig)
047        {
048                super(new JavaxUpgradeHttpRequest(session, endpointConfig), application);
049
050                onConnect(new JavaxWebSocketConnection(session, this));
051
052                session.addMessageHandler(new StringMessageHandler());
053                session.addMessageHandler(new BinaryMessageHandler());
054        }
055
056        @Override
057        public void onOpen(Object containerConnection)
058        {
059        }
060
061        private class StringMessageHandler implements MessageHandler.Whole<String>
062        {
063                @Override
064                public void onMessage(String message)
065                {
066                        JavaxWebSocketProcessor.this.onMessage(message);
067                }
068        }
069
070        private class BinaryMessageHandler implements MessageHandler.Whole<ByteBuffer>
071        {
072                @Override
073                public void onMessage(ByteBuffer message)
074                {
075                        byte[] array = message.array();
076                        JavaxWebSocketProcessor.this.onMessage(array, 0, array.length);
077                }
078        }
079
080
081}