?? qrsxrequestprocessor.java
字號:
/*
* @(#)QrsxRequestProcessor.java Dec 16, 2006
* Copyright 2006 qingdaosoftware, Inc. All rights reserved
*/
package com.qrsx.exam.struts;
import java.io.IOException;
import java.util.Set;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.RequestProcessor;
import com.qrsx.exam.Constants;
import com.qrsx.exam.cache.Cache;
import com.qrsx.exam.cache.CacheFactory;
import com.qrsx.exam.config.ConfigConstants;
import com.qrsx.exam.config.ExamConfigUtil;
import com.qrsx.exam.exception.ExamSystemException;
import com.qrsx.exam.model.Action;
import com.qrsx.exam.model.Function;
import com.qrsx.exam.model.Role;
import com.qrsx.exam.model.User;
import com.qrsx.exam.util.StringUtils;
/**
*
* 功能:
*
* <p>
* <a href="com.qrsx.exam.struts.QrsxRequestProcessor.java"> <i>View Source </i>
* </a></br>
*
* Company : QingdaoSoftware<br>
* Author : <a href="mailto:wxt1013@163.com">WangXitao</a></br> Version : 1.0<br>
* Date : Dec 16, 2006<br>
*/
public class QrsxRequestProcessor extends RequestProcessor {
protected final Log log = LogFactory.getLog(RequestProcessor.class);
/**
* 用戶認證方法
*/
@Override
protected boolean processRoles(HttpServletRequest request,
HttpServletResponse response, ActionMapping mapping)
throws IOException, ServletException {
// 得到映射的路徑
String path = mapping.getPath();
// 得到用戶所要調用的Action方法的名字
String method = request.getParameter(mapping.getParameter());
if (StringUtils.isEmpty(method)) {
method = Constants.DEFAULT_METHOD;
}
// 取得不需要校驗權限的Action方法
String[] roles = mapping.getRoleNames();
if (roles != null && roles.length > 0) {
// 進行方法的判斷
for (String role : roles) {
if (method.equals(role)) {
request.setAttribute(Constants.REQUEST_CHECK_FLAG, true);
return true;
}
}
}
// 得到Session對象和用戶對象
HttpSession session = request.getSession();
User u = (User) session.getAttribute(Constants.SESSION_USER);
// 如果用于對象不存在,那么說明用戶沒有登錄
if (u == null) {
// 用戶沒有執行的權限,跳轉到錯誤頁面
processLocale(request, response);
RequestDispatcher rd = request
.getRequestDispatcher("/errors/noauthority.jsp");
rd.forward(request, response);
return false;
}
// 判斷用戶是否為超級用戶
String superusers = ExamConfigUtil
.getSysConfigValue(ConfigConstants.SUPER_USER);
String[] users = StringUtils.splitString(superusers,
ConfigConstants.USER_DELIM);
if (StringUtils.contains(users, u.getName())) {
request.setAttribute(Constants.REQUEST_CHECK_FLAG, true);
return true;
}
// 得到用戶的角色信息
Cache cache = CacheFactory.getCache();
Role role = (Role) cache.get(u.getUserType());
if (role == null) {
throw new ExamSystemException("Couldn't find the role!");
}
// 進行用戶執行功能的判斷
Set<Function> functions = role.getFunctions();
for (Function function : functions) {
Set<Action> actions = function.getActions();
for (Action action : actions) {
if (path.equals(action.getPath())
&& method.equals(action.getParameter())) {
request.setAttribute(Constants.REQUEST_CHECK_FLAG, true);
return true;
}
}
}
// 用戶沒有執行的權限,跳轉到錯誤頁面
processLocale(request, response);
RequestDispatcher rd = request
.getRequestDispatcher("/errors/noauthority.jsp");
rd.forward(request, response);
return false;
}
@Override
protected void processLocale(HttpServletRequest request,
HttpServletResponse response) {
super.processLocale(request, response);
try {
request.setCharacterEncoding("utf-8");
} catch (Exception ex) {
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -