001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.directwebremoting.dwrp;
017:
018: import java.io.File;
019: import java.util.HashMap;
020: import java.util.List;
021: import java.util.Map;
022:
023: import javax.servlet.http.HttpServletRequest;
024: import javax.servlet.http.HttpSession;
025:
026: import org.apache.commons.fileupload.FileItem;
027: import org.apache.commons.fileupload.FileUploadException;
028: import org.apache.commons.fileupload.disk.DiskFileItemFactory;
029: import org.apache.commons.fileupload.servlet.ServletFileUpload;
030: import org.directwebremoting.event.SessionProgressListener;
031: import org.directwebremoting.extend.FormField;
032: import org.directwebremoting.extend.ServerException;
033: import org.directwebremoting.util.Messages;
034:
035: /**
036: * An implementation of {@link FileUpload} that uses Apache Commons FileUpload.
037: * This class with fail to classload if commons-fileupload.jar is not present
038: * on the classpath.
039: * @author Joe Walker [joe at getahead dot ltd dot uk]
040: */
041: public class CommonsFileUpload implements FileUpload {
042: /* (non-Javadoc)
043: * @see org.directwebremoting.dwrp.FileUpload#isMultipartContent(javax.servlet.http.HttpServletRequest)
044: */
045: public boolean isMultipartContent(HttpServletRequest req) {
046: return ServletFileUpload.isMultipartContent(req);
047: }
048:
049: /* (non-Javadoc)
050: * @see org.directwebremoting.dwrp.FileUpload#parseRequest(javax.servlet.http.HttpServletRequest)
051: */
052: @SuppressWarnings("unchecked")
053: public Map<String, FormField> parseRequest(HttpServletRequest req)
054: throws ServerException {
055: File location = new File(System.getProperty("java.io.tmpdir"));
056: DiskFileItemFactory itemFactory = new DiskFileItemFactory(
057: DEFAULT_SIZE_THRESHOLD, location);
058: ServletFileUpload fileUploader = new ServletFileUpload(
059: itemFactory);
060:
061: HttpSession session = req.getSession(false);
062: if (session != null) {
063: fileUploader
064: .setProgressListener(new SessionProgressListener(
065: session));
066: session.setAttribute(SessionProgressListener.CANCEL_UPLOAD,
067: null);
068: session.setAttribute(PROGRESS_LISTENER, fileUploader
069: .getProgressListener());
070: }
071:
072: try {
073: Map<String, FormField> map = new HashMap<String, FormField>();
074: List<FileItem> fileItems = fileUploader.parseRequest(req);
075: for (FileItem fileItem : fileItems) {
076: FormField formField;
077: if (fileItem.isFormField()) {
078: formField = new FormField(fileItem.getString());
079: } else {
080: formField = new FormField(fileItem.getName(),
081: fileItem.getContentType(), fileItem.get());
082: }
083: map.put(fileItem.getFieldName(), formField);
084: }
085:
086: return map;
087: } catch (FileUploadException e) {
088: throw new ServerException(Messages
089: .getString("ParseUtil.InputReadFailed"), e);
090: }
091: }
092:
093: /**
094: * The name of the attribute that stores the progress of an upload in a session.
095: */
096: public static final String PROGRESS_LISTENER = "PROGRESS_LISTENER";
097:
098: /**
099: * The threshold, in bytes, below which items will be retained in memory and above which they will be stored as a file
100: */
101: private static final int DEFAULT_SIZE_THRESHOLD = 256 * 1024;
102: }
|