001: /*
002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
003: *
004: * This file is part of TransferCM.
005: *
006: * TransferCM is free software; you can redistribute it and/or modify it under the
007: * terms of the GNU General Public License as published by the Free Software
008: * Foundation; either version 2 of the License, or (at your option) any later
009: * version.
010: *
011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
014: * details.
015: *
016: * You should have received a copy of the GNU General Public License along with
017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018: * Fifth Floor, Boston, MA 02110-1301 USA
019: */
020:
021: package com.methodhead.shim;
022:
023: import org.apache.struts.action.Action;
024: import org.apache.struts.action.ActionMapping;
025: import org.apache.struts.action.ActionForm;
026: import org.apache.struts.action.DynaActionForm;
027: import org.apache.struts.action.ActionForward;
028: import com.methodhead.util.StrutsUtil;
029:
030: import javax.servlet.http.HttpServletRequest;
031: import javax.servlet.http.HttpServletResponse;
032:
033: import com.methodhead.auth.AuthUtil;
034: import com.methodhead.auth.AuthUser;
035: import com.methodhead.auth.AuthAction;
036: import com.methodhead.util.OperationContext;
037: import com.methodhead.tree.FoldingTreeNode;
038: import com.methodhead.sitecontext.SiteContext;
039: import com.methodhead.aikp.IntKey;
040: import com.methodhead.event.Event;
041: import org.apache.commons.lang.StringUtils;
042:
043: public class HomeAction extends AuthAction {
044:
045: // constructors /////////////////////////////////////////////////////////////
046:
047: // constants ////////////////////////////////////////////////////////////////
048:
049: // classes //////////////////////////////////////////////////////////////////
050:
051: // methods //////////////////////////////////////////////////////////////////
052:
053: /**
054: * Returns the <tt>form</tt> forward.
055: */
056: protected ActionForward doHome(OperationContext op, Object policy)
057: throws Exception {
058:
059: String msg = ((ShimPolicy) policy).isHomeAuthorized(op);
060: if (msg != null) {
061: StrutsUtil.addMessage(op.request, msg, null, null, null);
062: return op.mapping.findForward("accessDenied");
063: }
064:
065: //
066: // forward to editing home page if there is one
067: //
068: if (SiteContext.getContext(op.request) != null) {
069: SiteMap siteMap = ShimUtils.getSiteMap(op.request);
070: Link root = (Link) siteMap.getRoot();
071: if (root != null) {
072: return new ActionForward("/editPage.do?id="
073: + root.getPageId());
074: }
075: }
076:
077: return op.mapping.findForward("form");
078: }
079:
080: /**
081: * Returns the <tt>form</tt> forward.
082: */
083: protected ActionForward doSiteMap(OperationContext op, Object policy)
084: throws Exception {
085:
086: String msg = ((ShimPolicy) policy).isSiteMapAuthorized(op);
087: if (msg != null) {
088: StrutsUtil.addMessage(op.request, msg, null, null, null);
089: return op.mapping.findForward("accessDenied");
090: }
091:
092: return op.mapping.findForward("form");
093: }
094:
095: protected ActionForward doLink(OperationContext op, Object policy)
096: throws Exception {
097:
098: String msg = ((ShimPolicy) policy).isLinkAuthorized(op);
099: if (msg != null) {
100: StrutsUtil.addMessage(op.request, msg, null, null, null);
101: return op.mapping.findForward("accessDenied");
102: }
103:
104: FoldingTreeNode root = (FoldingTreeNode) ShimUtils
105: .getSiteMapTree(op.request).getRoot();
106:
107: if (!StringUtils.isBlank((String) op.form.get("open")))
108: root.openNode(Integer
109: .parseInt((String) op.form.get("open")));
110: if (!StringUtils.isBlank((String) op.form.get("close")))
111: root.closeNode(Integer.parseInt((String) op.form
112: .get("close")));
113:
114: return new ActionForward("/siteMap.do");
115: }
116:
117: protected ActionForward doSwitch(OperationContext op, Object policy)
118: throws Exception {
119:
120: String msg = ((ShimPolicy) policy).isSwitchAuthorized(op);
121: if (msg != null) {
122: StrutsUtil.addMessage(op.request, msg, null, null, null);
123: return op.mapping.findForward("accessDenied");
124: }
125:
126: ShimUtils.tearDownShimSession(op.request);
127:
128: SiteContext siteContext = new SiteContext();
129: siteContext.load(new IntKey(op.form.get("id")));
130:
131: ShimUtils.setUpShimSession(op.request, siteContext);
132:
133: Event.log(null, op.user.getLogin(), "shim",
134: "Switched to site \"" + siteContext.getDomains().get(0)
135: + "\"");
136:
137: return new ActionForward("/home.do");
138: }
139:
140: public ActionForward doExecute(ActionMapping mapping,
141: ActionForm form, HttpServletRequest request,
142: HttpServletResponse response) throws Exception {
143:
144: //
145: // get some things we'll need
146: //
147: Object policy = StrutsUtil.getPolicy(mapping);
148:
149: DynaActionForm dynaForm = (DynaActionForm) form;
150: AuthUser user = AuthUtil.getUser(request);
151:
152: OperationContext op = new OperationContext(mapping, dynaForm,
153: request, response, user);
154:
155: //
156: // execute the appopriate method
157: //
158: if (mapping.getPath().equals("/home")) {
159: return doHome(op, policy);
160: }
161: if (mapping.getPath().equals("/link")) {
162: return doLink(op, policy);
163: }
164: if (mapping.getPath().equals("/switch")) {
165: return doSwitch(op, policy);
166: }
167: if (mapping.getPath().equals("/siteMap")) {
168: return doSiteMap(op, policy);
169: }
170:
171: throw new Exception("Unexpected mapping path \""
172: + mapping.getPath() + "\"");
173: }
174:
175: // properties ///////////////////////////////////////////////////////////////
176:
177: // attributes ///////////////////////////////////////////////////////////////
178: }
|