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: */package org.apache.geronimo.console.car;
017:
018: import org.apache.commons.logging.Log;
019: import org.apache.commons.logging.LogFactory;
020: import org.apache.geronimo.console.MultiPageModel;
021: import org.apache.geronimo.console.util.PortletManager;
022: import org.apache.geronimo.system.plugin.PluginRepositoryList;
023:
024: import javax.portlet.ActionRequest;
025: import javax.portlet.ActionResponse;
026: import javax.portlet.PortletException;
027: import javax.portlet.RenderRequest;
028: import javax.portlet.RenderResponse;
029: import java.io.IOException;
030: import java.io.InputStream;
031: import java.net.ConnectException;
032: import java.net.HttpURLConnection;
033: import java.net.MalformedURLException;
034: import java.net.UnknownHostException;
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: 612567 $ $Date: 2008-01-16 12:49:35 -0800 (Wed, 16 Jan 2008) $
045: */
046: public class AddRepositoryHandler extends BaseImportExportHandler {
047: private final static Log log = LogFactory
048: .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,
055: ActionResponse response, MultiPageModel model)
056: throws PortletException, IOException {
057: return getMode();
058: }
059:
060: public void renderView(RenderRequest request,
061: RenderResponse response, MultiPageModel model)
062: throws PortletException, IOException {
063: List<PluginRepositoryList> lists = ManagementHelper
064: .getManagementHelper(request)
065: .getPluginRepositoryLists();
066: List<URL> list = new ArrayList<URL>();
067: for (PluginRepositoryList repo : lists) {
068: list.addAll(repo.getRepositories());
069: }
070: String error = request.getParameter("repoError");
071: if (error != null && !error.equals("")) {
072: request.setAttribute("repoError", error);
073: }
074: request.setAttribute("repositories", list);
075: }
076:
077: public String actionAfterView(ActionRequest request,
078: ActionResponse response, MultiPageModel model)
079: throws PortletException, IOException {
080: String repo = request.getParameter("newRepository");
081: if (repo != null && !repo.equals("")) {
082: if (!addRepository(repo, request, response)) {
083: return getMode();
084: }
085: }
086: return INDEX_MODE + BEFORE_ACTION;
087: }
088:
089: private boolean addRepository(String repo, ActionRequest request,
090: ActionResponse response) throws IOException {
091: repo = repo.trim();
092: repo = repo.replaceAll(" ", "%20");
093: if (!repo.endsWith("/")) {
094: repo = repo + "/";
095: }
096:
097: List<PluginRepositoryList> lists = ManagementHelper
098: .getManagementHelper(request)
099: .getPluginRepositoryLists();
100:
101: // Check for duplicates
102: for (PluginRepositoryList test : lists) {
103: List<URL> all = test.getRepositories();
104: for (URL url : all) {
105: String existing = url.toString();
106: if (repo.equals(existing)) {
107: response.setRenderParameter("repoError",
108: "Already have an entry for repository "
109: + repo);
110: return false;
111: }
112: }
113: }
114:
115: // Verify the repository and add it if valid
116: if (lists.size() > 0) {
117: URL url;
118: try {
119: url = new URL(repo);
120: } catch (MalformedURLException e) {
121: response.setRenderParameter("repoError",
122: "Invalid repository URL " + repo);
123: return false;
124: }
125: URL test = new URL(repo + "geronimo-plugins.xml");
126: log.debug("Checking repository " + test);
127: URLConnection urlConnection = test.openConnection();
128: if (urlConnection instanceof HttpURLConnection) {
129: HttpURLConnection con = (HttpURLConnection) urlConnection;
130: try {
131: con.connect();
132: } catch (ConnectException e) {
133: response.setRenderParameter("repoError",
134: "Unable to connect to " + url + " ("
135: + e.getMessage() + ")");
136: return false;
137: } catch (UnknownHostException e) {
138: response.setRenderParameter("repoError",
139: "Unknown host: " + url.getHost());
140: return false;
141: }
142: int result = con.getResponseCode();
143: log.debug("Repository check response: " + result);
144: if (result == 404) {
145: response.setRenderParameter("repoError",
146: "Not a valid repository; no plugin list found at "
147: + test);
148: return false;
149: } else if (result == 401) {
150: log
151: .warn("Unable to validate repository -- it requires authentication. Assuming you know what you're doing.");
152: } else if (result != 200) {
153: log
154: .warn("Unexpected response code while validating repository ("
155: + result
156: + " "
157: + con.getResponseMessage()
158: + "). Assuming you know what you're doing.");
159: }
160: con.disconnect();
161: } else {
162: try {
163: urlConnection.connect();
164: InputStream in = urlConnection.getInputStream();
165: in.read();
166: in.close();
167: } catch (IOException e) {
168: response.setRenderParameter("repoError",
169: "Not a valid repository; no plugin list found at "
170: + test);
171: return false;
172: }
173: }
174: lists.get(0).addUserRepository(url);
175: request.setAttribute("repository", repo);
176: return true;
177: }
178: response
179: .setRenderParameter("repoError",
180: "No repository list found; unable to store new repository");
181: return false;
182: }
183: }
|