KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > shim > HomeAction


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

20
21 package com.methodhead.shim;
22
23 import org.apache.struts.action.Action;
24 import org.apache.struts.action.ActionMapping;
25 import org.apache.struts.action.ActionForm;
26 import org.apache.struts.action.DynaActionForm;
27 import org.apache.struts.action.ActionForward;
28 import com.methodhead.util.StrutsUtil;
29
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32
33 import com.methodhead.auth.AuthUtil;
34 import com.methodhead.auth.AuthUser;
35 import com.methodhead.auth.AuthAction;
36 import com.methodhead.util.OperationContext;
37 import com.methodhead.tree.FoldingTreeNode;
38 import com.methodhead.sitecontext.SiteContext;
39 import com.methodhead.aikp.IntKey;
40 import com.methodhead.event.Event;
41 import org.apache.commons.lang.StringUtils;
42
43 public class HomeAction
44 extends
45   AuthAction {
46
47   // constructors /////////////////////////////////////////////////////////////
48

49   // constants ////////////////////////////////////////////////////////////////
50

51   // classes //////////////////////////////////////////////////////////////////
52

53   // methods //////////////////////////////////////////////////////////////////
54

55   /**
56    * Returns the <tt>form</tt> forward.
57    */

58   protected ActionForward doHome(
59     OperationContext op,
60     Object JavaDoc policy )
61   throws
62     Exception JavaDoc {
63
64     String JavaDoc msg = ( ( ShimPolicy )policy ).isHomeAuthorized( op );
65     if ( msg != null ) {
66       StrutsUtil.addMessage( op.request, msg, null, null, null );
67       return op.mapping.findForward( "accessDenied" );
68     }
69     
70     //
71
// forward to editing home page if there is one
72
//
73
if ( SiteContext.getContext( op.request ) != null ) {
74       SiteMap siteMap = ShimUtils.getSiteMap( op.request );
75       Link root = ( Link )siteMap.getRoot();
76       if ( root != null ) {
77         return new ActionForward( "/editPage.do?id=" + root.getPageId() );
78       }
79     }
80
81     return op.mapping.findForward( "form" );
82   }
83
84   /**
85    * Returns the <tt>form</tt> forward.
86    */

87   protected ActionForward doSiteMap(
88     OperationContext op,
89     Object JavaDoc policy )
90   throws
91     Exception JavaDoc {
92
93     String JavaDoc msg = ( ( ShimPolicy )policy ).isSiteMapAuthorized( op );
94     if ( msg != null ) {
95       StrutsUtil.addMessage( op.request, msg, null, null, null );
96       return op.mapping.findForward( "accessDenied" );
97     }
98
99     return op.mapping.findForward( "form" );
100   }
101
102   protected ActionForward doLink(
103     OperationContext op,
104     Object JavaDoc policy )
105   throws
106     Exception JavaDoc {
107
108     String JavaDoc msg = ( ( ShimPolicy )policy ).isLinkAuthorized( op );
109     if ( msg != null ) {
110       StrutsUtil.addMessage( op.request, msg, null, null, null );
111       return op.mapping.findForward( "accessDenied" );
112     }
113
114     FoldingTreeNode root =
115       ( FoldingTreeNode )ShimUtils.getSiteMapTree( op.request ).getRoot();
116
117     if ( !StringUtils.isBlank( ( String JavaDoc )op.form.get( "open" ) ) )
118       root.openNode( Integer.parseInt( ( String JavaDoc )op.form.get( "open" ) ) );
119     if ( !StringUtils.isBlank( ( String JavaDoc )op.form.get( "close" ) ) )
120       root.closeNode( Integer.parseInt( ( String JavaDoc )op.form.get( "close" ) ) );
121
122     return new ActionForward( "/siteMap.do" );
123   }
124
125   protected ActionForward doSwitch(
126     OperationContext op,
127     Object JavaDoc policy )
128   throws
129     Exception JavaDoc {
130
131     String JavaDoc msg = ( ( ShimPolicy )policy ).isSwitchAuthorized( op );
132     if ( msg != null ) {
133       StrutsUtil.addMessage( op.request, msg, null, null, null );
134       return op.mapping.findForward( "accessDenied" );
135     }
136
137     ShimUtils.tearDownShimSession( op.request );
138
139     SiteContext siteContext = new SiteContext();
140     siteContext.load( new IntKey( op.form.get( "id" ) ) );
141
142     ShimUtils.setUpShimSession( op.request, siteContext );
143
144     Event.log(
145       null,
146       op.user.getLogin(),
147       "shim",
148       "Switched to site \"" + siteContext.getDomains().get( 0 ) + "\"" );
149
150     return new ActionForward( "/home.do" );
151   }
152
153   public ActionForward doExecute(
154     ActionMapping mapping,
155     ActionForm form,
156     HttpServletRequest JavaDoc request,
157     HttpServletResponse JavaDoc response )
158   throws
159     Exception JavaDoc {
160
161     //
162
// get some things we'll need
163
//
164
Object JavaDoc policy = StrutsUtil.getPolicy( mapping );
165
166     DynaActionForm dynaForm = ( DynaActionForm )form;
167     AuthUser user = AuthUtil.getUser( request );
168
169     OperationContext op =
170       new OperationContext( mapping, dynaForm, request, response, user );
171
172     //
173
// execute the appopriate method
174
//
175
if ( mapping.getPath().equals( "/home" ) ) {
176       return doHome( op, policy );
177     }
178     if ( mapping.getPath().equals( "/link" ) ) {
179       return doLink( op, policy );
180     }
181     if ( mapping.getPath().equals( "/switch" ) ) {
182       return doSwitch( op, policy );
183     }
184     if ( mapping.getPath().equals( "/siteMap" ) ) {
185       return doSiteMap( op, policy );
186     }
187
188     throw
189       new Exception JavaDoc( "Unexpected mapping path \"" + mapping.getPath() + "\"" );
190   }
191
192   // properties ///////////////////////////////////////////////////////////////
193

194   // attributes ///////////////////////////////////////////////////////////////
195
}
196
Popular Tags