Java tutorial
/* * Copyright 2011-2016 MSUN.com All right reserved. This software is the confidential and proprietary information of * MSUN.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 MSUN.com. */ package com.mmj.app.common.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.mmj.app.common.cookie.CookieKeyEnum; import com.mmj.app.common.cookie.cons.CookieMaxAge; import com.mmj.app.common.security.EncryptBuilder; /** * @author zxc Jul 16, 2014 2:07:26 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 = EncryptBuilder.getInstance().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; } }