com.bsb.cms.commons.web.MossActionUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.bsb.cms.commons.web.MossActionUtils.java

Source

/**
 * @(#)${file_name} ${date}
 *
 * Copyright (c) 2014-2015 BuShangBan (China) Int'l Co., Ltd
 * yongtai Road. pu dong District.Shanghai China
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of BuShangBan (China)
 *  Int'l Co., Ltd ("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 BuShangBan (China).
 */
package com.bsb.cms.commons.web;

import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.bsb.cms.model.po.auth.SysUser;
import com.bsb.cms.model.vo.User;

/**
 * <p>
 * ????
 * </p>
 * <ul>
 * <li>
 * ??
 * <li>
 * <li>
 * ??
 * <li>
 * </ul>
 * 
 * @author hongjian.liu
 * @version 1.0.0 2014-5-29
 * @since 1.0
 */
public class MossActionUtils {

    public static final String USER_KEY = "USER_KEY";// map
    public static final String USER_ID = "user_id";//  id
    public static final String USER_NAME = "user_name";//  ??
    public static HttpSession session = null;

    /***
     * ?id
     * 
     * @return
     */
    public static Long getUserId() {
        Long memberId = null;
        Object o = getUserSession().getAttribute(USER_ID);
        if (o != null) {
            memberId = (Long) o;
        }
        return memberId;
    }

    /**
     * session?
     * 
     * @return
     */
    public static User getUser() {
        User user = null;
        Object o = getUserSession().getAttribute(USER_KEY);
        if (o != null) {
            user = (User) o;
        }

        return user;
    }

    /**
     * session?
     * @param session
     * @return
     */
    public static User getUser(HttpSession session) {
        User user = null;
        Object o = session.getAttribute(USER_KEY);
        if (o != null) {
            user = (User) o;
        }

        return user;
    }

    /**
     * session???
     * 
     * @return
     */
    public static String getUserName() {
        String userName = null;
        Object o = getUserSession().getAttribute(USER_NAME);
        if (o != null) {
            userName = (String) o;
        }

        return userName;
    }

    /**
     * ?session
     * 
     * @param user ??
     */
    public static void setUserInSession(User user) {
        if (user != null) {
            getUserSession().setAttribute(USER_KEY, user);
            SysUser sysUser = user.getSysUser();
            if (sysUser != null) {
                getUserSession().setAttribute(USER_ID, user.getSysUser().getSysUserId());
                getUserSession().setAttribute(USER_NAME, user.getSysUser().getLoginName());
            }
        }

    }

    /**
     * ??
     * 
     * @return
     */
    public static boolean isAdmin() {
        Boolean isAdmin = false;
        String userName = getUserName();
        if ("admin".equalsIgnoreCase(userName)) {
            isAdmin = true;
        }

        return isAdmin;
    }

    /**
     * ??
     * 
     * @return
     */
    public static boolean isAdmin(HttpSession session) {
        Boolean isAdmin = false;
        String userName = (String) session.getAttribute(USER_NAME);
        if ("admin".equalsIgnoreCase(userName)) {
            isAdmin = true;
        }

        return isAdmin;
    }

    /**
     * ?session
     */
    public static void destroySession() {
        getUserSession().removeAttribute(USER_KEY);
        getUserSession().removeAttribute(USER_ID);
        getUserSession().removeAttribute(USER_NAME);
    }

    /**
     * 
     * ?session
     * 
     * @return
     */
    private static HttpSession getUserSession() {
        final ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder
                .currentRequestAttributes();
        final HttpSession contextSession = attr == null ? session : attr.getRequest().getSession(true);

        return contextSession;
    }

    /**
     * ?session
     * 
     * @return
     */
    public static HttpSession getSession() {
        session = getUserSession();
        return session;
    }

    /**
     * ?? ???<s:debug></s:debug> 
     * 
     * @param req
     */
    @SuppressWarnings("all")
    @Deprecated
    public static void print(HttpServletRequest req) {
        // Application
        Map<String, Object> parameters = new WeakHashMap<String, Object>();

        // attributes in scope RequestParameter
        for (Enumeration e = req.getParameterNames(); e.hasMoreElements();) {
            String name = (String) e.nextElement();
            String[] v = req.getParameterValues(name);
            if (v.length == 1) {
                if (v[0].equals(""))
                    continue;
                parameters.put(name, v[0]);
            } else
                parameters.put(name, v);
        }

        // attributes in scope Request
        for (Enumeration e = req.getAttributeNames(); e.hasMoreElements();) {
            String name = (String) e.nextElement();
            Object v = req.getAttribute(name);
            parameters.put(name, v);
        }

        // attributes in scope Session
        HttpSession session = req.getSession();
        for (Enumeration e = session.getAttributeNames(); e.hasMoreElements();) {
            String name = (String) e.nextElement();
            Object v = session.getAttribute(name);
            parameters.put(name, v);
        }

        Set keys = parameters.keySet();
        Iterator it = keys.iterator();
        String key;
        Object value;
        while (it.hasNext()) {
            key = (String) it.next();
            value = parameters.get(key);
            System.out.println("key:" + key + ", value:" + value);
        }

    }
}