001: /*
002: Copyright (c) 2003 eInnovation Inc. All rights reserved
003:
004: This library is free software; you can redistribute it and/or modify it under the terms
005: of the GNU Lesser General Public License as published by the Free Software Foundation;
006: either version 2.1 of the License, or (at your option) any later version.
007:
008: This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
009: without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: See the GNU Lesser General Public License for more details.
011: */
012:
013: /*
014: * FileUploadAction.java
015: *
016: * Created on July 31, 2002, 11:41 AM
017: */
018: package com.openedit.modules.admin.filemanager;
019:
020: import java.io.File;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Map;
025:
026: import javax.servlet.http.HttpServletRequest;
027:
028: import org.apache.commons.fileupload.FileItem;
029: import org.apache.commons.fileupload.disk.DiskFileItemFactory;
030: import org.apache.commons.fileupload.servlet.ServletFileUpload;
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033:
034: import com.openedit.OpenEditException;
035: import com.openedit.WebPageRequest;
036: import com.openedit.page.manage.PageManager;
037:
038: /**
039: * DOCUMENT ME!
040: *
041: * @author cnelson
042: */
043: public class FileUpload {
044: protected PageManager fieldPageManager;
045: protected File fieldRoot;
046: /** Defaults to 1MB */
047: public static final int BUFFER_SIZE = 1000000;
048:
049: private static final Log log = LogFactory.getLog(FileUpload.class);
050:
051: /* (non-Javadoc)
052: * @see com.openedit.action.Command#execute(java.util.Map, java.util.Map)
053: */
054: public UploadRequest uploadFiles(WebPageRequest inContext)
055: throws OpenEditException {
056: if (inContext.getUser() == null) {
057: throw new OpenEditException(
058: "You must be logged in to upload files");
059: } else if (!inContext.getUser().hasPermission("oe.edit.upload")) {
060: throw new OpenEditException(
061: "You don't have enough permissions to upload files");
062: }
063:
064: UploadRequest props = parseArguments(inContext);
065: if (props == null) {
066: return null;
067: }
068: String home = (String) inContext.getPageValue("home");
069:
070: for (Iterator iterator = props.getUploadItems().iterator(); iterator
071: .hasNext();) {
072: FileUploadItem item = (FileUploadItem) iterator.next();
073: props.saveFile(item, home, inContext.getUser());
074: //Page page = saveFile( props, finalpath, inContext );
075: }
076: inContext.putPageValue("uploadrequest", props);
077: //inContext.setRequestParameter("path", page.getPath());
078: return props;
079: }
080:
081: /**
082: * @param inContext
083: * @return
084: */
085: public UploadRequest parseArguments(WebPageRequest inContext)
086: throws OpenEditException {
087: UploadRequest upload = new UploadRequest();
088: upload.setPageManager(getPageManager());
089: upload.setRoot(getRoot());
090: if (inContext.getRequest() == null) //used in unit tests
091: {
092: upload.setProperties(inContext.getPageMap());
093: return upload;
094: }
095: String type = inContext.getRequest().getContentType();
096: if (type == null || !type.startsWith("multipart")) {
097: return null;
098: }
099:
100: ServletFileUpload uploadreader = new ServletFileUpload(
101: new DiskFileItemFactory());
102: //upload.setSizeThreshold(BUFFER_SIZE);
103:
104: HttpServletRequest req = inContext.getRequest();
105: String encode = req.getCharacterEncoding();
106: if (encode == null) {
107: log.info("Encoding not set.");
108: encode = "UTF-8";
109: }
110: log.info("Encoding is set to " + encode);
111: uploadreader.setHeaderEncoding(encode);
112:
113: //upload.setHeaderEncoding()
114: //Content-Transfer-Encoding: binary
115: //upload.setRepositoryPath(repository.pathToFile("admin
116: uploadreader.setSizeMax(-1);
117:
118: List fileItems;
119:
120: try {
121: fileItems = uploadreader.parseRequest(inContext
122: .getRequest());
123: } catch (Exception e) {
124: throw new OpenEditException(e);
125: }
126:
127: // This is a multipart MIME-encoded request, so the request
128: // parameters must all be parsed from the POST body, not
129: // gotten directly off the HttpServletRequest.
130:
131: for (int i = 0; i < fileItems.size(); i++) {
132: FileItem tmp = (FileItem) fileItems.get(i);
133: if (tmp.getFieldName().startsWith("file")
134: && tmp.getName() != null
135: && !tmp.getName().equals("")) {
136: FileUploadItem foundUpload = new FileUploadItem();
137: foundUpload.setFileItem(tmp);
138: foundUpload.setName(tmp.getName());
139: String num = "0";
140: int index = tmp.getFieldName().indexOf(".");
141: if (index > -1) {
142: num = tmp.getFieldName().substring(index + 1);
143: }
144: foundUpload.setCount(Integer.parseInt(num));
145: upload.addUploadItem(foundUpload);
146: }
147: }
148: for (int i = 0; i < fileItems.size(); i++) {
149: FileItem tmp = (FileItem) fileItems.get(i);
150: if (!tmp.getFieldName().startsWith("file")) {
151: int index = tmp.getFieldName().indexOf(".");
152: if (index > -1) {
153: String num = tmp.getFieldName()
154: .substring(index + 1);
155: FileUploadItem foundUpload = upload
156: .getUploadItem(Integer.parseInt(num));
157: if (foundUpload != null) {
158: foundUpload.putProperty(tmp.getFieldName()
159: .substring(0, index), tmp.getString());
160: } else {
161: inContext.setRequestParameter(tmp
162: .getFieldName(), tmp.getString());
163: }
164: } else {
165: if (tmp.getFieldName().equals("path")) {
166: inContext.putPageValue("path", tmp.getString());
167: }
168: inContext.setRequestParameter(tmp.getFieldName(),
169: tmp.getString());
170: }
171: }
172: }
173: upload.setProperties(inContext.getParameterMap());
174: return upload;
175: }
176:
177: public PageManager getPageManager() {
178: return fieldPageManager;
179: }
180:
181: public void setPageManager(PageManager pageManager) {
182: fieldPageManager = pageManager;
183: }
184:
185: public File getRoot() {
186: return fieldRoot;
187: }
188:
189: public void setRoot(File inRoot) {
190: fieldRoot = inRoot;
191: }
192: }
|