⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 extensionstoredispatchaction.java

📁 这是linux下ssl vpn的实现程序
💻 JAVA
字号:
/*
 *  SSL-Explorer
 *
 *  Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2 of
 *  the License, or (at your option) any later version.
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public
 *  License along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
			
package com.sslexplorer.extensions.actions;

import java.net.URLConnection;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import com.sslexplorer.core.BundleActionMessage;
import com.sslexplorer.core.CoreServlet;
import com.sslexplorer.core.CoreUtil;
import com.sslexplorer.core.GlobalWarning;
import com.sslexplorer.core.PropertyUtil;
import com.sslexplorer.core.actions.AuthenticatedDispatchAction;
import com.sslexplorer.extensions.ExtensionBundle;
import com.sslexplorer.extensions.ExtensionDescriptor;
import com.sslexplorer.extensions.forms.ExtensionStoreForm;
import com.sslexplorer.extensions.store.ExtensionStore;
import com.sslexplorer.extensions.store.ExtensionStoreDescriptor;
import com.sslexplorer.extensions.types.PluginType;
import com.sslexplorer.policyframework.Permission;
import com.sslexplorer.policyframework.PolicyConstants;
import com.sslexplorer.policyframework.PolicyUtil;
import com.sslexplorer.security.Constants;
import com.sslexplorer.security.SessionInfo;
import com.sslexplorer.vfs.UploadDetails;
import com.sslexplorer.vfs.utils.DAVUploadHandler;

/**
 * Implementation of
 * {@link com.sslexplorer.core.actions.AuthenticatedDispatchAction} that allows
 * an administrator to install, update and delete <i>Extensions</i>.
 * <p>
 * Extensions may be installed manually via an upload or from the <i>Extension
 * Store</i>.
 * 
 * @author Brett Smith <a href="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
 * @version $Revision: 1.15 $
 */
public class ExtensionStoreDispatchAction extends AuthenticatedDispatchAction {
    final static Log log = LogFactory.getLog(ExtensionStoreDispatchAction.class);

    /**
     * Constructor
     */
    public ExtensionStoreDispatchAction() {
        super(PolicyConstants.EXTENSIONS_RESOURCE_TYPE, new Permission[] { PolicyConstants.PERM_INSTALL,
                        PolicyConstants.PERM_UPDATE, PolicyConstants.PERM_DELETE, });
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.apache.struts.actions.DispatchAction#unspecified(org.apache.struts.action.ActionMapping,
     *      org.apache.struts.action.ActionForm,
     *      javax.servlet.http.HttpServletRequest,
     *      javax.servlet.http.HttpServletResponse)
     */
    public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                     HttpServletResponse response) throws Exception {
        return list(mapping, form, request, response);
    }

    /**
     * Upload an extension
     * 
     * @param mapping mapping
     * @param form form
     * @param request request
     * @param response response
     * @return forward
     * @throws Exception on any error
     */
    public ActionForward upload(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
                    throws Exception {
        PolicyUtil.checkPermissions(PolicyConstants.EXTENSIONS_RESOURCE_TYPE, new Permission[] { PolicyConstants.PERM_INSTALL,
                        PolicyConstants.PERM_UPDATE }, request);
        UploadDetails details = new UploadDetails(DAVUploadHandler.TYPE_EXTENSION, "Extension Store",
                        mapping.findForward("upload"), mapping.findForward("list"), mapping.findForward("list"));
        int id = CoreUtil.addUpload(request.getSession(), details);
        request.setAttribute(Constants.REQ_ATTR_UPLOAD_DETAILS, new Integer(id));

        return mapping.findForward("upload");
    }

    /**
     * Upload an extension
     * 
     * @param mapping mapping
     * @param form form
     * @param request request
     * @param response response
     * @return forward
     * @throws Exception on any error
     */
    public ActionForward refresh(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
                    throws Exception {
        PolicyUtil.checkPermissions(PolicyConstants.EXTENSIONS_RESOURCE_TYPE, new Permission[] { PolicyConstants.PERM_INSTALL,
                        PolicyConstants.PERM_UPDATE }, request);
        ExtensionStore extensionStore = ExtensionStore.getInstance();
        extensionStore.resetExtensionStoreUpdate();
        extensionStore.getDownloadableExtensionStoreDescriptor(true);
        ActionMessages msgs = new ActionMessages();
        msgs.add(Globals.MESSAGE_KEY, new BundleActionMessage("extensions", "extensionStore.message.refreshed"));
        saveMessages(request, msgs);
        return mapping.findForward("list");
    }

    /**
     * Remove selected extension
     * 
     * @param mapping mapping
     * @param form form
     * @param request request
     * @param response response
     * @return forward
     * @throws Exception on any error
     */
    public ActionForward remove(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
                    throws Exception {
        PolicyUtil.checkPermission(PolicyConstants.EXTENSIONS_RESOURCE_TYPE, PolicyConstants.PERM_DELETE, getSessionInfo()
                        .getUser());
        String id = request.getParameter("id");
        if (id == null || id.equals("")) {
            throw new Exception("No id parameter supplied.");
        }
        ExtensionBundle bundle = ExtensionStore.getInstance().getExtensionBundle(id);
        if (bundle == null) {
            throw new Exception("No application with an id of " + id);
        }
        boolean containsPlugin = false;
        try {
            for (Iterator i = bundle.iterator(); !containsPlugin && i.hasNext();) {
                ExtensionDescriptor des = (ExtensionDescriptor) i.next();
                if (des.getExtensionType() instanceof PluginType) {
                    log.info("Uninstalling plugin " + des.getId());
                    CoreServlet.getServlet().getPluginManager().uninstall(des.getId());
                    containsPlugin = true;
                }
            }
        } finally {
            ExtensionStore.getInstance().removeExtensionBundle(bundle);
            if (containsPlugin) {
                CoreUtil.addMultipleGlobalWarning(GlobalWarning.MANAGEMENT_USERS, new BundleActionMessage("extensions",
                                "extensionStore.message.pluginRemovedRestartRequired"));
            }
        }
        ActionMessages msgs = new ActionMessages();
        msgs.add(Globals.MESSAGE_KEY, new ActionMessage("extensionStore.message.applicationRemoved", bundle.getName()));
        saveMessages(request, msgs);
        return mapping.findForward("list");
    }

    /**
     * Install selected extension
     * 
     * @param mapping mapping
     * @param form form
     * @param request request
     * @param response response
     * @return forward
     * @throws Exception on any error
     */
    public ActionForward install(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
                    throws Exception {
        String id = request.getParameter("id");
        PolicyUtil.checkPermission(PolicyConstants.EXTENSIONS_RESOURCE_TYPE, PolicyConstants.PERM_INSTALL, getSessionInfo()
                        .getUser());
        URLConnection con = ExtensionStore.getInstance().downloadExtension(id);

        ExtensionBundle bundle = ExtensionStore.getInstance().installExtension(id, con.getInputStream(), request);
        ExtensionStore.getInstance().licenseCheck(bundle, request, mapping.findForward("list"));
        ExtensionStore.getInstance().postInstallExtension(bundle, request);
        ActionMessages msgs = new ActionMessages();
        msgs.add(Globals.MESSAGE_KEY, new ActionMessage(false ? "extensionStore.message.applicationUpdated"
                        : "extensionStore.message.applicationInstalled", bundle.getName()));
        saveMessages(request, msgs);
        return mapping.findForward("list");
    }

    /**
     * Update selected extension
     * 
     * @param mapping mapping
     * @param form form
     * @param request request
     * @param response response
     * @return forward
     * @throws Exception on any error
     */
    public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
                    throws Exception {
        String id = request.getParameter("id");
        PolicyUtil.checkPermission(PolicyConstants.EXTENSIONS_RESOURCE_TYPE, PolicyConstants.PERM_UPDATE, getSessionInfo()
                        .getUser());
        URLConnection con = ExtensionStore.getInstance().downloadExtension(id);
        ExtensionBundle bundle = ExtensionStore.getInstance().updateExtension(id, con.getInputStream(), request);
        ActionMessages msgs = new ActionMessages();
        msgs.add(Globals.MESSAGE_KEY, new ActionMessage(true ? "extensionStore.message.applicationUpdated"
                        : "extensionStore.message.applicationInstalled", bundle.getName()));
        saveMessages(request, msgs);
        return mapping.findForward("list");
    }

    /**
     * List extensions
     * 
     * @param mapping mapping
     * @param form form
     * @param request request
     * @param response response
     * @return forward
     * @throws Exception on any error
     */
    public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
                    throws Exception {
        if (!ExtensionStore.STORE_PREF.getBoolean("warnedAboutExtensionStoreConnect", true)) {
            if (request.getParameter("agree") != null) {
                ExtensionStore.STORE_PREF.putBoolean("warnedAboutExtensionStoreConnect", true);
                SessionInfo info = this.getSessionInfo();
                PropertyUtil.getPropertyUtil().setProperty(0, null, "updates.automaticallyConnectToApplicationStore",
                    request.getParameter("agree"), info);
                PropertyUtil.getPropertyUtil().setProperty(0, null, "updates.checkForCoreUpdates", request.getParameter("agree"),
                    info);
                PropertyUtil.getPropertyUtil().setProperty(0, null, "updates.checkForExtensionUpdates",
                    request.getParameter("agree"), info);
            } else {
                return mapping.findForward("agreement");
            }
        }
        ExtensionStoreForm storeForm = (ExtensionStoreForm) form;
        ExtensionStoreDescriptor storeDescriptor = null;
        try {
            storeDescriptor = ExtensionStore.getInstance().getDownloadableExtensionStoreDescriptor(true);
        } catch (Exception e) {
            ActionMessages errs = new ActionMessages();
            errs.add(Globals.ERROR_KEY, new ActionMessage("extensionStore.failedToContactStore", e.getMessage()));
            addErrors(request, errs);
        }
        storeForm.initialise(storeDescriptor, storeDescriptor == null ? ExtensionStore.getInstance().getExtensionBundles()
                        : ExtensionStore.getInstance().getAllAvailableExtensionBundles());
        return mapping.findForward("success");
    }

    /*
     * (non-Javadoc)
     * 
     * @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)
     */
    public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        return SessionInfo.MANAGEMENT_CONSOLE_CONTEXT;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -