KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > extensions > actions > DefaultExtensionsAction


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.extensions.actions;
21
22 import java.io.InputStream JavaDoc;
23 import java.net.URLConnection JavaDoc;
24
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpServletResponse JavaDoc;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.struts.Globals;
31 import org.apache.struts.action.ActionForm;
32 import org.apache.struts.action.ActionForward;
33 import org.apache.struts.action.ActionMapping;
34 import org.apache.struts.action.ActionMessage;
35 import org.apache.struts.action.ActionMessages;
36
37 import com.sslexplorer.boot.Util;
38 import com.sslexplorer.core.BundleActionMessage;
39 import com.sslexplorer.core.CoreException;
40 import com.sslexplorer.core.CoreUtil;
41 import com.sslexplorer.core.GlobalWarning;
42 import com.sslexplorer.core.RedirectWithMessages;
43 import com.sslexplorer.extensions.ExtensionBundle;
44 import com.sslexplorer.extensions.ExtensionUploadHandler;
45 import com.sslexplorer.extensions.ExtensionBundle.ExtensionBundleStatus;
46 import com.sslexplorer.extensions.forms.DefaultExtensionsForm;
47 import com.sslexplorer.extensions.store.ExtensionStore;
48 import com.sslexplorer.extensions.store.ExtensionStoreDescriptor;
49 import com.sslexplorer.policyframework.Permission;
50 import com.sslexplorer.policyframework.PolicyConstants;
51 import com.sslexplorer.policyframework.PolicyUtil;
52 import com.sslexplorer.properties.Property;
53 import com.sslexplorer.properties.impl.systemconfig.SystemConfigKey;
54 import com.sslexplorer.security.Constants;
55 import com.sslexplorer.security.SessionInfo;
56 import com.sslexplorer.table.actions.AbstractPagerAction;
57 import com.sslexplorer.tasks.Task;
58 import com.sslexplorer.tasks.TaskHttpServletRequest;
59 import com.sslexplorer.tasks.TaskInputStream;
60 import com.sslexplorer.tasks.TaskProgressBar;
61 import com.sslexplorer.vfs.UploadDetails;
62
63 public class DefaultExtensionsAction extends AbstractPagerAction {
64     
65     final static Log log = LogFactory.getLog(DefaultExtensionsAction.class);
66
67     String JavaDoc extensionCategory = null;
68     
69     public DefaultExtensionsAction(String JavaDoc extensionCategory) {
70         super(PolicyConstants.EXTENSIONS_RESOURCE_TYPE, new Permission[] { PolicyConstants.PERM_CHANGE });
71         this.extensionCategory = extensionCategory;
72     }
73
74     public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
75                     throws Exception JavaDoc {
76         return this.list(mapping, form, request, response);
77     }
78         
79     public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
80         throws Exception JavaDoc {
81         DefaultExtensionsForm defaultExtensionForm = (DefaultExtensionsForm) form;
82         CoreUtil.clearFlow(request);
83
84         if (!ExtensionStore.STORE_PREF.getBoolean("warnedAboutExtensionStoreConnect", true)) {
85             if (request.getParameter("agree") != null) {
86                 ExtensionStore.STORE_PREF.putBoolean("warnedAboutExtensionStoreConnect", true);
87                 SessionInfo info = this.getSessionInfo(request);
88                 Property.setProperty(new SystemConfigKey("updates.automaticallyConnectToApplicationStore"),
89                     request.getParameter("agree"),
90                     info);
91             } else {
92                 return mapping.findForward("agreement");
93             }
94         }
95         try {
96             ExtensionStoreDescriptor storeDescriptor = ExtensionStore.getInstance()
97             .getDownloadableExtensionStoreDescriptor(request.getParameter("connect") != null || Property.getPropertyBoolean(new SystemConfigKey("updates.automaticallyConnectToApplicationStore")));
98             Util.noCache(response);
99         } catch (Exception JavaDoc e) {
100         }
101         defaultExtensionForm.initialise(request.getSession(), ExtensionStore.getInstance().getAllAvailableExtensionBundles(extensionCategory));
102         return null; // Return null because we are in a subform
103
}
104
105     public ActionForward refresh(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
106                     throws Exception JavaDoc {
107         DefaultExtensionsForm defaultExtensionsForm = (DefaultExtensionsForm) form;
108         ExtensionStoreDescriptor storeDescriptor = null;
109         PolicyUtil.checkPermissions(PolicyConstants.EXTENSIONS_RESOURCE_TYPE, new Permission[] { PolicyConstants.PERM_CHANGE },
110             request);
111         ExtensionStore extensionStore = ExtensionStore.getInstance();
112         ActionMessages msgs = new ActionMessages();
113         try {
114             extensionStore.resetExtensionStoreUpdate();
115             storeDescriptor = extensionStore.getDownloadableExtensionStoreDescriptor(true, ExtensionStore.getWorkingVersion());
116             msgs.add(Globals.MESSAGE_KEY, new BundleActionMessage("extensions", "extensionStore.message.refreshed"));
117             saveMessages(request, msgs);
118         } catch (Exception JavaDoc e) {
119             log.error("Failed to refresh extension store.", e);
120             msgs.add(Globals.ERROR_KEY, new BundleActionMessage("extensions", "extensionStore.message.failedToRefresh", e
121                             .getMessage()));
122             saveErrors(request, msgs);
123         }
124         defaultExtensionsForm.initialise(request.getSession(), ExtensionStore.getInstance().getAllAvailableExtensionBundles(extensionCategory));
125         return null; // Return null because we are in a subform
126
}
127
128     /**
129      * Upload an extension
130      *
131      * @param mapping mapping
132      * @param form form
133      * @param request request
134      * @param response response
135      * @return forward
136      * @throws Exception on any error
137      */

138     public ActionForward upload(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
139                     throws Exception JavaDoc {
140         PolicyUtil.checkPermissions(PolicyConstants.EXTENSIONS_RESOURCE_TYPE,
141             new Permission[] { PolicyConstants.PERM_CHANGE },
142             request);
143         UploadDetails details = new UploadDetails("extensions",
144                         ExtensionUploadHandler.TYPE_EXTENSION,
145                         null,
146                         mapping.findForward("list"),
147                         null, // Keep this as NULL. We do not want to show Exit button here!
148
mapping.findForward("list"));
149         int id = CoreUtil.addUpload(request.getSession(), details);
150         request.setAttribute(Constants.REQ_ATTR_UPLOAD_DETAILS, new Integer JavaDoc(id));
151         return mapping.findForward("upload");
152     }
153
154     /**
155      * Remove selected extension
156      *
157      * @param mapping mapping
158      * @param form form
159      * @param request request
160      * @param response response
161      * @return forward
162      * @throws Exception on any error
163      */

164     public ActionForward remove(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
165                     throws Exception JavaDoc {
166         PolicyUtil.checkPermission(PolicyConstants.EXTENSIONS_RESOURCE_TYPE,
167             PolicyConstants.PERM_CHANGE,
168             getSessionInfo(request).getUser());
169         String JavaDoc id = request.getParameter("id");
170         if (id == null || id.equals("")) {
171             throw new Exception JavaDoc("No id parameter supplied.");
172         }
173         ExtensionBundle bundle = ExtensionStore.getInstance().getExtensionBundle(id);
174         if (bundle == null) {
175             throw new Exception JavaDoc("No application with an id of " + id);
176         }
177         ExtensionStore.getInstance().removeExtensionBundle(bundle);
178         if (bundle.isContainsPlugin())
179             CoreUtil.addMultipleGlobalWarning(GlobalWarning.MANAGEMENT_USERS, new BundleActionMessage("extensions",
180                             "extensionStore.message.pluginRemovedRestartRequired"));
181         ActionMessages msgs = new ActionMessages();
182         msgs.add(Globals.MESSAGE_KEY, new ActionMessage("extensionStore.message.applicationRemoved", bundle.getName()));
183         saveMessages(request, msgs);
184         return mapping.findForward("list");
185     }
186
187     /**
188      * Install selected extension
189      *
190      * @param mapping mapping
191      * @param form form
192      * @param request request
193      * @param response response
194      * @return forward
195      * @throws Exception on any error
196      */

197     public ActionForward install(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
198                     throws Exception JavaDoc {
199         String JavaDoc id = request.getParameter("id");
200         String JavaDoc version = request.getParameter("version");
201         PolicyUtil.checkPermission(PolicyConstants.EXTENSIONS_RESOURCE_TYPE,
202             PolicyConstants.PERM_CHANGE,
203             getSessionInfo(request).getUser());
204         ActionForward fwd = new RedirectWithMessages(mapping.findForward("refresh"), request);
205         URLConnection JavaDoc con = ExtensionStore.getInstance().downloadExtension(id, version);
206         
207         try {
208             InputStream JavaDoc in = con.getInputStream();
209             ExtensionBundle bundle = ExtensionStore.getInstance().installExtensionFromStore(id, in, request, con.getContentLength());
210             ExtensionStore.getInstance().licenseCheck(bundle, request, fwd);
211             ExtensionStore.getInstance().postInstallExtension(bundle, request);
212             ActionMessages msgs = new ActionMessages();
213             msgs.add(Globals.MESSAGE_KEY, new ActionMessage("extensionStore.message.applicationInstalled", bundle.getName()));
214             saveMessages(request, msgs);
215         } catch (CoreException ce) {
216             ActionMessages errs = new ActionMessages();
217             errs.add(Globals.ERROR_KEY, ce.getBundleActionMessage());
218             saveErrors(request, errs);
219         }
220         return new RedirectWithMessages(mapping.findForward("refresh"), request);
221     }
222
223     /**
224      * Update selected extension
225      *
226      * @param mapping mapping
227      * @param form form
228      * @param request request
229      * @param response response
230      * @return forward
231      * @throws Exception on any error
232      */

233     public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
234                     throws Exception JavaDoc {
235         String JavaDoc id = request.getParameter("id");
236         String JavaDoc version = request.getParameter("version");
237         PolicyUtil.checkPermission(PolicyConstants.EXTENSIONS_RESOURCE_TYPE,
238             PolicyConstants.PERM_CHANGE,
239             getSessionInfo(request).getUser());
240         URLConnection JavaDoc con = ExtensionStore.getInstance().downloadExtension(id, version);
241         try {
242             InputStream JavaDoc in = con.getInputStream();
243             ExtensionBundle bundle = ExtensionStore.getInstance().updateExtension(id, in, request, con.getContentLength());
244             if (bundle.isContainsPlugin())
245                 CoreUtil.addMultipleGlobalWarning(GlobalWarning.MANAGEMENT_USERS, new BundleActionMessage("extensions",
246                                 "extensionStore.message.extensionUpdatedRestartRequired"));
247             ActionMessages msgs = new ActionMessages();
248             msgs.add(Globals.MESSAGE_KEY, new ActionMessage("extensionStore.message.applicationUpdated", bundle.getName()));
249             saveMessages(request, msgs);
250         } catch (CoreException ce) {
251             ActionMessages errs = new ActionMessages();
252             errs.add(Globals.ERROR_KEY, ce.getBundleActionMessage());
253             saveErrors(request, errs);
254         }
255         return new RedirectWithMessages(mapping.findForward("refresh"), request);
256     }
257
258     /**
259      * Start an extension. The 'id' request parameter must be supplied providing
260      * the extension bundle ID to stop.
261      *
262      * @param mapping mapping
263      * @param form form
264      * @param request request
265      * @param response response
266      * @return ActionForward forward
267      * @throws Exception on any error
268      */

269     public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
270                     throws Exception JavaDoc {
271         String JavaDoc id = request.getParameter("id");
272         PolicyUtil.checkPermission(PolicyConstants.EXTENSIONS_RESOURCE_TYPE,
273             PolicyConstants.PERM_CHANGE,
274             getSessionInfo(request).getUser());
275         ExtensionBundle bundle = ExtensionStore.getInstance().getExtensionBundle(id);
276         if (bundle.getStatus() == ExtensionBundleStatus.ENABLED) {
277             bundle.start();
278         }
279         if (bundle.getStatus() == ExtensionBundleStatus.STARTED) {
280             bundle.activate();
281         }
282         ActionMessages messages = new ActionMessages();
283         messages.add(Globals.MESSAGE_KEY, new ActionMessage("extensionStore.message.applicationDisable", bundle.getName()));
284         saveMessages(request, messages);
285         return mapping.findForward("list");
286     }
287
288     /**
289      * Stop an extension. The 'id' request parameter must be supplied providing
290      * the extension bundle ID to stop.
291      *
292      * @param mapping mapping
293      * @param form form
294      * @param request request
295      * @param response response
296      * @return ActionForward forward
297      * @throws Exception on any error
298      */

299     public ActionForward stop(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
300                     throws Exception JavaDoc {
301         String JavaDoc id = request.getParameter("id");
302         PolicyUtil.checkPermission(PolicyConstants.EXTENSIONS_RESOURCE_TYPE,
303             PolicyConstants.PERM_CHANGE,
304             getSessionInfo(request).getUser());
305         ExtensionBundle bundle = ExtensionStore.getInstance().getExtensionBundle(id);
306         bundle.stop();
307         ActionMessages messages = new ActionMessages();
308         messages.add(Globals.MESSAGE_KEY, new ActionMessage("extensionStore.message.applicationDisable", bundle.getName()));
309         saveMessages(request, messages);
310         return mapping.findForward("list");
311     }
312
313     /**
314      * Disable an extension. The 'id' request parameter must be supplied
315      * providing the extension bundle ID to stop.
316      *
317      * @param mapping
318      * @param form
319      * @param request
320      * @param response
321      * @return ActionForward
322      * @throws Exception
323      */

324     public ActionForward disable(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
325                     throws Exception JavaDoc {
326         String JavaDoc id = request.getParameter("id");
327         PolicyUtil.checkPermission(PolicyConstants.EXTENSIONS_RESOURCE_TYPE,
328             PolicyConstants.PERM_CHANGE,
329             getSessionInfo(request).getUser());
330         ExtensionBundle bundle = ExtensionStore.getInstance().getExtensionBundle(id);
331         if (!bundle.canDisable()) {
332             throw new Exception JavaDoc("Bundle cannot be disabled.");
333         }
334         ExtensionStore.getInstance().disableExtension(id);
335         if(bundle.isContainsPlugin()) {
336             CoreUtil.addMultipleGlobalWarning(GlobalWarning.MANAGEMENT_USERS, new BundleActionMessage("extensions",
337                             "extensionStore.message.stateChangeRestartRequired"));
338         }
339         ActionMessages messages = new ActionMessages();
340         messages.add(Globals.MESSAGE_KEY, new ActionMessage("extensionStore.message.applicationDisable", bundle.getName()));
341         saveMessages(request, messages);
342         return new RedirectWithMessages(mapping.findForward("list"), request);
343     }
344
345     /**
346      * Enable an extension. The 'id' request parameter must be supplied
347      * providing the extension bundle ID to stop.
348      *
349      * @param mapping
350      * @param form
351      * @param request
352      * @param response
353      * @return ActionForward
354      * @throws Exception
355      */

356     public ActionForward enable(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
357                     throws Exception JavaDoc {
358         String JavaDoc id = request.getParameter("id");
359         PolicyUtil.checkPermission(PolicyConstants.EXTENSIONS_RESOURCE_TYPE,
360             PolicyConstants.PERM_CHANGE,
361             getSessionInfo(request).getUser());
362         ExtensionBundle bundle = ExtensionStore.getInstance().getExtensionBundle(id);
363         if (!bundle.canEnable()) {
364             throw new Exception JavaDoc("Bundle cannot be enabled.");
365         }
366         ExtensionStore.getInstance().enableExtension(id);
367         if(bundle.isContainsPlugin()) {
368             CoreUtil.addMultipleGlobalWarning(GlobalWarning.MANAGEMENT_USERS, new BundleActionMessage("extensions",
369                             "extensionStore.message.stateChangeRestartRequired"));
370         }
371         ActionMessages messages = new ActionMessages();
372         messages.add(Globals.MESSAGE_KEY, new ActionMessage("extensionStore.message.applicationEnable", bundle.getName()));
373         saveMessages(request, messages);
374         return new RedirectWithMessages(mapping.findForward("list"), request);
375     }
376
377     /* (non-Javadoc)
378      * @see com.sslexplorer.core.actions.CoreAction#getNavigationContext(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
379      */

380     public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
381         return SessionInfo.MANAGEMENT_CONSOLE_CONTEXT;
382     }
383 }
Popular Tags