Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
glassfishrobot opened this issue Dec 7, 2013 · 5 comments
Closed

Comments

@glassfishrobot
Copy link

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.

Affected Versions

[1.0]

@glassfishrobot
Copy link
Author

@glassfishrobot Commented
Reported by jcompagner

@glassfishrobot
Copy link
Author

@glassfishrobot Commented
beamerblvd said:
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();
    }
    ...

@glassfishrobot
Copy link
Author

@glassfishrobot Commented
This issue was imported from java.net JIRA WEBSOCKET_SPEC-217

@glassfishrobot
Copy link
Author

@markt-asf
Copy link
Contributor

This is covered by #238

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants