com.greenline.guahao.web.module.common.interceptor.CookieInterceptor.java Source code

Java tutorial

Introduction

Here is the source code for com.greenline.guahao.web.module.common.interceptor.CookieInterceptor.java

Source

/**
 * Project: guahao-portal-web-home
 * 
 * File Created at 2012-10-9
 * 
 * Copyright 2012 Greenline.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Greenline Company. ("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 Greenline.com.
 */
package com.greenline.guahao.web.module.common.interceptor;

import java.util.Date;

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

import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.ModelAndView;

import com.greenline.framework.mursi.beans.interceptor.MursiInterceptor;
import com.greenline.framework.mursi.web.module.cookie.CookieModule;
import com.greenline.guahao.web.module.common.cookie.UserCookieUtil;

/**
 * @Type CookieInterceptor
 * @Desc :sessionId30cookie
 * @author jianyun.zheng
 * @date 2012-10-9
 * @Version V1.0
 */
public class CookieInterceptor extends MursiInterceptor {

    /**
     * ?cookie
     */
    private final boolean isAddSessionCookie = true;

    /**
     * cookieModulesessionCookie??
     */
    private final String shortCookieName = "shortSessionId";

    /**
     * ??
     */
    private final String expiryMark = "expiryMark";

    /*
     * mark????mark??marksessionId
     * mark?sessionId30sessionIdsessionId
     * 
     * sessionIdsessionId ?Date.getTime()+5
     * expiryMarkDate.getTime()+10
     * 
     * 
     * (non-Javadoc)
     * 
     * @see
     * com.greenline.framework.mursi.beans.interceptor.MursiInterceptor#postHandle
     * (javax.servlet.http.HttpServletRequest,
     * javax.servlet.http.HttpServletResponse, java.lang.Object,
     * org.springframework.web.servlet.ModelAndView)
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        if (isAddSessionCookie) {
            CookieModule jar = (CookieModule) request.getAttribute(CookieModule.COOKIE);
            if (jar == null) {
                return;
            }
            String sessionId = jar.get(shortCookieName);
            // mark?marksessionId
            if (StringUtils.isBlank(UserCookieUtil.getMark(request))) {
                // ?
                Date now = new Date();
                jar.set(shortCookieName, now.getTime() + 5);
                jar.set(expiryMark, now.getTime() + 10);
            } else {
                // mark??sessionId
                if (StringUtils.isBlank(sessionId)) {
                    // ?
                    Date now = new Date();
                    jar.set(shortCookieName, now.getTime() + 5);
                }
            }
        }
    }
}