com.sniper.springmvc.utils.CookieUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.sniper.springmvc.utils.CookieUtils.java

Source

/**
 * Copyright &copy; 2012-2013 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.sniper.springmvc.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

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

import org.apache.commons.lang3.StringUtils;

/**
 * Cookie
 * 
 * @author ThinkGem
 * @version 2013-01-15
 */
public class CookieUtils {

    /**
     *  Cookie?1
     * 
     * @param name
     *            ??
     * @param value
     *            
     */
    public static void setCookie(HttpServletResponse response, String name, String value) {
        setCookie(response, name, value, 60 * 60 * 24);
    }

    /**
     *  Cookie
     * 
     * @param name
     *            ??
     * @param value
     *            
     * @param maxAge
     *            ??
     */
    public static void setCookie(HttpServletResponse response, String name, String value, int maxAge) {
        Cookie cookie = new Cookie(name, null);
        if (StringUtils.isNotBlank(SpringContextHolder.getApplicationContext().getApplicationName())) {
            cookie.setPath(SpringContextHolder.getApplicationContext().getApplicationName());
        } else {
            cookie.setPath("/");
        }
        cookie.setMaxAge(maxAge);
        try {
            cookie.setValue(URLEncoder.encode(value, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.addCookie(cookie);
    }

    /**
     * Cookie
     * 
     * @param name
     *            ??
     * @return 
     */
    public static String getCookie(HttpServletRequest request, String name) {
        return getCookie(request, null, name, false);
    }

    /**
     * Cookie
     * 
     * @param name
     *            ??
     * @return 
     */
    public static String getCookie(HttpServletRequest request, HttpServletResponse response, String name) {
        return getCookie(request, response, name, true);
    }

    /**
     * Cookie
     * 
     * @param request
     *            
     * @param response
     *            ?
     * @param name
     *            ??
     * @param isRemove
     *            ?
     * @return 
     */
    public static String getCookie(HttpServletRequest request, HttpServletResponse response, String name,
            boolean isRemove) {
        String value = null;
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(name)) {
                    try {
                        value = URLDecoder.decode(cookie.getValue(), "utf-8");
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    if (isRemove) {
                        cookie.setMaxAge(0);
                        response.addCookie(cookie);
                    }
                }
            }
        }
        return value;
    }
}