You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2023/06/20 12:46:41 UTC

[tomcat] 03/03: Add getSession() to SendResult

This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tomcat.git

commit 1b1685b377f547bbece829dfcbf7f93d56624274
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Tue Jun 20 13:46:31 2023 +0100

    Add getSession() to SendResult
    
    This aligns Tomcat 11.0.x with the latest proposals for Jakarta
    WebSocket 2.2 from the Jakarta WebSocket project.
---
 .../tomcat/websocket/WsRemoteEndpointImplBase.java | 24 ++++++++++--------
 .../websocket/WsRemoteEndpointImplClient.java      |  7 +++---
 java/org/apache/tomcat/websocket/WsSession.java    |  4 +--
 .../server/WsRemoteEndpointImplServer.java         | 29 ++++++++++++----------
 webapps/docs/changelog.xml                         |  6 +++++
 5 files changed, 42 insertions(+), 28 deletions(-)

diff --git a/java/org/apache/tomcat/websocket/WsRemoteEndpointImplBase.java b/java/org/apache/tomcat/websocket/WsRemoteEndpointImplBase.java
index 5dc9298b6e..5717a7bfca 100644
--- a/java/org/apache/tomcat/websocket/WsRemoteEndpointImplBase.java
+++ b/java/org/apache/tomcat/websocket/WsRemoteEndpointImplBase.java
@@ -58,8 +58,6 @@ public abstract class WsRemoteEndpointImplBase implements RemoteEndpoint {
 
     protected static final StringManager sm = StringManager.getManager(WsRemoteEndpointImplBase.class);
 
-    protected static final SendResult SENDRESULT_OK = new SendResult();
-
     private final Log log = LogFactory.getLog(WsRemoteEndpointImplBase.class); // must not be static
 
     private final StateMachine stateMachine = new StateMachine();
@@ -104,6 +102,11 @@ public abstract class WsRemoteEndpointImplBase implements RemoteEndpoint {
     }
 
 
+    protected WsSession getSession() {
+        return wsSession;
+    }
+
+
     @Override
     public void setBatchingAllowed(boolean batchingAllowed) throws IOException {
         boolean oldValue = this.batchingAllowed.getAndSet(batchingAllowed);
@@ -362,7 +365,7 @@ public abstract class WsRemoteEndpointImplBase implements RemoteEndpoint {
         try {
             messageParts = transformation.sendMessagePart(messageParts);
         } catch (IOException ioe) {
-            handler.onResult(new SendResult(ioe));
+            handler.onResult(new SendResult(getSession(), ioe));
             return;
         }
 
@@ -370,7 +373,7 @@ public abstract class WsRemoteEndpointImplBase implements RemoteEndpoint {
         // that no message parts will be returned. If this is the case the
         // trigger the supplied SendHandler
         if (messageParts.size() == 0) {
-            handler.onResult(new SendResult());
+            handler.onResult(new SendResult(getSession()));
             return;
         }
 
@@ -671,7 +674,7 @@ public abstract class WsRemoteEndpointImplBase implements RemoteEndpoint {
                 try (Writer w = getSendWriter()) {
                     ((Encoder.TextStream) encoder).encode(obj, w);
                 }
-                completion.onResult(new SendResult());
+                completion.onResult(new SendResult(getSession()));
             } else if (encoder instanceof Encoder.Binary) {
                 ByteBuffer msg = ((Encoder.Binary) encoder).encode(obj);
                 sendBytesByCompletion(msg, completion);
@@ -679,12 +682,12 @@ public abstract class WsRemoteEndpointImplBase implements RemoteEndpoint {
                 try (OutputStream os = getSendStream()) {
                     ((Encoder.BinaryStream) encoder).encode(obj, os);
                 }
-                completion.onResult(new SendResult());
+                completion.onResult(new SendResult(getSession()));
             } else {
                 throw new EncodeException(obj, sm.getString("wsRemoteEndpoint.noEncoder", obj.getClass()));
             }
         } catch (Exception e) {
-            SendResult sr = new SendResult(e);
+            SendResult sr = new SendResult(getSession(), e);
             completion.onResult(sr);
         }
     }
@@ -848,7 +851,8 @@ public abstract class WsRemoteEndpointImplBase implements RemoteEndpoint {
             } else if (!result.isOK()) {
                 handler.onResult(result);
             } else if (closed) {
-                SendResult sr = new SendResult(new IOException(sm.getString("wsRemoteEndpoint.closedDuringMessage")));
+                SendResult sr = new SendResult(getSession(),
+                        new IOException(sm.getString("wsRemoteEndpoint.closedDuringMessage")));
                 handler.onResult(sr);
             } else {
                 write();
@@ -933,12 +937,12 @@ public abstract class WsRemoteEndpointImplBase implements RemoteEndpoint {
             if (flushRequired) {
                 outputBuffer.flip();
                 if (outputBuffer.remaining() == 0) {
-                    handler.onResult(SENDRESULT_OK);
+                    handler.onResult(new SendResult(endpoint.getSession()));
                 } else {
                     endpoint.doWrite(this, blockingWriteTimeoutExpiry, outputBuffer);
                 }
             } else {
-                handler.onResult(SENDRESULT_OK);
+                handler.onResult(new SendResult(endpoint.getSession()));
             }
         }
 
diff --git a/java/org/apache/tomcat/websocket/WsRemoteEndpointImplClient.java b/java/org/apache/tomcat/websocket/WsRemoteEndpointImplClient.java
index 5f0cc67fc3..7b0386224d 100644
--- a/java/org/apache/tomcat/websocket/WsRemoteEndpointImplClient.java
+++ b/java/org/apache/tomcat/websocket/WsRemoteEndpointImplClient.java
@@ -54,7 +54,8 @@ public class WsRemoteEndpointImplClient extends WsRemoteEndpointImplBase {
             } else {
                 timeout = blockingWriteTimeoutExpiry - System.currentTimeMillis();
                 if (timeout < 0) {
-                    SendResult sr = new SendResult(new IOException(sm.getString("wsRemoteEndpoint.writeTimeout")));
+                    SendResult sr = new SendResult(getSession(),
+                            new IOException(sm.getString("wsRemoteEndpoint.writeTimeout")));
                     handler.onResult(sr);
                 }
             }
@@ -62,11 +63,11 @@ public class WsRemoteEndpointImplClient extends WsRemoteEndpointImplBase {
             try {
                 channel.write(byteBuffer).get(timeout, TimeUnit.MILLISECONDS);
             } catch (InterruptedException | ExecutionException | TimeoutException e) {
-                handler.onResult(new SendResult(e));
+                handler.onResult(new SendResult(getSession(), e));
                 return;
             }
         }
-        handler.onResult(SENDRESULT_OK);
+        handler.onResult(new SendResult(getSession()));
     }
 
 
diff --git a/java/org/apache/tomcat/websocket/WsSession.java b/java/org/apache/tomcat/websocket/WsSession.java
index 0c75f92184..41cc389eae 100644
--- a/java/org/apache/tomcat/websocket/WsSession.java
+++ b/java/org/apache/tomcat/websocket/WsSession.java
@@ -598,7 +598,7 @@ public class WsSession implements Session {
 
         // Fail any uncompleted messages.
         IOException ioe = new IOException(sm.getString("wsSession.messageFailed"));
-        SendResult sr = new SendResult(ioe);
+        SendResult sr = new SendResult(this, ioe);
         for (FutureToSendHandler f2sh : futures.keySet()) {
             f2sh.onResult(sr);
         }
@@ -825,7 +825,7 @@ public class WsSession implements Session {
         // second and subsequent attempts are ignored.
 
         IOException ioe = new IOException(sm.getString("wsSession.messageFailed"));
-        SendResult sr = new SendResult(ioe);
+        SendResult sr = new SendResult(this, ioe);
         f2sh.onResult(sr);
     }
 
diff --git a/java/org/apache/tomcat/websocket/server/WsRemoteEndpointImplServer.java b/java/org/apache/tomcat/websocket/server/WsRemoteEndpointImplServer.java
index 31779aa32a..396de502e3 100644
--- a/java/org/apache/tomcat/websocket/server/WsRemoteEndpointImplServer.java
+++ b/java/org/apache/tomcat/websocket/server/WsRemoteEndpointImplServer.java
@@ -38,6 +38,7 @@ import org.apache.tomcat.util.res.StringManager;
 import org.apache.tomcat.websocket.Constants;
 import org.apache.tomcat.websocket.Transformation;
 import org.apache.tomcat.websocket.WsRemoteEndpointImplBase;
+import org.apache.tomcat.websocket.WsSession;
 
 /**
  * This is the server side {@link jakarta.websocket.RemoteEndpoint} implementation - i.e. what the server uses to send
@@ -144,7 +145,7 @@ public class WsRemoteEndpointImplServer extends WsRemoteEndpointImplBase {
             if (block) {
                 timeout = blockingWriteTimeoutExpiry - System.currentTimeMillis();
                 if (timeout <= 0) {
-                    SendResult sr = new SendResult(new SocketTimeoutException());
+                    SendResult sr = new SendResult(getSession(), new SocketTimeoutException());
                     handler.onResult(sr);
                     return;
                 }
@@ -166,7 +167,7 @@ public class WsRemoteEndpointImplServer extends WsRemoteEndpointImplBase {
                                 if (timeout <= 0) {
                                     failed(new SocketTimeoutException(), null);
                                 } else {
-                                    handler.onResult(SENDRESULT_OK);
+                                    handler.onResult(new SendResult(getSession()));
                                 }
                             } else {
                                 wsWriteTimeout.unregister(WsRemoteEndpointImplServer.this);
@@ -177,7 +178,7 @@ public class WsRemoteEndpointImplServer extends WsRemoteEndpointImplBase {
                         @Override
                         public void failed(Throwable exc, Void attachment) {
                             if (block) {
-                                SendResult sr = new SendResult(exc);
+                                SendResult sr = new SendResult(getSession(), exc);
                                 handler.onResult(sr);
                             } else {
                                 wsWriteTimeout.unregister(WsRemoteEndpointImplServer.this);
@@ -199,7 +200,7 @@ public class WsRemoteEndpointImplServer extends WsRemoteEndpointImplBase {
                     for (ByteBuffer buffer : buffers) {
                         long timeout = blockingWriteTimeoutExpiry - System.currentTimeMillis();
                         if (timeout <= 0) {
-                            SendResult sr = new SendResult(new SocketTimeoutException());
+                            SendResult sr = new SendResult(getSession(), new SocketTimeoutException());
                             handler.onResult(sr);
                             return;
                         }
@@ -208,15 +209,15 @@ public class WsRemoteEndpointImplServer extends WsRemoteEndpointImplBase {
                     }
                     long timeout = blockingWriteTimeoutExpiry - System.currentTimeMillis();
                     if (timeout <= 0) {
-                        SendResult sr = new SendResult(new SocketTimeoutException());
+                        SendResult sr = new SendResult(getSession(), new SocketTimeoutException());
                         handler.onResult(sr);
                         return;
                     }
                     socketWrapper.setWriteTimeout(timeout);
                     socketWrapper.flush(true);
-                    handler.onResult(SENDRESULT_OK);
+                    handler.onResult(new SendResult(getSession()));
                 } catch (IOException e) {
-                    SendResult sr = new SendResult(e);
+                    SendResult sr = new SendResult(getSession(), e);
                     handler.onResult(sr);
                 }
             }
@@ -341,7 +342,7 @@ public class WsRemoteEndpointImplServer extends WsRemoteEndpointImplBase {
         buffers = null;
         if (sh != null) {
             if (useDispatch) {
-                OnResultRunnable r = new OnResultRunnable(sh, t);
+                OnResultRunnable r = new OnResultRunnable(getSession(), sh, t);
                 try {
                     socketWrapper.execute(r);
                 } catch (RejectedExecutionException ree) {
@@ -356,9 +357,9 @@ public class WsRemoteEndpointImplServer extends WsRemoteEndpointImplBase {
                 }
             } else {
                 if (t == null) {
-                    sh.onResult(new SendResult());
+                    sh.onResult(new SendResult(getSession()));
                 } else {
-                    sh.onResult(new SendResult(t));
+                    sh.onResult(new SendResult(getSession(), t));
                 }
             }
         }
@@ -373,10 +374,12 @@ public class WsRemoteEndpointImplServer extends WsRemoteEndpointImplBase {
 
     private static class OnResultRunnable implements Runnable {
 
+        private final WsSession session;
         private final SendHandler sh;
         private final Throwable t;
 
-        private OnResultRunnable(SendHandler sh, Throwable t) {
+        private OnResultRunnable(WsSession session, SendHandler sh, Throwable t) {
+            this.session = session;
             this.sh = sh;
             this.t = t;
         }
@@ -384,9 +387,9 @@ public class WsRemoteEndpointImplServer extends WsRemoteEndpointImplBase {
         @Override
         public void run() {
             if (t == null) {
-                sh.onResult(new SendResult());
+                sh.onResult(new SendResult(session));
             } else {
-                sh.onResult(new SendResult(t));
+                sh.onResult(new SendResult(session, t));
             }
         }
     }
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 880e0fc70d..17da9051e3 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -146,6 +146,12 @@
         WebSocket session could return false for <code>onOpen()</code> before
         the <code>onClose()</code> event had been completed. (markt)
       </fix>
+      <add>
+        Update the WebSocket API provided by Tomcat to align with the latest
+        proposals from the Jakarta WebSocket project and make the WebSocket
+        <code>Session</code> instance available via <code>SendResult</code>.
+        (markt)
+      </add>
     </changelog>
   </subsection>
   <subsection name="Web applications">


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