0

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.

vferrer
  • 31
  • 7
  • Can you share whole project or at least your web.xml? Session.getUserPrincipal() returns what is returned from HttpServletRequest and my guess is that your application config wont work for simple servlet (you can try it). TimeoutException somehow confirms it, seems like your client does NOT reach to websocket layer at all. – Pavel Bucek Feb 08 '14 at 10:43
  • Thanks again @PavelBucek, I´ve added the web-app.xml file. The "/chat" path is not in my secured resources because if I add it, the Websocket doesn´t invoke the OnOpen method. But of course, I´ve tested the application with and without the "/chat" path in the web-resource-collection; I´ve edited the question adding it. With this configuration, trying to open the websocket connection fires TimeoutException as I mentioned above. In this case, what do you think I could do? – vferrer Feb 08 '14 at 13:35
  • Hi again @PavelBucek.I´ve been reading the Tyrus documentation and searching the reason why my websocket doesn't work when I include its path in the web-resource-collection but I hadn´t success. I've seen a post in your Oracle's Blog talking about the wss configuration and I tried it, but that was also unsuccessful. What do you think it could be wrong? – vferrer Feb 10 '14 at 18:53
  • hmm.. thats weird. Can you share your project somehow? git repo or you can send it to me directly - pavel.bucek [at] oracle.com. (it can be more complex, but I need to be able to deploy and test it easily, so please no db or further setup) – Pavel Bucek Feb 11 '14 at 22:22
  • When I´ve been preparing a small project to share it... it worked! When this has occurred, I´ve been looking for an explanation. I tested the new code in my main project and it worked too! I didn´t understand anything. Then I reviewed my code one more time and I saw the difference. The code that didn´t work was accessing by using this URI ws://192.168.0.201:8080/chat/chat but the demo project that worked well used the localhost version: ws://localhost:8080/chat/chat. The mistake was on the use of the IP in place of localhost or 127.0.0.1. ¿Is that a normal behavior? – vferrer Feb 12 '14 at 20:18
  • You might have had issues with Origin check. Or the network layer configuration in glassfish (it might open ports for listening just on localhost and not on the interface to which 192.168.0.201 is attached). – Pavel Bucek Feb 12 '14 at 22:12
  • Anyway, I'm glad you could get your app working :-) – Pavel Bucek Feb 12 '14 at 22:12

0 Answers0