001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.console.car;
018    
019    import java.io.IOException;
020    import java.net.URL;
021    
022    import javax.portlet.PortletRequest;
023    import javax.portlet.PortletException;
024    import javax.portlet.PortletSession;
025    import javax.portlet.RenderRequest;
026    import javax.security.auth.login.FailedLoginException;
027    
028    import org.apache.geronimo.console.MultiPageAbstractHandler;
029    import org.apache.geronimo.system.plugin.model.PluginListType;
030    import org.apache.geronimo.system.plugin.model.PluginType;
031    import org.apache.geronimo.system.plugin.model.PluginArtifactType;
032    import org.apache.geronimo.system.plugin.PluginInstaller;
033    import org.apache.geronimo.system.plugin.PluginInstallerGBean;
034    import org.apache.geronimo.kernel.config.NoSuchStoreException;
035    
036    /**
037     * The base class for all handlers for this portlet
038     *
039     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
040     */
041    public abstract class BaseImportExportHandler extends MultiPageAbstractHandler {
042        protected static final String CONFIG_LIST_SESSION_KEY = "console.plugins.ConfigurationList";
043        protected static final String CONFIG_LIST_REPO_SESSION_KEY = "console.plugins.ConfigurationListRepo";
044        protected static final String SERVER_CONFIG_LIST_SESSION_KEY = "console.plugins.ServerConfigurationList";
045        public static final String DOWNLOAD_RESULTS_SESSION_KEY = "console.plugins.DownloadResults";
046        protected static final String INDEX_MODE = "index";
047        protected static final String ADD_REPO_MODE = "addRepository";
048        protected static final String LIST_MODE = "list";
049        protected static final String DOWNLOAD_MODE = "download";
050        protected static final String VIEW_FOR_DOWNLOAD_MODE = "viewForDownload";
051        protected static final String DOWNLOAD_STATUS_MODE = "downloadStatus";
052        protected static final String RESULTS_MODE = "results";
053        protected static final String CONFIGURE_EXPORT_MODE = "configure";
054        protected static final String CONFIRM_EXPORT_MODE = "confirm";
055        protected static final String UPDATE_REPOS_MODE = "updateList";
056        protected static final String ASSEMBLY_CONFIRM_MODE = "assemblyConfirm";
057        protected static final String LIST_SERVER_MODE = "listServer";
058        protected static final String ASSEMBLY_VIEW_MODE = "assemblyView";
059    
060        protected BaseImportExportHandler(String mode, String viewName) {
061            super(mode, viewName);
062        }
063    
064        protected PluginListType getRepoPluginList(PortletRequest request, PluginInstaller pluginInstaller, String repo, String user, String pass) throws IOException, PortletException {
065            PortletSession session = request.getPortletSession(true);
066            PluginListType list = (PluginListType) session.getAttribute(CONFIG_LIST_SESSION_KEY);
067            String listRepo = (String) session.getAttribute(CONFIG_LIST_REPO_SESSION_KEY);
068    
069            if (list == null || !repo.equals(listRepo)) {
070                try {
071                    list = pluginInstaller.listPlugins(new URL(repo), user, pass);
072                } catch (FailedLoginException e) {
073                    throw new PortletException("Invalid login for repository '" + repo + "'", e);
074                }
075                session.setAttribute(CONFIG_LIST_SESSION_KEY, list);
076                session.setAttribute(CONFIG_LIST_REPO_SESSION_KEY, repo);
077            }
078            return list;
079        }
080    
081        protected PluginListType getServerPluginList(PortletRequest request, PluginInstaller pluginInstaller) throws PortletException {
082            PluginListType data = (PluginListType) request.getPortletSession(true).getAttribute(SERVER_CONFIG_LIST_SESSION_KEY);
083            if (data==null) {
084                try {
085                    data = pluginInstaller.createPluginListForRepositories(null);
086                } catch (NoSuchStoreException e) {
087                    throw new PortletException("Server in unknown state", e);
088                }
089            }
090            return data;
091        }
092    
093        protected PluginListType getPluginsFromIds(String[] configIds, PluginListType list) throws PortletException {
094            PluginListType installList = new PluginListType();
095            for (String configId : configIds) {
096                PluginType plugin = null;
097                for (PluginType metadata : list.getPlugin()) {
098                    for (PluginArtifactType testInstance : metadata.getPluginArtifact()) {
099                        if (PluginInstallerGBean.toArtifact(testInstance.getModuleId()).toString().equals(configId)) {
100                            plugin = PluginInstallerGBean.copy(metadata, testInstance);
101                            installList.getPlugin().add(plugin);
102                            break;
103                        }
104                    }
105                }
106                if (plugin == null) {
107                    throw new PortletException("No configuration found for '" + configId + "'");
108                }
109            }
110            return installList;
111        }
112    }