Fixing failed upgrade due to invalid request websocket version

This commit is contained in:
Joakim Erdfelt 2012-06-29 09:12:59 -07:00
parent e30e5976b4
commit 9de009c1b2
2 changed files with 37 additions and 5 deletions

View File

@ -36,6 +36,7 @@ import org.eclipse.jetty.websocket.annotations.WebSocket;
import org.eclipse.jetty.websocket.api.EventMethodsCache;
import org.eclipse.jetty.websocket.api.ExtensionConfig;
import org.eclipse.jetty.websocket.api.WebSocketEventDriver;
import org.eclipse.jetty.websocket.api.WebSocketException;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.extensions.Extension;
import org.eclipse.jetty.websocket.extensions.deflate.DeflateFrameExtension;
@ -47,7 +48,7 @@ import org.eclipse.jetty.websocket.server.handshake.HandshakeRFC6455;
/**
* Factory to create WebSocket connections
*/
public class WebSocketServerFactory extends AbstractLifeCycle
public class WebSocketServerFactory extends AbstractLifeCycle implements WebSocketCreator
{
private static final Logger LOG = Log.getLogger(WebSocketServerFactory.class);
private final Queue<AsyncWebSocketConnection> connections = new ConcurrentLinkedQueue<AsyncWebSocketConnection>();
@ -70,11 +71,13 @@ public class WebSocketServerFactory extends AbstractLifeCycle
private WebSocketPolicy basePolicy;
private WebSocketCreator creator;
private EventMethodsCache methodsCache;
private Class<?> firstRegisteredClass;
public WebSocketServerFactory(WebSocketPolicy policy)
{
this.basePolicy = policy;
this.methodsCache = new EventMethodsCache();
this.creator = this;
// Create supportedVersions
List<Integer> versions = new ArrayList<>();
@ -132,6 +135,29 @@ public class WebSocketServerFactory extends AbstractLifeCycle
}
}
@Override
public Object createWebSocket(WebSocketRequest req, WebSocketResponse resp)
{
if (methodsCache.count() < 1)
{
throw new WebSocketException("No WebSockets have been registered with the factory. Cannot use default implementation of WebSocketCreator.");
}
if (methodsCache.count() > 1)
{
LOG.warn("You have registered more than 1 websocket object, and are using the default WebSocketCreator! Using first registered websocket.");
}
try
{
return firstRegisteredClass.newInstance();
}
catch (InstantiationException | IllegalAccessException e)
{
throw new WebSocketException("Unable to create instance of " + firstRegisteredClass,e);
}
}
@Override
protected void doStop() throws Exception
{
@ -247,6 +273,10 @@ public class WebSocketServerFactory extends AbstractLifeCycle
public void register(Class<?> websocketClass)
{
if (firstRegisteredClass == null)
{
firstRegisteredClass = websocketClass;
}
methodsCache.register(websocketClass);
}

View File

@ -44,7 +44,7 @@ import org.junit.Test;
public class WebSocketServletRFCTest
{
@SuppressWarnings("serial")
private static class RFCServlet extends WebSocketServlet
public static class RFCServlet extends WebSocketServlet
{
@Override
public void registerWebSockets(WebSocketServerFactory factory)
@ -53,7 +53,7 @@ public class WebSocketServletRFCTest
}
}
private static class RFCSocket extends WebSocketAdapter
public static class RFCSocket extends WebSocketAdapter
{
@Override
public void onWebSocketText(String message)
@ -204,8 +204,9 @@ public class WebSocketServletRFCTest
ByteBuffer txtbuf = bufferPool.acquire(policy.getBufferSize(),false);
try
{
BufferUtil.flipToFill(txtbuf);
generator.generate(txtbuf,txt);
txtbuf.flip();
BufferUtil.flipToFlush(txtbuf,0);
// Write Text Frame
BufferUtil.writeTo(txtbuf,out);
@ -219,10 +220,11 @@ public class WebSocketServletRFCTest
ByteBuffer rbuf = bufferPool.acquire(policy.getBufferSize(),false);
try
{
BufferUtil.flipToFill(rbuf);
read(in,rbuf);
// Parse Frame
rbuf.flip();
BufferUtil.flipToFlush(rbuf,0);
parser.parse(rbuf);
}
finally