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

org.swiftboot.web.util.HttpServletCookieUtils Maven / Gradle / Ivy

There is a newer version: 2.4.7
Show newest version
package org.swiftboot.web.util;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * @author swiftech 2019-08-29
 **/
public class HttpServletCookieUtils {

    public static String getCookieValue(HttpServletRequest request, String name) {
        Cookie cookieToken = WebUtils.getCookie(request, name);
        if (cookieToken == null) {
            return null;
        }
        return cookieToken.getValue();
    }

    /**
     * Try to get value from header, if not exists, try to get it from cookie,
     * return null if failed.
     *
     * @param request
     * @param name
     * @return
     */
    public static String getValueFromHeaderOrCookie(HttpServletRequest request, String name) {
        String value = request.getHeader(name);
        if (StringUtils.isBlank(value)) {
            value = getCookieValue(request, name);
            if (StringUtils.isNotBlank(value)) {
                return value;
            }
        }
        else {
            return value;
        }
        return null;
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy