Skip to content

Commit

Permalink
Migrate to jetty 10 (#747)
Browse files Browse the repository at this point in the history
* update pom

* fix JettyServer

* fix websocket
  • Loading branch information
goekay authored Jan 24, 2022
1 parent 05a7602 commit 7a41a3f
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 24 deletions.
10 changes: 8 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<spring.version>5.3.15</spring.version>
<spring.security.version>5.6.1</spring.security.version>
<mysql.jdbc.version>8.0.28</mysql.jdbc.version>
<jetty.version>9.4.44.v20210927</jetty.version>
<jetty.version>10.0.7</jetty.version>
<lombok.version>1.18.22</lombok.version>
<jackson.version>2.13.1</jackson.version>

Expand Down Expand Up @@ -646,7 +646,7 @@
</dependency>
<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>websocket-server</artifactId>
<artifactId>websocket-jetty-server</artifactId>
<version>${jetty.version}</version>
</dependency>

Expand All @@ -673,6 +673,12 @@
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>websocket-jetty-client</artifactId>
<version>${jetty.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/de/rwth/idsg/steve/JettyServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
public class JettyServer {

private Server server;
private SteveAppContext steveAppContext;

private static final int MIN_THREADS = 4;
private static final int MAX_THREADS = 50;
Expand Down Expand Up @@ -109,7 +110,7 @@ private void prepare() {
server.addConnector(httpsConnector(httpConfig));
}

SteveAppContext steveAppContext = new SteveAppContext();
steveAppContext = new SteveAppContext();
server.setHandler(steveAppContext.getHandlers());
}

Expand All @@ -125,7 +126,7 @@ private ServerConnector httpConnector(HttpConfiguration httpConfig) {
private ServerConnector httpsConnector(HttpConfiguration httpConfig) {
// === jetty-https.xml ===
// SSL Context Factory
SslContextFactory sslContextFactory = new SslContextFactory.Server();
SslContextFactory.Server sslContextFactory = new SslContextFactory.Server();
sslContextFactory.setKeyStorePath(CONFIG.getJetty().getKeyStorePath());
sslContextFactory.setKeyStorePassword(CONFIG.getJetty().getKeyStorePassword());
sslContextFactory.setKeyManagerPassword(CONFIG.getJetty().getKeyStorePassword());
Expand Down Expand Up @@ -160,6 +161,7 @@ public void start() throws Exception {

if (server != null) {
server.start();
steveAppContext.configureWebSocket();
populateEndpointInfo();
}
}
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/de/rwth/idsg/steve/SteveAppContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.websocket.core.WebSocketConstants;
import org.eclipse.jetty.websocket.server.JettyWebSocketServerContainer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
Expand All @@ -49,6 +51,8 @@
import java.util.List;

import static de.rwth.idsg.steve.SteveConfiguration.CONFIG;
import static de.rwth.idsg.steve.config.WebSocketConfiguration.IDLE_TIMEOUT;
import static de.rwth.idsg.steve.config.WebSocketConfiguration.MAX_MSG_SIZE;

/**
* @author Sevket Goekay <sevketgokay@gmail.com>
Expand All @@ -57,6 +61,7 @@
public class SteveAppContext {

private final AnnotationConfigWebApplicationContext springContext;
private WebAppContext ctx;

public SteveAppContext() {
springContext = new AnnotationConfigWebApplicationContext();
Expand All @@ -73,6 +78,17 @@ public HandlerCollection getHandlers() {
return handlerList;
}

/**
* Otherwise, defaults come from {@link WebSocketConstants}
*/
public void configureWebSocket() {
JettyWebSocketServerContainer container = JettyWebSocketServerContainer.getContainer(ctx.getServletContext());
container.setInputBufferSize(MAX_MSG_SIZE);
container.setOutputBufferSize(MAX_MSG_SIZE);
container.setMaxTextMessageSize(MAX_MSG_SIZE);
container.setIdleTimeout(IDLE_TIMEOUT);
}

private Handler getWebApp() {
if (CONFIG.getJetty().isGzipEnabled()) {
return enableGzip(initWebApp());
Expand All @@ -93,7 +109,7 @@ private Handler enableGzip(WebAppContext ctx) {
}

private WebAppContext initWebApp() {
WebAppContext ctx = new WebAppContext();
ctx = new WebAppContext();
ctx.setContextPath(CONFIG.getContextPath());
ctx.setResourceBase(getWebAppURIAsString());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@
import de.rwth.idsg.steve.ocpp.ws.ocpp16.Ocpp16WebSocketEndpoint;
import de.rwth.idsg.steve.service.ChargePointHelperService;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.jetty.websocket.api.WebSocketBehavior;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.DefaultHandshakeHandler;

import java.time.Duration;
import java.util.List;
import java.util.concurrent.TimeUnit;

Expand All @@ -54,20 +53,15 @@ public class WebSocketConfiguration implements WebSocketConfigurer {
@Autowired private Ocpp16WebSocketEndpoint ocpp16WebSocketEndpoint;

public static final long PING_INTERVAL = TimeUnit.MINUTES.toMinutes(15);
private static final long IDLE_TIMEOUT = TimeUnit.HOURS.toMillis(2);
public static final Duration IDLE_TIMEOUT = Duration.ofHours(2);
public static final int MAX_MSG_SIZE = 8_388_608; // 8 MB for max message size

@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
policy.setMaxTextMessageBufferSize(MAX_MSG_SIZE);
policy.setMaxTextMessageSize(MAX_MSG_SIZE);
policy.setIdleTimeout(IDLE_TIMEOUT);

List<AbstractWebSocketEndpoint> endpoints = getEndpoints();
String[] protocols = endpoints.stream().map(e -> e.getVersion().getValue()).toArray(String[]::new);

OcppWebSocketUpgrader upgradeStrategy = new OcppWebSocketUpgrader(policy, endpoints, chargePointHelperService);
OcppWebSocketUpgrader upgradeStrategy = new OcppWebSocketUpgrader(endpoints, chargePointHelperService);

DefaultHandshakeHandler handler = new DefaultHandshakeHandler(upgradeStrategy);
handler.setSupportedProtocols(protocols);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
package de.rwth.idsg.steve.ocpp.ws;

import de.rwth.idsg.steve.service.ChargePointHelperService;
import lombok.RequiredArgsConstructor;
import ocpp.cs._2015._10.RegistrationStatus;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.jetbrains.annotations.Nullable;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.socket.WebSocketExtension;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeFailureException;
import org.springframework.web.socket.server.jetty.JettyRequestUpgradeStrategy;
import org.springframework.web.socket.server.jetty.Jetty10RequestUpgradeStrategy;

import java.security.Principal;
import java.util.List;
Expand All @@ -38,18 +38,12 @@
* @author Sevket Goekay <sevketgokay@gmail.com>
* @since 13.03.2015
*/
public class OcppWebSocketUpgrader extends JettyRequestUpgradeStrategy {
@RequiredArgsConstructor
public class OcppWebSocketUpgrader extends Jetty10RequestUpgradeStrategy {

private final List<AbstractWebSocketEndpoint> endpoints;
private final ChargePointHelperService chargePointHelperService;

public OcppWebSocketUpgrader(WebSocketPolicy policy, List<AbstractWebSocketEndpoint> endpoints,
ChargePointHelperService chargePointHelperService) {
super(policy);
this.endpoints = endpoints;
this.chargePointHelperService = chargePointHelperService;
}

@Override
public void upgrade(ServerHttpRequest request, ServerHttpResponse response,
String selectedProtocol, List<WebSocketExtension> selectedExtensions, Principal user,
Expand Down

0 comments on commit 7a41a3f

Please sign in to comment.