📄 requestprocessor.java
字号:
package apusic.myshop.control.web;import java.util.Collection;import javax.servlet.ServletContext;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import javax.naming.NamingException;import javax.naming.InitialContext;import java.rmi.RemoteException;import apusic.myshop.control.web.ModelUpdateNotifier;import apusic.myshop.control.EventException;import apusic.myshop.control.LoginFailedException;import apusic.myshop.control.EventException;import apusic.myshop.control.event.BaseEvent;import apusic.myshop.util.SecurityAdapter;import apusic.myshop.util.Debug;import apusic.myshop.util.JNDINames;import apusic.myshop.control.event.CustomerEvent;import apusic.myshop.control.event.LoginEvent;import apusic.myshop.control.event.LogoutEvent;import apusic.myshop.control.event.DBLoginEvent;import apusic.myshop.util.WebKeys;//特定请求处理器类public class RequestProcessor { private ShoppingClientControllerWebImpl scc; private ModelManager mm; private HttpSession session; private ServletContext context; private ModelUpdateNotifier mun; private RequestToEventTranslator eventTranslator; private SecurityAdapter securityAdapter; private boolean logining;//构造函数 public RequestProcessor() { }//初始化 public void init(ServletContext context, HttpSession session) { this.session = session; this.context = context; mm = (ModelManager)session.getAttribute("modelManager"); mun = mm; scc = new ShoppingClientControllerWebImpl(session); mm.setSCC(scc); eventTranslator = new RequestToEventTranslator(this, mm); String securityAdapterClassName = null; String serverType = null; try { InitialContext ic = new InitialContext(); securityAdapterClassName = (String)ic.lookup(JNDINames.SECURITY_ADAPTER_CLASSNAME); serverType = (String)ic.lookup(JNDINames.SERVER_TYPE); context.setAttribute("serverType", serverType); } catch (NamingException ex){ Debug.println("Security Adapter not specified in deployment descriptor: using default J2ee Security Adapter"); } // use j2ee user manager if not specified if (securityAdapterClassName == null){ securityAdapterClassName = "apusic.myshop.util.ApusicSecurityAdapter"; } Debug.println("RequestProcessor: Security Adapter class name = " + securityAdapterClassName); try{ securityAdapter = (SecurityAdapter)Class.forName(securityAdapterClassName).newInstance(); } catch (java.lang.InstantiationException ie){ Debug.println("StateMachine caught: " + ie); } catch (java.lang.IllegalAccessException il){ Debug.println("StateMachine caught: " + il); } catch (java.lang.ClassNotFoundException e){ Debug.println("StateMachine caught: " + e); } } public void processRequest(HttpServletRequest req)throws EventException, MissingFormDataException, LoginFailedException{// try { logining = false; checkForWebServerLogin(req); // Process the request and get the necessary event depending on the URL BaseEvent event = eventTranslator.processRequest(req); if (event != null) { Collection updatedModelList = scc.handleEvent(event); mun.notifyListeners(updatedModelList); /* This portion of code automatically logs a user into the * web server when a new account is created. The user will * not see the form based login page. */ if ((event instanceof CustomerEvent) && ((CustomerEvent)event).getActionType() == CustomerEvent.CREATE_CUSTOMER) { logining = true; if (securityAdapter != null) { securityAdapter.loginUser(((CustomerEvent)event).getUserId(), ((CustomerEvent)event).getPassword(), session); } req.getSession().setAttribute(WebKeys.UserIdKey, ((CustomerEvent)event).getUserId()); checkForWebServerLogin(req); // invalidate current session and replace it with a new session } else if (event instanceof LogoutEvent) { session.invalidate(); scc.remove(); // get new session and put in a new gui controller HttpSession validSession = req.getSession(true); ModelManager modelManager = new ModelManager(); modelManager.init(context, validSession); validSession.setAttribute("modelManager", modelManager); this.init(context, validSession); validSession.setAttribute("rp", this); } else if (event instanceof DBLoginEvent) { logining = true; req.getSession().setAttribute(WebKeys.UserIdKey, ((DBLoginEvent)event).getUserId()); checkForWebServerLogin(req); } }/* } catch (LoginFailedException lfe) { }*/ } private void checkForWebServerLogin(HttpServletRequest req){ // check if the user has logged in using the form based login // spec says that userprincipal to null /* if ((req.getUserPrincipal() != null) && !req.getUserPrincipal().getName().equals("guest") && !mm.getCustomerModel().isLoggedIn()) { */ String userId = (String)req.getSession().getAttribute(WebKeys.UserIdKey); if (userId != null && !userId.equalsIgnoreCase("") && (!mm.getCustomerModel().isLoggedIn() || logining )) { Debug.println(userId); if (logining) Debug.println("logining is true"); logining = false; BaseEvent loginEvent = null; loginEvent = eventTranslator.createLoginEvent(req); try { Collection updatedModelList = scc.handleEvent(loginEvent); mun.notifyListeners(updatedModelList); } catch (LoginFailedException lfe) { } catch (EventException se) { Debug.print(se); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -