I have created a WebSocket with the JEE7 standard API. I have a class annotated with @ServerEndPoint and three methods annotated with @OnOpen, @OnClose and OnMessage. I´m deploying the application in a Glassfish v4 and it works but when I invoke the getUserPrincipal() method from some Session instance, it returns always null. This is the code I´m ussing to test it:
@ServerEndpoint(value = "/chat")
public class ChatServerEndpoint {
private static final Logger LOG = LoggerFactory.getLogger(ChatServerEndpoint.class);
@OnOpen
public void onOpen(Session session) {
LOG.debug("Opening a session with id " + session.getId() + " for " + session.getUserPrincipal());
}
@OnClose
public void onClose(Session session) {
LOG.debug("Closing the session " + session.getId() + " associated with " + session.getUserPrincipal());
}
@OnMessage
public void onMessage(Session session, String msg) {
LOG.debug("There are " + session.getOpenSessions().size() + " opened sessions");
for (Session sess : session.getOpenSessions()) {
LOG.debug("Session " + sess.getId() + " associated with " + sess.getUserPrincipal());
try {
sess.getBasicRemote().sendText(msg);
} catch (IOException e) {
LOG.error("Error sending message", e);
}
}
}
}
This code is as simple as possible to narrow the problem. This is the log output:
22:22:48.638 [http-listener-1(1)] DEBUG e.p.server.rs.ChatServerEndpoint - Opening a session with id 437a9b4d-c694-4a19-b912-47b88da4eb9a for null
22:22:54.350 [http-listener-1(5)] DEBUG e.p.server.rs.ChatServerEndpoint - There are 1 opened sessions
22:22:54.351 [http-listener-1(5)] DEBUG e.p.server.rs.ChatServerEndpoint - Session 437a9b4d-c694-4a19-b912-47b88da4eb9a associated with null
22:23:58.888 [http-listener-1(2)] DEBUG e.p.server.rs.ChatServerEndpoint - Closing the session 437a9b4d-c694-4a19-b912-47b88da4eb9a associated with null
I tried to make it work by including the websocket path in the web-resource-collection of the security-constraint for JAAS configuration (web.xml file) but in this case the access to the websocket from client side fails with a java.util.concurrent.TimeoutException after a while.
This is my web-app.xml file:
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<welcome-file-list>
<welcome-file>Ps.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rs/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>upload</servlet-name>
<servlet-class>es.ps.server.file.FileUploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>upload</servlet-name>
<url-pattern>/upload</url-pattern>
</servlet-mapping>
<login-config>
<auth-method>FORM</auth-method>
<realm-name>psRealm</realm-name>
<form-login-config>
<form-login-page>/login.html</form-login-page>
<form-error-page>/login.html?error=true</form-error-page>
</form-login-config>
</login-config>
<security-constraint>
<web-resource-collection>
<web-resource-name>Secure Pages</web-resource-name>
<description/>
<url-pattern>*.html</url-pattern>
<url-pattern>/Ps/*</url-pattern>
<url-pattern>/rs/*</url-pattern>
<url-pattern>/chat</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>ADMINISTRATOR</role-name>
</auth-constraint>
</security-constraint>
</web-app>
Somebody has had the same problem?
Thanks in advance.