WebMvcStompWebSocketEndpointRegistration.java 5.7 KB
Newer Older
1
/*
S
Sebastien Deleuze 已提交
2
 * Copyright 2002-2015 the original author or authors.
3 4 5 6 7
 *
 * 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
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9 10 11 12 13 14 15 16
 *
 * 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.config.annotation;
18

19 20
import java.util.Arrays;

21 22 23 24
import org.springframework.scheduling.TaskScheduler;
import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
25
import org.springframework.util.ObjectUtils;
26 27 28
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeHandler;
29
import org.springframework.web.socket.server.HandshakeInterceptor;
30
import org.springframework.web.socket.server.support.OriginHandshakeInterceptor;
31 32
import org.springframework.web.socket.server.support.WebSocketHttpRequestHandler;
import org.springframework.web.socket.sockjs.SockJsService;
33
import org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler;
34 35
import org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler;

36 37
import java.util.ArrayList;
import java.util.List;
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
/**
 * An abstract base class class for configuring STOMP over WebSocket/SockJS endpoints.
 *
 * @author Rossen Stoyanchev
 * @since 4.0
 */
public class WebMvcStompWebSocketEndpointRegistration implements StompWebSocketEndpointRegistration {

	private final String[] paths;

	private final WebSocketHandler webSocketHandler;

	private final TaskScheduler sockJsTaskScheduler;

	private HandshakeHandler handshakeHandler;

54 55 56
	private final List<HandshakeInterceptor> interceptors = new ArrayList<HandshakeInterceptor>();

	private final List<String> allowedOrigins = new ArrayList<String>();
57

58 59 60 61
	private StompSockJsServiceRegistration registration;


	public WebMvcStompWebSocketEndpointRegistration(String[] paths, WebSocketHandler webSocketHandler,
62
			TaskScheduler sockJsTaskScheduler) {
63 64

		Assert.notEmpty(paths, "No paths specified");
P
Phillip Webb 已提交
65
		Assert.notNull(webSocketHandler, "WebSocketHandler must not be null");
66 67 68 69 70 71 72 73 74 75 76 77 78

		this.paths = paths;
		this.webSocketHandler = webSocketHandler;
		this.sockJsTaskScheduler = sockJsTaskScheduler;
	}

	@Override
	public StompWebSocketEndpointRegistration setHandshakeHandler(HandshakeHandler handshakeHandler) {
		Assert.notNull(handshakeHandler, "'handshakeHandler' must not be null");
		this.handshakeHandler = handshakeHandler;
		return this;
	}

79 80
	@Override
	public StompWebSocketEndpointRegistration addInterceptors(HandshakeInterceptor... interceptors) {
81 82 83
		if (!ObjectUtils.isEmpty(interceptors)) {
			this.interceptors.addAll(Arrays.asList(interceptors));
		}
84 85 86
		return this;
	}

87
	@Override
88
	public StompWebSocketEndpointRegistration setAllowedOrigins(String... allowedOrigins) {
89
		this.allowedOrigins.clear();
90 91 92
		if (!ObjectUtils.isEmpty(allowedOrigins)) {
			this.allowedOrigins.addAll(Arrays.asList(allowedOrigins));
		}
93
		return this;
94 95
	}

96 97 98
	@Override
	public SockJsServiceRegistration withSockJS() {
		this.registration = new StompSockJsServiceRegistration(this.sockJsTaskScheduler);
99 100 101
		HandshakeInterceptor[] interceptors = getInterceptors();
		if (interceptors.length > 0) {
			this.registration.setInterceptors(interceptors);
102
		}
103 104 105 106
		if (this.handshakeHandler != null) {
			WebSocketTransportHandler transportHandler = new WebSocketTransportHandler(this.handshakeHandler);
			this.registration.setTransportHandlerOverrides(transportHandler);
		}
107 108 109
		if (!this.allowedOrigins.isEmpty()) {
			this.registration.setAllowedOrigins(this.allowedOrigins.toArray(new String[this.allowedOrigins.size()]));
		}
110 111 112
		return this.registration;
	}

113 114 115
	protected HandshakeInterceptor[] getInterceptors() {
		List<HandshakeInterceptor> interceptors = new ArrayList<HandshakeInterceptor>();
		interceptors.addAll(this.interceptors);
116
		interceptors.add(new OriginHandshakeInterceptor(this.allowedOrigins));
117 118 119
		return interceptors.toArray(new HandshakeInterceptor[interceptors.size()]);
	}

120
	public final MultiValueMap<HttpRequestHandler, String> getMappings() {
121 122 123 124 125 126 127 128 129 130 131
		MultiValueMap<HttpRequestHandler, String> mappings = new LinkedMultiValueMap<HttpRequestHandler, String>();
		if (this.registration != null) {
			SockJsService sockJsService = this.registration.getSockJsService();
			for (String path : this.paths) {
				String pattern = path.endsWith("/") ? path + "**" : path + "/**";
				SockJsHttpRequestHandler handler = new SockJsHttpRequestHandler(sockJsService, this.webSocketHandler);
				mappings.add(handler, pattern);
			}
		}
		else {
			for (String path : this.paths) {
132 133 134 135 136 137 138
				WebSocketHttpRequestHandler handler;
				if (this.handshakeHandler != null) {
					handler = new WebSocketHttpRequestHandler(this.webSocketHandler, this.handshakeHandler);
				}
				else {
					handler = new WebSocketHttpRequestHandler(this.webSocketHandler);
				}
139 140 141
				HandshakeInterceptor[] interceptors = getInterceptors();
				if (interceptors.length > 0) {
					handler.setHandshakeInterceptors(Arrays.asList(interceptors));
142
				}
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
				mappings.add(handler, path);
			}
		}
		return mappings;
	}


	private static class StompSockJsServiceRegistration extends SockJsServiceRegistration {

		public StompSockJsServiceRegistration(TaskScheduler defaultTaskScheduler) {
			super(defaultTaskScheduler);
		}

		protected SockJsService getSockJsService() {
			return super.getSockJsService();
		}
	}

}