001/*
002 * Copyright 2002-2018 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.reactive.socket.server;
018
019import java.util.function.Supplier;
020
021import reactor.core.publisher.Mono;
022
023import org.springframework.http.server.reactive.ServerHttpRequest;
024import org.springframework.http.server.reactive.ServerHttpResponse;
025import org.springframework.lang.Nullable;
026import org.springframework.web.reactive.socket.HandshakeInfo;
027import org.springframework.web.reactive.socket.WebSocketHandler;
028import org.springframework.web.server.ServerWebExchange;
029
030/**
031 * A strategy for upgrading an HTTP request to a WebSocket session depending
032 * on the underlying network runtime.
033 *
034 * <p>Typically there is one such strategy for every {@link ServerHttpRequest}
035 * and {@link ServerHttpResponse} type except in the case of Servlet containers
036 * for which the standard Java WebSocket API JSR-356 does not define a way to
037 * upgrade a request so a custom strategy is needed for every Servlet container.
038 *
039 * @author Rossen Stoyanchev
040 * @since 5.0
041 */
042public interface RequestUpgradeStrategy {
043
044        /**
045         * Upgrade to a WebSocket session and handle it with the given handler.
046         * @param exchange the current exchange
047         * @param webSocketHandler handler for the WebSocket session
048         * @param subProtocol the selected sub-protocol got the handler
049         * @return completion {@code Mono<Void>} to indicate the outcome of the
050         * WebSocket session handling.
051         * @deprecated as of 5.1 in favor of
052         * {@link #upgrade(ServerWebExchange, WebSocketHandler, String, Supplier)}
053         */
054        @Deprecated
055        default Mono<Void> upgrade(ServerWebExchange exchange, WebSocketHandler webSocketHandler,
056                        @Nullable String subProtocol) {
057
058                return Mono.error(new UnsupportedOperationException());
059        }
060
061        /**
062         * Upgrade to a WebSocket session and handle it with the given handler.
063         * @param exchange the current exchange
064         * @param webSocketHandler handler for the WebSocket session
065         * @param subProtocol the selected sub-protocol got the handler
066         * @param handshakeInfoFactory factory to create HandshakeInfo for the WebSocket session
067         * @return completion {@code Mono<Void>} to indicate the outcome of the
068         * WebSocket session handling.
069         * @since 5.1
070         */
071        @SuppressWarnings("deprecation")
072        default Mono<Void> upgrade(ServerWebExchange exchange, WebSocketHandler webSocketHandler,
073                        @Nullable String subProtocol, Supplier<HandshakeInfo> handshakeInfoFactory) {
074
075                return upgrade(exchange, webSocketHandler, subProtocol);
076        }
077
078}