CookieUtil.java 6.04 KB
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.qgutech.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;

public class CookieUtil {
    private static final int MINUTE_SECONDS = 60;

    public CookieUtil() {
    }

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

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

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

    public static void setCookie(HttpServletResponse response, String name, String value, String path, String age, String domain) {
        Cookie cookie = new Cookie(name, value);
        cookie.setSecure(false);
        cookie.setPath(path);
        if (StringUtils.isNotEmpty(domain)) {
            cookie.setDomain(domain);
        }

        int ageValue = getAgeIntValue(age);
        if (ageValue >= 0) {
            cookie.setMaxAge(ageValue);
        }

        response.addCookie(cookie);
    }

    public static void setHttpsCookie(HttpServletResponse response, String name, String value) {
        setHttpsCookie(response, name, value, "/", (String)null);
    }

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

    public static void setHttpsCookie(HttpServletResponse response, String name, String value, String path, String age) {
        setHttpsCookie(response, name, value, path, age, (String)null);
    }

    public static void setHttpsCookie(HttpServletResponse response, String name, String value, String path, String age, String domain) {
        Cookie cookie = new Cookie(name, value);
        cookie.setSecure(true);
        cookie.setPath(path);
        if (StringUtils.isNotEmpty(domain)) {
            cookie.setDomain(domain);
        }

        int ageValue = getAgeIntValue(age);
        if (ageValue >= 0) {
            cookie.setMaxAge(ageValue);
        }

        response.addCookie(cookie);
    }

    private static int getAgeIntValue(String age) {
        try {
            return Integer.valueOf(age) * 60;
        } catch (Exception var2) {
            return -1;
        }
    }

    public static Cookie getCookie(HttpServletRequest request, String name) {
        Cookie[] cookies = request.getCookies();
        Cookie returnCookie = null;
        if (cookies == null) {
            return returnCookie;
        } else {
            for(int i = 0; i < cookies.length; ++i) {
                Cookie thisCookie = cookies[i];
                if (thisCookie.getName().equals(name)) {
                    returnCookie = thisCookie;
                    break;
                }
            }

            return returnCookie;
        }
    }

    public static String getCookieValue(HttpServletRequest request, String name) {
        Cookie cookie = getCookie(request, name);
        return cookie == null ? null : cookie.getValue();
    }

    public static String getAllCookieNameAndValue(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        StringBuffer cs = new StringBuffer();

        for(int i = 0; i < cookies.length; ++i) {
            cs.append(cookies[i].getName()).append("=").append(cookies[i].getValue()).append(";");
        }

        return cs.toString();
    }

    public static void deleteCookie(HttpServletResponse response, Cookie cookie, String path) {
        if (cookie != null) {
            cookie.setMaxAge(0);
            cookie.setPath(path);
            response.addCookie(cookie);
        }

    }

    public static void deleteCookie(HttpServletResponse response, Cookie cookie) {
        deleteCookie(response, cookie, "/");
    }

    public static String getAppURL(HttpServletRequest request) {
        StringBuffer url = new StringBuffer();
        int port = request.getServerPort();
        if (port < 0) {
            port = 80;
        }

        String scheme = request.getScheme();
        url.append(scheme);
        url.append("://");
        url.append(request.getServerName());
        if (scheme.equals("http") && port != 80 || scheme.equals("https") && port != 443) {
            url.append(':');
            url.append(port);
        }

        url.append(request.getContextPath());
        return url.toString();
    }

    public static void addCookie(HttpServletResponse response, String name, String value, String path, String age, String domain, boolean isSecure, boolean isHttpOnly) {
        int maxAge = getAgeIntValue(age);
        addCookieByHeader(response, name, value, maxAge, path, domain, isSecure, isHttpOnly);
    }

    private static void addCookieByHeader(HttpServletResponse response, String name, String value, int maxAge, String path, String domain, boolean isSecure, boolean isHttpOnly) {
        StringBuilder buffer = new StringBuilder();
        buffer.append(name).append("=").append(value).append(";");
        if (maxAge == 0) {
            buffer.append("Expires=Thu Jan 01 08:00:00 CST 1970;");
        } else if (maxAge > 0) {
            buffer.append("Max-Age=").append(maxAge).append(";");
        }

        if (domain != null) {
            buffer.append("domain=").append(domain).append(";");
        }

        if (path != null) {
            buffer.append("path=").append(path).append(";");
        }

        if (isSecure) {
            buffer.append("secure;");
        }

        if (isHttpOnly) {
            buffer.append("HTTPOnly;");
        }

        response.addHeader("Set-Cookie", buffer.toString());
    }

    public static String getCookieDomain(HttpServletRequest request) {
        return request.getServerName();
    }
}