Search in sources :

Example 1 with TyrusServerContainer

use of org.glassfish.tyrus.server.TyrusServerContainer in project spring-framework by spring-projects.

the class AbstractTyrusRequestUpgradeStrategy method upgradeInternal.

@Override
public void upgradeInternal(ServerHttpRequest request, ServerHttpResponse response, String selectedProtocol, List<Extension> extensions, Endpoint endpoint) throws HandshakeFailureException {
    HttpServletRequest servletRequest = getHttpServletRequest(request);
    HttpServletResponse servletResponse = getHttpServletResponse(response);
    TyrusServerContainer serverContainer = (TyrusServerContainer) getContainer(servletRequest);
    TyrusWebSocketEngine engine = (TyrusWebSocketEngine) serverContainer.getWebSocketEngine();
    Object tyrusEndpoint = null;
    boolean success;
    try {
        // Shouldn't matter for processing but must be unique
        String path = "/" + random.nextLong();
        tyrusEndpoint = createTyrusEndpoint(endpoint, path, selectedProtocol, extensions, serverContainer, engine);
        register(engine, tyrusEndpoint);
        HttpHeaders headers = request.getHeaders();
        RequestContext requestContext = createRequestContext(servletRequest, path, headers);
        TyrusUpgradeResponse upgradeResponse = new TyrusUpgradeResponse();
        UpgradeInfo upgradeInfo = engine.upgrade(requestContext, upgradeResponse);
        success = SUCCESS.equals(upgradeInfo.getStatus());
        if (success) {
            if (logger.isTraceEnabled()) {
                logger.trace("Successful request upgrade: " + upgradeResponse.getHeaders());
            }
            handleSuccess(servletRequest, servletResponse, upgradeInfo, upgradeResponse);
        }
    } catch (Exception ex) {
        unregisterTyrusEndpoint(engine, tyrusEndpoint);
        throw new HandshakeFailureException("Error during handshake: " + request.getURI(), ex);
    }
    unregisterTyrusEndpoint(engine, tyrusEndpoint);
    if (!success) {
        throw new HandshakeFailureException("Unexpected handshake failure: " + request.getURI());
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletException(javax.servlet.ServletException) DeploymentException(javax.websocket.DeploymentException) IOException(java.io.IOException) HandshakeFailureException(org.springframework.web.socket.server.HandshakeFailureException) HttpServletRequest(javax.servlet.http.HttpServletRequest) TyrusServerContainer(org.glassfish.tyrus.server.TyrusServerContainer) TyrusUpgradeResponse(org.glassfish.tyrus.core.TyrusUpgradeResponse) TyrusWebSocketEngine(org.glassfish.tyrus.core.TyrusWebSocketEngine) UpgradeInfo(org.glassfish.tyrus.spi.WebSocketEngine.UpgradeInfo) HandshakeFailureException(org.springframework.web.socket.server.HandshakeFailureException) RequestContext(org.glassfish.tyrus.core.RequestContext)

Aggregations

IOException (java.io.IOException)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 DeploymentException (javax.websocket.DeploymentException)1 RequestContext (org.glassfish.tyrus.core.RequestContext)1 TyrusUpgradeResponse (org.glassfish.tyrus.core.TyrusUpgradeResponse)1 TyrusWebSocketEngine (org.glassfish.tyrus.core.TyrusWebSocketEngine)1 TyrusServerContainer (org.glassfish.tyrus.server.TyrusServerContainer)1 UpgradeInfo (org.glassfish.tyrus.spi.WebSocketEngine.UpgradeInfo)1 HttpHeaders (org.springframework.http.HttpHeaders)1 HandshakeFailureException (org.springframework.web.socket.server.HandshakeFailureException)1