1 19 20 package com.sslexplorer.extensions.actions; 21 22 import java.io.InputStream ; 23 import java.net.URLConnection ; 24 25 import javax.servlet.http.HttpServletRequest ; 26 import javax.servlet.http.HttpServletResponse ; 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 extensionCategory = null; 68 69 public DefaultExtensionsAction(String 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 request, HttpServletResponse response) 75 throws Exception { 76 return this.list(mapping, form, request, response); 77 } 78 79 public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 80 throws Exception { 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 e) { 100 } 101 defaultExtensionForm.initialise(request.getSession(), ExtensionStore.getInstance().getAllAvailableExtensionBundles(extensionCategory)); 102 return null; } 104 105 public ActionForward refresh(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 106 throws Exception { 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 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; } 127 128 138 public ActionForward upload(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 139 throws Exception { 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, mapping.findForward("list")); 149 int id = CoreUtil.addUpload(request.getSession(), details); 150 request.setAttribute(Constants.REQ_ATTR_UPLOAD_DETAILS, new Integer (id)); 151 return mapping.findForward("upload"); 152 } 153 154 164 public ActionForward remove(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 165 throws Exception { 166 PolicyUtil.checkPermission(PolicyConstants.EXTENSIONS_RESOURCE_TYPE, 167 PolicyConstants.PERM_CHANGE, 168 getSessionInfo(request).getUser()); 169 String id = request.getParameter("id"); 170 if (id == null || id.equals("")) { 171 throw new Exception ("No id parameter supplied."); 172 } 173 ExtensionBundle bundle = ExtensionStore.getInstance().getExtensionBundle(id); 174 if (bundle == null) { 175 throw new Exception ("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 197 public ActionForward install(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 198 throws Exception { 199 String id = request.getParameter("id"); 200 String 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 con = ExtensionStore.getInstance().downloadExtension(id, version); 206 207 try { 208 InputStream 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 233 public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 234 throws Exception { 235 String id = request.getParameter("id"); 236 String version = request.getParameter("version"); 237 PolicyUtil.checkPermission(PolicyConstants.EXTENSIONS_RESOURCE_TYPE, 238 PolicyConstants.PERM_CHANGE, 239 getSessionInfo(request).getUser()); 240 URLConnection con = ExtensionStore.getInstance().downloadExtension(id, version); 241 try { 242 InputStream 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 269 public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 270 throws Exception { 271 String 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 299 public ActionForward stop(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 300 throws Exception { 301 String 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 324 public ActionForward disable(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 325 throws Exception { 326 String 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 ("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 356 public ActionForward enable(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 357 throws Exception { 358 String 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 ("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 380 public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { 381 return SessionInfo.MANAGEMENT_CONSOLE_CONTEXT; 382 } 383 } | Popular Tags |