Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

some tweaks. a README

  • Loading branch information...
commit b00c1e41c99992d5e18355698c6e61eda11dfb2c 1 parent 9662095
Justin Lee authored September 27, 2011
10  README.markdown
Source Rendered
... ...
@@ -0,0 +1,10 @@
  1
+WebSockets and Java EE
  2
+======================
  3
+
  4
+This project is just a prototype/proposal for how WebSocket based applications could be standardized
  5
+inside a Java EE container.  It is by no means exhaustive nor complete.  What I hope to show here is
  6
+how I've been envisioning this working over the last two years I've spent implementing the Grizzly
  7
+and GlassFish support.  It is heavily influenced by the API I've built, of course.  That said, for
  8
+me this is a starting point not a final recommendation.  I do feel that it's a better basis than
  9
+binding any WebSocket JSR to the Servlet specification directly.  For more details, please see my
  10
+blog at http://antwerkz.com/websockets-and-java-ee/
20  pom.xml
@@ -5,6 +5,15 @@
5 5
     <groupId>org.jsr.websockets</groupId>
6 6
     <artifactId>jsr-parent</artifactId>
7 7
     <version>1.0-SNAPSHOT</version>
  8
+    <dependencyManagement>
  9
+        <dependencies>
  10
+            <dependency>
  11
+                <groupId>javax</groupId>
  12
+                <artifactId>javaee-web-api</artifactId>
  13
+                <version>6.0</version>
  14
+            </dependency>
  15
+        </dependencies>
  16
+    </dependencyManagement>
8 17
     <packaging>pom</packaging>
9 18
 
10 19
     <name>jsr-parent</name>
@@ -38,13 +47,4 @@
38 47
             </plugins>
39 48
         </pluginManagement>
40 49
     </build>
41  
-
42  
-    <dependencies>
43  
-        <dependency>
44  
-            <groupId>junit</groupId>
45  
-            <artifactId>junit</artifactId>
46  
-            <version>3.8.1</version>
47  
-            <scope>test</scope>
48  
-        </dependency>
49  
-    </dependencies>
50  
-</project>
  50
+</project>
2  servlet/pom.xml
@@ -18,9 +18,7 @@
18 18
         <dependency>
19 19
             <groupId>javax</groupId>
20 20
             <artifactId>javaee-web-api</artifactId>
21  
-            <version>6.0</version>
22 21
             <scope>provided</scope>
23 22
         </dependency>
24 23
     </dependencies>
25  
-
26 24
 </project>
1  websockets/pom.xml
@@ -23,7 +23,6 @@
23 23
         <dependency>
24 24
             <groupId>javax</groupId>
25 25
             <artifactId>javaee-web-api</artifactId>
26  
-            <version>6.0</version>
27 26
             <scope>provided</scope>
28 27
         </dependency>
29 28
     </dependencies>
56  websockets/src/main/java/org/jsr/websockets/DefaultWebSocket.java
... ...
@@ -0,0 +1,56 @@
  1
+package org.jsr.websockets;
  2
+
  3
+import javax.servlet.ServletRequest;
  4
+import javax.servlet.ServletResponse;
  5
+
  6
+public class DefaultWebSocket implements WebSocket {
  7
+    public DefaultWebSocket(ServletRequest request, ServletResponse response) {
  8
+    }
  9
+
  10
+    @Override
  11
+    public void send(String data, boolean last) {
  12
+    }
  13
+
  14
+    @Override
  15
+    public void send(byte[] data, boolean last) {
  16
+    }
  17
+
  18
+    @Override
  19
+    public void close() {
  20
+    }
  21
+
  22
+    @Override
  23
+    public void close(int code, String reason) {
  24
+    }
  25
+
  26
+    @Override
  27
+    public boolean isConnected() {
  28
+        return false;
  29
+    }
  30
+
  31
+    @Override
  32
+    public boolean add(WebSocketListener listener) {
  33
+        return false;
  34
+    }
  35
+
  36
+    @Override
  37
+    public boolean remove(WebSocketListener listener) {
  38
+        return false;
  39
+    }
  40
+
  41
+    @Override
  42
+    public void onConnect() {
  43
+    }
  44
+
  45
+    @Override
  46
+    public void onMessage(String text, boolean last) {
  47
+    }
  48
+
  49
+    @Override
  50
+    public void onMessage(byte[] data, boolean last) {
  51
+    }
  52
+
  53
+    @Override
  54
+    public void onClose(int code, String reason) {
  55
+    }
  56
+}
9  websockets/src/main/java/org/jsr/websockets/DefaultWebSocketContext.java
... ...
@@ -1,9 +0,0 @@
1  
-package org.jsr.websockets;
2  
-
3  
-import javax.servlet.ServletRequest;
4  
-import javax.servlet.ServletResponse;
5  
-
6  
-public class DefaultWebSocketContext implements WebSocketContext {
7  
-    public DefaultWebSocketContext(ServletRequest request, ServletResponse response) {
8  
-    }
9  
-}
33  websockets/src/main/java/org/jsr/websockets/SampleWebSocketContext.java
... ...
@@ -0,0 +1,33 @@
  1
+package org.jsr.websockets;
  2
+
  3
+import javax.servlet.ServletRequest;
  4
+import javax.servlet.ServletResponse;
  5
+
  6
+import org.jsr.servlet.HttpUpgradeHandler;
  7
+
  8
+@HttpUpgradeHandler(protocol ="websocket", urlPatterns={"/sample"})
  9
+public class SampleWebSocketContext extends WebSocketContext {
  10
+    @Override
  11
+    public void customizeHandshake(ServletRequest request, ServletResponse response) {
  12
+        super.customizeHandshake(request, response);
  13
+        // insert cookies here
  14
+    }
  15
+
  16
+    @Override
  17
+    public WebSocket createContext(ServletRequest request, ServletResponse response) {
  18
+        return new SampleWebSocket(request, response);
  19
+    }
  20
+
  21
+    private static class SampleWebSocket extends DefaultWebSocket {
  22
+        private String userName;
  23
+
  24
+        public SampleWebSocket(ServletRequest request, ServletResponse response) {
  25
+            super(request, response);
  26
+            userName = request.getParameter("user");
  27
+        }
  28
+
  29
+        public String getUserName() {
  30
+            return userName;
  31
+        }
  32
+    }
  33
+}
25  websockets/src/main/java/org/jsr/websockets/WebSocket.java
... ...
@@ -0,0 +1,25 @@
  1
+package org.jsr.websockets;
  2
+
  3
+public interface WebSocket {
  4
+    void send(String data, boolean last);
  5
+
  6
+    void send(byte[] data, boolean last);
  7
+
  8
+    void close();
  9
+
  10
+    void close(int code, String reason);
  11
+
  12
+    boolean isConnected();
  13
+
  14
+    boolean add(WebSocketListener listener);
  15
+
  16
+    boolean remove(WebSocketListener listener);
  17
+
  18
+    void onConnect();
  19
+
  20
+    void onMessage(String text, boolean last);
  21
+
  22
+    void onMessage(byte[] data, boolean last);
  23
+
  24
+    void onClose(int code, String reason);
  25
+}
36  websockets/src/main/java/org/jsr/websockets/WebSocketContext.java
... ...
@@ -1,17 +1,37 @@
1 1
 package org.jsr.websockets;
2 2
 
3  
-public interface WebSocketContext extends WebSocketListener {
4  
-   void send(String data, boolean last);
  3
+import javax.servlet.ServletRequest;
  4
+import javax.servlet.ServletResponse;
5 5
 
6  
-   void send(byte[] data, boolean last);
  6
+import org.jsr.servlet.UpgradeException;
  7
+import org.jsr.servlet.UpgradeHandler;
7 8
 
8  
-   void close();
  9
+public abstract class WebSocketContext implements UpgradeHandler, WebSocketListener {
  10
+    @Override
  11
+    public final void doUpgrade(ServletRequest request, ServletResponse response) throws UpgradeException {
  12
+       // websocket handshake logic done here
  13
+    }
9 14
 
10  
-   void close(int code, String reason);
  15
+    public void customizeHandshake(ServletRequest request, ServletResponse response) {
  16
+    }
11 17
 
12  
-   boolean isConnected();
  18
+    public WebSocket createContext(ServletRequest request, ServletResponse response) {
  19
+        return new DefaultWebSocket(request, response);
  20
+    }
13 21
 
14  
-   boolean add(WebSocketListener listener);
  22
+    @Override
  23
+    public void onConnect(WebSocket socket) {
  24
+    }
15 25
 
16  
-   boolean remove(WebSocketListener listener);
  26
+    @Override
  27
+    public void onMessage(WebSocket socket, String text, boolean last) {
  28
+    }
  29
+
  30
+    @Override
  31
+    public void onMessage(WebSocket socket, byte[] data, boolean last) {
  32
+    }
  33
+
  34
+    @Override
  35
+    public void onClose(WebSocket socket, int code, String reason) {
  36
+    }
17 37
 }
8  websockets/src/main/java/org/jsr/websockets/WebSocketListener.java
... ...
@@ -1,11 +1,11 @@
1 1
 package org.jsr.websockets;
2 2
 
3 3
 public interface WebSocketListener {
4  
-    void onConnect();
  4
+    void onConnect(WebSocket socket);
5 5
 
6  
-    void onMessage(String text, boolean last);
  6
+    void onMessage(WebSocket socket, String text, boolean last);
7 7
 
8  
-    void onMessage(byte[] data, boolean last);
  8
+    void onMessage(WebSocket socket, byte[] data, boolean last);
9 9
 
10  
-    void onClose(int code, String reason);
  10
+    void onClose(WebSocket socket, int code, String reason);
11 11
 }
23  websockets/src/main/java/org/jsr/websockets/WebSocketUpgradeHandler.java
... ...
@@ -1,23 +0,0 @@
1  
-package org.jsr.websockets;
2  
-
3  
-import javax.servlet.ServletRequest;
4  
-import javax.servlet.ServletResponse;
5  
-
6  
-import org.jsr.servlet.HttpUpgradeHandler;
7  
-import org.jsr.servlet.UpgradeException;
8  
-import org.jsr.servlet.UpgradeHandler;
9  
-
10  
-@HttpUpgradeHandler (protocol ="websocket", urlPatterns={"/sample"})
11  
-public class WebSocketUpgradeHandler implements UpgradeHandler {
12  
-    @Override
13  
-    public final void doUpgrade(ServletRequest request, ServletResponse response) throws UpgradeException {
14  
-       // websocket handshake logic done here
15  
-    }
16  
-
17  
-    public void customizeHandshake(ServletRequest request, ServletResponse response) {
18  
-    }
19  
-
20  
-    public WebSocketContext createContext(ServletRequest request, ServletResponse response) {
21  
-        return new DefaultWebSocketContext(request, response);
22  
-    }
23  
-}

0 notes on commit b00c1e4

Please sign in to comment.
Something went wrong with that request. Please try again.