001: package com.jat.presentation;
002:
003: import java.io.IOException;
004: import java.io.Writer;
005: import java.util.Enumeration;
006: import javax.servlet.ServletException;
007: import javax.servlet.http.HttpServlet;
008: import javax.servlet.http.HttpServletRequest;
009: import javax.servlet.http.HttpServletResponse;
010: import javax.servlet.http.HttpSession;
011:
012: import com.jat.business.BusinessException;
013: import com.jat.business.JatUser;
014: import com.jat.core.config.Config;
015: import com.jat.core.log.LogManager;
016: import com.jat.presentation.controller.ActionLoader;
017:
018: /**
019: * <p>Title: JAT</p>
020: * <p>Description: </p>
021: * <p>Copyright: Copyright (c) 2004 -2005 Stefano Fratini (stefano.fratini@gmail.com)</p>
022: * <p>Distributed under the terms of the GNU Lesser General Public License, v2.1 or later</p>
023: * @author stf
024: * @version 1.2
025: * @since 1.2
026: */
027:
028: public abstract class PresentationServlet extends HttpServlet {
029:
030: public abstract void doAction(HttpServletRequest request,
031: HttpServletResponse response) throws PresentationException,
032: IOException, ServletException;
033:
034: //Process the HTTP Post request
035: public void doPost(HttpServletRequest request,
036: HttpServletResponse response) throws ServletException,
037: IOException {
038: HttpSession session = request.getSession();
039: try {
040: LogManager.sendDebug(this .getClass().getName()
041: + "::doPost: start for session: '"
042: + session.getId() + "'");
043: this .checkSession(session);
044: this .doAction(request, response);
045: } catch (Exception ex) {
046: LogManager.sendDebug(this .getClass().getName()
047: + "::doPost: exception for session: "
048: + session.getId() + "': " + ex);
049: request.setAttribute("EXCEPTION", ex);
050: dispatchToPage(request, response, getDefaultErrorPage());
051: } finally {
052: LogManager.sendDebug(this .getClass().getName()
053: + "::doPost: end for session: '" + session.getId()
054: + "'");
055: }
056: }
057:
058: public static void disableBrowserCache(HttpServletResponse response) {
059: response.setHeader("Cache-Control", "no-cache"); //Forces caches to obtain a new copy of the page from the origin server
060: response.setHeader("Pragma", "no-cache"); //HTTP 1.0 backward compatibility
061: response.setHeader("Cache-Control", "no-store"); //Directs caches not to store the page under any circumstance
062: response.setDateHeader("Expires", 0); //Causes the proxy cache to see the page as "stale"
063: }
064:
065: public static void printHeadersHtml(HttpServletRequest request,
066: Writer out) throws IOException {
067: for (Enumeration e = request.getHeaderNames(); e
068: .hasMoreElements();) {
069: printHeaders(request, out, (String) e.nextElement(), 1,
070: "<br/>", " ");
071: }
072: }
073:
074: public static void printHeaders(HttpServletRequest request,
075: java.io.Writer out) throws IOException {
076: for (Enumeration e = request.getHeaderNames(); e
077: .hasMoreElements();) {
078: printHeaders(request, out, (String) e.nextElement(), 1,
079: "\n", " ");
080: }
081: }
082:
083: public static void printHeaders(HttpServletRequest request,
084: java.io.Writer out, String header, int deeph, String cr,
085: String indent) throws IOException {
086: if (indent != null && deeph > 1)
087: out.write(indent);
088: out.write(header + " = " + request.getHeader(header)
089: + (cr != null ? cr : ""));
090: if (indent != null && deeph > 1)
091: out.write(indent);
092: out.write(header + " = " + request.getHeaders(header)
093: + (cr != null ? cr : ""));
094: for (Enumeration e = request.getHeaders(header); e
095: .hasMoreElements();)
096: printHeaders(request, out, (String) e.nextElement(),
097: ++deeph, cr, indent += indent);
098: if (cr != null)
099: out.write(cr);
100: }
101:
102: public static void checkSession(HttpSession session)
103: throws ExpiredSessionException {
104: JatUser user = (JatUser) session.getAttribute(JatUser.JAT_USER);
105: if (user == null)
106: throw new ExpiredSessionException();
107: }
108:
109: public static void checkPrivilege(HttpSession session,
110: String privilege, Object object)
111: throws PresentationException, BusinessException {
112: JatUser user = (JatUser) session.getAttribute(JatUser.JAT_USER);
113: if (user == null)
114: throw new ExpiredSessionException();
115: if (privilege == null)
116: return;
117: if (!user.hasPrivilege(privilege, object))
118: throw new PresentationException(
119: "User has no privilege to access to this functionality");
120: }
121:
122: public static void checkPrivilege(HttpServletRequest request)
123: throws PresentationException, BusinessException {
124: String privilege = (String) request
125: .getAttribute("JAT_PRIVILEGE_NAME");
126: Object privilegeObject = request
127: .getAttribute("JAT_PRIVILEGE_OBJECT");
128: checkPrivilege(request.getSession(), privilege, privilegeObject);
129: }
130:
131: public static void dispatchToPage(HttpServletRequest request,
132: HttpServletResponse response, String s)
133: throws ServletException, IOException {
134: javax.servlet.RequestDispatcher requestdispatcher = request
135: .getRequestDispatcher(s);
136: if (requestdispatcher == null) {
137: response.sendError(204);
138: } else {
139: requestdispatcher.forward(request, response);
140: }
141: }
142:
143: public static String getDefaultErrorPage() throws ServletException {
144: try {
145: return Config.getCurrent().getValue(ActionLoader.SECTION,
146: "error_page");
147: } catch (Exception ex) {
148: throw new ServletException(ex);
149: }
150: }
151:
152: public static String getInitialPage() throws ServletException {
153: try {
154: return Config.getCurrent().getValue(ActionLoader.SECTION,
155: "initial_page");
156: } catch (Exception ex) {
157: throw new ServletException(ex);
158: }
159: }
160:
161: public static String getHomePage() throws ServletException {
162: try {
163: return Config.getCurrent().getValue(ActionLoader.SECTION,
164: "home_page");
165: } catch (Exception ex) {
166: throw new ServletException(ex);
167: }
168: }
169:
170: /** @link dependency
171: * @stereotype use*/
172: /*# com.jat.business.BusinessObjectFactory lnkBusinessObjectFactory; */
173: }
|