Ejemplo n.º 1
0
  @Override
  protected boolean acceptWebSocket(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    if (!super.acceptWebSocket(req, resp)) {
      return false;
    } else {
      resp.setHeader("upgrade", "websocket");
      resp.setHeader("connection", "upgrade");
    }

    String key = req.getHeader("Sec-WebSocket-Key");
    resp.setHeader("Sec-WebSocket-Accept", getWebSocketAccept(key));

    WebApplication application = getApplication();
    // Small hack until the Servlet API provides a way to do this.
    TomcatWebSocketProcessor webSocketHandler = new TomcatWebSocketProcessor(req, application);
    TomcatWebSocketProcessor.TomcatWebSocket tomcatWebSocket =
        webSocketHandler.new TomcatWebSocket();

    // the request can be a wrapper from application servlet filters
    while (req instanceof HttpServletRequestWrapper) {
      req = (HttpServletRequest) ((HttpServletRequestWrapper) req).getRequest();
    }
    ((RequestFacade) req).doUpgrade(tomcatWebSocket);
    return true;
  }