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 org.apache.commons.logging.Log;
020    import org.apache.commons.logging.LogFactory;
021    import org.apache.geronimo.console.MultiPageModel;
022    import org.apache.geronimo.console.util.PortletManager;
023    import org.apache.geronimo.system.plugin.PluginRepositoryList;
024    
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 java.io.IOException;
031    import java.io.InputStream;
032    import java.net.ConnectException;
033    import java.net.HttpURLConnection;
034    import java.net.MalformedURLException;
035    import java.net.UnknownHostException;
036    import java.net.URL;
037    import java.net.URLConnection;
038    import java.util.ArrayList;
039    import java.util.Arrays;
040    import java.util.List;
041    
042    /**
043     * Handler for the import export main screen.
044     *
045     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
046     */
047    public class AddRepositoryHandler extends BaseImportExportHandler {
048        private final static Log log = LogFactory.getLog(AddRepositoryHandler.class);
049    
050        public AddRepositoryHandler() {
051            super(ADD_REPO_MODE, "/WEB-INF/view/car/addRepository.jsp");
052        }
053    
054        public String actionBeforeView(ActionRequest request, ActionResponse response, MultiPageModel model) throws PortletException, IOException {
055            return getMode();
056        }
057    
058        public void renderView(RenderRequest request, RenderResponse response, MultiPageModel model) throws PortletException, IOException {
059            List<PluginRepositoryList> lists = ManagementHelper.getManagementHelper(request).getPluginRepositoryLists();
060            List<URL> list = new ArrayList<URL>();
061            for (PluginRepositoryList repo : lists) {
062                list.addAll(repo.getRepositories());
063            }
064            String error = request.getParameter("repoError");
065            if(error != null && !error.equals("")) {
066                request.setAttribute("repoError", error);
067            }
068            request.setAttribute("repositories", list);
069        }
070    
071        public String actionAfterView(ActionRequest request, ActionResponse response, MultiPageModel model) throws PortletException, IOException {
072            String repo = request.getParameter("newRepository");
073            if(repo != null && !repo.equals("")) {
074                if(!addRepository(repo, request, response)) {
075                    return getMode();
076                }
077            }
078            return INDEX_MODE+BEFORE_ACTION;
079        }
080    
081    
082        private boolean addRepository(String repo, ActionRequest request, ActionResponse response) throws IOException {
083            repo = repo.trim();
084            repo = repo.replaceAll(" ", "%20");
085            if(!repo.endsWith("/")) {
086                repo = repo+"/";
087            }
088            
089            List<PluginRepositoryList> lists = ManagementHelper.getManagementHelper(request).getPluginRepositoryLists();
090    
091            // Check for duplicates
092            for (PluginRepositoryList test: lists) {
093                List<URL> all = test.getRepositories();
094                for (URL url: all) {
095                    String existing = url.toString();
096                    if(repo.equals(existing)) {
097                        response.setRenderParameter("repoError", "Already have an entry for repository "+repo);
098                        return false;
099                    }
100                }
101            }
102    
103            // Verify the repository and add it if valid
104            if(lists.size() > 0) {
105                URL url;
106                try {
107                    url = new URL(repo);
108                } catch (MalformedURLException e) {
109                    response.setRenderParameter("repoError", "Invalid repository URL "+repo);
110                    return false;
111                }
112                URL test = new URL(repo+"geronimo-plugins.xml");
113                log.debug("Checking repository "+test);
114                URLConnection urlConnection = test.openConnection();
115                if(urlConnection instanceof HttpURLConnection) {
116                    HttpURLConnection con = (HttpURLConnection) urlConnection;
117                    try {
118                        con.connect();
119                    } catch (ConnectException e) {
120                        response.setRenderParameter("repoError", "Unable to connect to "+url+" ("+e.getMessage()+")");
121                        return false;
122                    } catch (UnknownHostException e) {
123                        response.setRenderParameter("repoError", "Unknown host: " + url.getHost());
124                        return false;
125                    }
126                    int result = con.getResponseCode();
127                    log.debug("Repository check response: "+result);
128                    if(result == 404) {
129                        response.setRenderParameter("repoError", "Not a valid repository; no plugin list found at "+test);
130                        return false;
131                    } else if(result == 401) {
132                        log.warn("Unable to validate repository -- it requires authentication.  Assuming you know what you're doing.");
133                    } else if(result != 200) {
134                        log.warn("Unexpected response code while validating repository ("+result+" "+con.getResponseMessage()+").  Assuming you know what you're doing.");
135                    }
136                    con.disconnect();
137                } else {
138                    try {
139                        urlConnection.connect();
140                        InputStream in = urlConnection.getInputStream();
141                        in.read();
142                        in.close();
143                    } catch (IOException e) {
144                        response.setRenderParameter("repoError", "Not a valid repository; no plugin list found at "+test);
145                        return false;
146                    }
147                }
148                lists.get(0).addUserRepository(url);
149                request.setAttribute("repository", repo);
150                return true;
151            }
152            response.setRenderParameter("repoError", "No repository list found; unable to store new repository");
153            return false;
154        }
155    }