Accessing ServletContext and HttpSession in @OnMessage of a JSR-356 @ServerEndpoint

前端 未结 4 2157
小鲜肉
小鲜肉 2020-12-05 04:29

I need to get the ServletContext from inside a @ServerEndpoint in order to find Spring ApplicationContext and lookup for a Bean.

相关标签:
4条回答
  • 2020-12-05 05:11

    Updated code for BalusC's answer, the onOpen method needs to be decorated with @OnOpen. Then there is no need anymore to extend the Endpoint class:

    @ServerEndpoint(value="/your_socket", configurator=ServletAwareConfig.class)
    public class YourSocket {
    
        private EndpointConfig config;
    
        @OnOpen
        public void onOpen(Session websocketSession, EndpointConfig config) {
            this.config = config;
        }
    
        @OnMessage
        public void onMessage(String message) {
            HttpSession httpSession = (HttpSession) config.getUserProperties().get("httpSession");
            ServletContext servletContext = httpSession.getServletContext();
            // ...
        }
    
    }
    
    0 讨论(0)
  • 2020-12-05 05:18

    The servlet HttpSession is in JSR-356 available by HandshakeRequest#getHttpSession() which is in turn available when a handshake request is made right before @OnOpen of a @ServerEndpoint. The ServletContext is in turn just available via HttpSession#getServletContext(). That's two birds with one stone.

    In order to capture the handshake request, implement a ServerEndpointConfig.Configurator and override the modifyHandshake() method. The HandshakeRequest is here available as method argument. You can put the HttpSession into EndpointConfig#getUserProperties(). The EndpointConfig is in turn available as method argument @OnOpen.

    Here's a kickoff example of the ServerEndpointConfig.Configurator implementation:

    public class ServletAwareConfig extends ServerEndpointConfig.Configurator {
    
        @Override
        public void modifyHandshake(ServerEndpointConfig config, HandshakeRequest request, HandshakeResponse response) {
            HttpSession httpSession = (HttpSession) request.getHttpSession();
            config.getUserProperties().put("httpSession", httpSession);
        }
    
    }
    

    Here's how you can use it, note the configurator attribute of the @ServerEndpoint:

    @ServerEndpoint(value="/your_socket", configurator=ServletAwareConfig.class)
    public class YourSocket {
    
        private EndpointConfig config;
    
        @OnOpen
        public void onOpen(Session websocketSession, EndpointConfig config) {
            this.config = config;
        }
    
        @OnMessage
        public void onMessage(String message) {
            HttpSession httpSession = (HttpSession) config.getUserProperties().get("httpSession");
            ServletContext servletContext = httpSession.getServletContext();
            // ...
        }
    
    }
    

    As a design hint, it's the best to keep your @ServerEndpoint fully free of servlet API dependencies. You'd in the modifyHandshake() implementation better immediately extract exactly that information (usually a mutable Javabean) you need from the servlet session or context and put them in the user properties map instead. If you don't do that, then you should keep in mind that a websocket session can live longer than the HTTP session. So when you still carry around HttpSession into the endpoint, then you may run into IllegalStateException when you try to access it while it's being expired.

    In case you happen to have CDI (and perhaps JSF) at hands, you may get inspiration from the source code of OmniFaces <o:socket> (links are at very bottom of showcase).

    See also:

    • Real time updates from database using JSF/Java EE
    • Notify only specific user(s) through WebSockets, when something is modified in the database
    0 讨论(0)
  • 2020-12-05 05:30

    I tried out BalusC's answer on Tomcat (Versions 7.0.56 and 8.0.14). On both containers, the modifyHandshake's request parameter does not contain a HttpSession (and thus no servletContext). As I needed the servlet context only to access "global" variables (web-application global, that is), I just stored these variables in an ordinary static field of a holder class. This is inelegant, but it worked.

    That ooks like a bug in this specific tomcat versions - has anyone out there also seen this?

    0 讨论(0)
  • 2020-12-05 05:32

    Somtimes we can't get session with above ServletAwareConfig of BalusC, this is because that the session is still not created. since we are not seek for session but servletContext, in tomcat we can do as below:

    public static class ServletAwareConfig extends ServerEndpointConfig.Configurator {
    
        @Override
        public void modifyHandshake(ServerEndpointConfig config, HandshakeRequest request, HandshakeResponse response) {
            try {
                Field reqfld = request.getClass().getDeclaredField("request");
                reqfld.setAccessible(true);
                HttpServletRequest req = (HttpServletRequest) reqfld.get(request);
                ServletContext ctxt = req.getServletContext();
                Map<String, Object> up = config.getUserProperties();
                up.put("servletContext", ctxt);
            } catch (NoSuchFieldException e) {
            } catch (SecurityException e) {
            } catch (IllegalArgumentException e) {
            } catch (IllegalAccessException e) {
            }
        }
    
    }
    

    if we want init session immediately, we can call request.getSession().

    Ref: Websocket - httpSession returns null

    0 讨论(0)
提交回复
热议问题