Skip to content

Commit

Permalink
Support Jetty WebSocket server parameters
Browse files Browse the repository at this point in the history
Closes gh-30344
  • Loading branch information
rstoyanchev committed Sep 13, 2023
1 parent 29e3acc commit 1f8913a
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 58 deletions.
60 changes: 8 additions & 52 deletions framework-docs/modules/ROOT/pages/web/websocket/server.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,7 @@ The following example shows the XML configuration equivalent of the preceding ex
NOTE: For client-side WebSocket configuration, you should use `WebSocketContainerFactoryBean`
(XML) or `ContainerProvider.getWebSocketContainer()` (Java configuration).

For Jetty, you need to supply a pre-configured Jetty `WebSocketServerFactory` and plug
that into Spring's `DefaultHandshakeHandler` through your WebSocket Java config.
The following example shows how to do so:
For Jetty, you need to supply a `Consumer` callback to configure the WebSocket server. For example:

[source,java,indent=0,subs="verbatim,quotes"]
----
Expand All @@ -292,62 +290,20 @@ The following example shows how to do so:
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(echoWebSocketHandler(),
"/echo").setHandshakeHandler(handshakeHandler());
}
@Bean
public DefaultHandshakeHandler handshakeHandler() {
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
policy.setInputBufferSize(8192);
policy.setIdleTimeout(600000);
JettyRequestUpgradeStrategy upgradeStrategy = new JettyRequestUpgradeStrategy();
upgradeStrategy.addWebSocketConfigurer(configurable -> {
policy.setInputBufferSize(8192);
policy.setIdleTimeout(600000);
});
return new DefaultHandshakeHandler(
new JettyRequestUpgradeStrategy(new WebSocketServerFactory(policy)));
registry.addHandler(echoWebSocketHandler(),
"/echo").setHandshakeHandler(new DefaultHandshakeHandler(upgradeStrategy));
}
}
----

The following example shows the XML configuration equivalent of the preceding example:

[source,xml,indent=0,subs="verbatim,quotes,attributes"]
----
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:websocket="http://www.springframework.org/schema/websocket"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/websocket
https://www.springframework.org/schema/websocket/spring-websocket.xsd">
<websocket:handlers>
<websocket:mapping path="/echo" handler="echoHandler"/>
<websocket:handshake-handler ref="handshakeHandler"/>
</websocket:handlers>
<bean id="handshakeHandler" class="org.springframework...DefaultHandshakeHandler">
<constructor-arg ref="upgradeStrategy"/>
</bean>
<bean id="upgradeStrategy" class="org.springframework...JettyRequestUpgradeStrategy">
<constructor-arg ref="serverFactory"/>
</bean>
<bean id="serverFactory" class="org.eclipse.jetty...WebSocketServerFactory">
<constructor-arg>
<bean class="org.eclipse.jetty...WebSocketPolicy">
<constructor-arg value="SERVER"/>
<property name="inputBufferSize" value="8092"/>
<property name="idleTimeout" value="600000"/>
</bean>
</constructor-arg>
</bean>
</beans>
----



Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
Expand All @@ -25,6 +25,7 @@
import java.util.function.Predicate;
import java.util.stream.Collectors;

import jakarta.servlet.ServletContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import reactor.core.publisher.Mono;
Expand All @@ -39,6 +40,7 @@
import org.springframework.util.ClassUtils;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.reactive.socket.HandshakeInfo;
import org.springframework.web.reactive.socket.WebSocketHandler;
import org.springframework.web.reactive.socket.server.RequestUpgradeStrategy;
Expand All @@ -63,7 +65,7 @@
* @author Juergen Hoeller
* @since 5.0
*/
public class HandshakeWebSocketService implements WebSocketService, Lifecycle {
public class HandshakeWebSocketService implements WebSocketService, ServletContextAware, Lifecycle {

private static final String SEC_WEBSOCKET_KEY = "Sec-WebSocket-Key";

Expand Down Expand Up @@ -99,6 +101,7 @@ public class HandshakeWebSocketService implements WebSocketService, Lifecycle {

private static final Log logger = LogFactory.getLog(HandshakeWebSocketService.class);


private final RequestUpgradeStrategy upgradeStrategy;

@Nullable
Expand Down Expand Up @@ -154,6 +157,13 @@ public Predicate<String> getSessionAttributePredicate() {
return this.sessionAttributePredicate;
}

@Override
public void setServletContext(ServletContext servletContext) {
if (getUpgradeStrategy() instanceof ServletContextAware servletContextAware) {
servletContextAware.setServletContext(servletContext);
}
}


@Override
public void start() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
Expand All @@ -16,11 +16,13 @@

package org.springframework.web.reactive.socket.server.support;

import jakarta.servlet.ServletContext;
import reactor.core.publisher.Mono;

import org.springframework.context.annotation.ImportRuntimeHints;
import org.springframework.core.Ordered;
import org.springframework.util.Assert;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.reactive.HandlerAdapter;
import org.springframework.web.reactive.HandlerResult;
import org.springframework.web.reactive.socket.WebSocketHandler;
Expand All @@ -47,7 +49,7 @@
* @since 5.0
*/
@ImportRuntimeHints(HandshakeWebSocketServiceRuntimeHints.class)
public class WebSocketHandlerAdapter implements HandlerAdapter, Ordered {
public class WebSocketHandlerAdapter implements HandlerAdapter, ServletContextAware, Ordered {

private final WebSocketService webSocketService;

Expand Down Expand Up @@ -97,6 +99,13 @@ public WebSocketService getWebSocketService() {
return this.webSocketService;
}

@Override
public void setServletContext(ServletContext servletContext) {
if (this.webSocketService instanceof ServletContextAware servletContextAware) {
servletContextAware.setServletContext(servletContext);
}
}


@Override
public boolean supports(Object handler) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@

package org.springframework.web.reactive.socket.server.upgrade;

import java.util.function.Consumer;
import java.util.function.Supplier;

import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.eclipse.jetty.ee10.websocket.server.JettyWebSocketCreator;
import org.eclipse.jetty.ee10.websocket.server.JettyWebSocketServerContainer;
import org.eclipse.jetty.websocket.api.Configurable;
import reactor.core.publisher.Mono;

import org.springframework.core.io.buffer.DataBufferFactory;
Expand All @@ -31,6 +33,7 @@
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
import org.springframework.lang.Nullable;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.reactive.socket.HandshakeInfo;
import org.springframework.web.reactive.socket.WebSocketHandler;
import org.springframework.web.reactive.socket.adapter.ContextWebSocketHandler;
Expand All @@ -45,7 +48,30 @@
* @author Rossen Stoyanchev
* @since 5.3.4
*/
public class JettyRequestUpgradeStrategy implements RequestUpgradeStrategy {
public class JettyRequestUpgradeStrategy implements RequestUpgradeStrategy, ServletContextAware {

@Nullable
private Consumer<Configurable> webSocketConfigurer;


/**
* Add a callback to configure WebSocket server parameters on
* {@link JettyWebSocketServerContainer}.
* @since 6.1.0
*/
public void addWebSocketConfigurer(Consumer<Configurable> webSocketConfigurer) {
this.webSocketConfigurer = (this.webSocketConfigurer != null ?
this.webSocketConfigurer.andThen(webSocketConfigurer) : webSocketConfigurer);
}

@Override
public void setServletContext(ServletContext servletContext) {
JettyWebSocketServerContainer container = JettyWebSocketServerContainer.getContainer(servletContext);
if (container != null && this.webSocketConfigurer != null) {
this.webSocketConfigurer.accept(container);
}
}


@Override
public Mono<Void> upgrade(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,22 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.eclipse.jetty.ee10.websocket.server.JettyWebSocketCreator;
import org.eclipse.jetty.ee10.websocket.server.JettyWebSocketServerContainer;
import org.eclipse.jetty.websocket.api.Configurable;

import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.http.server.ServletServerHttpResponse;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.socket.WebSocketExtension;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.adapter.jetty.JettyWebSocketHandlerAdapter;
Expand All @@ -47,11 +50,15 @@
* @author Rossen Stoyanchev
* @since 5.3.4
*/
public class JettyRequestUpgradeStrategy implements RequestUpgradeStrategy {
public class JettyRequestUpgradeStrategy implements RequestUpgradeStrategy, ServletContextAware {

private static final String[] SUPPORTED_VERSIONS = new String[] {"13"};


@Nullable
private Consumer<Configurable> webSocketConfigurer;


@Override
public String[] getSupportedVersions() {
return SUPPORTED_VERSIONS;
Expand All @@ -62,6 +69,24 @@ public List<WebSocketExtension> getSupportedExtensions(ServerHttpRequest request
return Collections.emptyList();
}

/**
* Add a callback to configure WebSocket server parameters on
* {@link JettyWebSocketServerContainer}.
* @since 6.1.0
*/
public void addWebSocketConfigurer(Consumer<Configurable> webSocketConfigurer) {
this.webSocketConfigurer = (this.webSocketConfigurer != null ?
this.webSocketConfigurer.andThen(webSocketConfigurer) : webSocketConfigurer);
}

@Override
public void setServletContext(ServletContext servletContext) {
JettyWebSocketServerContainer container = JettyWebSocketServerContainer.getContainer(servletContext);
if (container != null && this.webSocketConfigurer != null) {
this.webSocketConfigurer.accept(container);
}
}


@Override
public void upgrade(ServerHttpRequest request, ServerHttpResponse response,
Expand Down

0 comments on commit 1f8913a

Please sign in to comment.