001/*
002 * Copyright 2002-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.web.socket.messaging;
018
019import java.util.List;
020
021import org.springframework.messaging.Message;
022import org.springframework.messaging.MessageChannel;
023import org.springframework.web.socket.CloseStatus;
024import org.springframework.web.socket.WebSocketMessage;
025import org.springframework.web.socket.WebSocketSession;
026
027/**
028 * A contract for handling WebSocket messages as part of a higher level protocol,
029 * referred to as "sub-protocol" in the WebSocket RFC specification. Handles both
030 * {@link WebSocketMessage}s from a client as well as {@link Message}s to a client.
031 *
032 * <p>Implementations of this interface can be configured on a
033 * {@link SubProtocolWebSocketHandler} which selects a sub-protocol handler to
034 * delegate messages to based on the sub-protocol requested by the client through
035 * the {@code Sec-WebSocket-Protocol} request header.
036 *
037 * @author Andy Wilkinson
038 * @author Rossen Stoyanchev
039 * @since 4.0
040 */
041public interface SubProtocolHandler {
042
043        /**
044         * Return the list of sub-protocols supported by this handler (never {@code null}).
045         */
046        List<String> getSupportedProtocols();
047
048        /**
049         * Handle the given {@link WebSocketMessage} received from a client.
050         * @param session the client session
051         * @param message the client message
052         * @param outputChannel an output channel to send messages to
053         */
054        void handleMessageFromClient(WebSocketSession session, WebSocketMessage<?> message, MessageChannel outputChannel)
055                        throws Exception;
056
057        /**
058         * Handle the given {@link Message} to the client associated with the given WebSocket session.
059         * @param session the client session
060         * @param message the client message
061         */
062        void handleMessageToClient(WebSocketSession session, Message<?> message) throws Exception;
063
064        /**
065         * Resolve the session id from the given message or return {@code null}.
066         * @param message the message to resolve the session id from
067         */
068        String resolveSessionId(Message<?> message);
069
070        /**
071         * Invoked after a {@link WebSocketSession} has started.
072         * @param session the client session
073         * @param outputChannel a channel
074         */
075        void afterSessionStarted(WebSocketSession session, MessageChannel outputChannel) throws Exception;
076
077        /**
078         * Invoked after a {@link WebSocketSession} has ended.
079         * @param session the client session
080         * @param closeStatus the reason why the session was closed
081         * @param outputChannel a channel
082         */
083        void afterSessionEnded(WebSocketSession session, CloseStatus closeStatus, MessageChannel outputChannel)
084                        throws Exception;
085
086}