diff --git a/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/ClientContainer.java b/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/ClientContainer.java index 054f077ed17..c6650c2434e 100644 --- a/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/ClientContainer.java +++ b/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/ClientContainer.java @@ -306,10 +306,10 @@ public class ClientContainer extends ContainerLifeCycle implements WebSocketCont } @Override - public void setAsyncSendTimeout(long timeoutmillis) + public void setAsyncSendTimeout(long ms) { - client.setMaxIdleTimeout(timeoutmillis); - client.setAsyncWriteTimeout(timeoutmillis); + client.setMaxIdleTimeout(ms); + client.setAsyncWriteTimeout(ms); } @Override @@ -320,9 +320,9 @@ public class ClientContainer extends ContainerLifeCycle implements WebSocketCont } @Override - public void setDefaultMaxSessionIdleTimeout(long timeout) + public void setDefaultMaxSessionIdleTimeout(long ms) { - client.setMaxIdleTimeout(timeout); + client.setMaxIdleTimeout(ms); } @Override diff --git a/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/ServerContainer.java b/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/ServerContainer.java index a215a184333..f87c41660d7 100644 --- a/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/ServerContainer.java +++ b/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/ServerContainer.java @@ -138,4 +138,54 @@ public class ServerContainer extends ClientContainer implements javax.websocket. return metadata; } } + + @Override + public long getDefaultAsyncSendTimeout() + { + return webSocketServerFactory.getPolicy().getAsyncWriteTimeout(); + } + + @Override + public int getDefaultMaxBinaryMessageBufferSize() + { + return webSocketServerFactory.getPolicy().getMaxBinaryMessageBufferSize(); + } + + @Override + public long getDefaultMaxSessionIdleTimeout() + { + return webSocketServerFactory.getPolicy().getIdleTimeout(); + } + + @Override + public int getDefaultMaxTextMessageBufferSize() + { + return webSocketServerFactory.getPolicy().getMaxTextMessageBufferSize(); + } + + @Override + public void setAsyncSendTimeout(long ms) + { + webSocketServerFactory.getPolicy().setAsyncWriteTimeout(ms); + } + + @Override + public void setDefaultMaxBinaryMessageBufferSize(int max) + { + webSocketServerFactory.getPolicy().setMaxBinaryMessageSize(max); + webSocketServerFactory.getPolicy().setMaxBinaryMessageBufferSize(max); + } + + @Override + public void setDefaultMaxSessionIdleTimeout(long ms) + { + webSocketServerFactory.getPolicy().setIdleTimeout(ms); + } + + @Override + public void setDefaultMaxTextMessageBufferSize(int max) + { + webSocketServerFactory.getPolicy().setMaxTextMessageSize(max); + webSocketServerFactory.getPolicy().setMaxTextMessageBufferSize(max); + } } diff --git a/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/WebSocketConfiguration.java b/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/WebSocketConfiguration.java index b9c3e6ed12b..4481f9baeb9 100644 --- a/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/WebSocketConfiguration.java +++ b/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/WebSocketConfiguration.java @@ -30,6 +30,8 @@ import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.webapp.AbstractConfiguration; import org.eclipse.jetty.webapp.Configuration; import org.eclipse.jetty.webapp.WebAppContext; +import org.eclipse.jetty.websocket.api.WebSocketBehavior; +import org.eclipse.jetty.websocket.api.WebSocketPolicy; import org.eclipse.jetty.websocket.jsr356.server.deploy.DiscoveredEndpoints; import org.eclipse.jetty.websocket.jsr356.server.deploy.ServerEndpointAnnotationHandler; import org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter; @@ -43,7 +45,9 @@ public class WebSocketConfiguration extends AbstractConfiguration public static ServerContainer configureContext(ServletContextHandler context) { - WebSocketUpgradeFilter filter = new WebSocketUpgradeFilter(); + WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER); + + WebSocketUpgradeFilter filter = new WebSocketUpgradeFilter(policy); FilterHolder fholder = new FilterHolder(filter); fholder.setName("Jetty_WebSocketUpgradeFilter"); fholder.setDisplayName("WebSocket Upgrade Filter"); diff --git a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/LargeAnnotatedTest.java b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/LargeAnnotatedTest.java index 45c460dffd7..8fe0fc218d6 100644 --- a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/LargeAnnotatedTest.java +++ b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/LargeAnnotatedTest.java @@ -29,7 +29,7 @@ import org.eclipse.jetty.util.StringUtil; import org.eclipse.jetty.webapp.WebAppContext; import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.client.WebSocketClient; -import org.eclipse.jetty.websocket.jsr356.server.samples.echo.LargeEchoSocket; +import org.eclipse.jetty.websocket.jsr356.server.samples.echo.LargeEchoConfiguredSocket; import org.junit.Assert; import org.junit.Rule; import org.junit.Test; @@ -47,7 +47,7 @@ public class LargeAnnotatedTest { WSServer wsb = new WSServer(testdir,"app"); wsb.createWebInf(); - wsb.copyEndpoint(LargeEchoSocket.class); + wsb.copyEndpoint(LargeEchoConfiguredSocket.class); try { diff --git a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/LargeContainerTest.java b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/LargeContainerTest.java new file mode 100644 index 00000000000..fb1564dd259 --- /dev/null +++ b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/LargeContainerTest.java @@ -0,0 +1,88 @@ +// +// ======================================================================== +// Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + +package org.eclipse.jetty.websocket.jsr356.server; + +import java.net.URI; +import java.util.Arrays; +import java.util.Queue; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; + +import org.eclipse.jetty.toolchain.test.TestingDir; +import org.eclipse.jetty.util.StringUtil; +import org.eclipse.jetty.webapp.WebAppContext; +import org.eclipse.jetty.websocket.api.Session; +import org.eclipse.jetty.websocket.client.WebSocketClient; +import org.eclipse.jetty.websocket.jsr356.server.samples.echo.LargeEchoDefaultSocket; +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; + +/** + * Test Echo of Large messages, targeting the {@link javax.websocket.WebSocketContainer#setDefaultMaxTextMessageBufferSize(int)} functionality + */ +public class LargeContainerTest +{ + @Rule + public TestingDir testdir = new TestingDir(); + + @Test + public void testEcho() throws Exception + { + WSServer wsb = new WSServer(testdir,"app"); + wsb.copyWebInf("large-echo-config-web.xml"); + wsb.copyEndpoint(LargeEchoDefaultSocket.class); + + try + { + wsb.start(); + URI uri = wsb.getServerBaseURI(); + + WebAppContext webapp = wsb.createWebAppContext(); + wsb.deployWebapp(webapp); + // wsb.dump(); + + WebSocketClient client = new WebSocketClient(); + try + { + client.getPolicy().setMaxTextMessageSize(128*1024); + client.start(); + JettyEchoSocket clientEcho = new JettyEchoSocket(); + Future foo = client.connect(clientEcho,uri.resolve("echo/large")); + // wait for connect + foo.get(1,TimeUnit.SECONDS); + // The message size should be bigger than default, but smaller than the limit that LargeEchoSocket specifies + byte txt[] = new byte[100 * 1024]; + Arrays.fill(txt,(byte)'o'); + String msg = new String(txt,StringUtil.__UTF8_CHARSET); + clientEcho.sendMessage(msg); + Queue msgs = clientEcho.awaitMessages(1); + Assert.assertEquals("Expected message",msg,msgs.poll()); + } + finally + { + client.stop(); + } + } + finally + { + wsb.stop(); + } + } +} diff --git a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/samples/echo/LargeEchoSocket.java b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/samples/echo/LargeEchoConfiguredSocket.java similarity index 97% rename from jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/samples/echo/LargeEchoSocket.java rename to jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/samples/echo/LargeEchoConfiguredSocket.java index 9bbfa204eb6..f0acfb468d4 100644 --- a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/samples/echo/LargeEchoSocket.java +++ b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/samples/echo/LargeEchoConfiguredSocket.java @@ -27,7 +27,7 @@ import javax.websocket.server.ServerEndpoint; * Annotated echo socket */ @ServerEndpoint(value = "/echo/large") -public class LargeEchoSocket +public class LargeEchoConfiguredSocket { private Session session; diff --git a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/samples/echo/LargeEchoContextListener.java b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/samples/echo/LargeEchoContextListener.java new file mode 100644 index 00000000000..9a73823b441 --- /dev/null +++ b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/samples/echo/LargeEchoContextListener.java @@ -0,0 +1,42 @@ +// +// ======================================================================== +// Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + +package org.eclipse.jetty.websocket.jsr356.server.samples.echo; + +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; +import javax.websocket.server.ServerContainer; + +/** + * Configure the Large Text Message Size via the Container + */ +public class LargeEchoContextListener implements ServletContextListener +{ + @Override + public void contextDestroyed(ServletContextEvent sce) + { + /* do nothing */ + } + + @Override + public void contextInitialized(ServletContextEvent sce) + { + ServerContainer container = (ServerContainer)sce.getServletContext().getAttribute(ServerContainer.class.getName()); + container.setDefaultMaxTextMessageBufferSize(128 * 1024); + } +} diff --git a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/samples/echo/LargeEchoDefaultSocket.java b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/samples/echo/LargeEchoDefaultSocket.java new file mode 100644 index 00000000000..851a1ac2bbf --- /dev/null +++ b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/samples/echo/LargeEchoDefaultSocket.java @@ -0,0 +1,38 @@ +// +// ======================================================================== +// Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + +package org.eclipse.jetty.websocket.jsr356.server.samples.echo; + +import javax.websocket.OnMessage; +import javax.websocket.Session; +import javax.websocket.WebSocketContainer; +import javax.websocket.server.ServerEndpoint; + +/** + * Annotated echo socket (default behavior as defined from {@link WebSocketContainer#setDefaultMaxTextMessageBufferSize(int)}) + */ +@ServerEndpoint(value = "/echo/large") +public class LargeEchoDefaultSocket +{ + @OnMessage + public void echo(Session session, String msg) + { + // reply with echo + session.getAsyncRemote().sendText(msg); + } +} diff --git a/jetty-websocket/javax-websocket-server-impl/src/test/resources/large-echo-config-web.xml b/jetty-websocket/javax-websocket-server-impl/src/test/resources/large-echo-config-web.xml new file mode 100644 index 00000000000..08e696f8760 --- /dev/null +++ b/jetty-websocket/javax-websocket-server-impl/src/test/resources/large-echo-config-web.xml @@ -0,0 +1,12 @@ + + + + + org.eclipse.jetty.websocket.jsr356.server.samples.echo.LargeEchoContextListener + + \ No newline at end of file diff --git a/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClient.java b/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClient.java index 2db932e2c83..ab2b0bb08a0 100644 --- a/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClient.java +++ b/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClient.java @@ -427,16 +427,16 @@ public class WebSocketClient extends ContainerLifeCycle /** * Set the timeout for connecting to the remote server. * - * @param timeoutMilliseconds + * @param ms * the timeout in milliseconds */ - public void setConnectTimeout(long timeoutMilliseconds) + public void setConnectTimeout(long ms) { - if (timeoutMilliseconds < 0) + if (ms < 0) { throw new IllegalStateException("Connect Timeout cannot be negative"); } - this.connectTimeout = timeoutMilliseconds; + this.connectTimeout = ms; } public void setCookieStore(CookieStore cookieStore) @@ -469,12 +469,12 @@ public class WebSocketClient extends ContainerLifeCycle *

* Existing connections will not have their max idle timeout adjusted. * - * @param milliseconds + * @param ms * the timeout in milliseconds */ - public void setMaxIdleTimeout(long milliseconds) + public void setMaxIdleTimeout(long ms) { - this.policy.setIdleTimeout(milliseconds); + this.policy.setIdleTimeout(ms); } public void setMaxTextMessageBufferSize(int max) diff --git a/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/io/UpgradeConnection.java b/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/io/UpgradeConnection.java index c48a3bd4e87..2ab4e4bcbbd 100644 --- a/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/io/UpgradeConnection.java +++ b/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/io/UpgradeConnection.java @@ -227,7 +227,7 @@ public class UpgradeConnection extends AbstractConnection // Initialize / Negotiate Extensions EventDriver websocket = connectPromise.getDriver(); - WebSocketPolicy policy = connectPromise.getClient().getPolicy(); + WebSocketPolicy policy = websocket.getPolicy(); SessionFactory sessionFactory = connectPromise.getClient().getSessionFactory(); WebSocketSession session = sessionFactory.createSession(request.getRequestURI(),websocket,connection); diff --git a/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java b/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java index 65b07ceab33..47869d1bbef 100644 --- a/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java +++ b/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java @@ -23,7 +23,6 @@ import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.Collections; -import java.util.Enumeration; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -92,7 +91,7 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc private final Scheduler scheduler = new ScheduledExecutorScheduler(); private final Queue sessions = new ConcurrentLinkedQueue<>(); private final String supportedVersions; - private final WebSocketPolicy basePolicy; + private final WebSocketPolicy defaultPolicy; private final EventDriverFactory eventDriverFactory; private final WebSocketExtensionFactory extensionFactory; private List sessionFactories; @@ -116,9 +115,9 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc this.registeredSocketClasses = new ArrayList<>(); - this.basePolicy = policy; - this.eventDriverFactory = new EventDriverFactory(basePolicy); - this.extensionFactory = new WebSocketExtensionFactory(basePolicy,bufferPool); + this.defaultPolicy = policy; + this.eventDriverFactory = new EventDriverFactory(defaultPolicy); + this.extensionFactory = new WebSocketExtensionFactory(defaultPolicy,bufferPool); this.sessionFactories = new ArrayList<>(); this.sessionFactories.add(new WebSocketSessionFactory()); this.creator = this; @@ -307,7 +306,7 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc @Override public WebSocketPolicy getPolicy() { - return basePolicy; + return defaultPolicy; } @Override diff --git a/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketUpgradeFilter.java b/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketUpgradeFilter.java index 92bdf1df831..1d63ad2438d 100644 --- a/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketUpgradeFilter.java +++ b/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketUpgradeFilter.java @@ -35,7 +35,6 @@ import org.eclipse.jetty.util.component.ContainerLifeCycle; import org.eclipse.jetty.util.component.Dumpable; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; -import org.eclipse.jetty.websocket.api.WebSocketBehavior; import org.eclipse.jetty.websocket.api.WebSocketPolicy; import org.eclipse.jetty.websocket.server.pathmap.PathMappings; import org.eclipse.jetty.websocket.server.pathmap.PathMappings.MappedResource; @@ -52,9 +51,8 @@ public class WebSocketUpgradeFilter extends ContainerLifeCycle implements Filter private final WebSocketServerFactory factory; private PathMappings pathmap = new PathMappings<>(); - public WebSocketUpgradeFilter() + public WebSocketUpgradeFilter(WebSocketPolicy policy) { - WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER); factory = new WebSocketServerFactory(policy); addBean(factory,true); }