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:
Rossen Stoyanchev
2014-04-01 13:13:55 -04:00
parent a805f12374
commit 73ecbc047c
6 changed files with 257 additions and 50 deletions

View File

@@ -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());

View File

@@ -122,7 +122,7 @@ public class StandardWebSocketSession extends AbstractWebSocketSession<Session>
return this.user;
}
checkNativeSessionInitialized();
return getNativeSession().getUserPrincipal();
return (isOpen() ? getNativeSession().getUserPrincipal() : null);
}
@Override