JSR-356 - fixing Session.setMax(Text|Binary)MessageBufferSize

This commit is contained in:
Joakim Erdfelt 2013-07-31 09:52:51 -07:00
parent 0833d66dc6
commit 8a2ccdf8ae
8 changed files with 168 additions and 13 deletions

View File

@ -333,6 +333,7 @@ public class JsrSession extends WebSocketSession implements javax.websocket.Sess
@Override
public void setMaxBinaryMessageBufferSize(int length)
{
getPolicy().setMaxBinaryMessageSize(length);
getPolicy().setMaxBinaryMessageBufferSize(length);
}
@ -345,6 +346,7 @@ public class JsrSession extends WebSocketSession implements javax.websocket.Sess
@Override
public void setMaxTextMessageBufferSize(int length)
{
getPolicy().setMaxTextMessageSize(length);
getPolicy().setMaxTextMessageBufferSize(length);
}

View File

@ -60,6 +60,7 @@ public class WebSocketConfiguration extends AbstractConfiguration
// Create the Jetty ServerContainer implementation
ServerContainer jettyContainer = new ServerContainer(filter);
filter.setWebSocketServerFactoryListener(jettyContainer);
context.addBean(jettyContainer,true);
// Store a reference to the ServerContainer per javax.websocket spec 1.0 final section 6.4 Programmatic Server Deployment
context.setAttribute(javax.websocket.server.ServerContainer.class.getName(),jettyContainer);

View File

@ -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.LargeEchoSocket;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
/**
* Test Echo of Large messages, targeting the {@link javax.websocket.Session#setMaxTextMessageBufferSize(int)} functionality
*/
public class LargeAnnotatedTest
{
@Rule
public TestingDir testdir = new TestingDir();
@Test
public void testEcho() throws Exception
{
WSServer wsb = new WSServer(testdir,"app");
wsb.createWebInf();
wsb.copyEndpoint(LargeEchoSocket.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<Session> 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<String> msgs = clientEcho.awaitMessages(1);
Assert.assertEquals("Expected message",msg,msgs.poll());
}
finally
{
client.stop();
}
}
finally
{
wsb.stop();
}
}
}

View File

@ -0,0 +1,47 @@
//
// ========================================================================
// 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.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
/**
* Annotated echo socket
*/
@ServerEndpoint(value = "/echo/large")
public class LargeEchoSocket
{
private Session session;
@OnOpen
public void open(Session session)
{
this.session = session;
this.session.setMaxTextMessageBufferSize(128 * 1024);
}
@OnMessage
public void echo(String msg)
{
// reply with echo
session.getAsyncRemote().sendText(msg);
}
}

View File

@ -364,4 +364,21 @@ public class WebSocketPolicy
this.maxTextMessageSize = size;
}
@Override
public String toString()
{
StringBuilder builder = new StringBuilder();
builder.append("WebSocketPolicy@").append(Integer.toHexString(hashCode()));
builder.append("[behavior=").append(behavior);
builder.append(",maxTextMessageSize=").append(maxTextMessageSize);
builder.append(",maxTextMessageBufferSize=").append(maxTextMessageBufferSize);
builder.append(",maxBinaryMessageSize=").append(maxBinaryMessageSize);
builder.append(",maxBinaryMessageBufferSize=").append(maxBinaryMessageBufferSize);
builder.append(",asyncWriteTimeout=").append(asyncWriteTimeout);
builder.append(",idleTimeout=").append(idleTimeout);
builder.append(",inputBufferSize=").append(inputBufferSize);
builder.append("]");
return builder.toString();
}
}

View File

@ -91,7 +91,11 @@ public class Parser
private void assertSanePayloadLength(long len)
{
LOG.debug("Payload Length: " + len);
if (LOG.isDebugEnabled())
{
LOG.debug("Payload Length: {} - {}",len,this);
}
// Since we use ByteBuffer so often, having lengths over Integer.MAX_VALUE is really impossible.
if (len > Integer.MAX_VALUE)
{
@ -602,7 +606,8 @@ public class Parser
public String toString()
{
StringBuilder builder = new StringBuilder();
builder.append("Parser[");
builder.append("Parser@").append(Integer.toHexString(hashCode()));
builder.append("[");
if (incomingFramesHandler == null)
{
builder.append("NO_HANDLER");
@ -611,14 +616,11 @@ public class Parser
{
builder.append(incomingFramesHandler.getClass().getSimpleName());
}
builder.append(",s=");
builder.append(state);
builder.append(",c=");
builder.append(cursor);
builder.append(",len=");
builder.append(payloadLength);
builder.append(",f=");
builder.append(frame);
builder.append(",s=").append(state);
builder.append(",c=").append(cursor);
builder.append(",len=").append(payloadLength);
builder.append(",f=").append(frame);
builder.append(",p=").append(policy);
builder.append("]");
return builder.toString();
}

View File

@ -516,7 +516,7 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
// Setup Session
WebSocketSession session = createSession(request.getRequestURI(),driver,connection);
session.setPolicy(getPolicy().clonePolicy());
session.setPolicy(driver.getPolicy());
session.setUpgradeRequest(request);
response.setExtensions(extensionStack.getNegotiatedExtensions());
session.setUpgradeResponse(response);

View File

@ -76,8 +76,6 @@ public class WebSocketUpgradeFilter implements Filter, MappedWebSocketCreator, D
return;
}
LOG.debug("doFilter({})",request);
if ((request instanceof HttpServletRequest) && (response instanceof HttpServletResponse))
{
HttpServletRequest httpreq = (HttpServletRequest)request;