001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.upload;
016    
017    import com.liferay.portal.kernel.util.ProgressTracker;
018    import com.liferay.portal.kernel.util.Validator;
019    
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.FileItemFactory;
028    import org.apache.commons.fileupload.FileUploadException;
029    import org.apache.commons.fileupload.servlet.ServletFileUpload;
030    
031    /**
032     * @author Brian Myunghun Kim
033     * @author Brian Wing Shun Chan
034     */
035    public class LiferayFileUpload extends ServletFileUpload {
036    
037            public static final String FILE_NAME =
038                    LiferayFileUpload.class.getName() + "_FILE_NAME";
039    
040            public static final String PERCENT = ProgressTracker.PERCENT;
041    
042            public LiferayFileUpload(
043                    FileItemFactory fileItemFactory, HttpServletRequest request) {
044    
045                    super(fileItemFactory);
046    
047                    _session = request.getSession();
048            }
049    
050            public List<LiferayFileItem> parseRequest(HttpServletRequest request)
051                    throws FileUploadException {
052    
053                    _session.removeAttribute(LiferayFileUpload.FILE_NAME);
054                    _session.removeAttribute(LiferayFileUpload.PERCENT);
055    
056                    return super.parseRequest(request);
057            }
058    
059            /**
060             * @deprecated
061             */
062            protected FileItem createItem(Map headers, boolean formField)
063                    throws FileUploadException {
064    
065                    LiferayFileItem item = (LiferayFileItem)super.createItem(
066                            headers, formField);
067    
068                    String fileName = item.getFileName();
069    
070                    if (Validator.isNotNull(fileName)) {
071                            _session.setAttribute(LiferayFileUpload.FILE_NAME, fileName);
072                    }
073    
074                    return item;
075            }
076    
077            private HttpSession _session;
078    
079    }