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 */
016package org.springframework.web.reactive.socket.server.support;
017
018import reactor.core.publisher.Mono;
019
020import org.springframework.util.Assert;
021import org.springframework.web.reactive.DispatcherHandler;
022import org.springframework.web.reactive.HandlerAdapter;
023import org.springframework.web.reactive.HandlerResult;
024import org.springframework.web.reactive.socket.WebSocketHandler;
025import org.springframework.web.reactive.socket.server.WebSocketService;
026import org.springframework.web.server.ServerWebExchange;
027
028/**
029 * {@link HandlerAdapter} that allows using a {@link WebSocketHandler} with the
030 * generic {@link DispatcherHandler} mapping URLs directly to such handlers.
031 * Requests are handled by delegating to the configured {@link WebSocketService}
032 * which by default is {@link HandshakeWebSocketService}.
033 *
034 * @author Rossen Stoyanchev
035 * @since 5.0
036 */
037public class WebSocketHandlerAdapter implements HandlerAdapter {
038
039        private final WebSocketService webSocketService;
040
041
042        /**
043         * Default constructor that creates and uses a
044         * {@link HandshakeWebSocketService}.
045         */
046        public WebSocketHandlerAdapter() {
047                this(new HandshakeWebSocketService());
048        }
049
050        /**
051         * Alternative constructor with the {@link WebSocketService} to use.
052         */
053        public WebSocketHandlerAdapter(WebSocketService webSocketService) {
054                Assert.notNull(webSocketService, "'webSocketService' is required");
055                this.webSocketService = webSocketService;
056        }
057
058
059        /**
060         * Return the configured {@code WebSocketService} to handle requests.
061         */
062        public WebSocketService getWebSocketService() {
063                return this.webSocketService;
064        }
065
066
067        @Override
068        public boolean supports(Object handler) {
069                return WebSocketHandler.class.isAssignableFrom(handler.getClass());
070        }
071
072        @Override
073        public Mono<HandlerResult> handle(ServerWebExchange exchange, Object handler) {
074                WebSocketHandler webSocketHandler = (WebSocketHandler) handler;
075                return getWebSocketService().handleRequest(exchange, webSocketHandler).then(Mono.empty());
076        }
077
078}