Improve access to WebSocketSession fields

Ensure the Standard- and the JettyWebSocketSession 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-03-31 16:35:40 +02:00
parent e21c47d4ce
commit 7b014eaa55
9 changed files with 280 additions and 12 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

@@ -54,7 +54,9 @@ public class StandardWebSocketSession extends AbstractWebSocketSession<Session>
private final InetSocketAddress remoteAddress;
private final Principal user;
private Principal user;
private String acceptedProtocol;
private List<WebSocketExtension> extensions;
@@ -120,7 +122,7 @@ public class StandardWebSocketSession extends AbstractWebSocketSession<Session>
return this.user;
}
checkNativeSessionInitialized();
return getNativeSession().getUserPrincipal();
return (isOpen() ? getNativeSession().getUserPrincipal() : null);
}
@Override
@@ -136,8 +138,7 @@ public class StandardWebSocketSession extends AbstractWebSocketSession<Session>
@Override
public String getAcceptedProtocol() {
checkNativeSessionInitialized();
String protocol = getNativeSession().getNegotiatedSubprotocol();
return StringUtils.isEmpty(protocol)? null : protocol;
return this.acceptedProtocol;
}
@Override
@@ -182,6 +183,15 @@ public class StandardWebSocketSession extends AbstractWebSocketSession<Session>
return (getNativeSession() != null && getNativeSession().isOpen());
}
@Override
public void initializeNativeSession(Session session) {
super.initializeNativeSession(session);
if(this.user == null) {
this.user = session.getUserPrincipal();
}
this.acceptedProtocol = session.getNegotiatedSubprotocol();
}
@Override
protected void sendTextMessage(TextMessage message) throws IOException {
getNativeSession().getBasicRemote().sendText(message.getPayload(), message.isLast());