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