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

io.github.firefang.power.web.util.CurrentRequestUtil Maven / Gradle / Ivy

There is a newer version: 0.0.6
Show newest version
package io.github.firefang.power.web.util;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * Utility class for getting current request
 * 
 * @author xinufo
 *
 */
public abstract class CurrentRequestUtil {

    public static HttpServletRequest getCurrentRequest() {
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        if (ra == null) {
            throw new IllegalStateException("Can't get current request");
        }
        return ((ServletRequestAttributes) ra).getRequest();
    }

    public static String getHeader(String key) {
        HttpServletRequest request = getCurrentRequest();
        return request.getHeader(key);
    }

    public static Object getSessionAttribute(String key) {
        HttpServletRequest request = getCurrentRequest();
        return request.getSession().getAttribute(key);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy