Update JettyWebSocketSession
Ensure the JettyWebSocket session can return the Principal and accepted WebSocket sub-protocol even after the session is closed. Issue: SPR-11621
This commit is contained in:
@@ -50,7 +50,9 @@ public class JettyWebSocketSession extends AbstractWebSocketSession<Session> {
|
||||
|
||||
private List<WebSocketExtension> extensions;
|
||||
|
||||
private final Principal user;
|
||||
private Principal user;
|
||||
|
||||
private String acceptedProtocol;
|
||||
|
||||
|
||||
/**
|
||||
@@ -105,7 +107,7 @@ public class JettyWebSocketSession extends AbstractWebSocketSession<Session> {
|
||||
return this.user;
|
||||
}
|
||||
checkNativeSessionInitialized();
|
||||
return getNativeSession().getUpgradeRequest().getUserPrincipal();
|
||||
return (isOpen() ? getNativeSession().getUpgradeRequest().getUserPrincipal() : null);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -123,7 +125,7 @@ public class JettyWebSocketSession extends AbstractWebSocketSession<Session> {
|
||||
@Override
|
||||
public String getAcceptedProtocol() {
|
||||
checkNativeSessionInitialized();
|
||||
return getNativeSession().getUpgradeResponse().getAcceptedSubProtocol();
|
||||
return this.acceptedProtocol;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -168,6 +170,15 @@ public class JettyWebSocketSession extends AbstractWebSocketSession<Session> {
|
||||
return ((getNativeSession() != null) && getNativeSession().isOpen());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initializeNativeSession(Session session) {
|
||||
super.initializeNativeSession(session);
|
||||
if (this.user == null) {
|
||||
this.user = session.getUpgradeRequest().getUserPrincipal();
|
||||
}
|
||||
this.acceptedProtocol = session.getUpgradeResponse().getAcceptedSubProtocol();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void sendTextMessage(TextMessage message) throws IOException {
|
||||
getNativeSession().getRemote().sendString(message.getPayload());
|
||||
|
||||
@@ -122,7 +122,7 @@ public class StandardWebSocketSession extends AbstractWebSocketSession<Session>
|
||||
return this.user;
|
||||
}
|
||||
checkNativeSessionInitialized();
|
||||
return getNativeSession().getUserPrincipal();
|
||||
return (isOpen() ? getNativeSession().getUserPrincipal() : null);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user