View Javadoc
1   /**
2    * This file Copyright (c) 2003-2014 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.cms.beans.runtime;
35  
36  import org.apache.commons.lang.StringUtils;
37  
38  import java.io.File;
39  import java.util.Enumeration;
40  import java.util.Hashtable;
41  import java.util.Map;
42  
43  
44  /**
45   * A util class to access file uploads in multipart post requests. Use {@link #getDocument(String)} to access an uploaded file.
46   * @author Sameer Charles
47   * @version 1.1
48   */
49  public class MultipartForm {
50  
51      /**
52       * The name of the request attribute containing a MultipartForm instance.
53       */
54      public static final String REQUEST_ATTRIBUTE_NAME = "multipartform"; //$NON-NLS-1$
55  
56      private final Map<String, String[]> parameters;
57  
58      private final Map<String, Document> documents;
59  
60      private final Map parameterList;
61  
62      public MultipartForm() {
63          this.parameters = new Hashtable<String, String[]>();
64          this.documents = new Hashtable<String, Document>();
65          this.parameterList = new Hashtable();
66      }
67  
68      /**
69       * @deprecated since 4.0 - should not be needed anymore since MAGNOLIA-2449 - request parameters should be correctly wrapped.
70       */
71      public void addParameter(String name, Object value) {
72          if (value instanceof String[]) {
73              this.parameters.put(name, (String[]) value);
74          } else {
75              this.parameters.put(name, new String[]{(String) value });
76          }
77      }
78  
79      /**
80       * @deprecated since 4.0 - should not be needed anymore since MAGNOLIA-2449 - request parameters should be correctly wrapped.
81       */
82      public void removeParameter(String name) {
83          this.parameters.remove(name);
84      }
85  
86      /**
87       * @deprecated since 4.0 - should not be needed anymore since MAGNOLIA-2449 - request parameters should be correctly wrapped.
88       */
89      public Map<String, String[]> getParameters() {
90          return this.parameters;
91      }
92  
93      /**
94       * @deprecated since 4.0 - should not be needed anymore since MAGNOLIA-2449 - request parameters should be correctly wrapped.
95       */
96      public String getParameter(String name) {
97          try {
98              String[] params = this.parameters.get(name);
99              if (params != null && params.length > 0) {
100                 return params[0];
101             }
102             return null;
103         } catch (Exception e) {
104             return null;
105         }
106     }
107 
108     /**
109      * @deprecated since 4.0 - should not be needed anymore since MAGNOLIA-2449 - request parameters should be correctly wrapped.
110      */
111     public String[] getParameterValues(String name) {
112         try {
113             return ((String[]) this.parameterList.get(name));
114         } catch (Exception e) {
115             return null;
116         }
117     }
118 
119     /**
120      * @deprecated since 4.0 - should not be needed anymore since MAGNOLIA-2449 - request parameters should be correctly wrapped.
121      */
122     public void addparameterValues(String name, String[] values) {
123         this.parameterList.put(name, values);
124     }
125 
126     public void addDocument(String atomName, String fileName, String type, File file) {
127         if (StringUtils.isEmpty(fileName)) {
128             return;
129         }
130         Document document = new Document();
131         document.setAtomName(atomName);
132         document.setType(type);
133         document.setFile(file);
134         if (!StringUtils.contains(fileName, ".")) { //$NON-NLS-1$
135             document.setExtension(StringUtils.EMPTY);
136             document.setFileName(fileName);
137         } else {
138             document.setExtension(StringUtils.substringAfterLast(fileName, ".")); //$NON-NLS-1$
139             document.setFileName(StringUtils.substringBeforeLast(fileName, ".")); //$NON-NLS-1$
140         }
141         this.documents.put(atomName, document);
142     }
143 
144     public Document getDocument(String name) {
145         return this.documents.get(name);
146     }
147 
148     public Map<String, Document> getDocuments() {
149         return this.documents;
150     }
151 
152     /**
153      * @deprecated since 4.0 - should not be needed anymore since MAGNOLIA-2449 - request parameters should be correctly wrapped.
154      */
155     public Enumeration<String> getParameterNames() {
156         return ((Hashtable<String, String[]>) this.parameters).keys();
157     }
158 }