Author: markt
Date: Fri Feb 15 17:10:48 2013
New Revision: 1446694

URL: http://svn.apache.org/r1446694
Log:
Expose negotiated subProtocol to the Session
Add some plumbing for exposing path parameters

Modified:
    tomcat/trunk/java/org/apache/tomcat/websocket/Constants.java
    tomcat/trunk/java/org/apache/tomcat/websocket/LocalStrings.properties
    tomcat/trunk/java/org/apache/tomcat/websocket/WsSession.java
    tomcat/trunk/java/org/apache/tomcat/websocket/WsWebSocketContainer.java
    tomcat/trunk/java/org/apache/tomcat/websocket/server/WsProtocolHandler.java
    tomcat/trunk/java/org/apache/tomcat/websocket/server/WsServlet.java

Modified: tomcat/trunk/java/org/apache/tomcat/websocket/Constants.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/Constants.java?rev=1446694&r1=1446693&r2=1446694&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/Constants.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/Constants.java Fri Feb 15 
17:10:48 2013
@@ -48,6 +48,8 @@ public class Constants {
     public static final String WS_VERSION_HEADER_NAME = 
"Sec-WebSocket-Version";
     public static final String WS_VERSION_HEADER_VALUE = "13";
     public static final String WS_KEY_HEADER_NAME = "Sec-WebSocket-Key";
+    public static final String WS_PROTOCOL_HEADER_NAME =
+            "Sec-WebSocket-Protocol";
 
 
     private Constants() {

Modified: tomcat/trunk/java/org/apache/tomcat/websocket/LocalStrings.properties
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/LocalStrings.properties?rev=1446694&r1=1446693&r2=1446694&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/LocalStrings.properties 
(original)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/LocalStrings.properties Fri 
Feb 15 17:10:48 2013
@@ -49,7 +49,9 @@ wsSession.invalidHandlerTypePong=A pong 
 wsSession.removeHandlerFailed=Unable to remove the handler [{0}] as it was not 
registered with this session
 wsSession.unknownHandler=Unable to add the message handler [{0}] as it was for 
the unrecognised type [{1}]
 
+wsWebSocketContainer.httpRequestFailed=The HTTP request to initiate the 
WebSocket conenction failed
 wsWebSocketContainer.invalidStatus=The HTTP response from the server [{0}] did 
not permit the HTTP upgrade to WebSocket
+wsWebSocketContainer.invalidSubProtocol=The WebSocket server returned multiple 
values for the Sec-WebSocket-Protocol header
 wsWebSocketContainer.maxBuffer=This implementation limits the maximum size of 
a buffer to Integer.MAX_VALUE
 wsWebSocketContainer.pathNoHost=No host was specified in URI
 wsWebSocketContainer.pathWrongScheme=The scheme [{0}] is not supported

Modified: tomcat/trunk/java/org/apache/tomcat/websocket/WsSession.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/WsSession.java?rev=1446694&r1=1446693&r2=1446694&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/WsSession.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/WsSession.java Fri Feb 15 
17:10:48 2013
@@ -54,6 +54,8 @@ public class WsSession implements Sessio
     private final ClassLoader applicationClassLoader;
     private final WsWebSocketContainer webSocketContainer;
     private final WsRequest request;
+    private final String subProtocol;
+    private final Map<String,String> pathParameters;
 
     private MessageHandler textMessageHandler = null;
     private MessageHandler binaryMessageHandler = null;
@@ -81,7 +83,8 @@ public class WsSession implements Sessio
     public WsSession(Endpoint localEndpoint,
             WsRemoteEndpointBase wsRemoteEndpoint,
             WsWebSocketContainer wsWebSocketContainer,
-            WsRequest request) {
+            WsRequest request, String subProtocol,
+            Map<String,String> pathParameters) {
         this.localEndpoint = localEndpoint;
         this.wsRemoteEndpoint = wsRemoteEndpoint;
         this.wsRemoteEndpoint.setSession(this);
@@ -96,6 +99,8 @@ public class WsSession implements Sessio
         this.sessionIdleTimeout =
                 webSocketContainer.getMaxSessionIdleTimeout();
         this.request = request;
+        this.subProtocol = subProtocol;
+        this.pathParameters = pathParameters;
     }
 
 
@@ -185,8 +190,7 @@ public class WsSession implements Sessio
 
     @Override
     public String getNegotiatedSubprotocol() {
-        // TODO Auto-generated method stub
-        return null;
+        return subProtocol;
     }
 
 
@@ -343,8 +347,7 @@ public class WsSession implements Sessio
 
     @Override
     public Map<String,String> getPathParameters() {
-        // TODO Auto-generated method stub
-        return null;
+        return pathParameters;
     }
 
 

Modified: 
tomcat/trunk/java/org/apache/tomcat/websocket/WsWebSocketContainer.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/WsWebSocketContainer.java?rev=1446694&r1=1446693&r2=1446694&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/WsWebSocketContainer.java 
(original)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/WsWebSocketContainer.java Fri 
Feb 15 17:10:48 2013
@@ -121,6 +121,7 @@ public class WsWebSocketContainer
         Future<Void> fConnect = channel.connect(sa);
 
         ByteBuffer response;
+        String subProtocol;
         try {
             fConnect.get();
 
@@ -141,8 +142,21 @@ public class WsWebSocketContainer
             HandshakeResponse handshakeResponse =
                     processResponse(response, channel);
             clientEndpointConfiguration.afterResponse(handshakeResponse);
+
+            // Sub-protocol
+            List<String> values = handshakeResponse.getHeaders().get(
+                    Constants.WS_PROTOCOL_HEADER_NAME);
+            if (values == null || values.size() == 0) {
+                subProtocol = null;
+            } else if (values.size() == 1) {
+                subProtocol = values.get(0);
+            } else {
+                throw new DeploymentException(
+                        sm.getString("Sec-WebSocket-Protocol"));
+            }
         } catch (ExecutionException | InterruptedException e) {
-            throw new DeploymentException("", e);
+            throw new DeploymentException(
+                    sm.getString("wsWebSocketContainer.httpRequestFailed"), e);
         }
 
         // Switch to WebSocket
@@ -157,8 +171,9 @@ public class WsWebSocketContainer
                     "wsWebSocketContainer.endpointCreateFail", 
clazz.getName()),
                     e);
         }
-        WsSession wsSession =
-                new WsSession(endpoint, wsRemoteEndpointClient, this, null);
+
+        WsSession wsSession = new WsSession(endpoint, wsRemoteEndpointClient,
+                this, null, subProtocol, Collections.EMPTY_MAP);
         endpoint.onOpen(wsSession, clientEndpointConfiguration);
         registerSession(clazz, wsSession);
 

Modified: 
tomcat/trunk/java/org/apache/tomcat/websocket/server/WsProtocolHandler.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/server/WsProtocolHandler.java?rev=1446694&r1=1446693&r2=1446694&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/server/WsProtocolHandler.java 
(original)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/server/WsProtocolHandler.java 
Fri Feb 15 17:10:48 2013
@@ -52,16 +52,18 @@ public class WsProtocolHandler implement
     private final ClassLoader applicationClassLoader;
     private final ServerContainerImpl webSocketContainer;
     private final WsRequest request;
+    private final String subProtocol;
 
     private WsSession wsSession;
 
 
     public WsProtocolHandler(Endpoint ep, EndpointConfiguration endpointConfig,
-            ServerContainerImpl wsc, WsRequest request) {
+            ServerContainerImpl wsc, WsRequest request, String subProtocol) {
         this.ep = ep;
         this.endpointConfig = endpointConfig;
         this.webSocketContainer = wsc;
         this.request = request;
+        this.subProtocol = subProtocol;
         applicationClassLoader = 
Thread.currentThread().getContextClassLoader();
     }
 
@@ -86,8 +88,9 @@ public class WsProtocolHandler implement
         try {
             WsRemoteEndpointServer wsRemoteEndpointServer =
                     new WsRemoteEndpointServer(sos, webSocketContainer);
+            // TODO Replace null with path parameter map
             wsSession = new WsSession(ep, wsRemoteEndpointServer,
-                    webSocketContainer, request);
+                    webSocketContainer, request, subProtocol, null);
             WsFrameServer wsFrame = new WsFrameServer(
                     sis,
                     wsSession);

Modified: tomcat/trunk/java/org/apache/tomcat/websocket/server/WsServlet.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/server/WsServlet.java?rev=1446694&r1=1446693&r2=1446694&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/server/WsServlet.java 
(original)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/server/WsServlet.java Fri Feb 
15 17:10:48 2013
@@ -137,7 +137,7 @@ public class WsServlet extends HttpServl
         }
         WsRequest wsRequest = createWsRequest(req);
         HttpUpgradeHandler wsHandler =
-                new WsProtocolHandler(ep, sec, sc, wsRequest);
+                new WsProtocolHandler(ep, sec, sc, wsRequest, subProtocol);
         req.upgrade(wsHandler);
     }
 



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to