KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > wizard > actions > AbstractWizardAction


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.wizard.actions;
21
22 import java.util.Iterator JavaDoc;
23
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.struts.action.ActionForm;
30 import org.apache.struts.action.ActionForward;
31 import org.apache.struts.action.ActionMapping;
32
33 import com.sslexplorer.boot.PropertyList;
34 import com.sslexplorer.boot.Util;
35 import com.sslexplorer.core.RedirectException;
36 import com.sslexplorer.core.actions.AuthenticatedDispatchAction;
37 import com.sslexplorer.policyframework.Permission;
38 import com.sslexplorer.policyframework.PolicyDatabaseFactory;
39 import com.sslexplorer.policyframework.Resource;
40 import com.sslexplorer.policyframework.ResourceType;
41 import com.sslexplorer.policyframework.ResourceUtil;
42 import com.sslexplorer.policyframework.forms.AbstractWizardPolicySelectionForm;
43 import com.sslexplorer.security.Constants;
44 import com.sslexplorer.wizard.AbstractWizardSequence;
45 import com.sslexplorer.wizard.WizardActionStatus;
46 import com.sslexplorer.wizard.WizardStep;
47 import com.sslexplorer.wizard.forms.AbstractWizardForm;
48
49 /**
50  * Abstract class that extends
51  * {@link com.sslexplorer.core.actions.AuthenticatedDispatchAction} and should
52  * be used by all actions in a wizard sequence.
53  *
54  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
55  */

56 public abstract class AbstractWizardAction extends AuthenticatedDispatchAction {
57
58     final static Log log = LogFactory.getLog(AbstractWizardAction.class);
59
60         /**
61      * Use this constructor for actions that do not require any resource
62      * permissions
63      */

64     public AbstractWizardAction() {
65     }
66
67     /**
68      * Use this constructor for actions that require a resource permission to
69      * operator
70      *
71      * @param resourceType resource type
72      * @param permissions permission required
73      */

74     public AbstractWizardAction(ResourceType resourceType, Permission permissions[]) {
75         super(resourceType, permissions);
76     }
77
78     /**
79      * Start the sequence. Subclasses that overide this method should always
80      * invoke this as well.
81      *
82      * @param mapping mapping
83      * @param form form
84      * @param request request
85      * @param response response
86      * @return forward
87      * @throws Exception
88      */

89     public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
90                     throws Exception JavaDoc {
91         request.getSession().removeAttribute(Constants.WIZARD_SEQUENCE);
92         return unspecified(mapping, form, request, response);
93     }
94
95     /*
96      * (non-Javadoc)
97      *
98      * @see org.apache.struts.actions.DispatchAction#unspecified(org.apache.struts.action.ActionMapping,
99      * org.apache.struts.action.ActionForm,
100      * javax.servlet.http.HttpServletRequest,
101      * javax.servlet.http.HttpServletResponse)
102      */

103     public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request,
104                     HttpServletResponse JavaDoc response) throws Exception JavaDoc {
105         AbstractWizardSequence seq = (AbstractWizardSequence) request.getSession().getAttribute(Constants.WIZARD_SEQUENCE);
106         if (seq == null) {
107             try {
108                 seq = createWizardSequence(mapping, form, request, response);
109             }
110             catch(RedirectException rde) {
111                 log.error("Redirect to . " + rde.getForward().getPath(), rde);
112                 return rde.getForward();
113             }
114             request.getSession().setAttribute(Constants.WIZARD_SEQUENCE, seq);
115         }
116         ((AbstractWizardForm) form).init(seq, request);
117         seq.setCurrentPageForm((AbstractWizardForm) form);
118         Util.noCache(response);
119         return mapping.findForward("display");
120     }
121
122     /**
123      * Create a new {@link com.sslexplorer.wizard.AbstractWizardSequence} to
124      * store the state for this sequence of wizard pages. <strong>Only the first
125      * action in the sequence should implement this method, all other pages
126      * should thrown an exception when invoked (this default implementation
127      * does just that)
128      *
129      * @param mapping mapping
130      * @param form form
131      * @param request request
132      * @param response response
133      * @return wizard sequence object for storing state between pages
134      * @throws Exception if object cannot be create or this page is not the
135      * first in a sequence
136      */

137     protected AbstractWizardSequence createWizardSequence(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request,
138                                                           HttpServletResponse JavaDoc response) throws Exception JavaDoc {
139         throw new RedirectException(cancel(mapping, form, request, response), "Cannot create sequence on this page.");
140     }
141
142     /**
143      * Move the wizard to the next page an Exception will be thrown.
144      *
145      * @param mapping mapping
146      * @param form form
147      * @param request request
148      * @param response response
149      * @return forward forward
150      * @throws Exception on any error
151      */

152     public ActionForward next(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
153                     throws Exception JavaDoc {
154         applyToSequence(mapping, (AbstractWizardForm) form, request, response);
155         return mapping.findForward("next");
156     }
157
158     /**
159      * Move the wizard to a different step
160      *
161      * @param mapping mapping
162      * @param form form
163      * @param request request
164      * @param response response
165      * @return forward forward
166      * @throws Exception on any error
167      */

168     public ActionForward gotoStep(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
169                     throws Exception JavaDoc {
170         applyToSequence(mapping, (AbstractWizardForm) form, request, response);
171         AbstractWizardSequence seq = getWizardSequence(request);
172         return new ActionForward(((WizardStep) seq.getSteps().get(((AbstractWizardForm) form).getGotoStep())).getPath(), true);
173     }
174
175     /*
176      * Apply the current form to the wizard sequence object
177      */

178     protected void applyToSequence(ActionMapping mapping, AbstractWizardForm form, HttpServletRequest JavaDoc request,
179                     HttpServletResponse JavaDoc response) throws Exception JavaDoc {
180         AbstractWizardSequence seq = (AbstractWizardSequence) request.getSession().getAttribute(Constants.WIZARD_SEQUENCE);
181         if (seq == null) {
182             throw new Exception JavaDoc("No sequence.");
183         }
184         form.apply(seq);
185     }
186
187     /**
188      * Move the wizard to the previous page. If no previous page is available an
189      * Exception will be thrown.
190      *
191      * @param mapping
192      * @param form
193      * @param request
194      * @param response
195      * @return forward
196      * @throws Exception
197      */

198     public ActionForward previous(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
199                     throws Exception JavaDoc {
200         return mapping.findForward("previous");
201     }
202
203     /**
204      * Move the wizard to the last page.
205      *
206      * @param mapping
207      * @param form
208      * @param request
209      * @param response
210      * @return forward
211      * @throws Exception
212      */

213     public ActionForward finish(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
214                     throws Exception JavaDoc {
215         applyToSequence(mapping, (AbstractWizardForm) form, request, response);
216         AbstractWizardSequence seq = (AbstractWizardSequence) request.getSession().getAttribute(Constants.WIZARD_SEQUENCE);
217         if (seq == null) {
218             throw new Exception JavaDoc("No sequence.");
219         }
220         return seq.getFinishActionForward();
221     }
222
223     /**
224      * Cancel the wizard and return to the page the user was at before starting
225      * the wizard. This is done using the referer attribute of the sequence
226      * object.
227      *
228      * @param mapping
229      * @param form
230      * @param request
231      * @param response
232      * @return forward
233      * @throws Exception
234      */

235     public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
236                     throws Exception JavaDoc {
237         AbstractWizardSequence seq = getWizardSequence(request);
238         if(seq != null) {
239             request.getSession().removeAttribute(Constants.WIZARD_SEQUENCE);
240             for (Iterator JavaDoc i = seq.getForms().iterator(); i.hasNext();) {
241                 String JavaDoc formName = ((AbstractWizardForm) i.next()).getPageName() + "Form";
242                 request.getSession().removeAttribute(formName);
243             }
244             ActionForward fwd = new ActionForward(seq.getReferer(), true);
245             return fwd;
246         }
247         return mapping.findForward("home");
248     }
249
250     /**
251      * Convenience method to get the current
252      * {@link com.sslexplorer.wizard.AbstractWizardSequence} for the session
253      *
254      * @param request request from which to get session
255      * @return current wizard sequence object or null if none
256      */

257     public AbstractWizardSequence getWizardSequence(HttpServletRequest JavaDoc request) {
258         return (AbstractWizardSequence) request.getSession().getAttribute(Constants.WIZARD_SEQUENCE);
259     }
260
261     /**
262      * Re-run the wizard
263      *
264      * @param mapping mapping
265      * @param form form
266      * @param request request
267      * @param response response
268      * @return forward
269      * @throws Exception on any error
270      */

271     public ActionForward rerun(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
272                     throws Exception JavaDoc {
273         return mapping.findForward("rerun");
274     }
275
276     /**
277      * <p>
278      * Attach the resource to the policies requested and add to favorites.
279      *
280      * @param messagePrefix prefix in message resources
281      * @param seq The WizardSequence for this wizard.
282      * @param resource resource
283      * @param addToFavorites addToFavorites
284      * @return wizard status
285      */

286     protected WizardActionStatus attachToPoliciesAndAddToFavorites(String JavaDoc messagePrefix, AbstractWizardSequence seq, Resource resource, boolean addToFavorites, HttpServletRequest JavaDoc request) {
287         PropertyList selectedPolicies = (PropertyList) seq.getAttribute(AbstractWizardPolicySelectionForm.ATTR_SELECTED_POLICIES,
288             null);
289         try {
290             PolicyDatabaseFactory.getInstance().attachResourceToPolicyList(resource, selectedPolicies, getSessionInfo(request));
291             ResourceUtil.setResourceGlobalFavorite(resource, addToFavorites);
292             return new WizardActionStatus(WizardActionStatus.COMPLETED_OK, messagePrefix + ".status.attachedToPolicies");
293         } catch (Exception JavaDoc e) {
294             log.error("Failed to create profile.", e);
295             return new WizardActionStatus(WizardActionStatus.COMPLETED_WITH_ERRORS, messagePrefix
296                             + ".status.failedToAttachToPolicies", e.getMessage());
297         }
298     }
299 }
300
Popular Tags