com.ms.app.web.commons.valve.BaseWebUserBuilderValve.java Source code

Java tutorial

Introduction

Here is the source code for com.ms.app.web.commons.valve.BaseWebUserBuilderValve.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.app.web.commons.valve;

import java.util.List;

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

import org.apache.commons.lang.StringUtils;

import com.ms.app.web.commons.request.RequestDigger;
import com.ms.app.web.commons.tools.StaticsTools;
import com.ms.commons.cookie.manager.CookieManager;
import com.ms.commons.cookie.manager.CookieManagerLocator;
import com.ms.commons.log.ExpandLogger;
import com.ms.commons.log.LoggerFactoryWrapper;
import com.ms.commons.summer.web.pipeline.AbstractPipelineValves;
import com.ms.commons.summer.web.pipeline.PipelineMap;
import com.ms.commons.summer.web.pipeline.PipelineResult;

/**
 * WebUser
 * 
 * @author zxc Apr 12, 2013 11:14:51 PM
 */
public abstract class BaseWebUserBuilderValve<T extends BaseWebUser> extends AbstractPipelineValves {

    protected static ExpandLogger logger = LoggerFactoryWrapper.getLogger(BaseWebUserBuilderValve.class);
    private String noPermissionUrl = "/login/nopermission.htm";
    private String loginUrl = "/login/login.htm";
    private List<String> noCheckUrlLikeList;
    private List<String> noCheckUrlList;
    // ??
    protected static final long MAX_LAST_ACCESS_TIME = 1000 * 3600 * 12;

    protected boolean checkUrl;

    public PipelineResult invoke(HttpServletRequest request, HttpServletResponse response, PipelineMap map)
            throws Exception {

        // static?debug?
        StaticsTools.setDebugModeIfEixisted(request);

        // ???
        RequestDigger.saveRequestInfo(request);

        String uri = request.getRequestURI();
        logger.info("<value>" + uri + "</value>");

        // 1. WebUser
        CookieManager cookieManager = CookieManagerLocator.get(request, response);
        T webUser = createWebUser(request, cookieManager);

        // 2. ???
        boolean canAccessAnonymous = canAccessAnonymous(uri);
        if (!webUser.hasLogin()) {
            if (!canAccessAnonymous) {
                return getToLogin(request);
            } else {
                return null;
            }
        }

        // //////////////////////??/////////////////////////////

        // 3. (??,login?????judgeAccessTime??????
        if (!judgeAccessTime(cookieManager) && !canAccessAnonymous) {
            return getToLogin(request);
        }

        // 4. URL??(??
        if (!canAccessAnonymous && !judgePermission(request, uri, webUser)) {
            return PipelineResult.gotoFinally("gotoLogin", noPermissionUrl);
        }
        return null;
    }

    protected PipelineResult getToLogin(HttpServletRequest request) {
        String url = loginUrl;
        String uri = request.getRequestURI();
        // ????returnurl
        if (request.getParameterMap().isEmpty() && !StringUtils.contains("/login", uri)) {
            url = loginUrl + "?returnurl=" + uri;
        }
        return PipelineResult.gotoFinally("gotoLogin", url);
    }

    /**
     * URL????
     * 
     * @param uri
     * @return ???<code>true</code>?<code>false</code>
     */
    private boolean canAccessAnonymous(String uri) {
        if (!checkUrl) {
            return true;
        }
        boolean needcheck = needcheck(uri);
        if (!needcheck) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * ???URL?????????
     * 
     * @return true ????false
     */
    protected boolean judgePermission(HttpServletRequest request, String uri, T webUser) {
        return true;
    }

    /**
     * ??true??(??
     * 
     * @return true ????false
     */
    protected boolean judgeAccessTime(CookieManager cookieManager) {
        return true;
    }

    /**
     * Cookie?????WebUser?(??
     * 
     * @param request
     */
    protected abstract T createWebUser(HttpServletRequest request, CookieManager cookieManager);

    protected boolean needcheck(String uri) {
        if (noCheckUrlList != null && noCheckUrlList.contains(uri)) {
            return false;
        }
        if (noCheckUrlLikeList != null) {
            for (String s : noCheckUrlLikeList) {
                int index = s.lastIndexOf("*");
                if (index != -1) {
                    s = s.substring(0, index);
                    if (uri.startsWith(s)) {
                        return false;
                    }
                }
            }
        }
        return true;
    }

    protected static long parserLong(String number) {
        try {
            return Long.parseLong(number);
        } catch (NumberFormatException e) {
            return -1;
        }
    }

    public void setNoPermissionUrl(String noPermissionUrl) {
        this.noPermissionUrl = noPermissionUrl;
    }

    public void setLoginUrl(String loginUrl) {
        this.loginUrl = loginUrl;
    }

    public void setNoCheckUrlLikeList(List<String> noCheckUrlLikeList) {
        this.noCheckUrlLikeList = noCheckUrlLikeList;
    }

    public boolean isCheckUrl() {
        return checkUrl;
    }

    public void setCheckUrl(boolean checkUrl) {
        this.checkUrl = checkUrl;
    }

    public List<String> getNoCheckUrlList() {
        return noCheckUrlList;
    }

    public void setNoCheckUrlList(List<String> noCheckUrlList) {
        this.noCheckUrlList = noCheckUrlList;
    }
}