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