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.filters;
35  
36  import info.magnolia.cms.beans.runtime.Document;
37  import info.magnolia.cms.beans.runtime.MultipartForm;
38  import info.magnolia.cms.core.Path;
39  import info.magnolia.context.MgnlContext;
40  
41  import java.io.IOException;
42  import java.util.Enumeration;
43  import java.util.Iterator;
44  
45  import javax.servlet.FilterChain;
46  import javax.servlet.ServletException;
47  import javax.servlet.http.HttpServletRequest;
48  import javax.servlet.http.HttpServletResponse;
49  
50  import org.apache.commons.lang.StringUtils;
51  
52  import com.oreilly.servlet.MultipartRequest;
53  
54  /**
55   * Processes multipart post requests (fileuploads). Files can be accessed with {@link MultipartForm#getDocument(String)}.
56   * @author Sameer Charles
57   * @version $Id$
58   */
59  public class CosMultipartRequestFilter extends OncePerRequestAbstractMgnlFilter {
60  
61      /**
62       * Max file upload size.
63       */
64      private static final int MAX_FILE_SIZE = 2000000000; // 2GB
65  
66      @Override
67      public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
68          throws IOException, ServletException {
69  
70          String type = null;
71          String type1 = request.getHeader("Content-Type");
72          String type2 = request.getContentType();
73          if (type1 == null && type2 != null) {
74              type = type2;
75          }
76          else if (type2 == null && type1 != null) {
77              type = type1;
78          }
79          else if (type1 != null) {
80              type = (type1.length() > type2.length() ? type1 : type2);
81          }
82          boolean isMultipart = (type != null) && type.toLowerCase().startsWith("multipart/form-data");
83          MultipartForm mpf = null;
84          try{
85              if (isMultipart) {
86                  mpf = parseParameters(request);
87                  request = new MultipartRequestWrapper(request, mpf);
88                  MgnlContext.push(request, response);
89              }
90              try {
91                  chain.doFilter(request, response);
92              } finally {
93                  if (isMultipart) {
94                      MgnlContext.pop();
95                  }
96              }
97          } finally {
98              if (mpf != null) {
99                  Iterator<Document> keys = mpf.getDocuments().values().iterator();
100                 while (keys.hasNext()) {
101                     Document doc = keys.next();
102                     if (doc != null && doc.getFile() != null && doc.getFile().exists()) {
103                         doc.delete();
104                     }
105                 }
106             }
107         }
108     }
109 
110     /**
111      * Adds all request paramaters as request attributes.
112      * @param request HttpServletRequest
113      */
114     private static MultipartForm parseParameters(HttpServletRequest request) throws IOException {
115         MultipartForm form = new MultipartForm();
116         String encoding = StringUtils.defaultString(request.getCharacterEncoding(), "UTF-8");
117         MultipartRequest multi = new MultipartRequest(
118             request,
119             Path.getTempDirectoryPath(),
120             MAX_FILE_SIZE,
121             encoding,
122             null);
123         Enumeration params = multi.getParameterNames();
124         while (params.hasMoreElements()) {
125             String name = (String) params.nextElement();
126             String value = multi.getParameter(name);
127             form.addParameter(name, value);
128             String[] s = multi.getParameterValues(name);
129             if (s != null) {
130                 form.addparameterValues(name, s);
131             }
132         }
133         Enumeration files = multi.getFileNames();
134         while (files.hasMoreElements()) {
135             String name = (String) files.nextElement();
136             form.addDocument(name, multi.getFilesystemName(name), multi.getContentType(name), multi.getFile(name));
137         }
138         request.setAttribute(MultipartForm.REQUEST_ATTRIBUTE_NAME, form);
139         return form;
140     }
141 }