Permalink
Browse files

refactor

  • Loading branch information...
1 parent 5faa16c commit 641060b752742c08f141ca97a06c3f1ea54b871f @patriot1burke patriot1burke committed May 3, 2012
Showing with 487 additions and 310 deletions.
  1. +1 −1 pom.xml
  2. +5 −33 src/main/java/org/jboss/as/websockets/WebSocket.java
  3. +55 −0 src/main/java/org/jboss/as/websockets/servlet/WebSocketDelegate.java
  4. +83 −85 src/main/java/org/jboss/as/websockets/servlet/WebSocketServlet.java
  5. +1 −1 src/main/java/org/jboss/{as → }/websockets/Frame.java
  6. +1 −1 src/main/java/org/jboss/{as → }/websockets/FrameType.java
  7. +5 −4 src/main/java/org/jboss/{as → }/websockets/frame/AbstractFrame.java
  8. +2 −3 src/main/java/org/jboss/{as → }/websockets/frame/BinaryFrame.java
  9. +2 −2 src/main/java/org/jboss/{as → }/websockets/frame/CloseFrame.java
  10. +2 −2 src/main/java/org/jboss/{as → }/websockets/frame/PingFrame.java
  11. +2 −2 src/main/java/org/jboss/{as → }/websockets/frame/PongFrame.java
  12. +2 −2 src/main/java/org/jboss/{as → }/websockets/frame/TextFrame.java
  13. +1 −1 src/main/java/org/jboss/{as/websockets/protocol → websockets/oio}/ClosingStrategy.java
  14. +17 −0 src/main/java/org/jboss/websockets/oio/HttpRequestBridge.java
  15. +33 −0 src/main/java/org/jboss/websockets/oio/HttpResponseBridge.java
  16. +44 −0 src/main/java/org/jboss/websockets/oio/OioWebSocket.java
  17. +85 −0 src/main/java/org/jboss/websockets/oio/WebSocketConnectionManager.java
  18. +7 −19 src/main/java/org/jboss/{as/websockets → websockets/oio/internal}/AbstractWebSocket.java
  19. +11 −10 src/main/java/org/jboss/{as/websockets → websockets/oio/internal}/Handshake.java
  20. +11 −11 src/main/java/org/jboss/{as/websockets → websockets/oio/internal}/WebSocketHeaders.java
  21. +16 −17 ...ain/java/org/jboss/{as/websockets → websockets/oio/internal}/protocol/ietf00/Hybi00Handshake.java
  22. +15 −16 src/main/java/org/jboss/{as/websockets → websockets/oio/internal}/protocol/ietf00/Hybi00Socket.java
  23. +21 −22 ...ain/java/org/jboss/{as/websockets → websockets/oio/internal}/protocol/ietf07/Hybi07Handshake.java
  24. +22 −32 src/main/java/org/jboss/{as/websockets → websockets/oio/internal}/protocol/ietf07/Hybi07Socket.java
  25. +2 −2 ...ain/java/org/jboss/{as/websockets → websockets/oio/internal}/protocol/ietf08/Hybi08Handshake.java
  26. +15 −18 ...ain/java/org/jboss/{as/websockets → websockets/oio/internal}/protocol/ietf13/Hybi13Handshake.java
  27. +22 −22 src/main/java/org/jboss/{as/websockets → websockets/oio/internal}/protocol/ietf13/Hybi13Socket.java
  28. +1 −1 src/main/java/org/jboss/{as/websockets → websockets/oio/internal}/util/Assert.java
  29. +1 −1 src/main/java/org/jboss/{as/websockets → websockets/oio/internal}/util/Base64.java
  30. +1 −1 src/main/java/org/jboss/{as/websockets → websockets/oio/internal}/util/Hash.java
  31. +1 −1 src/test/java/org/jboss/as/websocket/HandshakeTests.java
View
@@ -64,7 +64,7 @@
<artifactId>jbossweb</artifactId>
<scope>provided</scope>
<!-- TODO: don't hard code this -->
- <version>7.0.14.WebSocketsR0</version>
+ <version>7.0.16.Final</version>
</dependency>
<dependency>
@@ -16,9 +16,10 @@
package org.jboss.as.websockets;
+import org.jboss.websockets.oio.OioWebSocket;
+
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
-import java.io.IOException;
/**
* Represents a handle to a single WebSocket connection. It has reader and writer methods to get data in and out.
@@ -27,36 +28,11 @@
*
* @author Mike Brock
*/
-public interface WebSocket {
-
- /**
- * A unique ID associated with the socket, which can be used for session association. This ID is generated by the
- * WebSockets framework as a random hash when the socket is open and has no association with any external API or
- * the websocket handshake process.
- *
- * @return A hex string representing the unique ID of the socket.
- */
- public String getSocketID();
-
- /**
- * Read a single frame from the socket.
- *
- * @return
- * @throws IOException
- */
- public Frame readFrame() throws IOException;
+public interface WebSocket extends OioWebSocket
+{
- /**
- * Write a frame to the socket.
- *
- * @param frame
- * @throws IOException
- */
- public void writeFrame(Frame frame) throws IOException;
-
-
- /**
+ /**
* Return the HTTP Session with which this WebSocket is associated.
* @return an instance of the HttpSession
*/
@@ -72,8 +48,4 @@
*/
public HttpServletRequest getServletRequest();
- /**
- * Terminates the connection with the client and closes the socket.
- */
- public void closeSocket() throws IOException;
}
@@ -0,0 +1,55 @@
+package org.jboss.as.websockets.servlet;
+
+import org.jboss.as.websockets.WebSocket;
+import org.jboss.websockets.Frame;
+import org.jboss.websockets.oio.OioWebSocket;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpSession;
+import java.io.IOException;
+
+/**
+ * @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
+ * @version $Revision: 1 $
+ */
+class WebSocketDelegate implements WebSocket
+{
+ protected HttpServletRequest request;
+ protected OioWebSocket delegate;
+
+ public WebSocketDelegate(HttpServletRequest request, OioWebSocket delegate)
+ {
+ this.request = request;
+ this.delegate = delegate;
+ }
+
+ public HttpSession getHttpSession()
+ {
+ return request.getSession();
+ }
+
+ public HttpServletRequest getServletRequest()
+ {
+ return request;
+ }
+
+ public String getSocketID()
+ {
+ return delegate.getSocketID();
+ }
+
+ public Frame readFrame() throws IOException
+ {
+ return delegate.readFrame();
+ }
+
+ public void writeFrame(Frame frame) throws IOException
+ {
+ delegate.writeFrame(frame);
+ }
+
+ public void closeSocket() throws IOException
+ {
+ delegate.closeSocket();
+ }
+}
@@ -16,14 +16,18 @@
package org.jboss.as.websockets.servlet;
-import org.jboss.as.websockets.Handshake;
+import org.jboss.websockets.oio.ClosingStrategy;
+import org.jboss.websockets.oio.HttpRequestBridge;
+import org.jboss.websockets.oio.HttpResponseBridge;
+import org.jboss.websockets.oio.OioWebSocket;
+import org.jboss.websockets.oio.WebSocketConnectionManager;
+import org.jboss.websockets.oio.internal.Handshake;
import org.jboss.as.websockets.WebSocket;
-import org.jboss.as.websockets.WebSocketHeaders;
-import org.jboss.as.websockets.protocol.ClosingStrategy;
-import org.jboss.as.websockets.protocol.ietf00.Hybi00Handshake;
-import org.jboss.as.websockets.protocol.ietf07.Hybi07Handshake;
-import org.jboss.as.websockets.protocol.ietf08.Hybi08Handshake;
-import org.jboss.as.websockets.protocol.ietf13.Hybi13Handshake;
+import org.jboss.websockets.oio.internal.WebSocketHeaders;
+import org.jboss.websockets.oio.internal.protocol.ietf00.Hybi00Handshake;
+import org.jboss.websockets.oio.internal.protocol.ietf07.Hybi07Handshake;
+import org.jboss.websockets.oio.internal.protocol.ietf08.Hybi08Handshake;
+import org.jboss.websockets.oio.internal.protocol.ietf13.Hybi13Handshake;
import org.jboss.servlet.http.HttpEvent;
import org.jboss.servlet.http.HttpEventServlet;
import org.jboss.servlet.http.UpgradableHttpServletResponse;
@@ -37,6 +41,8 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@@ -48,22 +54,10 @@
* @author Mike Brock
*/
public abstract class WebSocketServlet extends HttpServlet implements HttpEventServlet {
- private static final List<Handshake> websocketHandshakes;
-
private static final Logger log = LoggerFactory.getLogger(WebSocketServlet.class);
private final String protocolName;
- static {
- final List<Handshake> handshakeList = new ArrayList<Handshake>();
- handshakeList.add(new Hybi13Handshake());
- handshakeList.add(new Hybi07Handshake());
- handshakeList.add(new Hybi08Handshake());
- handshakeList.add(new Hybi00Handshake());
-
- websocketHandshakes = Collections.unmodifiableList(handshakeList);
- }
-
/**
* Set the protocol name to be returned in the Sec-WebSocket-Protocol header attribute during negotiation. This is
* not thread-safe. It should only be set from the init() method of the servlet.
@@ -84,23 +78,6 @@ protected WebSocketServlet() {
*/
private static final String SESSION_WEBSOCKET_HANDLE = "JBoss:AS:WebSocket:Handle";
- /**
- * Sets the standard upgrade headers that are common to all HTTP 101 upgrades, as well as the
- * SEC_WEBSOCKETS_PROTOCOL header (if the protocol is specified) common to all WebSocket implementations.
- *
- * @param response
- */
- private void setStandardUpgradeHeaders(final HttpServletResponse response) {
- response.setHeader("Upgrade", "WebSocket");
- response.setHeader("Connection", "Upgrade");
-
- if (protocolName != null)
- WebSocketHeaders.SEC_WEBSOCKET_PROTOCOL.set(response, protocolName);
- else {
- WebSocketHeaders.SEC_WEBSOCKET_PROTOCOL.set(response, "*");
- }
- }
-
/**
* Handle an event from the web container.
*
@@ -120,55 +97,76 @@ public final void event(final HttpEvent event) throws IOException, ServletExcept
* Check to see if this request is an HTTP Upgrade request.
*/
if (response instanceof UpgradableHttpServletResponse) {
-
- /**
- * Interrogate the request with the available handshakes.
- */
- for (Handshake handshake : websocketHandshakes) {
- if (handshake.matches(request)) {
- /**
- * We found a matching handshake, so let's tell the web server we'd like to begin the process of
- * upgrading this connection to a WebSocket.
- */
- ((UpgradableHttpServletResponse) response).startUpgrade();
-
- log.debug("Found a compatible handshake: (Version:"
- + handshake.getVersion() + "; Handler: " + handshake.getClass().getName() + ")");
-
- setStandardUpgradeHeaders(response);
-
- /**
- * Generate the server handshake response -- setting the necessary headers and also capturing
- * any data bound for the body of the response.
- */
- final byte[] handShakeData = handshake.generateResponse(event);
-
- // write the handshake data
- event.getHttpServletResponse().getOutputStream().write(handShakeData);
-
- /**
- * Obtain an WebSocket instance from the handshaker.
- */
- final WebSocket webSocket
- = handshake.getWebSocket(event.getHttpServletRequest(), event.getHttpServletResponse(),
- new ClosingStrategy() {
- public void doClose() throws IOException{
- event.close();
- }
- });
-
- log.debug("Using WebSocket implementation: " + webSocket.getClass().getName());
-
- /**
- * Record a reference to this WebSocket into the HttpServletRequest so it can be re-referenced
- * on READ, ERROR, and EOF events.
- */
- request.setAttribute(SESSION_WEBSOCKET_HANDLE, webSocket);
-
- ((UpgradableHttpServletResponse) response).sendUpgrade();
- onSocketOpened(webSocket);
- }
- }
+ HttpRequestBridge requestBridge = new HttpRequestBridge()
+ {
+ public String getHeader(String name)
+ {
+ return request.getHeader(name);
+ }
+
+ public String getRequestURI()
+ {
+ return request.getRequestURI();
+ }
+
+ public InputStream getInputStream()
+ {
+ try
+ {
+ return request.getInputStream();
+ }
+ catch (IOException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+ };
+
+ HttpResponseBridge responseBridge = new HttpResponseBridge()
+ {
+ public String getHeader(String name)
+ {
+ return response.getHeader(name);
+ }
+
+ public void setHeader(String name, String val)
+ {
+ response.setHeader(name, val);
+ }
+
+ public OutputStream getOutputStream()
+ {
+ try
+ {
+ return response.getOutputStream();
+ }
+ catch (IOException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public void startUpgrade()
+ {
+ ((UpgradableHttpServletResponse)response).startUpgrade();
+ }
+
+ public void sendUpgrade() throws IOException
+ {
+ ((UpgradableHttpServletResponse)response).sendUpgrade();
+ }
+ };
+ OioWebSocket oioWebSocket = WebSocketConnectionManager.establish(protocolName, requestBridge, responseBridge,
+ new ClosingStrategy()
+ {
+ public void doClose() throws IOException
+ {
+ event.close();
+ }
+ });
+ WebSocket webSocket = new WebSocketDelegate(request, oioWebSocket);
+ request.setAttribute(SESSION_WEBSOCKET_HANDLE, webSocket);
+ onSocketOpened(webSocket);
}
else {
throw new IllegalStateException("cannot upgrade connection");
@@ -1,4 +1,4 @@
-package org.jboss.as.websockets;
+package org.jboss.websockets;
/**
* @author Mike Brock
@@ -1,4 +1,4 @@
-package org.jboss.as.websockets;
+package org.jboss.websockets;
import java.io.InputStream;
@@ -1,12 +1,13 @@
-package org.jboss.as.websockets.frame;
+package org.jboss.websockets.frame;
-import org.jboss.as.websockets.Frame;
-import org.jboss.as.websockets.FrameType;
+import org.jboss.websockets.Frame;
+import org.jboss.websockets.FrameType;
/**
* @author Mike Brock
*/
-public class AbstractFrame implements Frame {
+public class AbstractFrame implements Frame
+{
private final FrameType type;
protected AbstractFrame(final FrameType type) {
@@ -1,7 +1,6 @@
-package org.jboss.as.websockets.frame;
+package org.jboss.websockets.frame;
-import org.jboss.as.websockets.Frame;
-import org.jboss.as.websockets.FrameType;
+import org.jboss.websockets.FrameType;
/**
* @author Mike Brock
@@ -1,6 +1,6 @@
-package org.jboss.as.websockets.frame;
+package org.jboss.websockets.frame;
-import org.jboss.as.websockets.FrameType;
+import org.jboss.websockets.FrameType;
/**
* @author Mike Brock
@@ -1,6 +1,6 @@
-package org.jboss.as.websockets.frame;
+package org.jboss.websockets.frame;
-import org.jboss.as.websockets.FrameType;
+import org.jboss.websockets.FrameType;
/**
* @author Mike Brock
Oops, something went wrong.

0 comments on commit 641060b

Please sign in to comment.