All Downloads are FREE. Search and download functionalities are using the official Maven repository.

pl.fhframework.HttpSessionInterceptor Maven / Gradle / Ivy

There is a newer version: 4.10.401
Show newest version
package pl.fhframework;

import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

import javax.servlet.http.HttpSession;
import java.util.Map;


public class HttpSessionInterceptor extends HttpSessionHandshakeInterceptor {
    public HttpSessionInterceptor() {
        setCreateSession(true); // for guests
    }

    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map attributes) throws Exception {
        boolean res = super.beforeHandshake(request, response, wsHandler, attributes);
        HttpSession httpSession = getSession(request);
        WebSocketSessionManager.prepareHttpSession(httpSession);
        attributes.put(WebSocketSessionManager.HTTP_SESSION_KEY, httpSession);
        return res;
    }

    private HttpSession getSession(ServerHttpRequest request) {
        if (request instanceof ServletServerHttpRequest) {
            ServletServerHttpRequest serverRequest = (ServletServerHttpRequest) request;
            return serverRequest.getServletRequest().getSession(isCreateSession());
        }
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy