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