use of org.springframework.web.socket.server.HandshakeHandler in project spring-framework by spring-projects.
the class TransportHandlingSockJsService method handleRawWebSocketRequest.
@Override
protected void handleRawWebSocketRequest(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler handler) throws IOException {
TransportHandler transportHandler = this.handlers.get(TransportType.WEBSOCKET);
if (!(transportHandler instanceof HandshakeHandler)) {
logger.error("No handler configured for raw WebSocket messages");
response.setStatusCode(HttpStatus.NOT_FOUND);
return;
}
HandshakeInterceptorChain chain = new HandshakeInterceptorChain(this.interceptors, handler);
HandshakeFailureException failure = null;
try {
Map<String, Object> attributes = new HashMap<>();
if (!chain.applyBeforeHandshake(request, response, attributes)) {
return;
}
((HandshakeHandler) transportHandler).doHandshake(request, response, handler, attributes);
chain.applyAfterHandshake(request, response, null);
} catch (HandshakeFailureException ex) {
failure = ex;
} catch (Exception ex) {
failure = new HandshakeFailureException("Uncaught failure for request " + request.getURI(), ex);
} finally {
if (failure != null) {
chain.applyAfterHandshake(request, response, failure);
throw failure;
}
}
}
use of org.springframework.web.socket.server.HandshakeHandler in project spring-framework by spring-projects.
the class AbstractWebSocketHandlerRegistration method getMappings.
protected final M getMappings() {
M mappings = createMappings();
if (this.sockJsServiceRegistration != null) {
SockJsService sockJsService = this.sockJsServiceRegistration.getSockJsService();
this.handlerMap.forEach((wsHandler, paths) -> {
for (String path : paths) {
String pathPattern = (path.endsWith("/") ? path + "**" : path + "/**");
addSockJsServiceMapping(mappings, sockJsService, wsHandler, pathPattern);
}
});
} else {
HandshakeHandler handshakeHandler = getOrCreateHandshakeHandler();
HandshakeInterceptor[] interceptors = getInterceptors();
this.handlerMap.forEach((wsHandler, paths) -> {
for (String path : paths) {
addWebSocketHandlerMapping(mappings, wsHandler, handshakeHandler, interceptors, path);
}
});
}
return mappings;
}
use of org.springframework.web.socket.server.HandshakeHandler in project spring-framework by spring-projects.
the class WebSocketHandlerRegistrationTests method handshakeHandler.
@Test
public void handshakeHandler() {
WebSocketHandler handler = new TextWebSocketHandler();
HandshakeHandler handshakeHandler = new DefaultHandshakeHandler();
this.registration.addHandler(handler, "/foo").setHandshakeHandler(handshakeHandler);
List<Mapping> mappings = this.registration.getMappings();
assertThat(mappings.size()).isEqualTo(1);
Mapping mapping = mappings.get(0);
assertThat(mapping.webSocketHandler).isEqualTo(handler);
assertThat(mapping.path).isEqualTo("/foo");
assertThat(mapping.handshakeHandler).isSameAs(handshakeHandler);
}
use of org.springframework.web.socket.server.HandshakeHandler in project spring-framework by spring-projects.
the class WebSocketHandlerRegistrationTests method handshakeHandlerPassedToSockJsRegistration.
@Test
public void handshakeHandlerPassedToSockJsRegistration() {
WebSocketHandler handler = new TextWebSocketHandler();
HandshakeHandler handshakeHandler = new DefaultHandshakeHandler();
this.registration.addHandler(handler, "/foo").setHandshakeHandler(handshakeHandler).withSockJS();
this.registration.getSockJsServiceRegistration().setTaskScheduler(this.taskScheduler);
List<Mapping> mappings = this.registration.getMappings();
assertThat(mappings.size()).isEqualTo(1);
Mapping mapping = mappings.get(0);
assertThat(mapping.webSocketHandler).isEqualTo(handler);
assertThat(mapping.path).isEqualTo("/foo/**");
assertThat(mapping.sockJsService).isNotNull();
WebSocketTransportHandler transportHandler = (WebSocketTransportHandler) mapping.sockJsService.getTransportHandlers().get(TransportType.WEBSOCKET);
assertThat(transportHandler.getHandshakeHandler()).isSameAs(handshakeHandler);
}
use of org.springframework.web.socket.server.HandshakeHandler in project spring-framework by spring-projects.
the class TestMessageCodec method webSocketHandlersAttributes.
@Test
public void webSocketHandlersAttributes() {
loadBeanDefinitions("websocket-config-handlers-attributes.xml");
HandlerMapping handlerMapping = this.appContext.getBean(HandlerMapping.class);
assertThat(handlerMapping).isNotNull();
boolean condition2 = handlerMapping instanceof SimpleUrlHandlerMapping;
assertThat(condition2).isTrue();
SimpleUrlHandlerMapping urlHandlerMapping = (SimpleUrlHandlerMapping) handlerMapping;
assertThat(urlHandlerMapping.getOrder()).isEqualTo(2);
WebSocketHttpRequestHandler handler = (WebSocketHttpRequestHandler) urlHandlerMapping.getUrlMap().get("/foo");
assertThat(handler).isNotNull();
unwrapAndCheckDecoratedHandlerType(handler.getWebSocketHandler(), FooWebSocketHandler.class);
HandshakeHandler handshakeHandler = handler.getHandshakeHandler();
assertThat(handshakeHandler).isNotNull();
boolean condition1 = handshakeHandler instanceof TestHandshakeHandler;
assertThat(condition1).isTrue();
List<HandshakeInterceptor> interceptors = handler.getHandshakeInterceptors();
assertThat(interceptors).extracting("class").containsExactlyInAnyOrder(FooTestInterceptor.class, BarTestInterceptor.class, OriginHandshakeInterceptor.class);
handler = (WebSocketHttpRequestHandler) urlHandlerMapping.getUrlMap().get("/test");
assertThat(handler).isNotNull();
unwrapAndCheckDecoratedHandlerType(handler.getWebSocketHandler(), TestWebSocketHandler.class);
handshakeHandler = handler.getHandshakeHandler();
assertThat(handshakeHandler).isNotNull();
boolean condition = handshakeHandler instanceof TestHandshakeHandler;
assertThat(condition).isTrue();
interceptors = handler.getHandshakeInterceptors();
assertThat(interceptors).extracting("class").containsExactlyInAnyOrder(FooTestInterceptor.class, BarTestInterceptor.class, OriginHandshakeInterceptor.class);
}
Aggregations