From 7019c1811711588ff1e28b0b4c704d8689c50e04 Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Tue, 14 May 2019 15:02:29 +1000 Subject: [PATCH] refactor of javax WebSocketContainer configuration the client and server subclasses of JavaxWebSocketContainer now share a common configuration instance which is used as the default configuration for both server and client endpoints to do this a setter was added for the configuration on the CoreClient Signed-off-by: Lachlan Roberts --- .../client/JavaxWebSocketClientContainer.java | 2 + .../javax/common/JavaxWebSocketContainer.java | 65 +++++++++----- .../server/JavaxWebSocketServerContainer.java | 84 +++---------------- .../core/client/WebSocketCoreClient.java | 21 +++-- 4 files changed, 72 insertions(+), 100 deletions(-) diff --git a/jetty-websocket/javax-websocket-client/src/main/java/org/eclipse/jetty/websocket/javax/client/JavaxWebSocketClientContainer.java b/jetty-websocket/javax-websocket-client/src/main/java/org/eclipse/jetty/websocket/javax/client/JavaxWebSocketClientContainer.java index c293ce34d53..4f9a7be0f22 100644 --- a/jetty-websocket/javax-websocket-client/src/main/java/org/eclipse/jetty/websocket/javax/client/JavaxWebSocketClientContainer.java +++ b/jetty-websocket/javax-websocket-client/src/main/java/org/eclipse/jetty/websocket/javax/client/JavaxWebSocketClientContainer.java @@ -114,6 +114,8 @@ public class JavaxWebSocketClientContainer extends JavaxWebSocketContainer imple addBean(coreClient,false); else addManaged(coreClient); + + coreClient.setCustomizer(defaultCustomizer); } return coreClient; diff --git a/jetty-websocket/javax-websocket-common/src/main/java/org/eclipse/jetty/websocket/javax/common/JavaxWebSocketContainer.java b/jetty-websocket/javax-websocket-common/src/main/java/org/eclipse/jetty/websocket/javax/common/JavaxWebSocketContainer.java index e7185b1fc05..08737530bff 100644 --- a/jetty-websocket/javax-websocket-common/src/main/java/org/eclipse/jetty/websocket/javax/common/JavaxWebSocketContainer.java +++ b/jetty-websocket/javax-websocket-common/src/main/java/org/eclipse/jetty/websocket/javax/common/JavaxWebSocketContainer.java @@ -18,6 +18,7 @@ package org.eclipse.jetty.websocket.javax.common; +import java.time.Duration; import java.util.ArrayList; import java.util.HashSet; import java.util.List; @@ -25,6 +26,7 @@ import java.util.Set; import java.util.concurrent.CompletableFuture; import java.util.concurrent.Executor; import java.util.function.Consumer; + import javax.websocket.Extension; import javax.websocket.Session; import javax.websocket.WebSocketContainer; @@ -34,16 +36,15 @@ import org.eclipse.jetty.util.DecoratedObjectFactory; import org.eclipse.jetty.util.component.ContainerLifeCycle; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; +import org.eclipse.jetty.websocket.core.FrameHandler; import org.eclipse.jetty.websocket.core.WebSocketExtensionRegistry; public abstract class JavaxWebSocketContainer extends ContainerLifeCycle implements javax.websocket.WebSocketContainer { private final static Logger LOG = Log.getLogger(JavaxWebSocketContainer.class); private final SessionTracker sessionTracker = new SessionTracker(); - private long defaultAsyncSendTimeout = -1; - private int defaultMaxBinaryMessageBufferSize = 64 * 1024; - private int defaultMaxTextMessageBufferSize = 64 * 1024; private List sessionListeners = new ArrayList<>(); + protected FrameHandler.ConfigurationCustomizer defaultCustomizer = new FrameHandler.ConfigurationCustomizer(); public JavaxWebSocketContainer() { @@ -53,40 +54,68 @@ public abstract class JavaxWebSocketContainer extends ContainerLifeCycle impleme public abstract ByteBufferPool getBufferPool(); + public abstract Executor getExecutor(); + + public abstract DecoratedObjectFactory getObjectFactory(); + + protected abstract WebSocketExtensionRegistry getExtensionRegistry(); + + protected abstract JavaxWebSocketFrameHandlerFactory getFrameHandlerFactory(); + @Override public long getDefaultAsyncSendTimeout() { - return this.defaultAsyncSendTimeout; + return defaultCustomizer.getWriteTimeout().toMillis(); } @Override public int getDefaultMaxBinaryMessageBufferSize() { - return this.defaultMaxBinaryMessageBufferSize; + long max = defaultCustomizer.getMaxBinaryMessageSize(); + if (max > (long)Integer.MAX_VALUE) + return Integer.MAX_VALUE; + return (int)max; } - public abstract DecoratedObjectFactory getObjectFactory(); - @Override - public void setDefaultMaxBinaryMessageBufferSize(int max) + public long getDefaultMaxSessionIdleTimeout() { - this.defaultMaxBinaryMessageBufferSize = max; + return defaultCustomizer.getIdleTimeout().toMillis(); } @Override public int getDefaultMaxTextMessageBufferSize() { - return this.defaultMaxTextMessageBufferSize; + long max = defaultCustomizer.getMaxTextMessageSize(); + if (max > (long)Integer.MAX_VALUE) + return Integer.MAX_VALUE; + return (int)max; + } + + @Override + public void setAsyncSendTimeout(long ms) + { + defaultCustomizer.setWriteTimeout(Duration.ofMillis(ms)); + } + + @Override + public void setDefaultMaxBinaryMessageBufferSize(int max) + { + defaultCustomizer.setMaxBinaryMessageSize(max); + } + + @Override + public void setDefaultMaxSessionIdleTimeout(long ms) + { + defaultCustomizer.setIdleTimeout(Duration.ofMillis(ms)); } @Override public void setDefaultMaxTextMessageBufferSize(int max) { - this.defaultMaxTextMessageBufferSize = max; + defaultCustomizer.setMaxTextMessageSize(max); } - public abstract Executor getExecutor(); - /** * {@inheritDoc} * @@ -122,16 +151,6 @@ public abstract class JavaxWebSocketContainer extends ContainerLifeCycle impleme return getFrameHandlerFactory().newJavaxWebSocketFrameHandler(websocketPojo, upgradeRequest, upgradeResponse, futureSession); } - @Override - public void setAsyncSendTimeout(long timeoutInMillis) - { - this.defaultAsyncSendTimeout = timeoutInMillis; - } - - protected abstract WebSocketExtensionRegistry getExtensionRegistry(); - - protected abstract JavaxWebSocketFrameHandlerFactory getFrameHandlerFactory(); - /** * Register a WebSocketSessionListener with the container * diff --git a/jetty-websocket/javax-websocket-server/src/main/java/org/eclipse/jetty/websocket/javax/server/JavaxWebSocketServerContainer.java b/jetty-websocket/javax-websocket-server/src/main/java/org/eclipse/jetty/websocket/javax/server/JavaxWebSocketServerContainer.java index 3c73c75a236..f8a874d8185 100644 --- a/jetty-websocket/javax-websocket-server/src/main/java/org/eclipse/jetty/websocket/javax/server/JavaxWebSocketServerContainer.java +++ b/jetty-websocket/javax-websocket-server/src/main/java/org/eclipse/jetty/websocket/javax/server/JavaxWebSocketServerContainer.java @@ -18,6 +18,18 @@ package org.eclipse.jetty.websocket.javax.server; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.Executor; + +import javax.servlet.ServletContext; +import javax.websocket.DeploymentException; +import javax.websocket.EndpointConfig; +import javax.websocket.WebSocketContainer; +import javax.websocket.server.ServerContainer; +import javax.websocket.server.ServerEndpoint; +import javax.websocket.server.ServerEndpointConfig; + import org.eclipse.jetty.client.HttpClient; import org.eclipse.jetty.http.pathmap.PathSpec; import org.eclipse.jetty.http.pathmap.UriTemplatePathSpec; @@ -29,7 +41,6 @@ import org.eclipse.jetty.util.annotation.ManagedObject; import org.eclipse.jetty.util.component.LifeCycle; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; -import org.eclipse.jetty.websocket.core.FrameHandler; import org.eclipse.jetty.websocket.core.WebSocketComponents; import org.eclipse.jetty.websocket.core.WebSocketException; import org.eclipse.jetty.websocket.core.WebSocketExtensionRegistry; @@ -40,19 +51,6 @@ import org.eclipse.jetty.websocket.javax.server.internal.JavaxWebSocketCreator; import org.eclipse.jetty.websocket.javax.server.internal.UndefinedServerEndpointConfig; import org.eclipse.jetty.websocket.servlet.WebSocketMapping; -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import javax.websocket.DeploymentException; -import javax.websocket.EndpointConfig; -import javax.websocket.WebSocketContainer; -import javax.websocket.server.ServerContainer; -import javax.websocket.server.ServerEndpoint; -import javax.websocket.server.ServerEndpointConfig; -import java.time.Duration; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.Executor; - @ManagedObject("JSR356 Server Container") public class JavaxWebSocketServerContainer extends JavaxWebSocketClientContainer @@ -123,8 +121,6 @@ public class JavaxWebSocketServerContainer private final WebSocketComponents webSocketComponents; private final JavaxWebSocketServerFrameHandlerFactory frameHandlerFactory; private final Executor executor; - private final FrameHandler.ConfigurationCustomizer customizer = new FrameHandler.ConfigurationCustomizer(); - private long asyncSendTimeout = -1; private List> deferredEndpointClasses; private List deferredEndpointConfigs; @@ -293,7 +289,7 @@ public class JavaxWebSocketServerContainer .getExtensionRegistry()); PathSpec pathSpec = new UriTemplatePathSpec(config.getPath()); - webSocketMapping.addMapping(pathSpec, creator, frameHandlerFactory, customizer); + webSocketMapping.addMapping(pathSpec, creator, frameHandlerFactory, defaultCustomizer); } @Override @@ -321,58 +317,4 @@ public class JavaxWebSocketServerContainer deferredEndpointConfigs.clear(); } } - - @Override - public long getDefaultAsyncSendTimeout() - { - return this.asyncSendTimeout; - } - - @Override - public int getDefaultMaxBinaryMessageBufferSize() - { - long max = customizer.getMaxBinaryMessageSize(); - if (max > (long)Integer.MAX_VALUE) - return Integer.MAX_VALUE; - return (int)max; - } - - @Override - public long getDefaultMaxSessionIdleTimeout() - { - return customizer.getIdleTimeout().toMillis(); - } - - @Override - public int getDefaultMaxTextMessageBufferSize() - { - long max = customizer.getMaxTextMessageSize(); - if (max > (long)Integer.MAX_VALUE) - return Integer.MAX_VALUE; - return (int)max; - } - - @Override - public void setAsyncSendTimeout(long ms) - { - this.asyncSendTimeout = ms; - } - - @Override - public void setDefaultMaxBinaryMessageBufferSize(int max) - { - customizer.setMaxBinaryMessageSize(max); - } - - @Override - public void setDefaultMaxSessionIdleTimeout(long ms) - { - customizer.setIdleTimeout(Duration.ofMillis(ms)); - } - - @Override - public void setDefaultMaxTextMessageBufferSize(int max) - { - customizer.setMaxTextMessageSize(max); - } } diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/core/client/WebSocketCoreClient.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/core/client/WebSocketCoreClient.java index 3a9a2c680fb..625a939410f 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/core/client/WebSocketCoreClient.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/core/client/WebSocketCoreClient.java @@ -40,7 +40,7 @@ public class WebSocketCoreClient extends ContainerLifeCycle implements FrameHand private final HttpClient httpClient; private WebSocketExtensionRegistry extensionRegistry; private DecoratedObjectFactory objectFactory; - private final FrameHandler.Customizer customizer; + private FrameHandler.ConfigurationCustomizer customizer; // TODO: Things to consider for inclusion in this class (or removal if they can be set elsewhere, like HttpClient) // - AsyncWrite Idle Timeout @@ -51,15 +51,15 @@ public class WebSocketCoreClient extends ContainerLifeCycle implements FrameHand public WebSocketCoreClient() { - this(null,null); + this(null, new FrameHandler.ConfigurationCustomizer()); } public WebSocketCoreClient(HttpClient httpClient) { - this(httpClient, null); + this(httpClient, new FrameHandler.ConfigurationCustomizer()); } - public WebSocketCoreClient(HttpClient httpClient, FrameHandler.Customizer customizer) + public WebSocketCoreClient(HttpClient httpClient, FrameHandler.ConfigurationCustomizer customizer) { if (httpClient == null) httpClient = Objects.requireNonNull(HttpClientProvider.get()); @@ -71,11 +71,20 @@ public class WebSocketCoreClient extends ContainerLifeCycle implements FrameHand addBean(httpClient); } + public FrameHandler.ConfigurationCustomizer getCustomizer() + { + return customizer; + } + + public void setCustomizer(FrameHandler.ConfigurationCustomizer customizer) + { + this.customizer = customizer; + } + @Override public void customize(FrameHandler.CoreSession session) { - if (customizer != null) - customizer.customize(session); + customizer.customize(session); } public CompletableFuture connect(FrameHandler frameHandler, URI wsUri) throws IOException