final TyrusHttpUpgradeHandlerProxy handler = new TyrusHttpUpgradeHandlerProxy();
final TyrusServletWriter webSocketConnection = new TyrusServletWriter(handler);
final RequestContext requestContext = RequestContext.Builder.create()
.requestURI(URI.create(httpServletRequest.getRequestURI()))
.queryString(httpServletRequest.getQueryString())
.httpSession(httpServletRequest.getSession(false))
.secure(httpServletRequest.isSecure())
.userPrincipal(httpServletRequest.getUserPrincipal())
.isUserInRoleDelegate(new RequestContext.Builder.IsUserInRoleDelegate() {
@Override
public boolean isUserInRole(String role) {
return httpServletRequest.isUserInRole(role);
}
})
.parameterMap(httpServletRequest.getParameterMap())
.remoteAddr(httpServletRequest.getRemoteAddr())
.build();
Enumeration<String> headerNames = httpServletRequest.getHeaderNames();
while (headerNames.hasMoreElements()) {
String name = headerNames.nextElement();
final List<String> values = requestContext.getHeaders().get(name);
if (values == null) {
requestContext.getHeaders().put(name, Utils.parseHeaderValue(httpServletRequest.getHeader(name).trim()));
} else {
values.addAll(Utils.parseHeaderValue(httpServletRequest.getHeader(name).trim()));
}
}
final TyrusUpgradeResponse tyrusUpgradeResponse = new TyrusUpgradeResponse();
final WebSocketEngine.UpgradeInfo upgradeInfo = engine.upgrade(requestContext, tyrusUpgradeResponse);
switch (upgradeInfo.getStatus()) {
case HANDSHAKE_FAILED:
httpServletResponse.sendError(tyrusUpgradeResponse.getStatus());
break;
case NOT_APPLICABLE:
filterChain.doFilter(request, response);
break;
case SUCCESS:
LOGGER.fine("Upgrading Servlet request");
handler.setHandler(httpServletRequest.upgrade(TyrusHttpUpgradeHandler.class));
final String frameBufferSize = request.getServletContext().getInitParameter(TyrusHttpUpgradeHandler.FRAME_BUFFER_SIZE);
if (frameBufferSize != null) {
handler.setIncomingBufferSize(Integer.parseInt(frameBufferSize));
}
handler.preInit(upgradeInfo, webSocketConnection, httpServletRequest.getUserPrincipal() != null);
if (requestContext.getHttpSession() != null) {
sessionToHandler.put((HttpSession) requestContext.getHttpSession(), handler);
}
httpServletResponse.setStatus(tyrusUpgradeResponse.getStatus());
for (Map.Entry<String, List<String>> entry : tyrusUpgradeResponse.getHeaders().entrySet()) {
httpServletResponse.addHeader(entry.getKey(), Utils.getHeaderFromList(entry.getValue()));