001/*
002 * Copyright 2002-2014 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.server;
018
019import java.util.Map;
020
021import org.springframework.http.server.ServerHttpRequest;
022import org.springframework.http.server.ServerHttpResponse;
023import org.springframework.web.socket.WebSocketHandler;
024
025/**
026 * Interceptor for WebSocket handshake requests. Can be used to inspect the
027 * handshake request and response as well as to pass attributes to the target
028 * {@link WebSocketHandler}.
029 *
030 * @author Rossen Stoyanchev
031 * @since 4.0
032 * @see org.springframework.web.socket.server.support.WebSocketHttpRequestHandler
033 * @see org.springframework.web.socket.sockjs.transport.handler.DefaultSockJsService
034 */
035public interface HandshakeInterceptor {
036
037        /**
038         * Invoked before the handshake is processed.
039         * @param request the current request
040         * @param response the current response
041         * @param wsHandler the target WebSocket handler
042         * @param attributes attributes from the HTTP handshake to associate with the WebSocket
043         * session; the provided attributes are copied, the original map is not used.
044         * @return whether to proceed with the handshake ({@code true}) or abort ({@code false})
045         */
046        boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response,
047                        WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception;
048
049        /**
050         * Invoked after the handshake is done. The response status and headers indicate
051         * the results of the handshake, i.e. whether it was successful or not.
052         * @param request the current request
053         * @param response the current response
054         * @param wsHandler the target WebSocket handler
055         * @param exception an exception raised during the handshake, or {@code null} if none
056         */
057        void afterHandshake(ServerHttpRequest request, ServerHttpResponse response,
058                        WebSocketHandler wsHandler, Exception exception);
059
060}