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.ArrayList; 020import java.util.LinkedHashMap; 021import java.util.List; 022import java.util.Map; 023 024import org.springframework.scheduling.TaskScheduler; 025import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; 026import org.springframework.util.MultiValueMap; 027import org.springframework.web.HttpRequestHandler; 028import org.springframework.web.servlet.HandlerMapping; 029import org.springframework.web.servlet.handler.AbstractHandlerMapping; 030import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping; 031import org.springframework.web.socket.WebSocketHandler; 032import org.springframework.web.socket.server.support.WebSocketHandlerMapping; 033import org.springframework.web.util.UrlPathHelper; 034 035/** 036 * A {@link WebSocketHandlerRegistry} that maps {@link WebSocketHandler}s to URLs for use 037 * in a Servlet container. 038 * 039 * @author Rossen Stoyanchev 040 * @since 4.0 041 */ 042public class ServletWebSocketHandlerRegistry implements WebSocketHandlerRegistry { 043 044 private final List<ServletWebSocketHandlerRegistration> registrations = 045 new ArrayList<ServletWebSocketHandlerRegistration>(); 046 047 private TaskScheduler sockJsTaskScheduler; 048 049 private int order = 1; 050 051 private UrlPathHelper urlPathHelper; 052 053 054 public ServletWebSocketHandlerRegistry(ThreadPoolTaskScheduler sockJsTaskScheduler) { 055 this.sockJsTaskScheduler = sockJsTaskScheduler; 056 } 057 058 @Override 059 public WebSocketHandlerRegistration addHandler(WebSocketHandler webSocketHandler, String... paths) { 060 ServletWebSocketHandlerRegistration registration = 061 new ServletWebSocketHandlerRegistration(this.sockJsTaskScheduler); 062 registration.addHandler(webSocketHandler, paths); 063 this.registrations.add(registration); 064 return registration; 065 } 066 067 /** 068 * Set the order for the resulting {@link SimpleUrlHandlerMapping} relative to 069 * other handler mappings configured in Spring MVC. 070 * <p>The default value is 1. 071 */ 072 public void setOrder(int order) { 073 this.order = order; 074 } 075 076 public int getOrder() { 077 return this.order; 078 } 079 080 /** 081 * Set the UrlPathHelper to configure on the {@code SimpleUrlHandlerMapping} 082 * used to map handshake requests. 083 */ 084 public void setUrlPathHelper(UrlPathHelper urlPathHelper) { 085 this.urlPathHelper = urlPathHelper; 086 } 087 088 public UrlPathHelper getUrlPathHelper() { 089 return this.urlPathHelper; 090 } 091 092 /** 093 * Return a {@link HandlerMapping} with mapped {@link HttpRequestHandler}s. 094 */ 095 public AbstractHandlerMapping getHandlerMapping() { 096 Map<String, Object> urlMap = new LinkedHashMap<String, Object>(); 097 for (ServletWebSocketHandlerRegistration registration : this.registrations) { 098 MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings(); 099 for (HttpRequestHandler httpHandler : mappings.keySet()) { 100 for (String pattern : mappings.get(httpHandler)) { 101 urlMap.put(pattern, httpHandler); 102 } 103 } 104 } 105 WebSocketHandlerMapping hm = new WebSocketHandlerMapping(); 106 hm.setUrlMap(urlMap); 107 hm.setOrder(this.order); 108 if (this.urlPathHelper != null) { 109 hm.setUrlPathHelper(this.urlPathHelper); 110 } 111 return hm; 112 } 113 114}