001: package org.claros.mini.common;
002:
003: import javax.servlet.http.Cookie;
004: import javax.servlet.http.HttpServletRequest;
005: import javax.servlet.http.HttpServletResponse;
006:
007: import org.apache.struts.action.Action;
008: import org.apache.struts.action.ActionForm;
009: import org.apache.struts.action.ActionForward;
010: import org.apache.struts.action.ActionMapping;
011: import org.claros.commons.exception.ClarosBaseException;
012: import org.claros.commons.mail.exception.ServerDownException;
013: import org.claros.commons.mail.models.ConnectionMetaHandler;
014: import org.claros.commons.mail.models.ConnectionProfile;
015: import org.claros.mini.utility.*;
016:
017: import com.jenkov.mrpersister.itf.IGenericDao;
018:
019: /**
020: * @author Umut Gokbayrak
021: *
022: */
023: public abstract class BaseClarosAction extends Action {
024: public abstract ActionForward myExecute(ActionMapping mapping,
025: ActionForm form, HttpServletRequest request,
026: HttpServletResponse response) throws Exception;
027:
028: /* (non-Javadoc)
029: * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
030: */
031: public ActionForward execute(ActionMapping mapping,
032: ActionForm form, HttpServletRequest request,
033: HttpServletResponse response) throws Exception {
034: commonFunctions(response);
035:
036: ActionForward forward = null;
037: try {
038: forward = myExecute(mapping, form, request, response);
039: } catch (ServerDownException e) {
040: request.setAttribute("myexception", e.getMessage());
041: forward = mapping.findForward("login");
042: } catch (Exception e) {
043: ClarosBaseException ce = new ClarosBaseException(e);
044: request.setAttribute("myexception", ce);
045: forward = mapping.findForward("generalerror");
046: }
047: return forward;
048: }
049:
050: /**
051: * Adds the response headers etc...
052: * @param mapping
053: * @param form
054: * @param request
055: * @param response
056: */
057: public void commonFunctions(HttpServletResponse response) {
058: response.setHeader("Expires", "-1");
059: response.setHeader("Pragma", "no-cache");
060: response.setHeader("Cache-control", "no-cache");
061: response.setContentType("text/html; charset=utf-8");
062: }
063:
064: /**
065: * Searches for the variable in various places and returns it.
066: * @param request
067: * @param name
068: * @return Object
069: */
070: public Object getVariable(HttpServletRequest request, String name) {
071: Object obj = request.getParameter(name);
072: if (obj == null) {
073: obj = request.getAttribute(name);
074: if (obj == null) {
075: obj = request.getSession().getAttribute(name);
076: if (obj == null) {
077: obj = getCookie(request, name);
078: if (obj == null) {
079: obj = getServlet().getServletContext()
080: .getAttribute(name);
081: }
082: }
083: }
084: }
085: return obj;
086: }
087:
088: public String getCookie(HttpServletRequest request, String name) {
089: Cookie cookies[] = request.getCookies();
090: Cookie cookie = null;
091: if (cookies != null) {
092: for (int i = 0; i < cookies.length; i++) {
093: cookie = cookies[i];
094: if (cookie.getName().equals(name)) {
095: return cookie.getValue();
096: }
097: }
098: }
099: return null;
100: }
101:
102: public ConnectionProfile getConnectionProfile(
103: HttpServletRequest request) {
104: return (ConnectionProfile) request.getSession().getAttribute(
105: "profile");
106: }
107:
108: public ConnectionMetaHandler getConnectionHandler(
109: HttpServletRequest request) {
110: return (ConnectionMetaHandler) request.getSession()
111: .getAttribute("handler");
112: }
113:
114: public IGenericDao getDbConnection() throws Exception {
115: return Utility.getDbConnection("file");
116: }
117:
118: public IGenericDao getDbConnection(String name) throws Exception {
119: return Utility.getDbConnection(name);
120: }
121:
122: }
|