websocket-spec
  1. websocket-spec
  2. WEBSOCKET_SPEC-217

It is not easy to get to the ServletContext in a websocket session

    Details

    • Type: Improvement Improvement
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 1.0
    • Fix Version/s: None
    • Labels:
      None

      Description

      You could use a Configurator for this:

      public class GetServletContextConfigurator extends ServerEndpointConfig.Configurator
      {
      @Override
      public void modifyHandshake(ServerEndpointConfig config,
      HandshakeRequest request,
      HandshakeResponse response)

      { EndpointConfig.getUserProperties().put("ServletContext",((HttpSession)request.getHttpSession()).getServletContext()); }

      }

      But that doesn't work for me, because there is no http session..
      and the above call does not create it. It could be made that that always just creates a session, but can then a jsessionid cookie be send back over that upgraded websocket request so that the browser does use that?

      I need the ServletContext to read files and that kind of stuff right from the WAR. Its impossible to get to that directory (or war file itself if it is not extracted) in side a websocket.

      I think what we need is

      EndpointConfig.getUserProperties().put("ServletContext",request.getServletContext());

      or something like that.

        Activity

        Hide
        Nick Williams added a comment -

        I would suggest adding a getHttpContext (alternatively getApplicationContext) method to WebSocketContainer:

        public interface WebSocketContainer
        {
            ...
            /*
             * Returns the HTTP application context for the current deployed application. This can be anything,
             * but in a Servlet API environment it should be a {@code javax.servlet.ServletContext}.
             * 
             * @return the HTTP application context, such as the {@code ServletContext}, but never {@code null}.
             */
            Object getHttpContext();
            ...
        }
        

        If provided in this manner, getting a ServletContext is as simple as, using a Session as an example, this:

            ...
            @OnOpen
            public void sessionOpened(Session session)
            {
                ServletContext context = (ServletContext)session.getContainer().getHttpContext();
            }
            ...
        
        Show
        Nick Williams added a comment - I would suggest adding a getHttpContext (alternatively getApplicationContext ) method to WebSocketContainer : public interface WebSocketContainer { ... /* * Returns the HTTP application context for the current deployed application. This can be anything, * but in a Servlet API environment it should be a {@code javax.servlet.ServletContext}. * * @ return the HTTP application context, such as the {@code ServletContext}, but never {@code null }. */ Object getHttpContext(); ... } If provided in this manner, getting a ServletContext is as simple as, using a Session as an example, this: ... @OnOpen public void sessionOpened(Session session) { ServletContext context = (ServletContext)session.getContainer().getHttpContext(); } ...

          People

          • Assignee:
            Unassigned
            Reporter:
            jcompagner
          • Votes:
            2 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated: