001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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    }