@Override
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
if (req.getHeader(Headers.UPGRADE_STRING) != null) {
final ServletWebSocketHttpExchange facade = new ServletWebSocketHttpExchange(req, resp, peerConnections);
String path;
if (req.getPathInfo() == null) {
path = req.getServletPath();
} else {
path = req.getServletPath() + req.getPathInfo();
}
if (!path.startsWith("/")) {
path = "/" + path;
}
UndertowLogger.REQUEST_LOGGER.debugf("path %s", path);
PathTemplateMatcher.PathMatchResult<WebSocketHandshakeHolder> matchResult = pathTemplateMatcher.match(path);
if (matchResult != null) {
Handshake handshaker = null;
for (Handshake method : matchResult.getValue().handshakes) {
if (method.matches(facade)) {
handshaker = method;
break;
}
}
if (handshaker != null) {
if(container.isClosed()) {
resp.sendError(StatusCodes.SERVICE_UNAVAILABLE);
return;
}
facade.putAttachment(HandshakeUtil.PATH_PARAMS, matchResult.getParameters());
facade.putAttachment(HandshakeUtil.PRINCIPAL, req.getUserPrincipal());
final Handshake selected = handshaker;
final HttpSessionImpl session = (HttpSessionImpl) req.getSession(false);
facade.upgradeChannel(new HttpUpgradeListener() {
@Override
public void handleUpgrade(StreamConnection streamConnection, HttpServerExchange exchange) {
WebSocketChannel channel = selected.createChannel(facade, streamConnection, facade.getBufferPool());
peerConnections.add(channel);
if(session != null) {
final Session underlying;
if (System.getSecurityManager() == null) {
underlying = session.getSession();
} else {
underlying = AccessController.doPrivileged(new HttpSessionImpl.UnwrapSessionAction(session));
}
List<WebSocketChannel> connections;
synchronized (underlying) {
connections = (List<WebSocketChannel>) underlying.getAttribute(SESSION_ATTRIBUTE);
if(connections == null) {
underlying.setAttribute(SESSION_ATTRIBUTE, connections = new ArrayList<>());
}
connections.add(channel);
}
final List<WebSocketChannel> finalConnections = connections;
channel.addCloseTask(new ChannelListener<WebSocketChannel>() {
@Override
public void handleEvent(WebSocketChannel channel) {
synchronized (underlying) {
finalConnections.remove(channel);
}
}
});
}
callback.onConnect(facade, channel);
}
});
handshaker.handshake(facade);
return;
}
}
}
chain.doFilter(request, response);
}