tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r1354469 - in /tomcat/trunk: java/org/apache/catalina/websocket/ test/org/apache/catalina/websocket/ webapps/examples/WEB-INF/classes/websocket/chat/ webapps/examples/WEB-INF/classes/websocket/echo/ webapps/examples/WEB-INF/classes/websocke...
Date Wed, 27 Jun 2012 12:46:07 GMT
Author: markt
Date: Wed Jun 27 12:46:05 2012
New Revision: 1354469

URL: http://svn.apache.org/viewvc?rev=1354469&view=rev
Log:
Make the request available when the WebSocket connection is being created

Added:
    tomcat/trunk/java/org/apache/catalina/websocket/WsHttpServletRequestWrapper.java   (with
props)
Modified:
    tomcat/trunk/java/org/apache/catalina/websocket/LocalStrings.properties
    tomcat/trunk/java/org/apache/catalina/websocket/WebSocketServlet.java
    tomcat/trunk/test/org/apache/catalina/websocket/TestWebSocket.java
    tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/chat/ChatWebSocketServlet.java
    tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/echo/EchoMessage.java
    tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/echo/EchoStream.java
    tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/snake/SnakeWebSocketServlet.java

Modified: tomcat/trunk/java/org/apache/catalina/websocket/LocalStrings.properties
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/websocket/LocalStrings.properties?rev=1354469&r1=1354468&r2=1354469&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/websocket/LocalStrings.properties (original)
+++ tomcat/trunk/java/org/apache/catalina/websocket/LocalStrings.properties Wed Jun 27 12:46:05
2012
@@ -26,4 +26,6 @@ message.bufferTooSmall=The buffer is not
 
 servlet.reqUpgradeFail=Unable to cast to the Tomcat internal request class in order to complete
HTTP upgrade
 
-outbound.closed=The WebSocket connection has been closed
\ No newline at end of file
+outbound.closed=The WebSocket connection has been closed
+
+wrapper.invalid=An attempt was made to access the request object passed to WebSocketServlet.createWebSocketInbound()
outside of that method
\ No newline at end of file

Modified: tomcat/trunk/java/org/apache/catalina/websocket/WebSocketServlet.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/websocket/WebSocketServlet.java?rev=1354469&r1=1354468&r2=1354469&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/websocket/WebSocketServlet.java (original)
+++ tomcat/trunk/java/org/apache/catalina/websocket/WebSocketServlet.java Wed Jun 27 12:46:05
2012
@@ -117,7 +117,9 @@ public abstract class WebSocketServlet e
             // TODO
         }
 
-        StreamInbound inbound = createWebSocketInbound(subProtocol);
+        WsHttpServletRequestWrapper wrapper = new WsHttpServletRequestWrapper(req);
+        StreamInbound inbound = createWebSocketInbound(subProtocol, wrapper);
+        wrapper.invalidate();
 
         // Small hack until the Servlet API provides a way to do this.
         ServletRequest inner = req;
@@ -234,6 +236,13 @@ public abstract class WebSocketServlet e
      *
      * @param subProtocol   The sub-protocol agreed between the client and
      *                      server or <code>null</code> if none was agreed
+     * @param request       The HTTP request that initiated this WebSocket
+     *                      connection. Note that this object is <b>only</b>
+     *                      valid inside this method. You must not retain a
+     *                      reference to it outside the execution of this
+     *                      method. If Tomcat detects such access, it will throw
+     *                      an IllegalStateException
      */
-    protected abstract StreamInbound createWebSocketInbound(String subProtocol);
+    protected abstract StreamInbound createWebSocketInbound(String subProtocol,
+            HttpServletRequest request);
 }

Added: tomcat/trunk/java/org/apache/catalina/websocket/WsHttpServletRequestWrapper.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/websocket/WsHttpServletRequestWrapper.java?rev=1354469&view=auto
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/websocket/WsHttpServletRequestWrapper.java (added)
+++ tomcat/trunk/java/org/apache/catalina/websocket/WsHttpServletRequestWrapper.java Wed Jun
27 12:46:05 2012
@@ -0,0 +1,415 @@
+/*
+ * 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.catalina.websocket;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.security.Principal;
+import java.util.Collection;
+import java.util.Enumeration;
+import java.util.Locale;
+import java.util.Map;
+
+import javax.servlet.AsyncContext;
+import javax.servlet.DispatcherType;
+import javax.servlet.RequestDispatcher;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+import javax.servlet.ServletInputStream;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+import javax.servlet.http.Part;
+import javax.servlet.http.ProtocolHandler;
+
+import org.apache.tomcat.util.res.StringManager;
+
+/**
+ * Wrapper for the HttpServletRequest object that allows the underlying request
+ * object to be invalidated.
+ */
+public class WsHttpServletRequestWrapper implements HttpServletRequest {
+
+    private static final StringManager sm =
+            StringManager.getManager(Constants.Package);
+
+    private HttpServletRequest request;
+
+    public WsHttpServletRequestWrapper(HttpServletRequest request) {
+        this.request = request;
+    }
+
+    private HttpServletRequest getRequest() {
+        if (request == null) {
+            throw new IllegalStateException(sm.getString("wrapper.invalid"));
+        }
+        return request;
+    }
+
+    protected void invalidate() {
+        request = null;
+    }
+
+    @Override
+    public Object getAttribute(String name) {
+        return getRequest().getAttribute(name);
+    }
+
+    @Override
+    public Enumeration<String> getAttributeNames() {
+        return getRequest().getAttributeNames();
+    }
+
+    @Override
+    public String getCharacterEncoding() {
+        return getRequest().getCharacterEncoding();
+    }
+
+    @Override
+    public void setCharacterEncoding(String env)
+            throws UnsupportedEncodingException {
+        getRequest().setCharacterEncoding(env);
+    }
+
+    @Override
+    public int getContentLength() {
+        return getRequest().getContentLength();
+    }
+
+    @Override
+    public long getContentLengthLong() {
+        return getRequest().getContentLengthLong();
+    }
+
+    @Override
+    public String getContentType() {
+        return getRequest().getContentType();
+    }
+
+    @Override
+    public ServletInputStream getInputStream() throws IOException {
+        return getRequest().getInputStream();
+    }
+
+    @Override
+    public String getParameter(String name) {
+        return getRequest().getParameter(name);
+    }
+
+    @Override
+    public Enumeration<String> getParameterNames() {
+        return getRequest().getParameterNames();
+    }
+
+    @Override
+    public String[] getParameterValues(String name) {
+        return getRequest().getParameterValues(name);
+    }
+
+    @Override
+    public Map<String, String[]> getParameterMap() {
+        return getRequest().getParameterMap();
+    }
+
+    @Override
+    public String getProtocol() {
+        return getRequest().getProtocol();
+    }
+
+    @Override
+    public String getScheme() {
+        return getRequest().getScheme();
+    }
+
+    @Override
+    public String getServerName() {
+        return getRequest().getServerName();
+    }
+
+    @Override
+    public int getServerPort() {
+        return getRequest().getServerPort();
+    }
+
+    @Override
+    public BufferedReader getReader() throws IOException {
+        return getRequest().getReader();
+    }
+
+    @Override
+    public String getRemoteAddr() {
+        return getRequest().getRemoteAddr();
+    }
+
+    @Override
+    public String getRemoteHost() {
+        return getRequest().getRemoteHost();
+    }
+
+    @Override
+    public void setAttribute(String name, Object o) {
+        getRequest().setAttribute(name, o);
+    }
+
+    @Override
+    public void removeAttribute(String name) {
+        getRequest().removeAttribute(name);
+    }
+
+    @Override
+    public Locale getLocale() {
+        return getRequest().getLocale();
+    }
+
+    @Override
+    public Enumeration<Locale> getLocales() {
+        return getRequest().getLocales();
+    }
+
+    @Override
+    public boolean isSecure() {
+        return getRequest().isSecure();
+    }
+
+    @Override
+    public RequestDispatcher getRequestDispatcher(String path) {
+        return getRequest().getRequestDispatcher(path);
+    }
+
+    @Override
+    @Deprecated
+    public String getRealPath(String path) {
+        return getRequest().getRealPath(path);
+    }
+
+    @Override
+    public int getRemotePort() {
+        return getRequest().getRemotePort();
+    }
+
+    @Override
+    public String getLocalName() {
+        return getRequest().getLocalName();
+    }
+
+    @Override
+    public String getLocalAddr() {
+        return getRequest().getLocalAddr();
+    }
+
+    @Override
+    public int getLocalPort() {
+        return getRequest().getLocalPort();
+    }
+
+    @Override
+    public ServletContext getServletContext() {
+        return getRequest().getServletContext();
+    }
+
+    @Override
+    public AsyncContext startAsync() throws IllegalStateException {
+        return getRequest().startAsync();
+    }
+
+    @Override
+    public AsyncContext startAsync(ServletRequest servletRequest,
+            ServletResponse servletResponse) throws IllegalStateException {
+        return getRequest().startAsync(servletRequest, servletResponse);
+    }
+
+    @Override
+    public boolean isAsyncStarted() {
+        return getRequest().isAsyncStarted();
+    }
+
+    @Override
+    public boolean isAsyncSupported() {
+        return getRequest().isAsyncSupported();
+    }
+
+    @Override
+    public AsyncContext getAsyncContext() {
+        return getRequest().getAsyncContext();
+    }
+
+    @Override
+    public DispatcherType getDispatcherType() {
+        return getRequest().getDispatcherType();
+    }
+
+    @Override
+    public String getAuthType() {
+        return getRequest().getAuthType();
+    }
+
+    @Override
+    public Cookie[] getCookies() {
+        return getRequest().getCookies();
+    }
+
+    @Override
+    public long getDateHeader(String name) {
+        return getRequest().getDateHeader(name);
+    }
+
+    @Override
+    public String getHeader(String name) {
+        return getRequest().getHeader(name);
+    }
+
+    @Override
+    public Enumeration<String> getHeaders(String name) {
+        return getRequest().getHeaders(name);
+    }
+
+    @Override
+    public Enumeration<String> getHeaderNames() {
+        return getRequest().getHeaderNames();
+    }
+
+    @Override
+    public int getIntHeader(String name) {
+        return getRequest().getIntHeader(name);
+    }
+
+    @Override
+    public String getMethod() {
+        return getRequest().getMethod();
+    }
+
+    @Override
+    public String getPathInfo() {
+        return getRequest().getPathInfo();
+    }
+
+    @Override
+    public String getPathTranslated() {
+        return getRequest().getPathTranslated();
+    }
+
+    @Override
+    public String getContextPath() {
+        return getRequest().getContextPath();
+    }
+
+    @Override
+    public String getQueryString() {
+        return getRequest().getQueryString();
+    }
+
+    @Override
+    public String getRemoteUser() {
+        return getRequest().getRemoteUser();
+    }
+
+    @Override
+    public boolean isUserInRole(String role) {
+        return getRequest().isUserInRole(role);
+    }
+
+    @Override
+    public Principal getUserPrincipal() {
+        return getRequest().getUserPrincipal();
+    }
+
+    @Override
+    public String getRequestedSessionId() {
+        return getRequest().getRequestedSessionId();
+    }
+
+    @Override
+    public String getRequestURI() {
+        return getRequest().getRequestURI();
+    }
+
+    @Override
+    public StringBuffer getRequestURL() {
+        return getRequest().getRequestURL();
+    }
+
+    @Override
+    public String getServletPath() {
+        return getRequest().getServletPath();
+    }
+
+    @Override
+    public HttpSession getSession(boolean create) {
+        return getRequest().getSession(create);
+    }
+
+    @Override
+    public HttpSession getSession() {
+        return getRequest().getSession();
+    }
+
+    @Override
+    public boolean isRequestedSessionIdValid() {
+        return getRequest().isRequestedSessionIdValid();
+    }
+
+    @Override
+    public boolean isRequestedSessionIdFromCookie() {
+        return getRequest().isRequestedSessionIdFromCookie();
+    }
+
+    @Override
+    public boolean isRequestedSessionIdFromURL() {
+        return getRequest().isRequestedSessionIdFromURL();
+    }
+
+    @Override
+    @Deprecated
+    public boolean isRequestedSessionIdFromUrl() {
+        return getRequest().isRequestedSessionIdFromUrl();
+    }
+
+    @Override
+    public boolean authenticate(HttpServletResponse response)
+            throws IOException, ServletException {
+        return getRequest().authenticate(response);
+    }
+
+    @Override
+    public void login(String username, String password) throws ServletException {
+        getRequest().login(username, password);
+    }
+
+    @Override
+    public void logout() throws ServletException {
+        getRequest().logout();
+    }
+
+    @Override
+    public Collection<Part> getParts() throws IOException, ServletException {
+        return getRequest().getParts();
+    }
+
+    @Override
+    public Part getPart(String name) throws IOException, ServletException {
+        return getRequest().getPart(name);
+    }
+
+    @Override
+    public void upgrade(ProtocolHandler handler) throws IOException {
+        getRequest().upgrade(handler);
+    }
+}

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

Modified: tomcat/trunk/test/org/apache/catalina/websocket/TestWebSocket.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/websocket/TestWebSocket.java?rev=1354469&r1=1354468&r2=1354469&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/catalina/websocket/TestWebSocket.java (original)
+++ tomcat/trunk/test/org/apache/catalina/websocket/TestWebSocket.java Wed Jun 27 12:46:05
2012
@@ -36,6 +36,7 @@ import java.util.List;
 
 import javax.naming.InitialContext;
 import javax.naming.NamingException;
+import javax.servlet.http.HttpServletRequest;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
@@ -296,7 +297,8 @@ public class TestWebSocket extends Tomca
         private static final long serialVersionUID = 1L;
 
         @Override
-        protected StreamInbound createWebSocketInbound(String subProtocol) {
+        protected StreamInbound createWebSocketInbound(String subProtocol,
+                HttpServletRequest request) {
             return new Bug53339WsInbound();
         }
     }

Modified: tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/chat/ChatWebSocketServlet.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/chat/ChatWebSocketServlet.java?rev=1354469&r1=1354468&r2=1354469&view=diff
==============================================================================
--- tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/chat/ChatWebSocketServlet.java
(original)
+++ tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/chat/ChatWebSocketServlet.java
Wed Jun 27 12:46:05 2012
@@ -23,6 +23,8 @@ import java.util.Set;
 import java.util.concurrent.CopyOnWriteArraySet;
 import java.util.concurrent.atomic.AtomicInteger;
 
+import javax.servlet.http.HttpServletRequest;
+
 import org.apache.catalina.websocket.MessageInbound;
 import org.apache.catalina.websocket.StreamInbound;
 import org.apache.catalina.websocket.WebSocketServlet;
@@ -44,7 +46,8 @@ public class ChatWebSocketServlet extend
             new CopyOnWriteArraySet<ChatMessageInbound>();
 
     @Override
-    protected StreamInbound createWebSocketInbound(String subProtocol) {
+    protected StreamInbound createWebSocketInbound(String subProtocol,
+            HttpServletRequest request) {
         return new ChatMessageInbound(connectionIds.incrementAndGet());
     }
 

Modified: tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/echo/EchoMessage.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/echo/EchoMessage.java?rev=1354469&r1=1354468&r2=1354469&view=diff
==============================================================================
--- tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/echo/EchoMessage.java (original)
+++ tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/echo/EchoMessage.java Wed Jun
27 12:46:05 2012
@@ -21,6 +21,7 @@ import java.nio.ByteBuffer;
 import java.nio.CharBuffer;
 
 import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
 
 import org.apache.catalina.websocket.MessageInbound;
 import org.apache.catalina.websocket.StreamInbound;
@@ -59,7 +60,8 @@ public class EchoMessage extends WebSock
 
 
     @Override
-    protected StreamInbound createWebSocketInbound(String subProtocol) {
+    protected StreamInbound createWebSocketInbound(String subProtocol,
+            HttpServletRequest request) {
         return new EchoMessageInbound(byteBufSize,charBufSize);
     }
 

Modified: tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/echo/EchoStream.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/echo/EchoStream.java?rev=1354469&r1=1354468&r2=1354469&view=diff
==============================================================================
--- tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/echo/EchoStream.java (original)
+++ tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/echo/EchoStream.java Wed Jun 27
12:46:05 2012
@@ -20,6 +20,8 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.Reader;
 
+import javax.servlet.http.HttpServletRequest;
+
 import org.apache.catalina.websocket.StreamInbound;
 import org.apache.catalina.websocket.WebSocketServlet;
 import org.apache.catalina.websocket.WsOutbound;
@@ -30,7 +32,8 @@ public class EchoStream extends WebSocke
     private static final long serialVersionUID = 1L;
 
     @Override
-    protected StreamInbound createWebSocketInbound(String subProtocol) {
+    protected StreamInbound createWebSocketInbound(String subProtocol,
+            HttpServletRequest request) {
         return new EchoStreamInbound();
     }
 

Modified: tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/snake/SnakeWebSocketServlet.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/snake/SnakeWebSocketServlet.java?rev=1354469&r1=1354468&r2=1354469&view=diff
==============================================================================
--- tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/snake/SnakeWebSocketServlet.java
(original)
+++ tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/snake/SnakeWebSocketServlet.java
Wed Jun 27 12:46:05 2012
@@ -30,6 +30,7 @@ import java.util.concurrent.ConcurrentHa
 import java.util.concurrent.atomic.AtomicInteger;
 
 import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
 
 import org.apache.catalina.websocket.MessageInbound;
 import org.apache.catalina.websocket.StreamInbound;
@@ -148,7 +149,8 @@ public class SnakeWebSocketServlet exten
     }
 
     @Override
-    protected StreamInbound createWebSocketInbound(String subProtocol) {
+    protected StreamInbound createWebSocketInbound(String subProtocol,
+            HttpServletRequest request) {
         return new SnakeMessageInbound(connectionIds.incrementAndGet());
     }
 



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


Mime
View raw message