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

com.firefly.codec.http2.model.CookieGenerator Maven / Gradle / Ivy

There is a newer version: 5.0.0-dev6
Show newest version
package com.firefly.codec.http2.model;

import com.firefly.utils.VerifyUtils;

import java.util.List;

abstract public class CookieGenerator {

    public static String generateCookies(List cookies) {
        if (cookies == null) {
            throw new IllegalArgumentException("the cookie list is null");
        }

        if (cookies.size() == 1) {
            return generateCookie(cookies.get(0));
        } else if (cookies.size() > 1) {
            StringBuilder sb = new StringBuilder();

            sb.append(generateCookie(cookies.get(0)));
            for (int i = 1; i < cookies.size(); i++) {
                sb.append(';').append(generateCookie(cookies.get(i)));
            }

            return sb.toString();
        } else {
            throw new IllegalArgumentException("the cookie list size is 0");
        }
    }

    public static String generateCookie(Cookie cookie) {
        if (cookie == null) {
            throw new IllegalArgumentException("the cookie is null");
        } else {
            return cookie.getName() + '=' + cookie.getValue();
        }
    }

    public static String generateSetCookie(Cookie cookie) {
        if (cookie == null) {
            throw new IllegalArgumentException("the cookie is null");
        } else {
            StringBuilder sb = new StringBuilder();

            sb.append(cookie.getName()).append('=').append(cookie.getValue());

            if (VerifyUtils.isNotEmpty(cookie.getComment())) {
                sb.append(";Comment=").append(cookie.getComment());
            }

            if (VerifyUtils.isNotEmpty(cookie.getDomain())) {
                sb.append(";Domain=").append(cookie.getDomain());
            }
            if (cookie.getMaxAge() >= 0) {
                sb.append(";Max-Age=").append(cookie.getMaxAge());
            }

            String path = VerifyUtils.isEmpty(cookie.getPath()) ? "/" : cookie.getPath();
            sb.append(";Path=").append(path);

            if (cookie.getSecure()) {
                sb.append(";Secure");
            }

            sb.append(";Version=").append(cookie.getVersion());

            return sb.toString();
        }
    }

    public static String generateServletSetCookie(javax.servlet.http.Cookie cookie) {
        if (cookie == null) {
            throw new IllegalArgumentException("the cookie is null");
        } else {
            StringBuilder sb = new StringBuilder();

            sb.append(cookie.getName()).append('=').append(cookie.getValue());

            if (VerifyUtils.isNotEmpty(cookie.getComment())) {
                sb.append(";Comment=").append(cookie.getComment());
            }

            if (VerifyUtils.isNotEmpty(cookie.getDomain())) {
                sb.append(";Domain=").append(cookie.getDomain());
            }
            if (cookie.getMaxAge() >= 0) {
                sb.append(";Max-Age=").append(cookie.getMaxAge());
            }

            String path = VerifyUtils.isEmpty(cookie.getPath()) ? "/" : cookie.getPath();
            sb.append(";Path=").append(path);

            if (cookie.getSecure()) {
                sb.append(";Secure");
            }

            sb.append(";Version=").append(cookie.getVersion());

            return sb.toString();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy