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.socket.config.annotation;
018
019import java.util.Arrays;
020
021import org.springframework.util.LinkedMultiValueMap;
022import org.springframework.util.MultiValueMap;
023import org.springframework.util.ObjectUtils;
024import org.springframework.web.HttpRequestHandler;
025import org.springframework.web.socket.WebSocketHandler;
026import org.springframework.web.socket.server.HandshakeHandler;
027import org.springframework.web.socket.server.HandshakeInterceptor;
028import org.springframework.web.socket.server.support.WebSocketHttpRequestHandler;
029import org.springframework.web.socket.sockjs.SockJsService;
030import org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler;
031
032/**
033 * A helper class for configuring {@link WebSocketHandler} request handling
034 * including SockJS fallback options.
035 *
036 * @author Rossen Stoyanchev
037 * @since 4.0
038 */
039public class ServletWebSocketHandlerRegistration
040                extends AbstractWebSocketHandlerRegistration<MultiValueMap<HttpRequestHandler, String>> {
041
042
043        @Override
044        protected MultiValueMap<HttpRequestHandler, String> createMappings() {
045                return new LinkedMultiValueMap<>();
046        }
047
048        @Override
049        protected void addSockJsServiceMapping(MultiValueMap<HttpRequestHandler, String> mappings,
050                        SockJsService sockJsService, WebSocketHandler handler, String pathPattern) {
051
052                SockJsHttpRequestHandler httpHandler = new SockJsHttpRequestHandler(sockJsService, handler);
053                mappings.add(httpHandler, pathPattern);
054        }
055
056        @Override
057        protected void addWebSocketHandlerMapping(MultiValueMap<HttpRequestHandler, String> mappings,
058                        WebSocketHandler webSocketHandler, HandshakeHandler handshakeHandler,
059                        HandshakeInterceptor[] interceptors, String path) {
060
061                WebSocketHttpRequestHandler httpHandler =
062                                new WebSocketHttpRequestHandler(webSocketHandler, handshakeHandler);
063
064                if (!ObjectUtils.isEmpty(interceptors)) {
065                        httpHandler.setHandshakeInterceptors(Arrays.asList(interceptors));
066                }
067                mappings.add(httpHandler, path);
068        }
069
070}