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.configcreator;
017:
018: import java.io.File;
019: import java.io.IOException;
020:
021: import javax.portlet.ActionRequest;
022: import javax.portlet.ActionResponse;
023: import javax.portlet.PortletException;
024: import javax.portlet.RenderRequest;
025: import javax.portlet.RenderResponse;
026:
027: import org.apache.commons.fileupload.FileItem;
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.apache.geronimo.console.MultiPageModel;
031:
032: /**
033: * A handler for ...
034: *
035: * @version $Rev: 565397 $ $Date: 2007-08-13 09:21:44 -0700 (Mon, 13 Aug 2007) $
036: */
037: public class GetArchiveHandler extends AbstractHandler {
038: private static final Log log = LogFactory
039: .getLog(GetArchiveHandler.class);
040:
041: public GetArchiveHandler() {
042: super (GET_ARCHIVE_MODE,
043: "/WEB-INF/view/configcreator/getArchive.jsp");
044: }
045:
046: public String actionBeforeView(ActionRequest request,
047: ActionResponse response, MultiPageModel model)
048: throws PortletException, IOException {
049: return getMode();
050: }
051:
052: public void renderView(RenderRequest request,
053: RenderResponse response, MultiPageModel model)
054: throws PortletException, IOException {
055: setNewSessionData(request);
056: if ("true".equals(request
057: .getParameter(ARCHIVE_NOT_SUPPORTED_PARAMETER))) {
058: request.setAttribute(ARCHIVE_NOT_SUPPORTED_PARAMETER,
059: "true");
060: }
061: }
062:
063: public String actionAfterView(ActionRequest request,
064: ActionResponse response, MultiPageModel model)
065: throws PortletException, IOException {
066: WARConfigData data = getSessionData(request);
067: FileItem fileItem = (FileItem) getUploadFiles().get(
068: MODULE_URI_PARAMETER);
069:
070: String fileName = fileItem.getName();
071: if (fileName == null || fileName.length() <= 0) {
072: return getMode();
073: }
074:
075: // TODO Is there a better way of checking whether the archive is a WAR or not?
076: int i = fileName.length() - 4;
077: if (!fileName.substring(i).equalsIgnoreCase(".war")) {
078: response.setRenderParameter(
079: ARCHIVE_NOT_SUPPORTED_PARAMETER, "true");
080: return getMode();
081: }
082:
083: File uploadedFile = uploadFile(fileItem);
084: data.setUploadedWarUri(uploadedFile.toURI().toString());
085:
086: String str = getBasename(fileItem.getName().trim());
087: String warName = str.substring(0, str.length() - 4);
088: data.setContextRoot(warName);
089: data.setGroupId("default");
090: data.setArtifactId(warName);
091: data.setVersion("1.0");
092: data.setType("war");
093: data.setHiddenClasses("");
094: data.setNonOverridableClasses("");
095: data.setInverseClassLoading(false);
096:
097: try {
098: JSR88_Util.parseWarReferences(request, data, uploadedFile
099: .toURL());
100: } catch (Exception e) {
101: log.error(e.getMessage(), e);
102: return getMode();
103: }
104:
105: return ENVIRONMENT_MODE + "-before";
106: }
107:
108: private File uploadFile(FileItem fileItem) throws PortletException,
109: IOException {
110: File tempDir = File.createTempFile("geronimo-planCreator",
111: ".tmpdir");
112: tempDir.delete();
113: tempDir.mkdir();
114: tempDir.deleteOnExit();
115: String fileName = getBasename(fileItem.getName().trim());
116: File file = new File(tempDir, fileName);
117: file.deleteOnExit();
118: try {
119: fileItem.write(file);
120: } catch (Exception e) {
121: throw new PortletException(e);
122: }
123: return file;
124: }
125:
126: private String getBasename(String fileName) {
127: // Firefox sends basename, IE sends full path
128: int index = fileName.lastIndexOf('\\');
129: if (index != -1) {
130: return fileName.substring(index + 1);
131: }
132: return fileName;
133: }
134: }
|