com.ms.commons.cookie.parser.CookieNameHelper.java Source code

Java tutorial

Introduction

Here is the source code for com.ms.commons.cookie.parser.CookieNameHelper.java

Source

/*
 * Copyright 2011-2016 ZXC.com All right reserved. This software is the confidential and proprietary information of
 * ZXC.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into with ZXC.com.
 */
package com.ms.commons.cookie.parser;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.ms.commons.cookie.CookieKeyEnum;
import com.ms.commons.cookie.annotation.CookieMaxAge;

/**
 * ??Cookie
 * 
 * @author zxc Apr 12, 2014 7:39:51 PM
 */
public class CookieNameHelper {

    private CookieNameConfig config;

    private String cookieName;
    /**
     * ?Cookie???
     */
    private boolean isModified = false;
    /** CookieNameCookie */
    private Map<CookieKeyEnum, String> allCookieKeyValues = new HashMap<CookieKeyEnum, String>();

    private String simpleValue;

    public CookieNameHelper(String cookieName, CookieNameConfig config) {
        this.cookieName = cookieName;
        this.config = config;
    }

    /**
     * CookieKey?cookieKey???
     */
    public void update(CookieKeyEnum cookieKey, String value) {
        if (config.isSimpleValue()) {
            throw new RuntimeException("?CookieNameupdateSimpleValue()");
        }
        allCookieKeyValues.put(cookieKey, value);
        isModified = true;
    }

    public void updateSimpleValue(String value) {
        if (!config.isSimpleValue()) {
            throw new RuntimeException("??CookieNameupdate(key,value)");
        }
        simpleValue = value;
        isModified = true;
    }

    public void clear() {
        if (!config.isSimpleValue()) {
            allCookieKeyValues.clear();
        } else {
            simpleValue = null;
        }
        this.isModified = true;
    }

    public Set<CookieKeyEnum> getAllKeys() {
        return allCookieKeyValues.keySet();
    }

    public String getValue() {
        if (!config.isSimpleValue()) {
            throw new RuntimeException("??CookieNamegetValue(key)");
        }
        return simpleValue;
    }

    public String getValue(CookieKeyEnum cookieKeyEnum) {
        if (config.isSimpleValue()) {
            throw new RuntimeException("?CookieNamegetValue()");
        }
        return allCookieKeyValues.get(cookieKeyEnum);
    }

    public boolean isEmpty() {
        return (simpleValue == null) && allCookieKeyValues.isEmpty();
    }

    public String getCookieName() {
        return this.cookieName;
    }

    /**
     * ????CookieName?Response
     * 
     * <pre>
     * cookie<code>null</code>blankCookie
     * </pre>
     */
    public void saveIfModified(HttpServletResponse response) {
        if (!isModified) {
            return;
        }
        String value = config.isSimpleValue() ? simpleValue : CookieUtils.mapToStr(allCookieKeyValues);
        if (config.isEncrypt()) {
            value = CookieUtils.encrypt(value);
        }
        Cookie cookie = new Cookie(cookieName, value);
        if (StringUtils.isBlank(value)) {
            cookie.setMaxAge(CookieMaxAge.OUT_OF_DATE);
        } else {
            cookie.setMaxAge(config.getMaxAge());
        }
        cookie.setDomain(config.getDomain().getDomain());
        cookie.setPath(config.getPath().getPath());
        response.addCookie(cookie);

        // ?????
        this.isModified = false;
    }

    /**
     * ?CookieName,?Modified
     */
    void parserAllValues(Map<CookieKeyEnum, String> values) {
        if (config.isSimpleValue()) {
            throw new RuntimeException("?CookieNameinitValue(value)");
        }
        this.allCookieKeyValues.putAll(values);
    }

    /**
     * ??Cookie
     */
    void parserValue(String value) {
        if (!config.isSimpleValue()) {
            throw new RuntimeException("??CookieNameinitAllValues(value)");
        }
        this.simpleValue = value;
    }

}