Extract configuration for WebSocket max message sizes

This commit is contained in:
Chris Eager
2021-08-13 14:29:13 -05:00
committed by Chris Eager
parent a398e2269c
commit 19f7b207b7
5 changed files with 89 additions and 52 deletions

View File

@@ -23,6 +23,7 @@ import org.whispersystems.websocket.auth.AuthenticationException;
import org.whispersystems.websocket.auth.WebSocketAuthenticator;
import org.whispersystems.websocket.auth.WebSocketAuthenticator.AuthenticationResult;
import org.whispersystems.websocket.auth.WebsocketAuthValueFactoryProvider;
import org.whispersystems.websocket.configuration.WebSocketConfiguration;
import org.whispersystems.websocket.session.WebSocketSessionContextValueFactoryProvider;
import org.whispersystems.websocket.setup.WebSocketEnvironment;
@@ -31,9 +32,11 @@ public class WebSocketResourceProviderFactory<T extends Principal> extends WebSo
private static final Logger logger = LoggerFactory.getLogger(WebSocketResourceProviderFactory.class);
private final WebSocketEnvironment<T> environment;
private final ApplicationHandler jerseyApplicationHandler;
private final ApplicationHandler jerseyApplicationHandler;
private final WebSocketConfiguration configuration;
public WebSocketResourceProviderFactory(WebSocketEnvironment<T> environment, Class<T> principalClass) {
public WebSocketResourceProviderFactory(WebSocketEnvironment<T> environment, Class<T> principalClass,
WebSocketConfiguration configuration) {
this.environment = environment;
environment.jersey().register(new WebSocketSessionContextValueFactoryProvider.Binder());
@@ -41,6 +44,8 @@ public class WebSocketResourceProviderFactory<T extends Principal> extends WebSo
environment.jersey().register(new JacksonMessageBodyProvider(environment.getObjectMapper()));
this.jerseyApplicationHandler = new ApplicationHandler(environment.jersey());
this.configuration = configuration;
}
@Override
@@ -79,9 +84,8 @@ public class WebSocketResourceProviderFactory<T extends Principal> extends WebSo
@Override
public void configure(WebSocketServletFactory factory) {
factory.setCreator(this);
// TODO extract to configuration
factory.getPolicy().setMaxBinaryMessageSize(512 * 1024);
factory.getPolicy().setMaxTextMessageSize(512 * 1024);
factory.getPolicy().setMaxBinaryMessageSize(configuration.getMaxBinaryMessageSize());
factory.getPolicy().setMaxTextMessageSize(configuration.getMaxTextMessageSize());
}
private String getRemoteAddress(ServletUpgradeRequest request) {

View File

@@ -5,10 +5,11 @@
package org.whispersystems.websocket.configuration;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.whispersystems.websocket.logging.WebsocketRequestLoggerFactory;
import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import org.whispersystems.websocket.logging.WebsocketRequestLoggerFactory;
public class WebSocketConfiguration {
@@ -17,7 +18,25 @@ public class WebSocketConfiguration {
@JsonProperty
private WebsocketRequestLoggerFactory requestLog = new WebsocketRequestLoggerFactory();
@Min(512 * 1024) // 512 KB
@Max(10 * 1024 * 1024) // 10 MB
@JsonProperty
private int maxBinaryMessageSize = 512 * 1024;
@Min(512 * 1024) // 512 KB
@Max(10 * 1024 * 1024) // 10 MB
@JsonProperty
private int maxTextMessageSize = 512 * 1024;
public WebsocketRequestLoggerFactory getRequestLog() {
return requestLog;
}
public int getMaxBinaryMessageSize() {
return maxBinaryMessageSize;
}
public int getMaxTextMessageSize() {
return maxTextMessageSize;
}
}