/** * Default constructor that creates and uses a * {@link HandshakeWebSocketService}. */ public WebSocketHandlerAdapter() { this(new HandshakeWebSocketService()); }
@Test public void sessionAttributePredicate() { MockWebSession session = new MockWebSession(); session.getAttributes().put("a1", "v1"); session.getAttributes().put("a2", "v2"); session.getAttributes().put("a3", "v3"); session.getAttributes().put("a4", "v4"); session.getAttributes().put("a5", "v5"); MockServerHttpRequest request = initHandshakeRequest(); MockServerWebExchange exchange = MockServerWebExchange.builder(request).session(session).build(); TestRequestUpgradeStrategy upgradeStrategy = new TestRequestUpgradeStrategy(); HandshakeWebSocketService service = new HandshakeWebSocketService(upgradeStrategy); service.setSessionAttributePredicate(name -> Arrays.asList("a1", "a3", "a5").contains(name)); service.handleRequest(exchange, mock(WebSocketHandler.class)).block(); HandshakeInfo info = upgradeStrategy.handshakeInfo; assertNotNull(info); Map<String, Object> attributes = info.getAttributes(); assertEquals(3, attributes.size()); assertThat(attributes, Matchers.hasEntry("a1", "v1")); assertThat(attributes, Matchers.hasEntry("a3", "v3")); assertThat(attributes, Matchers.hasEntry("a5", "v5")); }
@Override public Mono<Void> handleRequest(ServerWebExchange exchange, WebSocketHandler handler) { ServerHttpRequest request = exchange.getRequest(); HttpMethod method = request.getMethod(); HttpHeaders headers = request.getHeaders(); if (HttpMethod.GET != method) { return Mono.error(new MethodNotAllowedException( request.getMethodValue(), Collections.singleton(HttpMethod.GET))); } if (!"WebSocket".equalsIgnoreCase(headers.getUpgrade())) { return handleBadRequest(exchange, "Invalid 'Upgrade' header: " + headers); } List<String> connectionValue = headers.getConnection(); if (!connectionValue.contains("Upgrade") && !connectionValue.contains("upgrade")) { return handleBadRequest(exchange, "Invalid 'Connection' header: " + headers); } String key = headers.getFirst(SEC_WEBSOCKET_KEY); if (key == null) { return handleBadRequest(exchange, "Missing \"Sec-WebSocket-Key\" header"); } String protocol = selectProtocol(headers, handler); return initAttributes(exchange).flatMap(attributes -> this.upgradeStrategy.upgrade(exchange, handler, protocol, () -> createHandshakeInfo(exchange, request, protocol, attributes)) ); }
protected void doStart() { if (getUpgradeStrategy() instanceof Lifecycle) { ((Lifecycle) getUpgradeStrategy()).start(); } }
@Override public Mono<Void> handleRequest(ServerWebExchange exchange, WebSocketHandler handler) { ServerHttpRequest request = exchange.getRequest(); HttpMethod method = request.getMethod(); HttpHeaders headers = request.getHeaders(); if (logger.isDebugEnabled()) { logger.debug("Handling " + request.getURI() + " with headers: " + headers); } if (HttpMethod.GET != method) { return Mono.error(new MethodNotAllowedException(method.name(), Collections.singleton("GET"))); } if (!"WebSocket".equalsIgnoreCase(headers.getUpgrade())) { return handleBadRequest("Invalid 'Upgrade' header: " + headers); } List<String> connectionValue = headers.getConnection(); if (!connectionValue.contains("Upgrade") && !connectionValue.contains("upgrade")) { return handleBadRequest("Invalid 'Connection' header: " + headers); } String key = headers.getFirst(SEC_WEBSOCKET_KEY); if (key == null) { return handleBadRequest("Missing \"Sec-WebSocket-Key\" header"); } Optional<String> protocol = selectProtocol(headers, handler); return this.upgradeStrategy.upgrade(exchange, handler, protocol); }
/** * Default constructor automatic, classpath detection based discovery of the * {@link RequestUpgradeStrategy} to use. */ public HandshakeWebSocketService() { this(initUpgradeStrategy()); }
protected void doStop() { if (getUpgradeStrategy() instanceof Lifecycle) { ((Lifecycle) getUpgradeStrategy()).stop(); } }
/** * Default constructor automatic, classpath detection based discovery of the * {@link RequestUpgradeStrategy} to use. */ public HandshakeWebSocketService() { this(initUpgradeStrategy()); }
@Bean public WebSocketService webSocketService() { return new HandshakeWebSocketService(); }
protected void doStart() { if (getUpgradeStrategy() instanceof Lifecycle) { ((Lifecycle) getUpgradeStrategy()).start(); } }
@Bean public WebSocketService webSocketService() { return new HandshakeWebSocketService(getUpgradeStrategy()); }
protected void doStop() { if (getUpgradeStrategy() instanceof Lifecycle) { ((Lifecycle) getUpgradeStrategy()).stop(); } }
@Bean public WebSocketService webSocketService() { return new HandshakeWebSocketService(getUpgradeStrategy()); }
/** * Default constructor that creates and uses a * {@link HandshakeWebSocketService}. */ public WebSocketHandlerAdapter() { this(new HandshakeWebSocketService()); }
@Bean public WebSocketService webSocketService() { return new HandshakeWebSocketService(); }