001: package com.jat.presentation.controller;
002:
003: import java.io.IOException;
004: import javax.servlet.ServletException;
005: import javax.servlet.http.HttpServletRequest;
006: import javax.servlet.http.HttpServletResponse;
007:
008: import com.jat.core.log.LogManager;
009: import com.jat.presentation.ExpiredSessionException;
010: import com.jat.presentation.PresentationException;
011: import com.jat.presentation.PresentationServlet;
012: import com.jat.business.BusinessException;
013: import com.jat.business.JatUser;
014: import com.jat.presentation.controller.navigation.Navigation;
015:
016: /**
017: * <p>Title: JAT</p>
018: * <p>Description: </p>
019: * <p>Copyright: Copyright (c) 2004 -2005 Stefano Fratini (stefano.fratini@gmail.com)</p>
020: * <p>Distributed under the terms of the GNU Lesser General Public License, v2.1 or later</p>
021: * @author stf
022: * @version 1.0
023: * @since 1.2
024: */
025:
026: public class ControllerServlet extends PresentationServlet {
027:
028: /** @link dependency
029: * @stereotype use*/
030: /*# Action lnkAction; */
031:
032: /** @link dependency
033: * @stereotype use*/
034: /*# ActionFactory lnkActionFactory; */
035: //Initialize global variables
036: public void init() throws ServletException {
037: }
038:
039: //Process the HTTP Get request
040: public void doPost(HttpServletRequest request,
041: HttpServletResponse response) throws ServletException,
042: IOException {
043: long startTime = System.currentTimeMillis();
044: try {
045: LogManager.sendDebug(this .getClass().getName()
046: + "::doPost: start for session: '"
047: + request.getSession().getId() + "'");
048: this .doAction(request, response);
049: } catch (Exception ex) {
050: LogManager.sendDebug(this .getClass().getName()
051: + "::doPost: exception for session: "
052: + request.getSession().getId() + "': " + ex);
053: request.setAttribute("EXCEPTION", ex);
054: dispatchToPage(request, response, getDefaultErrorPage());
055: } finally {
056: LogManager.sendDebug(this .getClass().getName()
057: + "::doPost: end for session: '"
058: + request.getSession().getId() + "'");
059: try {
060: LogManager
061: .sendTime(
062: this .getClass().getName()
063: + "::doPost: "
064: + request
065: .getParameter(Action.ACTION_PARAMETER),
066: System.currentTimeMillis() - startTime);
067: } catch (Exception ignored) {
068: }
069: }
070: }
071:
072: //Process the HTTP Post request
073: public void doGet(HttpServletRequest request,
074: HttpServletResponse response) throws ServletException,
075: IOException {
076: doPost(request, response);
077: }
078:
079: //Clean up resources
080: public void destroy() {
081: }
082:
083: public void doAction(HttpServletRequest request,
084: HttpServletResponse response) throws PresentationException,
085: IOException, ServletException {
086: String nextPage = "";
087: String actionPar = request
088: .getParameter(Action.ACTION_PARAMETER);
089: if (actionPar == null) {
090: if (request.getSession().getAttribute(JatUser.JAT_USER) == null)
091: nextPage = getInitialPage();
092: else
093: nextPage = getHomePage();
094: } else {
095: String menuItem = request.getParameter("menu_item");
096: if (menuItem != null)
097: request.getSession().setAttribute("MENU_ITEM_SELECTED",
098: menuItem);
099: Action action = ActionFactory.getDefault().getAction(
100: actionPar);
101: if (action == null)
102: throw new ServletException("No action found for "
103: + actionPar);
104: LogManager.sendDebug(this .getClass().getName()
105: + "::doAction: requested action '" + actionPar
106: + "': " + action);
107: if (!action.checkMethod(request))
108: throw new ServletException("Http method '"
109: + request.getMethod()
110: + "' is not allowed for action " + actionPar);
111: if (!action.checkUserLogged(request.getSession()))
112: throw new ExpiredSessionException();
113: if (!action.checkFlow(request.getSession()))
114: throw new FlowException();
115: try {
116: action.doAction(request, response);
117: nextPage = action.getNextPage();
118: request.getSession().setAttribute(
119: Action.JAT_PAGES_FLOW, actionPar);
120: Navigation.getDefault(request.getSession()).addAction(
121: request, action);
122: } catch (PresentationException ex) {
123: LogManager.sendDebug(this .getClass().getName()
124: + "::doAction: exception :" + ex);
125: nextPage = action.getErrorPage();
126: request.setAttribute("EXCEPTION", ex);
127: }
128: }
129: LogManager.sendDebug(this .getClass().getName()
130: + "::doAction: dispatching to " + nextPage);
131: dispatchToPage(request, response, nextPage);
132: }
133:
134: }
|