WebMvcStompEndpointRegistry.java 4.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright 2002-2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

17
package org.springframework.web.socket.messaging.config;
18

19
import java.util.*;
20

21
import org.springframework.messaging.simp.handler.UserSessionRegistry;
22
import org.springframework.web.socket.messaging.StompSubProtocolHandler;
23 24 25 26 27 28
import org.springframework.scheduling.TaskScheduler;
import org.springframework.util.Assert;
import org.springframework.util.MultiValueMap;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.servlet.handler.AbstractHandlerMapping;
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
29
import org.springframework.web.socket.WebSocketHandler;
30
import org.springframework.web.socket.messaging.SubProtocolWebSocketHandler;
31
import org.springframework.web.socket.support.WebSocketHandlerDecorator;
32 33 34


/**
35 36
 * A registry for STOMP over WebSocket endpoints that maps the endpoints with a
 * {@link SimpleUrlHandlerMapping} for use in Spring MVC.
37 38 39 40
 *
 * @author Rossen Stoyanchev
 * @since 4.0
 */
41
public class WebMvcStompEndpointRegistry implements StompEndpointRegistry {
42

43 44 45
	private final WebSocketHandler webSocketHandler;

	private final SubProtocolWebSocketHandler subProtocolWebSocketHandler;
46

47
	private final StompSubProtocolHandler stompHandler;
48

49 50
	private final List<WebMvcStompWebSocketEndpointRegistration> registrations =
			new ArrayList<WebMvcStompWebSocketEndpointRegistration>();
51 52 53

	private final TaskScheduler sockJsScheduler;

54
	private int order = 1;
55

56 57

	public WebMvcStompEndpointRegistry(WebSocketHandler webSocketHandler,
58
			UserSessionRegistry userSessionRegistry, TaskScheduler defaultSockJsTaskScheduler) {
59 60

		Assert.notNull(webSocketHandler);
61
		Assert.notNull(userSessionRegistry);
62

63
		this.webSocketHandler = webSocketHandler;
64 65
		this.subProtocolWebSocketHandler = unwrapSubProtocolWebSocketHandler(webSocketHandler);
		this.stompHandler = new StompSubProtocolHandler();
66
		this.stompHandler.setUserSessionRegistry(userSessionRegistry);
67 68 69
		this.sockJsScheduler = defaultSockJsTaskScheduler;
	}

70
	private static SubProtocolWebSocketHandler unwrapSubProtocolWebSocketHandler(WebSocketHandler webSocketHandler) {
71 72 73 74 75 76 77 78 79 80

		WebSocketHandler actual = (webSocketHandler instanceof WebSocketHandlerDecorator) ?
				((WebSocketHandlerDecorator) webSocketHandler).getLastHandler() : webSocketHandler;

		Assert.isInstanceOf(SubProtocolWebSocketHandler.class, actual,
						"No SubProtocolWebSocketHandler found: " + webSocketHandler);

		return (SubProtocolWebSocketHandler) actual;
	}

81 82

	@Override
83 84
	public StompWebSocketEndpointRegistration addEndpoint(String... paths) {

85
		this.subProtocolWebSocketHandler.addProtocolHandler(this.stompHandler);
86 87

		WebMvcStompWebSocketEndpointRegistration registration = new WebMvcStompWebSocketEndpointRegistration(
88
				paths, this.webSocketHandler, this.sockJsScheduler);
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
		this.registrations.add(registration);

		return registration;
	}

	/**
	 * Set the order for the resulting {@link SimpleUrlHandlerMapping} relative to
	 * other handler mappings configured in Spring MVC.
	 * <p>
	 * The default value is 1.
	 */
	public void setOrder(int order) {
		this.order = order;
	}

	public int getOrder() {
		return this.order;
106 107 108 109 110 111 112
	}

	/**
	 * Returns a handler mapping with the mapped ViewControllers; or {@code null} in case of no registrations.
	 */
	protected AbstractHandlerMapping getHandlerMapping() {
		Map<String, Object> urlMap = new LinkedHashMap<String, Object>();
113
		for (WebMvcStompWebSocketEndpointRegistration registration : this.registrations) {
114 115 116 117 118 119 120 121 122
			MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings();
			for (HttpRequestHandler httpHandler : mappings.keySet()) {
				for (String pattern : mappings.get(httpHandler)) {
					urlMap.put(pattern, httpHandler);
				}
			}
		}
		SimpleUrlHandlerMapping hm = new SimpleUrlHandlerMapping();
		hm.setUrlMap(urlMap);
123
		hm.setOrder(this.order);
124 125 126 127
		return hm;
	}

}