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

org.kaizen4j.common.util.CookieUtils Maven / Gradle / Ivy

package org.kaizen4j.common.util;

import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import static java.util.Objects.isNull;
import static org.kaizen4j.common.base.Symbols.EMPTY;

/**
 * Cookie工具类
 *
 * @author John
 */
public final class CookieUtils {

    private static final Logger logger = LoggerFactory.getLogger(CookieUtils.class);

    /* Cookie过期时间,默认1个月 */
    private static final int MAX_AGE = 30 * 24 * 3600;

    private static final String PATH = "/";

    public static void setCookie(HttpServletResponse response, String name, String value, String domain) {
        Cookie cookie = new Cookie(name, value);
        cookie.setPath(PATH);
        cookie.setMaxAge(MAX_AGE);

        if (StringUtils.isNotEmpty(domain)) {
            cookie.setDomain(domain);
        }
        setCookie(response, cookie);
    }

    public static void setCookie(HttpServletResponse response, String name, String value) {
        setCookie(response, name, value, null);
    }

    public static void setCookie(HttpServletResponse response, Enum enums, String value, String domain) {
        setCookie(response, enums.name(), value, domain);
    }

    public static void setCookie(HttpServletResponse response, Enum enums, String value) {
        setCookie(response, enums.name(), value, null);
    }

    public static void setCookie(HttpServletResponse response, Cookie cookie) {
        try {
            response.addCookie(cookie);
        } catch (Exception e) {
            logger.error("Set cookie failed", e);
        }
    }

    public static void removeCookie(HttpServletResponse response, String name, String domain) {
        Cookie cookie = new Cookie(name, EMPTY);
        cookie.setPath(PATH);
        cookie.setMaxAge(0);

        if (StringUtils.isNotEmpty(domain)) {
            cookie.setDomain(domain);
        }
        setCookie(response, cookie);
    }

    public static void removeCookie(HttpServletResponse response, String name) {
        removeCookie(response, name, null);
    }

    public static void removeCookie(HttpServletResponse response, Enum enums, String domain) {
        removeCookie(response, enums.name(), domain);
    }

    public static void removeCookie(HttpServletResponse response, Enum enums) {
        removeCookie(response, enums.name(), null);
    }

    public static void removeCookie(HttpServletResponse response, Cookie cookie) {
        cookie.setValue(EMPTY);
        cookie.setMaxAge(0);
        setCookie(response, cookie);
    }

    public static Cookie getCookie(HttpServletRequest request, String name) {
        Map cookieMap = readCookies(request);
        return cookieMap.get(name);
    }

    public static Cookie getCookie(HttpServletRequest request, Enum enums) {
        return getCookie(request, enums.name());
    }

    public static Optional getValue(HttpServletRequest request, String name) {
        Cookie cookie = getCookie(request, name);
        return Objects.isNull(cookie) ? Optional.empty() : Optional.of(cookie.getValue());
    }

    public static Optional getValue(HttpServletRequest request, Enum enums) {
        return getValue(request, enums.name());
    }

    public static Map readCookies(HttpServletRequest request) {
        Map cookieMap = Maps.newHashMap();
        Cookie[] cookies = request.getCookies();

        if (Objects.nonNull(cookies)) {
            for (Cookie cookie : cookies) {
                cookieMap.put(cookie.getName(), cookie);
            }
        }
        return cookieMap;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy