001    /**
002     * Copyright (c) 2000-2012 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            @Override
051            public List<LiferayFileItem> parseRequest(HttpServletRequest request)
052                    throws FileUploadException {
053    
054                    _session.removeAttribute(LiferayFileUpload.FILE_NAME);
055                    _session.removeAttribute(LiferayFileUpload.PERCENT);
056    
057                    return super.parseRequest(request);
058            }
059    
060            /**
061             * @deprecated
062             */
063            @Override
064            @SuppressWarnings("rawtypes")
065            protected FileItem createItem(Map headers, boolean formField)
066                    throws FileUploadException {
067    
068                    LiferayFileItem item = (LiferayFileItem)super.createItem(
069                            headers, formField);
070    
071                    String fileName = item.getFileName();
072    
073                    if (Validator.isNotNull(fileName)) {
074                            _session.setAttribute(LiferayFileUpload.FILE_NAME, fileName);
075                    }
076    
077                    return item;
078            }
079    
080            private HttpSession _session;
081    
082    }