KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > policyframework > actions > AbstractLaunchAction


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.policyframework.actions;
21
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24
25 import org.apache.struts.Globals;
26 import org.apache.struts.action.ActionForm;
27 import org.apache.struts.action.ActionForward;
28 import org.apache.struts.action.ActionMapping;
29 import org.apache.struts.action.ActionMessages;
30
31 import com.sslexplorer.agent.DefaultAgentManager;
32 import com.sslexplorer.boot.Util;
33 import com.sslexplorer.core.CoreException;
34 import com.sslexplorer.core.CoreUtil;
35 import com.sslexplorer.core.RedirectWithMessages;
36 import com.sslexplorer.core.actions.AuthenticatedAction;
37 import com.sslexplorer.policyframework.LaunchSession;
38 import com.sslexplorer.policyframework.LaunchSessionFactory;
39 import com.sslexplorer.policyframework.LaunchSessionManager;
40 import com.sslexplorer.policyframework.Policy;
41 import com.sslexplorer.policyframework.Resource;
42 import com.sslexplorer.policyframework.ResourceType;
43 import com.sslexplorer.security.SessionInfo;
44
45 /**
46  * Abstract implementation of {@link com.sslexplorer.core.actions.AuthenticatedAction}
47  * that 'Launches' some kind of <i>Resource</i>.
48  * <p>
49  *
50  *
51  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
52  */

53 public abstract class AbstractLaunchAction extends AuthenticatedAction {
54     
55     private ResourceType resourceType;
56     private int navigationContext;
57
58     /**
59      * Constructor.
60      *
61      * @param resourceType
62      * @param navigationContext
63      *
64      */

65     public AbstractLaunchAction(ResourceType resourceType, int navigationContext) {
66         super();
67         this.resourceType = resourceType;
68         this.navigationContext = navigationContext;
69     }
70
71     /* (non-Javadoc)
72      * @see com.sslexplorer.core.actions.AuthenticatedAction#onExecute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
73      */

74     public ActionForward onExecute(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
75                     throws Exception JavaDoc {
76         String JavaDoc id = request.getParameter("resourceId");
77         String JavaDoc returnTo = request.getParameter("returnTo");
78         if (Util.isNullOrTrimmedBlank(id)) {
79             throw new Exception JavaDoc("No resource ID supplied.");
80         }
81         if (Util.isNullOrTrimmedBlank(returnTo)) {
82             throw new Exception JavaDoc("No returnTo path supplied.");
83         }
84         Resource resource = resourceType.getResourceById(Integer.parseInt(id));
85         if(resource == null) {
86             throw new Exception JavaDoc("No resource with ID " + id);
87         }
88         SessionInfo session = getSessionInfo(request);
89         Policy policy = LaunchSessionManager.getLaunchRequestPolicy(request, session, resource);
90         Util.noCache(response);
91         if(resource.sessionPasswordRequired(session)) {
92             // Prompt for the session password then come back here
93
return new ActionForward("/promptForSessionPassword.do?forwardTo=" + Util.urlEncode(CoreUtil.getRealRequestURI(request) + "?resourceId=" + id + "&policy=" + policy.getResourceId() + "&returnTo=" + Util.urlEncode(returnTo)), true);
94         }
95         else {
96             // Launch the agent and return here
97
if(isAgentRequired(resource) && !DefaultAgentManager.getInstance().hasActiveAgent(request)) {
98                 return new ActionForward("/launchAgent.do?returnTo=" + Util.urlEncode(CoreUtil.getRealRequestURI(request) + "?resourceId=" + id + "&policy=" + policy.getResourceId() + "&returnTo=" + Util.urlEncode(returnTo)), true);
99             }
100             else {
101                 LaunchSession launchSession = LaunchSessionFactory.getInstance().createLaunchSession(session, resource, policy);
102                 launchSession.checkAccessRights(null, session);
103                 try {
104                     return launch(mapping, launchSession, request, returnTo);
105                 }
106                 catch(CoreException ce) {
107                     ActionMessages errs = new ActionMessages();
108                     errs.add(Globals.ERROR_KEY, ce.getBundleActionMessage());
109                     saveErrors(request, errs);
110                     return new RedirectWithMessages(returnTo, request);
111                 }
112             }
113         }
114     }
115
116     protected abstract ActionForward launch(ActionMapping mapping, LaunchSession launchSession, HttpServletRequest JavaDoc request, String JavaDoc returnTo)
117                     throws Exception JavaDoc;
118     
119     protected abstract boolean isAgentRequired(Resource resource);
120
121     /*
122      * (non-Javadoc)
123      *
124      * @see com.sslexplorer.core.actions.CoreAction#getNavigationContext(org.apache.struts.action.ActionMapping,
125      * org.apache.struts.action.ActionForm,
126      * javax.servlet.http.HttpServletRequest,
127      * javax.servlet.http.HttpServletResponse)
128      */

129
130     public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
131         return navigationContext;
132     }
133 }
134
Popular Tags