001: /*
002: * (C) Copyright 2005 Nabh Information Systems, Inc.
003: *
004: * All copyright notices regarding Nabh's products MUST remain
005: * intact in the scripts and in the outputted HTML.
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021: package com.nabhinc.portlet.mvcportlet.actionprocessor;
022:
023: import java.io.File;
024: import java.io.FileNotFoundException;
025: import java.io.FileOutputStream;
026: import java.io.IOException;
027: import java.io.InputStream;
028:
029: import javax.portlet.ActionRequest;
030: import javax.portlet.ActionResponse;
031: import javax.portlet.PortletException;
032:
033: import org.w3c.dom.Element;
034:
035: import com.nabhinc.portlet.mvcportlet.core.ActionConfig;
036: import com.nabhinc.portlet.mvcportlet.core.ActionProcessor;
037: import com.nabhinc.portlet.mvcportlet.core.BaseRequestProcessor;
038: import com.nabhinc.portlet.mvcportlet.core.Constants;
039: import com.nabhinc.portlet.mvcportlet.core.ControllerPortletConfig;
040: import com.nabhinc.util.XMLUtil;
041: import org.apache.commons.fileupload.FileItem;
042:
043: /**
044: * Upload file to the specified upload folder in <code>upload-directory</code>
045: * global-param. By default, if this parameter is not specified,
046: * <code>/WEB-INF/file_upload/uploads</code> folder is assumed.
047: * If <code>rootDir</code> init-param is specified in the portlet definition,
048: * the upload folder will be relative to this root directory. Otherwise, the
049: * folder's path will be relative to the Web application's context path (must start with "/").
050: * An optional <code>upload-field-name</code> tag is to specify the form's
051: * file field name. Default field name is <code>file_path</code>.
052: *
053: * @author Wirawan Chokry
054: * (c) 2005 Nabh Information Systems, Inc. All Rights Reserved.
055: */
056: public class FileUploadProcessor extends BaseRequestProcessor implements
057: ActionProcessor {
058: private String fupUploadDir = null;
059: private String fupFieldName = null;
060:
061: public void init(Element config, ControllerPortletConfig conConfig)
062: throws PortletException {
063: super .init(config, conConfig);
064:
065: String temp = conConfig.getParameter("upload-directory");
066: if (temp == null)
067: temp = "/WEB-INF/file_upload/uploads";
068: if (new File(temp).exists()) {
069: // absolute path
070: fupUploadDir = temp;
071: } else {
072: fupUploadDir = conConfig.getRealPath(conConfig
073: .getPortletContext(), temp);
074: }
075:
076: temp = XMLUtil.getSubElementText(config, "upload-field-name");
077: if (temp == null)
078: fupFieldName = "mvcportlet.file.file_path";
079: else
080: fupFieldName = "mvcportlet.file." + temp;
081:
082: // Make sure the directory exists
083: new File(fupUploadDir).mkdirs();
084: }
085:
086: public String process(ActionRequest request,
087: ActionResponse response, ActionConfig actionConfig)
088: throws PortletException, IOException {
089:
090: String portletMode = request.getPortletMode().toString()
091: .toLowerCase();
092: try {
093: FileItem fileItem = (FileItem) request
094: .getAttribute(fupFieldName);
095: if (fileItem == null || "".equals(fileItem.getName())) {
096: response.setRenderParameter(
097: Constants.ERROR_MESSAGE_PARAM + portletMode,
098: "File to upload must be specified.");
099: return "failure";
100: }
101:
102: String fileName = fileItem.getName();
103: // Some browsers, e.g. IE include path information
104: int slashIndex = fileName.lastIndexOf("/");
105: if (slashIndex > -1) {
106: fileName = fileName.substring(slashIndex + 1);
107: }
108: slashIndex = fileName.lastIndexOf("\\");
109: if (slashIndex > -1) {
110: fileName = fileName.substring(slashIndex + 1);
111: }
112:
113: File uploadFile = new File(fupUploadDir, fileName);
114: InputStream is = fileItem.getInputStream();
115: FileOutputStream out = new FileOutputStream(uploadFile);
116: byte[] content = new byte[1000];
117: int bufSize = -1;
118: while ((bufSize = is.read(content)) > 0) {
119: out.write(content, 0, bufSize);
120: }
121: out.flush();
122: out.close();
123:
124: } catch (FileNotFoundException e) {
125: response.setRenderParameter(Constants.ERROR_MESSAGE_PARAM
126: + portletMode, "Failed to upload file.");
127: return "failure";
128: } finally {
129:
130: }
131:
132: return "success";
133: }
134:
135: }
|