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

net.n2oapp.framework.ui.context.SessionContextEngine Maven / Gradle / Ivy

There is a newer version: 7.28.2
Show newest version
package net.n2oapp.framework.ui.context;

import net.n2oapp.framework.api.context.ContextEngine;
import net.n2oapp.framework.api.user.UserContext;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

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

/**
 * Хранение значений контекста в http сессии пользователя
 */
public class SessionContextEngine implements ContextEngine {

    @Override
    public Object get(String name) {
        HttpSession session = getHttpSession();
        if (UserContext.SESSION.equals(name)) {
            return session.getId();
        }
        return session.getAttribute(name);
    }

    @Override
    public void set(Map dataSet) {
        final HttpSession httpSession = getHttpSession();
        dataSet.forEach(httpSession::setAttribute);
    }

    @Override
    public Object get(String param, Map baseParams) {
        if (baseParams.containsKey(param))
            return baseParams.get(param);
        return get(param);
    }

    @Override
    public void set(Map dataSet, Map baseParams) {
        Map result = new HashMap<>(baseParams);
        result.putAll(dataSet);
        set(result);
    }

    private HttpSession getHttpSession() {
        RequestAttributes attrs = RequestContextHolder.getRequestAttributes();
        if (attrs instanceof NativeWebRequest) {
            HttpServletRequest request = (HttpServletRequest) ((NativeWebRequest) attrs).getNativeRequest();
            return request.getSession();
        }
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        return requestAttributes.getRequest().getSession();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy