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.repository;
017:
018: import org.apache.commons.fileupload.FileItem;
019: import org.apache.commons.fileupload.FileUploadException;
020: import org.apache.commons.fileupload.disk.DiskFileItemFactory;
021: import org.apache.commons.fileupload.portlet.PortletFileUpload;
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.apache.geronimo.console.BasePortlet;
025: import org.apache.geronimo.console.util.PortletManager;
026: import org.apache.geronimo.kernel.Kernel;
027: import org.apache.geronimo.kernel.KernelRegistry;
028: import org.apache.geronimo.kernel.repository.Artifact;
029: import org.apache.geronimo.kernel.repository.FileWriteMonitor;
030: import org.apache.geronimo.kernel.repository.ListableRepository;
031: import org.apache.geronimo.kernel.repository.WriteableRepository;
032:
033: import javax.portlet.ActionRequest;
034: import javax.portlet.ActionResponse;
035: import javax.portlet.PortletConfig;
036: import javax.portlet.PortletContext;
037: import javax.portlet.PortletException;
038: import javax.portlet.PortletRequestDispatcher;
039: import javax.portlet.RenderRequest;
040: import javax.portlet.RenderResponse;
041: import javax.portlet.WindowState;
042: import java.io.File;
043: import java.io.IOException;
044: import java.util.ArrayList;
045: import java.util.Arrays;
046: import java.util.Collections;
047: import java.util.Iterator;
048: import java.util.List;
049: import java.util.SortedSet;
050:
051: /**
052: * @version $Rev: 480565 $ $Date: 2006-11-29 05:22:23 -0800 (Wed, 29 Nov 2006) $
053: */
054: public class RepositoryViewPortlet extends BasePortlet {
055:
056: private final static Log log = LogFactory
057: .getLog(RepositoryViewPortlet.class);
058:
059: private Kernel kernel;
060:
061: private PortletContext ctx;
062:
063: private PortletRequestDispatcher normalView;
064:
065: private PortletRequestDispatcher helpView;
066:
067: private PortletRequestDispatcher usageView;
068:
069: public void init(PortletConfig portletConfig)
070: throws PortletException {
071: super .init(portletConfig);
072: kernel = KernelRegistry.getSingleKernel();
073: ctx = portletConfig.getPortletContext();
074: normalView = ctx
075: .getRequestDispatcher("/WEB-INF/view/repository/normal.jsp");
076: helpView = ctx
077: .getRequestDispatcher("/WEB-INF/view/repository/help.jsp");
078: usageView = ctx
079: .getRequestDispatcher("/WEB-INF/view/repository/usage.jsp");
080: }
081:
082: public void processAction(ActionRequest actionRequest,
083: ActionResponse actionResponse) throws PortletException,
084: IOException {
085: String action = actionRequest.getParameter("action");
086: if (action != null && action.equals("usage")) {
087: // User clicked on a repository entry to view usage
088: String res = actionRequest.getParameter("res");
089: actionResponse.setRenderParameter("mode", "usage");
090: actionResponse.setRenderParameter("res", res);
091: return;
092: }
093:
094: try {
095:
096: List list = new ArrayList();
097: WriteableRepository repo = PortletManager.getCurrentServer(
098: actionRequest).getWritableRepositories()[0];
099:
100: File uploadFile = null;
101: File file = null;
102: String name = null;
103: String basename = null;
104: String fileType = null;
105: String artifact = null;
106: String version = null;
107: String group = null;
108:
109: PortletFileUpload uploader = new PortletFileUpload(
110: new DiskFileItemFactory());
111: try {
112: List items = uploader.parseRequest(actionRequest);
113: for (Iterator i = items.iterator(); i.hasNext();) {
114: FileItem item = (FileItem) i.next();
115: if (!item.isFormField()) {
116: String fieldName = item.getFieldName().trim();
117: name = item.getName().trim();
118:
119: if (name.length() == 0) {
120: file = null;
121: } else {
122: // IE sends full path while Firefox sends just basename
123: // in the case of "FullName" we may be able to infer the group
124: // Note, we can't use File.separatorChar because the file separator
125: // is dependent upon the client and not the server.
126: String fileChar = "\\";
127: int fileNameIndex = name
128: .lastIndexOf(fileChar);
129: if (fileNameIndex == -1) {
130: fileChar = "/";
131: fileNameIndex = name
132: .lastIndexOf(fileChar);
133: }
134: if (fileNameIndex != -1) {
135: basename = name
136: .substring(fileNameIndex + 1);
137: } else {
138: basename = name;
139: }
140:
141: // Create the temporary file to be used for import to the server
142: file = File.createTempFile(
143: "geronimo-import", "");
144: file.deleteOnExit();
145: log
146: .debug("Writing repository import file to "
147: + file.getAbsolutePath());
148: }
149:
150: if ("local".equals(fieldName)) {
151: uploadFile = file;
152: }
153:
154: if (file != null) {
155: try {
156: item.write(file);
157: } catch (Exception e) {
158: throw new PortletException(e);
159: }
160: }
161: // This is not the file itself, but one of the form fields for the URI
162: } else {
163: String fieldName = item.getFieldName().trim();
164: if ("group".equals(fieldName)) {
165: group = item.getString().trim();
166: } else if ("artifact".equals(fieldName)) {
167: artifact = item.getString().trim();
168: } else if ("version".equals(fieldName)) {
169: version = item.getString().trim();
170: } else if ("fileType".equals(fieldName)) {
171: fileType = item.getString().trim();
172: }
173: }
174: }
175:
176: repo.copyToRepository(file, new Artifact(group,
177: artifact, version, fileType),
178: new FileWriteMonitor() {
179: public void writeStarted(
180: String fileDescription, int fileSize) {
181: log.info("Copying into repository "
182: + fileDescription + "...");
183: }
184:
185: public void writeProgress(int bytes) {
186: }
187:
188: public void writeComplete(int bytes) {
189: log.info("Finished.");
190: }
191: });
192: } catch (FileUploadException e) {
193: throw new PortletException(e);
194: }
195: } catch (PortletException e) {
196: throw e;
197: }
198: }
199:
200: protected void doView(RenderRequest request, RenderResponse response)
201: throws PortletException, IOException {
202: // i think generic portlet already does this
203: if (WindowState.MINIMIZED.equals(request.getWindowState())) {
204: return;
205: }
206:
207: String mode = request.getParameter("mode");
208: if (mode != null && mode.equals("usage")) {
209: String res = request.getParameter("res");
210: String[] parts = res.split("/");
211: request.setAttribute("res", res);
212: request.setAttribute("groupId", parts[0]);
213: request.setAttribute("artifactId", parts[1]);
214: request.setAttribute("version", parts[2]);
215: request.setAttribute("type", parts[3]);
216: usageView.include(request, response);
217: return;
218: }
219:
220: try {
221: List list = new ArrayList();
222: ListableRepository[] repos = PortletManager
223: .getCurrentServer(request).getRepositories();
224: for (int i = 0; i < repos.length; i++) {
225: ListableRepository repo = repos[i];
226: final SortedSet artifacts = repo.list();
227: for (Iterator iterator = artifacts.iterator(); iterator
228: .hasNext();) {
229: String fileName = iterator.next().toString();
230: list.add(fileName);
231: }
232: }
233: Collections.sort(list);
234:
235: request.setAttribute(
236: "org.apache.geronimo.console.repo.list", list);
237:
238: } catch (Exception e) {
239: throw new PortletException(e);
240: }
241:
242: normalView.include(request, response);
243: }
244:
245: public void doHelp(RenderRequest request, RenderResponse response)
246: throws PortletException, IOException {
247: helpView.include(request, response);
248: }
249:
250: public List listing(File dir, String basepath)
251: throws java.io.IOException {
252: if (dir == null) {
253: throw new IllegalArgumentException(
254: "directory argument is null");
255: }
256:
257: if (!dir.isDirectory()) {
258: throw new IllegalArgumentException(
259: "directory argument expected");
260: }
261:
262: List listing = new ArrayList();
263:
264: List ls = Arrays.asList(dir.listFiles());
265: Iterator iter = ls.iterator();
266:
267: while (iter.hasNext()) {
268: File f = (File) iter.next();
269:
270: if (f.isDirectory()) {
271: List listing1 = listing(f, basepath);
272: listing.addAll(listing1);
273: } else {
274: listing.add(f.getCanonicalPath().substring(
275: basepath.length() + 1));
276: }
277: }
278: return listing;
279: }
280:
281: }
|