001: package com.technoetic.xplanner.actions;
002:
003: import com.technoetic.xplanner.db.hibernate.ThreadSession;
004: import com.technoetic.xplanner.security.SecurityHelper;
005: import com.technoetic.xplanner.security.auth.Authorizer;
006: import org.apache.log4j.Logger;
007: import org.apache.struts.action.Action;
008: import org.apache.struts.action.ActionError;
009: import org.apache.struts.action.ActionErrors;
010: import org.apache.struts.action.ActionForm;
011: import org.apache.struts.action.ActionForward;
012: import org.apache.struts.action.ActionMapping;
013:
014: import javax.servlet.ServletException;
015: import javax.servlet.http.HttpServletRequest;
016: import javax.servlet.http.HttpServletResponse;
017: import java.io.IOException;
018:
019: /**
020: * Generic dispatcher to ActionForwards.
021: * Original Author: Ted Husted
022: * Source: http://husted.com/about/scaffolding/catalog.htm
023: */
024: public final class DispatchForward extends Action {
025: private Logger log = Logger.getLogger(getClass());
026: private boolean isAuthorizationRequired = true;
027: private Authorizer authorizer;
028:
029: /**
030: * Forward request to "cancel", {forward}, or "error" mapping, where {forward}
031: * is an action path given in the parameter mapping or in the request as
032: * "forward=actionPath".
033: *
034: * @param mapping The ActionMapping used to select this instance
035: * @param form The optional ActionForm bean for this request (if any)
036: * @param request The HTTP request we are processing
037: * @param response The HTTP response we are creating
038: *
039: * @exception IOException if an input/output error occurs
040: * @exception ServletException if a servlet exception occurs
041: */
042: public ActionForward execute(ActionMapping mapping,
043: ActionForm form, HttpServletRequest request,
044: HttpServletResponse response) throws Exception {
045: if (isSecure(mapping)) {
046: int projectId = 0;
047: String projectIdParameter = request
048: .getParameter("projectId");
049: if (projectIdParameter != null) {
050: projectId = Integer.parseInt(request
051: .getParameter("projectId"));
052: }
053: if (projectId == 0) {
054: log
055: .error("no project identifier supplied for secure access");
056: return mapping.findForward("security/notAuthorized");
057: }
058: if (!authorizer.hasPermission(projectId, SecurityHelper
059: .getRemoteUserId(request), "system.project",
060: projectId, "read")) {
061: return mapping.findForward("security/notAuthorized");
062: }
063: }
064:
065: // -- Locals
066: ActionForward this Forward = null;
067: String wantForward = null;
068:
069: // -- Check internal parameter for forward
070: wantForward = mapping.getParameter();
071:
072: // -- If found, consult mappings
073: if (wantForward != null)
074: this Forward = mapping.findForward(wantForward);
075:
076: // -- If anything not found, dispatch error
077: if (this Forward == null) {
078: this Forward = mapping.findForward("error");
079: ActionErrors errors = new ActionErrors();
080: errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
081: "action.missing.parameter"));
082: saveErrors(request, errors);
083: }
084:
085: return this Forward;
086:
087: }
088:
089: public void setAuthorizationRequired(boolean authorizationRequired) {
090: isAuthorizationRequired = authorizationRequired;
091: }
092:
093: public void setAuthorizer(Authorizer authorizer) {
094: this .authorizer = authorizer;
095: }
096:
097: private boolean isSecure(ActionMapping mapping) {
098: return mapping.findForward("@secure") != null ? Boolean
099: .valueOf(mapping.findForward("@secure").getPath()) != Boolean.FALSE
100: : isAuthorizationRequired;
101: }
102:
103: }
|