forked from spring-projects/spring-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure configuration provided for WebSocketHandler's (eg interceptors, or HandshakeHandler) are passed on to the SockJsService if congiured. Better separate Servlet-specific parts of the configuration to make it more obvious where non-Servlet alternatives could fit in. Add more tests. Improve WebSocket integration tests.
- Loading branch information
1 parent
5d69700
commit e21bbdd
Showing
19 changed files
with
1,110 additions
and
371 deletions.
There are no files selected for viewing
143 changes: 143 additions & 0 deletions
143
...ain/java/org/springframework/messaging/simp/config/AbstractStompEndpointRegistration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package org.springframework.messaging.simp.config; | ||
|
||
import java.util.Set; | ||
|
||
import org.springframework.messaging.handler.websocket.SubProtocolWebSocketHandler; | ||
import org.springframework.scheduling.TaskScheduler; | ||
import org.springframework.util.Assert; | ||
import org.springframework.util.ObjectUtils; | ||
import org.springframework.web.socket.server.DefaultHandshakeHandler; | ||
import org.springframework.web.socket.server.HandshakeHandler; | ||
import org.springframework.web.socket.server.config.SockJsServiceRegistration; | ||
import org.springframework.web.socket.sockjs.SockJsService; | ||
import org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler; | ||
|
||
|
||
/** | ||
* A helper class for configuring STOMP protocol handling over WebSocket | ||
* with optional SockJS fallback options. | ||
* | ||
* @author Rossen Stoyanchev | ||
* @since 4.0 | ||
*/ | ||
public abstract class AbstractStompEndpointRegistration<M> implements StompEndpointRegistration { | ||
|
||
private final String[] paths; | ||
|
||
private final SubProtocolWebSocketHandler wsHandler; | ||
|
||
private HandshakeHandler handshakeHandler; | ||
|
||
private StompSockJsServiceRegistration sockJsServiceRegistration; | ||
|
||
private final TaskScheduler defaultSockJsTaskScheduler; | ||
|
||
|
||
public AbstractStompEndpointRegistration(String[] paths, SubProtocolWebSocketHandler webSocketHandler, | ||
TaskScheduler defaultSockJsTaskScheduler) { | ||
|
||
Assert.notEmpty(paths, "No paths specified"); | ||
this.paths = paths; | ||
this.wsHandler = webSocketHandler; | ||
this.defaultSockJsTaskScheduler = defaultSockJsTaskScheduler; | ||
} | ||
|
||
|
||
protected SubProtocolWebSocketHandler getWsHandler() { | ||
return this.wsHandler; | ||
} | ||
|
||
@Override | ||
public StompEndpointRegistration setHandshakeHandler(HandshakeHandler handshakeHandler) { | ||
this.handshakeHandler = handshakeHandler; | ||
return this; | ||
} | ||
|
||
@Override | ||
public SockJsServiceRegistration withSockJS() { | ||
|
||
this.sockJsServiceRegistration = new StompSockJsServiceRegistration(this.defaultSockJsTaskScheduler); | ||
|
||
if (this.handshakeHandler != null) { | ||
WebSocketTransportHandler transportHandler = new WebSocketTransportHandler(this.handshakeHandler); | ||
this.sockJsServiceRegistration.setTransportHandlerOverrides(transportHandler); | ||
} | ||
|
||
return this.sockJsServiceRegistration; | ||
} | ||
|
||
protected M getMappings() { | ||
|
||
M mappings = createMappings(); | ||
|
||
if (this.sockJsServiceRegistration != null) { | ||
SockJsService sockJsService = this.sockJsServiceRegistration.getSockJsService(); | ||
for (String path : this.paths) { | ||
String pathPattern = path.endsWith("/") ? path + "**" : path + "/**"; | ||
addSockJsServiceMapping(mappings, sockJsService, this.wsHandler, pathPattern); | ||
} | ||
} | ||
else { | ||
HandshakeHandler handshakeHandler = getOrCreateHandshakeHandler(); | ||
for (String path : this.paths) { | ||
addWebSocketHandlerMapping(mappings, this.wsHandler, handshakeHandler, path); | ||
} | ||
} | ||
|
||
return mappings; | ||
} | ||
|
||
protected abstract M createMappings(); | ||
|
||
private HandshakeHandler getOrCreateHandshakeHandler() { | ||
|
||
HandshakeHandler handler = (this.handshakeHandler != null) | ||
? this.handshakeHandler : new DefaultHandshakeHandler(); | ||
|
||
if (handler instanceof DefaultHandshakeHandler) { | ||
DefaultHandshakeHandler defaultHandshakeHandler = (DefaultHandshakeHandler) handler; | ||
if (ObjectUtils.isEmpty(defaultHandshakeHandler.getSupportedProtocols())) { | ||
Set<String> protocols = this.wsHandler.getSupportedProtocols(); | ||
defaultHandshakeHandler.setSupportedProtocols(protocols.toArray(new String[protocols.size()])); | ||
} | ||
} | ||
|
||
return handler; | ||
} | ||
|
||
protected abstract void addSockJsServiceMapping(M mappings, SockJsService sockJsService, | ||
SubProtocolWebSocketHandler wsHandler, String pathPattern); | ||
|
||
protected abstract void addWebSocketHandlerMapping(M mappings, | ||
SubProtocolWebSocketHandler wsHandler, HandshakeHandler handshakeHandler, String path); | ||
|
||
|
||
|
||
private class StompSockJsServiceRegistration extends SockJsServiceRegistration { | ||
|
||
public StompSockJsServiceRegistration(TaskScheduler defaultTaskScheduler) { | ||
super(defaultTaskScheduler); | ||
} | ||
|
||
protected SockJsService getSockJsService() { | ||
return super.getSockJsService(paths); | ||
} | ||
} | ||
|
||
} |
68 changes: 68 additions & 0 deletions
68
...main/java/org/springframework/messaging/simp/config/ServletStompEndpointRegistration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package org.springframework.messaging.simp.config; | ||
|
||
import org.springframework.messaging.handler.websocket.SubProtocolWebSocketHandler; | ||
import org.springframework.scheduling.TaskScheduler; | ||
import org.springframework.util.LinkedMultiValueMap; | ||
import org.springframework.util.MultiValueMap; | ||
import org.springframework.web.HttpRequestHandler; | ||
import org.springframework.web.socket.server.HandshakeHandler; | ||
import org.springframework.web.socket.server.support.WebSocketHttpRequestHandler; | ||
import org.springframework.web.socket.sockjs.SockJsHttpRequestHandler; | ||
import org.springframework.web.socket.sockjs.SockJsService; | ||
|
||
|
||
/** | ||
* A helper class for configuring STOMP protocol handling over WebSocket | ||
* with optional SockJS fallback options. | ||
* | ||
* @author Rossen Stoyanchev | ||
* @since 4.0 | ||
*/ | ||
public class ServletStompEndpointRegistration | ||
extends AbstractStompEndpointRegistration<MultiValueMap<HttpRequestHandler, String>> { | ||
|
||
|
||
public ServletStompEndpointRegistration(String[] paths, SubProtocolWebSocketHandler wsHandler, | ||
TaskScheduler sockJsTaskScheduler) { | ||
|
||
super(paths, wsHandler, sockJsTaskScheduler); | ||
} | ||
|
||
@Override | ||
protected MultiValueMap<HttpRequestHandler, String> createMappings() { | ||
return new LinkedMultiValueMap<HttpRequestHandler, String>(); | ||
} | ||
|
||
@Override | ||
protected void addSockJsServiceMapping(MultiValueMap<HttpRequestHandler, String> mappings, | ||
SockJsService sockJsService, SubProtocolWebSocketHandler wsHandler, String pathPattern) { | ||
|
||
SockJsHttpRequestHandler httpHandler = new SockJsHttpRequestHandler(sockJsService, wsHandler); | ||
mappings.add(httpHandler, pathPattern); | ||
} | ||
|
||
@Override | ||
protected void addWebSocketHandlerMapping(MultiValueMap<HttpRequestHandler, String> mappings, | ||
SubProtocolWebSocketHandler wsHandler, HandshakeHandler handshakeHandler, String path) { | ||
|
||
WebSocketHttpRequestHandler handler = new WebSocketHttpRequestHandler(wsHandler, handshakeHandler); | ||
mappings.add(handler, path); | ||
} | ||
|
||
} |
91 changes: 91 additions & 0 deletions
91
...src/main/java/org/springframework/messaging/simp/config/ServletStompEndpointRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package org.springframework.messaging.simp.config; | ||
|
||
import java.util.ArrayList; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.springframework.messaging.handler.websocket.SubProtocolWebSocketHandler; | ||
import org.springframework.messaging.simp.handler.MutableUserQueueSuffixResolver; | ||
import org.springframework.messaging.simp.stomp.StompProtocolHandler; | ||
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; | ||
|
||
|
||
/** | ||
* A helper class for configuring STOMP protocol handling over WebSocket. | ||
* | ||
* @author Rossen Stoyanchev | ||
* @since 4.0 | ||
*/ | ||
public class ServletStompEndpointRegistry implements StompEndpointRegistry { | ||
|
||
private final SubProtocolWebSocketHandler wsHandler; | ||
|
||
private final StompProtocolHandler stompHandler; | ||
|
||
private final List<ServletStompEndpointRegistration> registrations = new ArrayList<ServletStompEndpointRegistration>(); | ||
|
||
private final TaskScheduler sockJsScheduler; | ||
|
||
|
||
public ServletStompEndpointRegistry(SubProtocolWebSocketHandler webSocketHandler, | ||
MutableUserQueueSuffixResolver userQueueSuffixResolver, TaskScheduler defaultSockJsTaskScheduler) { | ||
|
||
Assert.notNull(webSocketHandler); | ||
Assert.notNull(userQueueSuffixResolver); | ||
|
||
this.wsHandler = webSocketHandler; | ||
this.stompHandler = new StompProtocolHandler(); | ||
this.stompHandler.setUserQueueSuffixResolver(userQueueSuffixResolver); | ||
this.sockJsScheduler = defaultSockJsTaskScheduler; | ||
} | ||
|
||
|
||
@Override | ||
public StompEndpointRegistration addEndpoint(String... paths) { | ||
this.wsHandler.addProtocolHandler(this.stompHandler); | ||
ServletStompEndpointRegistration r = new ServletStompEndpointRegistration(paths, this.wsHandler, this.sockJsScheduler); | ||
this.registrations.add(r); | ||
return r; | ||
} | ||
|
||
/** | ||
* 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>(); | ||
for (ServletStompEndpointRegistration registration : this.registrations) { | ||
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); | ||
return hm; | ||
} | ||
|
||
} |
Oops, something went wrong.