KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > console > car > AddRepositoryHandler


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.console.car;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.apache.geronimo.console.MultiPageModel;
22 import org.apache.geronimo.console.util.PortletManager;
23 import org.apache.geronimo.system.plugin.PluginRepositoryList;
24
25 import javax.portlet.ActionRequest;
26 import javax.portlet.ActionResponse;
27 import javax.portlet.PortletException;
28 import javax.portlet.RenderRequest;
29 import javax.portlet.RenderResponse;
30 import java.io.IOException JavaDoc;
31 import java.io.InputStream JavaDoc;
32 import java.net.ConnectException JavaDoc;
33 import java.net.HttpURLConnection JavaDoc;
34 import java.net.MalformedURLException JavaDoc;
35 import java.net.URL JavaDoc;
36 import java.net.URLConnection JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import java.util.Arrays JavaDoc;
39 import java.util.List JavaDoc;
40
41 /**
42  * Handler for the import export main screen.
43  *
44  * @version $Rev: 409817 $ $Date: 2006-05-27 03:56:38 -0400 (Sat, 27 May 2006) $
45  */

46 public class AddRepositoryHandler extends BaseImportExportHandler {
47     private final static Log log = LogFactory.getLog(AddRepositoryHandler.class);
48
49     public AddRepositoryHandler() {
50         super(ADD_REPO_MODE, "/WEB-INF/view/car/addRepository.jsp");
51     }
52
53     public String JavaDoc actionBeforeView(ActionRequest request, ActionResponse response, MultiPageModel model) throws PortletException, IOException JavaDoc {
54         return getMode();
55     }
56
57     public void renderView(RenderRequest request, RenderResponse response, MultiPageModel model) throws PortletException, IOException JavaDoc {
58         PluginRepositoryList[] lists = PortletManager.getCurrentServer(request).getPluginRepositoryLists();
59         List list = new ArrayList JavaDoc();
60         for (int i = 0; i < lists.length; i++) {
61             PluginRepositoryList repo = lists[i];
62             list.addAll(Arrays.asList(repo.getRepositories()));
63         }
64         String JavaDoc error = request.getParameter("repoError");
65         if(error != null && !error.equals("")) {
66             request.setAttribute("repoError", error);
67         }
68         request.setAttribute("repositories", list);
69     }
70
71     public String JavaDoc actionAfterView(ActionRequest request, ActionResponse response, MultiPageModel model) throws PortletException, IOException JavaDoc {
72         String JavaDoc repo = request.getParameter("newRepository");
73         if(repo != null && !repo.equals("")) {
74             if(!addRepository(repo, request, response)) {
75                 return getMode();
76             }
77         }
78         return INDEX_MODE+BEFORE_ACTION;
79     }
80
81
82     private boolean addRepository(String JavaDoc repo, ActionRequest request, ActionResponse response) throws IOException JavaDoc {
83         if(!repo.endsWith("/")) {
84             repo = repo+"/";
85         }
86         PluginRepositoryList[] lists = PortletManager.getCurrentServer(request).getPluginRepositoryLists();
87
88         // Check for duplicates
89
for (int i = 0; i < lists.length; i++) {
90             PluginRepositoryList test = lists[i];
91             URL JavaDoc[] all = test.getRepositories();
92             for (int j = 0; j < all.length; j++) {
93                 String JavaDoc existing = all[j].toString();
94                 if(!existing.endsWith("/")) {
95                     existing = existing + "/";
96                 }
97                 if(repo.equals(existing)) {
98                     response.setRenderParameter("repoError", "Already have an entry for repository "+repo);
99                     return false;
100                 }
101             }
102         }
103
104         // Verify the repository and add it if valid
105
if(lists.length > 0) {
106             URL JavaDoc url;
107             try {
108                 url = new URL JavaDoc(repo);
109             } catch (MalformedURLException JavaDoc e) {
110                 response.setRenderParameter("repoError", "Invalid repository URL "+repo);
111                 return false;
112             }
113             URL JavaDoc test = new URL JavaDoc(repo+"geronimo-plugins.xml");
114             log.debug("Checking repository "+test);
115             URLConnection JavaDoc urlConnection = test.openConnection();
116             if(urlConnection instanceof HttpURLConnection JavaDoc) {
117                 HttpURLConnection JavaDoc con = (HttpURLConnection JavaDoc) urlConnection;
118                 try {
119                     con.connect();
120                 } catch (ConnectException JavaDoc 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 JavaDoc in = urlConnection.getInputStream();
139                     in.read();
140                     in.close();
141                 } catch (IOException JavaDoc 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 }
154
Popular Tags