398105 - Clean up WebSocketPolicy

This commit is contained in:
Joakim Erdfelt 2013-01-14 14:43:11 -07:00
parent 0e617a091a
commit eff1262e49
7 changed files with 15 additions and 71 deletions

View File

@ -18,12 +18,13 @@
package org.eclipse.jetty.websocket.api;
/**
* Settings for WebSocket operations.
*/
public class WebSocketPolicy
{
private static final int KB = 1024;
public static WebSocketPolicy newClientPolicy()
{
return new WebSocketPolicy(WebSocketBehavior.CLIENT);
@ -34,45 +35,26 @@ public class WebSocketPolicy
return new WebSocketPolicy(WebSocketBehavior.SERVER);
}
/**
* Automatically fragment large frames.
* <p>
* If frames are encountered at size larger than {@link #maxPayloadSize} then they are automatically fragmented into pieces fitting within the
* maxPayloadSize.
* <p>
* Default: false
*/
private boolean autoFragment = false;
/**
* The maximum allowed payload size (validated in both directions)
* <p>
* Default: 65536 (64K)
*/
private int maxPayloadSize = 65536;
private int maxPayloadSize = 64 * KB;
/**
* The maximum size of a text message during parsing/generating.
* <p>
* Default: 16384 (16 K)
*/
private int maxTextMessageSize = 16384;
private int maxTextMessageSize = 64 * KB;
/**
* The maximum size of a binary message during parsing/generating.
* <p>
* Default: -1 (no validation)
*/
private int maxBinaryMessageSize = -1;
/**
* Maximum Message Buffer size, which is also the max frame byte size.
* <p>
* Default: 65536 (64 K)
*/
private int bufferSize = 65536;
// TODO: change bufferSize to windowSize for FrameBytes logic?
private int maxBinaryMessageSize = 64 * KB;
/**
* The time in ms (milliseconds) that a websocket may be idle before closing.
@ -127,9 +109,7 @@ public class WebSocketPolicy
public WebSocketPolicy clonePolicy()
{
WebSocketPolicy clone = new WebSocketPolicy(this.behavior);
clone.autoFragment = this.autoFragment;
clone.idleTimeout = this.idleTimeout;
clone.bufferSize = this.bufferSize;
clone.maxPayloadSize = this.maxPayloadSize;
clone.maxBinaryMessageSize = this.maxBinaryMessageSize;
clone.maxTextMessageSize = this.maxTextMessageSize;
@ -141,11 +121,6 @@ public class WebSocketPolicy
return behavior;
}
public int getBufferSize()
{
return bufferSize;
}
public int getIdleTimeout()
{
return idleTimeout;
@ -166,21 +141,6 @@ public class WebSocketPolicy
return maxTextMessageSize;
}
public boolean isAutoFragment()
{
return autoFragment;
}
public void setAutoFragment(boolean autoFragment)
{
this.autoFragment = autoFragment;
}
public void setBufferSize(int bufferSize)
{
this.bufferSize = bufferSize;
}
public void setIdleTimeout(int idleTimeout)
{
this.idleTimeout = idleTimeout;
@ -193,9 +153,9 @@ public class WebSocketPolicy
public void setMaxPayloadSize(int maxPayloadSize)
{
if (maxPayloadSize < bufferSize)
if (maxPayloadSize < 0)
{
throw new IllegalStateException("Cannot have payload size be smaller than buffer size");
throw new IllegalStateException("Cannot have payload size be a negative number");
}
this.maxPayloadSize = maxPayloadSize;
}
@ -204,5 +164,4 @@ public class WebSocketPolicy
{
this.maxTextMessageSize = maxTextMessageSize;
}
}

View File

@ -176,7 +176,6 @@ public class WebSocketClientTest
try
{
int bufferSize = 512;
factSmall.getPolicy().setBufferSize(512);
TrackingSocket wsocket = new TrackingSocket();
WebSocketClient client = factSmall.newWebSocketClient(wsocket);

View File

@ -49,10 +49,6 @@ public class AnnotatedEventDriver extends EventDriver
WebSocket anno = websocket.getClass().getAnnotation(WebSocket.class);
// Setup the policy
if (anno.maxBufferSize() > 0)
{
this.policy.setBufferSize(anno.maxBufferSize());
}
if (anno.maxBinarySize() > 0)
{
this.policy.setMaxBinaryMessageSize(anno.maxBinarySize());

View File

@ -175,7 +175,6 @@ public class RFC6455ExamplesParserTest
buf.flip();
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
policy.setBufferSize(80000);
Parser parser = new UnitParser(policy);
IncomingFramesCapture capture = new IncomingFramesCapture();
parser.setIncomingFramesHandler(capture);

View File

@ -38,7 +38,6 @@ public class TextPayloadParserTest
{
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
// Artificially small buffer/payload
policy.setBufferSize(512);
policy.setMaxPayloadSize(1024);
byte utf[] = new byte[2048];
Arrays.fill(utf,(byte)'a');
@ -89,7 +88,6 @@ public class TextPayloadParserTest
buf.flip();
WebSocketPolicy policy = WebSocketPolicy.newServerPolicy();
policy.setBufferSize(100000);
policy.setMaxPayloadSize(100000);
Parser parser = new UnitParser(policy);
IncomingFramesCapture capture = new IncomingFramesCapture();

View File

@ -36,7 +36,6 @@ public class ABServlet extends WebSocketServlet
// Test cases 9.x uses BIG frame sizes, let policy handle them.
int bigFrameSize = 20 * MBYTE;
factory.getPolicy().setBufferSize(bigFrameSize);
factory.getPolicy().setMaxPayloadSize(bigFrameSize);
factory.getPolicy().setMaxTextMessageSize(bigFrameSize);
factory.getPolicy().setMaxBinaryMessageSize(bigFrameSize);

View File

@ -66,10 +66,6 @@ import org.eclipse.jetty.websocket.api.annotations.WebSocket;
* basis.
*
* <dl>
* <dt>bufferSize</dt>
* <dd>can be used to set the buffer size, which is also the max frame byte size<br>
* <i>Default: 8192</i></dd>
*
* <dt>maxIdleTime</dt>
* <dd>set the time in ms that a websocket may be idle before closing<br>
* <i>Default:</i></dd>
@ -104,12 +100,7 @@ public abstract class WebSocketServlet extends HttpServlet
{
try
{
String bs = getInitParameter("bufferSize");
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
if (bs != null)
{
policy.setBufferSize(Integer.parseInt(bs));
}
String max = getInitParameter("maxIdleTime");
if (max != null)
@ -129,15 +120,18 @@ public abstract class WebSocketServlet extends HttpServlet
policy.setMaxBinaryMessageSize(Integer.parseInt(max));
}
WebSocketServletFactory baseFactory ;
WebSocketServletFactory baseFactory;
Iterator<WebSocketServletFactory> factories = ServiceLoader.load(WebSocketServletFactory.class).iterator();
if (factories.hasNext())
baseFactory=factories.next();
{
baseFactory = factories.next();
}
else
{
Class<WebSocketServletFactory> wssf= (Class<WebSocketServletFactory>)getServletContext().getClass().getClassLoader().loadClass("org.eclipse.jetty.websocket.server.WebSocketServerFactory");
baseFactory=wssf.newInstance();
Class<WebSocketServletFactory> wssf = (Class<WebSocketServletFactory>)getServletContext().getClass().getClassLoader()
.loadClass("org.eclipse.jetty.websocket.server.WebSocketServerFactory");
baseFactory = wssf.newInstance();
}
factory = baseFactory.createFactory(policy);