1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.upload;
16  
17  import com.liferay.portal.kernel.util.ProgressTracker;
18  import com.liferay.portal.kernel.util.Validator;
19  
20  import java.util.List;
21  import java.util.Map;
22  
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpSession;
25  
26  import org.apache.commons.fileupload.FileItem;
27  import org.apache.commons.fileupload.FileItemFactory;
28  import org.apache.commons.fileupload.FileUploadException;
29  import org.apache.commons.fileupload.servlet.ServletFileUpload;
30  
31  /**
32   * <a href="LiferayFileUpload.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Myunghun Kim
35   * @author Brian Wing Shun Chan
36   */
37  public class LiferayFileUpload extends ServletFileUpload {
38  
39      public static final String FILE_NAME =
40          LiferayFileUpload.class.getName() + "_FILE_NAME";
41  
42      public static final String PERCENT = ProgressTracker.PERCENT;
43  
44      public LiferayFileUpload(
45          FileItemFactory fileItemFactory, HttpServletRequest request) {
46  
47          super(fileItemFactory);
48  
49          _session = request.getSession();
50      }
51  
52      public List<LiferayFileItem> parseRequest(HttpServletRequest request)
53          throws FileUploadException {
54  
55          _session.removeAttribute(LiferayFileUpload.FILE_NAME);
56          _session.removeAttribute(LiferayFileUpload.PERCENT);
57  
58          return super.parseRequest(request);
59      }
60  
61      /**
62       * @deprecated
63       */
64      protected FileItem createItem(Map headers, boolean formField)
65          throws FileUploadException {
66  
67          LiferayFileItem item = (LiferayFileItem)super.createItem(
68              headers, formField);
69  
70          String fileName = item.getFileName();
71  
72          if (Validator.isNotNull(fileName)) {
73              _session.setAttribute(LiferayFileUpload.FILE_NAME, fileName);
74          }
75  
76          return item;
77      }
78  
79      private HttpSession _session;
80  
81  }