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 at007 *008 * https://www.apache.org/licenses/LICENSE-2.0009 *010 * Unless required by applicable law or agreed to in writing, software011 * 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 and014 * limitations under the License.015 */016package org.springframework.web.reactive.socket.server.support;017018import reactor.core.publisher.Mono;019020import 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;027028/**029 * {@link HandlerAdapter} that allows using a {@link WebSocketHandler} with the030 * 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 Stoyanchev035 * @since 5.0036 */037public class WebSocketHandlerAdapter implements HandlerAdapter {038039 private final WebSocketService webSocketService;040041042 /**043 * Default constructor that creates and uses a044 * {@link HandshakeWebSocketService}.045 */046 public WebSocketHandlerAdapter() {047 this(new HandshakeWebSocketService());048 }049050 /**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 }057058059 /**060 * Return the configured {@code WebSocketService} to handle requests.061 */062 public WebSocketService getWebSocketService() {063 return this.webSocketService;064 }065066067 @Override068 public boolean supports(Object handler) {069 return WebSocketHandler.class.isAssignableFrom(handler.getClass());070 }071072 @Override073 public Mono<HandlerResult> handle(ServerWebExchange exchange, Object handler) {074 WebSocketHandler webSocketHandler = (WebSocketHandler) handler;075 return getWebSocketService().handleRequest(exchange, webSocketHandler).then(Mono.empty());076