Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jetty-users] Accepted subprotocol is not provided when @OnWebSocketConnect method is invoked

Hi,

I'm following the example [1] for Jetty WebSocket Server API.

In my WebSocketCreator implementation I'm setting the accepted subprotocol to the response:


response.setAcceptedSubProtocol(subprotocol);


Then in my @WebSocket implementation, I expect in the method annotated with @OnWebSocketConnect when I call
the statement below to receive the subprotocol that was set earlier by the WebSocketCreator:


session.getUpgradeResponse().getAcceptedSubProtocol();


With the latest available Jetty 9.4.x this call returns "null".

I think that the following line [2] causes this "null" to be returned. When the specified key is not available in the map, the default is returned, but this default is not added to the map.

With the patch below when I call getAcceptedSubProtocol, the expected value is returned.

diff --git a/jetty-websocket/websocket-servlet/src/main/java/org/eclipse/jetty/websocket/servlet/ServletUpgradeResponse.java b/jetty-websocket/websocket-servlet/src/main/java/org/eclipse/jetty/websocket/servlet/ServletUpgradeResponse.java
index ff6846f..b44daf7 100644
--- a/jetty-websocket/websocket-servlet/src/main/java/org/eclipse/jetty/websocket/servlet/ServletUpgradeResponse.java
+++ b/jetty-websocket/websocket-servlet/src/main/java/org/eclipse/jetty/websocket/servlet/ServletUpgradeResponse.java
@@ -111,7 +111,12 @@ public class ServletUpgradeResponse implements UpgradeResponse
         {
             String name = entry.getKey();
             Collection<String> prepend = entry.getValue();
-            List<String> values = headers.getOrDefault(name,headers.containsKey(name)?null:new ArrayList<>());
+            List<String> values = headers.get(name);
+            if (values==null)
+            {
+                values = new ArrayList<>();
+                headers.put(name,values);
+            }
             values.addAll(0,prepend);
         }
         
What do you think?

Best Regards,
Violeta Georgieva

[1] https://www.eclipse.org/jetty/documentation/9.4.x/jetty-websocket-server-api.html

Back to the top