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.URL; 036 import java.net.URLConnection; 037 import java.util.ArrayList; 038 import java.util.Arrays; 039 import java.util.List; 040 041 /** 042 * Handler for the import export main screen. 043 * 044 * @version $Rev: 514091 $ $Date: 2007-03-03 01:26:39 -0500 (Sat, 03 Mar 2007) $ 045 */ 046 public class AddRepositoryHandler extends BaseImportExportHandler { 047 private final static Log log = LogFactory.getLog(AddRepositoryHandler.class); 048 049 public AddRepositoryHandler() { 050 super(ADD_REPO_MODE, "/WEB-INF/view/car/addRepository.jsp"); 051 } 052 053 public String actionBeforeView(ActionRequest request, ActionResponse response, MultiPageModel model) throws PortletException, IOException { 054 return getMode(); 055 } 056 057 public void renderView(RenderRequest request, RenderResponse response, MultiPageModel model) throws PortletException, IOException { 058 PluginRepositoryList[] lists = PortletManager.getCurrentServer(request).getPluginRepositoryLists(); 059 List list = new ArrayList(); 060 for (int i = 0; i < lists.length; i++) { 061 PluginRepositoryList repo = lists[i]; 062 list.addAll(Arrays.asList(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 if(!repo.endsWith("/")) { 084 repo = repo+"/"; 085 } 086 PluginRepositoryList[] lists = PortletManager.getCurrentServer(request).getPluginRepositoryLists(); 087 088 // Check for duplicates 089 for (int i = 0; i < lists.length; i++) { 090 PluginRepositoryList test = lists[i]; 091 URL[] all = test.getRepositories(); 092 for (int j = 0; j < all.length; j++) { 093 String existing = all[j].toString(); 094 if(!existing.endsWith("/")) { 095 existing = existing + "/"; 096 } 097 if(repo.equals(existing)) { 098 response.setRenderParameter("repoError", "Already have an entry for repository "+repo); 099 return false; 100 } 101 } 102 } 103 104 // Verify the repository and add it if valid 105 if(lists.length > 0) { 106 URL url; 107 try { 108 url = new URL(repo); 109 } catch (MalformedURLException e) { 110 response.setRenderParameter("repoError", "Invalid repository URL "+repo); 111 return false; 112 } 113 URL test = new URL(repo+"geronimo-plugins.xml"); 114 log.debug("Checking repository "+test); 115 URLConnection urlConnection = test.openConnection(); 116 if(urlConnection instanceof HttpURLConnection) { 117 HttpURLConnection con = (HttpURLConnection) urlConnection; 118 try { 119 con.connect(); 120 } catch (ConnectException e) { 121 response.setRenderParameter("repoError", "Unable to connect to "+url+" ("+e.getMessage()+")"); 122 return false; 123 } 124 int result = con.getResponseCode(); 125 log.debug("Repository check response: "+result); 126 if(result == 404) { 127 response.setRenderParameter("repoError", "Not a valid repository; no plugin list found at "+test); 128 return false; 129 } else if(result == 401) { 130 log.warn("Unable to validate repository -- it requires authentication. Assuming you know what you're doing."); 131 } else if(result != 200) { 132 log.warn("Unexpected response code while validating repository ("+result+" "+con.getResponseMessage()+"). Assuming you know what you're doing."); 133 } 134 con.disconnect(); 135 } else { 136 try { 137 urlConnection.connect(); 138 InputStream in = urlConnection.getInputStream(); 139 in.read(); 140 in.close(); 141 } catch (IOException e) { 142 response.setRenderParameter("repoError", "Not a valid repository; no plugin list found at "+test); 143 return false; 144 } 145 } 146 lists[0].addUserRepository(url); 147 request.setAttribute("repository", repo); 148 return true; 149 } 150 response.setRenderParameter("repoError", "No repository list found; unable to store new repository"); 151 return false; 152 } 153 }