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    import java.util.ArrayList;
022    import java.util.HashMap;
023    import java.util.List;
024    import java.util.Map;
025    import javax.portlet.ActionRequest;
026    import javax.portlet.ActionResponse;
027    import javax.portlet.PortletException;
028    import javax.portlet.RenderRequest;
029    import javax.portlet.RenderResponse;
030    import javax.security.auth.login.FailedLoginException;
031    import org.apache.commons.logging.Log;
032    import org.apache.commons.logging.LogFactory;
033    import org.apache.geronimo.console.MultiPageModel;
034    import org.apache.geronimo.console.util.PortletManager;
035    import org.apache.geronimo.system.plugin.PluginList;
036    import org.apache.geronimo.system.plugin.PluginMetadata;
037    import java.util.Collections;
038    import java.util.Collection;
039    import java.util.Iterator;
040    
041    /**
042     * Handler for the import export list screen.
043     *
044     * @version $Rev: 476061 $ $Date: 2006-11-17 01:36:50 -0500 (Fri, 17 Nov 2006) $
045     */
046    public class ListHandler extends BaseImportExportHandler {
047        private final static Log log = LogFactory.getLog(ListHandler.class);
048    
049        public ListHandler() {
050            super(LIST_MODE, "/WEB-INF/view/car/list.jsp");
051        }
052    
053        public String actionBeforeView(ActionRequest request, ActionResponse response, MultiPageModel model) throws PortletException, IOException {
054            String repository = (String) request.getAttribute("repository");
055            if(repository == null || repository.equals("")) {
056                return INDEX_MODE+BEFORE_ACTION;
057            }
058            response.setRenderParameter("repository", repository);
059            String user = (String) request.getAttribute("repo-user");
060            String pass = (String) request.getAttribute("repo-pass");
061            if(!isEmpty(user)) response.setRenderParameter("repo-user", user);
062            if(!isEmpty(pass)) response.setRenderParameter("repo-pass", pass);
063            return getMode();
064        }
065    
066        public void renderView(RenderRequest request, RenderResponse response, MultiPageModel model) throws PortletException, IOException {
067            String repository = request.getParameter("repository");
068            String user = request.getParameter("repo-user");
069            String pass = request.getParameter("repo-pass");
070            if(!loadFromRepository(request, repository, user, pass)) {
071                //todo: loading failed -- do something!
072            }
073            request.setAttribute("repository", repository);
074            request.setAttribute("repouser", user);
075            request.setAttribute("repopass", pass);
076        }
077    
078        public String actionAfterView(ActionRequest request, ActionResponse response, MultiPageModel model) throws PortletException, IOException {
079            return getMode()+BEFORE_ACTION;
080        }
081    
082        private boolean loadFromRepository(RenderRequest request, String repository, String username, String password) throws IOException, PortletException {
083            PluginList data;
084            try {
085                data = PortletManager.getCurrentServer(request).getPluginInstaller().listPlugins(new URL(repository), username, password);
086            } catch (FailedLoginException e) {
087                throw new PortletException("Invalid login for Maven repository '"+repository+"'", e);
088            }
089            Map results = new HashMap();
090            if(data == null || data.getPlugins() == null) {
091                return false;
092            }
093            for (int i = 0; i < data.getPlugins().length; i++) {
094                PluginMetadata metadata = data.getPlugins()[i];
095                List values = (List) results.get(metadata.getCategory());
096                if(values == null) {
097                    values = new ArrayList();
098                    results.put(metadata.getCategory(), values);
099                }
100                values.add(metadata);
101            }
102            Collection values = results.values();
103            for (Iterator it = values.iterator(); it.hasNext();) {
104                List list = (List) it.next();
105                Collections.sort(list);
106            }
107            request.setAttribute("categories", results);
108            request.getPortletSession(true).setAttribute(CONFIG_LIST_SESSION_KEY, data);
109            return true;
110        }
111    }