top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Tomcat 8 Websocket API: Cookies & Headers

+1 vote
974 views

I'm trying to figure out how to get access to the cookies and headers passed up in the Websocket handshake request on Tomcat 8.

In Tomcat 7 the whole HttpServletRequest was passed into the WebSocketServlet. createWebSocketInbound method so it was easy to grab from the request headers. In Tomcat 8 the querystring and URI are both exposed by the javax.websocket.Session passed to ServerEndPoint.onOpen, but I don't see a mechanism for getting the cookies or headers.

We are integrating Websocket connections into an existing web app and want to use the cookies set by our web app in the Websocket connection process.

posted Aug 23, 2013 by Bob Wise

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

You can supply an extension of http://docs.oracle.com/javaee/7/api/javax/websocket/server/ServerEndpointConfig.Configurator.html
and get
http://docs.oracle.com/javaee/7/api/javax/websocket/server/HandshakeRequest.html through
modifyHandshake invoked by the container during processing of client 'GET' handshake message. Handshake request containes methods for inspecting the http request parameters and headers.

answer Aug 23, 2013 by Garima Jain
I went down the configurator path too, but then I could not find a way to pass the cookie values into the ServerEndPoint.onOpen where I need to use it. I tried passing it via session.getRequestParameterMap() but that is a Collections.unmodifiableMap(). In my scenario the session.getHttpSession() is NULL so I can't put it in there. I didn't like the idea of putting it in ThreadLocal (unless I am guaranteed by the spec that ServerEndPoint.onOpen is always called on the same thread that processes the handshake).

That was when I started thinking I must be missing something simple.
Similar Questions
+1 vote

I'm trying to use the Tomcat8 jsr client functionality in a standalone java client. I'm trying to use the minimal number of jars, so I gabbed websocket-api.jar ONLY. When I call ContainerProvider.getWebSocketContainer(), it returns null. Do I need another jar to resolve this on the client?

0 votes

I have a questions regarding the Non Blocking API introduced in Tomcat 8. I'm looking at the TestNonBlockingAPI test as an example, specifically the NBReadServlet class.

@WebServlet(asyncSupported = true)
public class NBReadServlet extends TesterServlet {
 private static final long serialVersionUID = 1L;
 public volatile TestReadListener listener;
 @Override
 protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 // step 1 - start async
 AsyncContext actx = req.startAsync();
 actx.setTimeout(Long.MAX_VALUE);
 actx.addListener(new AsyncListener() {
 // removed for brevity 
 });
 // step 2 - notify on read
 ServletInputStream in = req.getInputStream();
 listener = new TestReadListener(actx);
 in.setReadListener(listener);

 listener.onDataAvailable();
 }
}

My question is why does the test call "listener.onDataAvailable()" at the end?

In regards to the "onDataAvailable()" method, Section 3.7 of the spec says…

"The onDataAvailable method is invoked on the ReadListener when data is available to read from the incoming request stream. The container will invoke the method the first time when data is available to read. The container will subsequently invoke the onDataAvailable method if and only if isReady method on ServletInputStream, described below, returns false."

...which leads me to believe that the container should be calling onDataAvailable and not the servlet.

As a side note, my test works if I call "onDataAvailable()" from my test servlet. Otherwise it fails and times out.

0 votes

I am just trying upgrade tomcat 7 to latest GA 8 for my application, I am seeing quite lots of change in web dav functionality.

The org.apache.naming.resources.ProxyDirContext do not exists do anybody know where I can find the alternative? in the past we got use resource to lookup something but now is not this one, can anybody know any doc?

+1 vote

I want to access the JNDI Directory context in Tomcat8. I've used DirContextURLStreamHandler class which was in Tomcat 7. But now that has been removed.

Can someone tell me how can I access the JNDI Directory Context in Tomcat 8?

+2 votes

Is there a standard way to access ServletContext from a WebSocket ServerEndpoint ?

...