AbstractWebSocketHandlerRegistration.java 5.9 KB
Newer Older
1
/*
2
 * Copyright 2002-2014 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
import java.util.ArrayList;
20
import java.util.Arrays;
21
import java.util.List;
22 23 24 25 26

import org.springframework.scheduling.TaskScheduler;
import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
27
import org.springframework.util.ObjectUtils;
28 29 30
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;
31
import org.springframework.web.socket.server.support.OriginHandshakeInterceptor;
32
import org.springframework.web.socket.server.support.DefaultHandshakeHandler;
33 34 35 36 37 38 39 40
import org.springframework.web.socket.sockjs.SockJsService;
import org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler;

/**
 * Base class for {@link WebSocketHandlerRegistration}s that gathers all the configuration
 * options but allows sub-classes to put together the actual HTTP request mappings.
 *
 * @author Rossen Stoyanchev
41
 * @author Sebastien Deleuze
42 43 44 45
 * @since 4.0
 */
public abstract class AbstractWebSocketHandlerRegistration<M> implements WebSocketHandlerRegistration {

R
Rossen Stoyanchev 已提交
46
	private final TaskScheduler sockJsTaskScheduler;
47

R
Rossen Stoyanchev 已提交
48
	private MultiValueMap<WebSocketHandler, String> handlerMap = new LinkedMultiValueMap<WebSocketHandler, String>();
49 50 51

	private HandshakeHandler handshakeHandler;

52 53 54
	private final List<HandshakeInterceptor> interceptors = new ArrayList<HandshakeInterceptor>();

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

R
Rossen Stoyanchev 已提交
56
	private SockJsServiceRegistration sockJsServiceRegistration;
57 58 59 60 61 62


	public AbstractWebSocketHandlerRegistration(TaskScheduler defaultTaskScheduler) {
		this.sockJsTaskScheduler = defaultTaskScheduler;
	}

P
Phillip Webb 已提交
63

64 65 66 67 68 69 70 71 72 73 74 75 76 77
	@Override
	public WebSocketHandlerRegistration addHandler(WebSocketHandler handler, String... paths) {
		Assert.notNull(handler);
		Assert.notEmpty(paths);
		this.handlerMap.put(handler, Arrays.asList(paths));
		return this;
	}

	@Override
	public WebSocketHandlerRegistration setHandshakeHandler(HandshakeHandler handshakeHandler) {
		this.handshakeHandler = handshakeHandler;
		return this;
	}

R
Rossen Stoyanchev 已提交
78 79
	protected HandshakeHandler getHandshakeHandler() {
		return this.handshakeHandler;
80 81 82 83
	}

	@Override
	public WebSocketHandlerRegistration addInterceptors(HandshakeInterceptor... interceptors) {
84 85 86
		if (!ObjectUtils.isEmpty(interceptors)) {
			this.interceptors.addAll(Arrays.asList(interceptors));
		}
87 88 89
		return this;
	}

90 91 92 93 94 95 96
	@Override
	public WebSocketHandlerRegistration setAllowedOrigins(String... origins) {
		this.allowedOrigins.clear();
		if (!ObjectUtils.isEmpty(origins)) {
			this.allowedOrigins.addAll(Arrays.asList(origins));
		}
		return this;
97 98 99 100 101
	}

	@Override
	public SockJsServiceRegistration withSockJS() {
		this.sockJsServiceRegistration = new SockJsServiceRegistration(this.sockJsTaskScheduler);
102 103 104
		HandshakeInterceptor[] interceptors = getInterceptors();
		if (interceptors.length > 0) {
			this.sockJsServiceRegistration.setInterceptors(interceptors);
R
Rossen Stoyanchev 已提交
105
		}
106 107 108 109
		if (this.handshakeHandler != null) {
			WebSocketTransportHandler transportHandler = new WebSocketTransportHandler(this.handshakeHandler);
			this.sockJsServiceRegistration.setTransportHandlerOverrides(transportHandler);
		}
110 111 112
		if (!this.allowedOrigins.isEmpty()) {
			this.sockJsServiceRegistration.setAllowedOrigins(this.allowedOrigins.toArray(new String[this.allowedOrigins.size()]));
		}
113 114 115
		return this.sockJsServiceRegistration;
	}

116 117 118
	protected HandshakeInterceptor[] getInterceptors() {
		List<HandshakeInterceptor> interceptors = new ArrayList<HandshakeInterceptor>();
		interceptors.addAll(this.interceptors);
R
Rossen Stoyanchev 已提交
119
		if (!this.allowedOrigins.isEmpty()) {
120 121 122 123 124 125 126
			OriginHandshakeInterceptor interceptor = new OriginHandshakeInterceptor();
			interceptor.setAllowedOrigins(this.allowedOrigins);
			interceptors.add(interceptor);
		}
		return interceptors.toArray(new HandshakeInterceptor[interceptors.size()]);
	}

R
Rossen Stoyanchev 已提交
127
	protected final M getMappings() {
128 129
		M mappings = createMappings();
		if (this.sockJsServiceRegistration != null) {
130
			SockJsService sockJsService = this.sockJsServiceRegistration.getSockJsService();
131 132 133 134 135 136 137 138 139
			for (WebSocketHandler wsHandler : this.handlerMap.keySet()) {
				for (String path : this.handlerMap.get(wsHandler)) {
					String pathPattern = path.endsWith("/") ? path + "**" : path + "/**";
					addSockJsServiceMapping(mappings, sockJsService, wsHandler, pathPattern);
				}
			}
		}
		else {
			HandshakeHandler handshakeHandler = getOrCreateHandshakeHandler();
140
			HandshakeInterceptor[] interceptors = getInterceptors();
141 142
			for (WebSocketHandler wsHandler : this.handlerMap.keySet()) {
				for (String path : this.handlerMap.get(wsHandler)) {
143
					addWebSocketHandlerMapping(mappings, wsHandler, handshakeHandler, interceptors, path);
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
				}
			}
		}

		return mappings;
	}

	private HandshakeHandler getOrCreateHandshakeHandler() {
		return (this.handshakeHandler != null) ? this.handshakeHandler : new DefaultHandshakeHandler();
	}

	protected abstract M createMappings();

	protected abstract void addSockJsServiceMapping(M mappings, SockJsService sockJsService,
			WebSocketHandler handler, String pathPattern);

	protected abstract void addWebSocketHandlerMapping(M mappings, WebSocketHandler wsHandler,
			HandshakeHandler handshakeHandler, HandshakeInterceptor[] interceptors, String path);

}