Author: markt
Date: Thu Feb 14 15:48:40 2013
New Revision: 1446247

URL: http://svn.apache.org/r1446247
Log:
Make the HTTP request information available via the session

Added:
    tomcat/trunk/java/org/apache/tomcat/websocket/WsRequest.java   (with props)
Modified:
    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

Added: tomcat/trunk/java/org/apache/tomcat/websocket/WsRequest.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/WsRequest.java?rev=1446247&view=auto
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/WsRequest.java (added)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/WsRequest.java Thu Feb 14 
15:48:40 2013
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tomcat.websocket;
+
+import java.net.URI;
+import java.security.Principal;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Represents the request that this session was opened under.
+ */
+public class WsRequest {
+
+    private final URI requestURI;
+    private final Map<String,List<String>> parameterMap;
+    private final String queryString;
+    private final Principal userPrincipal;
+
+    public WsRequest(URI requestURI, Map<String,List<String>> parameterMap,
+            String queryString, Principal userPrincipal) {
+        this.requestURI = requestURI;
+        this.parameterMap = parameterMap;
+        this.queryString = queryString;
+        this.userPrincipal = userPrincipal;
+    }
+
+    public URI getRequestURI() {
+        return requestURI;
+    }
+
+    public Map<String,List<String>> getRequestParameterMap() {
+        return parameterMap;
+    }
+
+    public String getQueryString() {
+        return queryString;
+    }
+
+    public Principal getUserPrincipal() {
+        return userPrincipal;
+    }
+}

Propchange: tomcat/trunk/java/org/apache/tomcat/websocket/WsRequest.java
------------------------------------------------------------------------------
    svn:eol-style = native

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=1446247&r1=1446246&r2=1446247&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/WsSession.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/WsSession.java Thu Feb 14 
15:48:40 2013
@@ -24,6 +24,7 @@ import java.net.URI;
 import java.nio.ByteBuffer;
 import java.nio.charset.Charset;
 import java.security.Principal;
+import java.util.Collections;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
@@ -52,6 +53,7 @@ public class WsSession implements Sessio
     private final WsRemoteEndpointBase wsRemoteEndpoint;
     private final ClassLoader applicationClassLoader;
     private final WsWebSocketContainer webSocketContainer;
+    private final WsRequest request;
 
     private MessageHandler textMessageHandler = null;
     private MessageHandler binaryMessageHandler = null;
@@ -78,7 +80,8 @@ public class WsSession implements Sessio
      */
     public WsSession(Endpoint localEndpoint,
             WsRemoteEndpointBase wsRemoteEndpoint,
-            WsWebSocketContainer wsWebSocketContainer) {
+            WsWebSocketContainer wsWebSocketContainer,
+            WsRequest request) {
         this.localEndpoint = localEndpoint;
         this.wsRemoteEndpoint = wsRemoteEndpoint;
         this.wsRemoteEndpoint.setSession(this);
@@ -92,6 +95,7 @@ public class WsSession implements Sessio
                 webSocketContainer.getDefaultMaxTextMessageBufferSize();
         this.sessionIdleTimeout =
                 webSocketContainer.getMaxSessionIdleTimeout();
+        this.request = request;
     }
 
 
@@ -188,8 +192,7 @@ public class WsSession implements Sessio
 
     @Override
     public List<Extension> getNegotiatedExtensions() {
-        // TODO Auto-generated method stub
-        return null;
+        return Collections.EMPTY_LIST;
     }
 
 
@@ -307,41 +310,49 @@ public class WsSession implements Sessio
 
     @Override
     public URI getRequestURI() {
-        // TODO Auto-generated method stub
-        return null;
+        if (request == null) {
+            return null;
+        }
+        return request.getRequestURI();
     }
 
 
     @Override
     public Map<String,List<String>> getRequestParameterMap() {
-        // TODO Auto-generated method stub
-        return null;
+        if (request == null) {
+            return Collections.EMPTY_MAP;
+        }
+        return request.getRequestParameterMap();
     }
 
 
     @Override
     public String getQueryString() {
-        // TODO Auto-generated method stub
-        return null;
+        if (request == null) {
+            return null;
+        }
+        return request.getQueryString();
     }
 
 
     @Override
-    public Map<String,String> getPathParameters() {
-        // TODO Auto-generated method stub
-        return null;
+    public Principal getUserPrincipal() {
+        if (request == null) {
+            return null;
+        }
+        return request.getUserPrincipal();
     }
 
 
     @Override
-    public String getId() {
+    public Map<String,String> getPathParameters() {
         // TODO Auto-generated method stub
         return null;
     }
 
 
     @Override
-    public Principal getUserPrincipal() {
+    public String getId() {
         // TODO Auto-generated method stub
         return null;
     }

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=1446247&r1=1446246&r2=1446247&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/WsWebSocketContainer.java 
(original)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/WsWebSocketContainer.java Thu 
Feb 14 15:48:40 2013
@@ -158,7 +158,7 @@ public class WsWebSocketContainer
                     e);
         }
         WsSession wsSession =
-                new WsSession(endpoint, wsRemoteEndpointClient, this);
+                new WsSession(endpoint, wsRemoteEndpointClient, this, null);
         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=1446247&r1=1446246&r2=1446247&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/server/WsProtocolHandler.java 
(original)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/server/WsProtocolHandler.java 
Thu Feb 14 15:48:40 2013
@@ -34,6 +34,7 @@ import org.apache.juli.logging.Log;
 import org.apache.juli.logging.LogFactory;
 import org.apache.tomcat.util.res.StringManager;
 import org.apache.tomcat.websocket.WsIOException;
+import org.apache.tomcat.websocket.WsRequest;
 import org.apache.tomcat.websocket.WsSession;
 
 /**
@@ -50,16 +51,17 @@ public class WsProtocolHandler implement
     private final EndpointConfiguration endpointConfig;
     private final ClassLoader applicationClassLoader;
     private final ServerContainerImpl webSocketContainer;
+    private final WsRequest request;
 
     private WsSession wsSession;
 
 
-    public WsProtocolHandler(Endpoint ep,
-            EndpointConfiguration endpointConfig,
-            ServerContainerImpl wsc) {
+    public WsProtocolHandler(Endpoint ep, EndpointConfiguration endpointConfig,
+            ServerContainerImpl wsc, WsRequest request) {
         this.ep = ep;
         this.endpointConfig = endpointConfig;
         this.webSocketContainer = wsc;
+        this.request = request;
         applicationClassLoader = 
Thread.currentThread().getContextClassLoader();
     }
 
@@ -84,8 +86,8 @@ public class WsProtocolHandler implement
         try {
             WsRemoteEndpointServer wsRemoteEndpointServer =
                     new WsRemoteEndpointServer(sos, webSocketContainer);
-            wsSession = new WsSession(
-                    ep, wsRemoteEndpointServer, webSocketContainer);
+            wsSession = new WsSession(ep, wsRemoteEndpointServer,
+                    webSocketContainer, request);
             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=1446247&r1=1446246&r2=1446247&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/server/WsServlet.java 
(original)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/server/WsServlet.java Thu Feb 
14 15:48:40 2013
@@ -17,14 +17,20 @@
 package org.apache.tomcat.websocket.server;
 
 import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.nio.charset.Charset;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.Enumeration;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
 import java.util.Queue;
 import java.util.concurrent.ConcurrentLinkedQueue;
 
@@ -39,6 +45,7 @@ import javax.websocket.server.ServerEndp
 import javax.xml.bind.DatatypeConverter;
 
 import org.apache.tomcat.websocket.Constants;
+import org.apache.tomcat.websocket.WsRequest;
 
 /**
  * Handles the initial HTTP connection for WebSocket connections.
@@ -128,11 +135,45 @@ public class WsServlet extends HttpServl
         } catch (InstantiationException | IllegalAccessException e) {
             throw new ServletException(e);
         }
-        HttpUpgradeHandler wsHandler = new WsProtocolHandler(ep, sec, sc);
+        WsRequest wsRequest = createWsRequest(req);
+        HttpUpgradeHandler wsHandler =
+                new WsProtocolHandler(ep, sec, sc, wsRequest);
         req.upgrade(wsHandler);
     }
 
 
+    private WsRequest createWsRequest(HttpServletRequest req)
+            throws ServletException {
+
+        String queryString = req.getQueryString();
+
+        StringBuffer sb = req.getRequestURL();
+        if (queryString != null) {
+            sb.append("?");
+            sb.append(queryString);
+        }
+        URI requestURI;
+        try {
+            requestURI = new URI(sb.toString());
+        } catch (URISyntaxException e) {
+            throw new ServletException(e);
+        }
+
+        Map<String,String[]> originalParameters = req.getParameterMap();
+        Map<String,List<String>> newParameters = new HashMap<>();
+        for (Entry<String,String[]> entry : originalParameters.entrySet()) {
+            newParameters.put(entry.getKey(),
+                    Collections.unmodifiableList(
+                            Arrays.asList(entry.getValue())));
+        }
+        Map<String,List<String>> parameterMap =
+                Collections.unmodifiableMap(newParameters);
+
+        return new WsRequest(requestURI, parameterMap, queryString,
+                req.getUserPrincipal());
+    }
+
+
     /*
      * This only works for tokens. Quoted strings need more sophisticated
      * parsing.



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to