View Javadoc
1   /**
2    * This file Copyright (c) 2003-2015 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.lang3.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   */
57  public class CosMultipartRequestFilter extends OncePerRequestAbstractMgnlFilter {
58  
59      /**
60       * Max file upload size.
61       */
62      private static final int MAX_FILE_SIZE = 2000000000; // 2GB
63  
64      @Override
65      public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
66              throws IOException, ServletException {
67  
68          String type = null;
69          String type1 = request.getHeader("Content-Type");
70          String type2 = request.getContentType();
71          if (type1 == null && type2 != null) {
72              type = type2;
73          } else if (type2 == null && type1 != null) {
74              type = type1;
75          } else if (type1 != null) {
76              type = (type1.length() > type2.length() ? type1 : type2);
77          }
78          boolean isMultipart = (type != null) && type.toLowerCase().startsWith("multipart/form-data");
79          MultipartForm mpf = null;
80          try {
81              if (isMultipart) {
82                  mpf = parseParameters(request);
83                  request = new MultipartRequestWrapper(request, mpf);
84                  MgnlContext.push(request, response);
85              }
86              try {
87                  chain.doFilter(request, response);
88              } finally {
89                  if (isMultipart) {
90                      MgnlContext.pop();
91                  }
92              }
93          } finally {
94              if (mpf != null) {
95                  Iterator<Document> keys = mpf.getDocuments().values().iterator();
96                  while (keys.hasNext()) {
97                      Document doc = keys.next();
98                      if (doc != null && doc.getFile() != null && doc.getFile().exists()) {
99                          doc.delete();
100                     }
101                 }
102             }
103         }
104     }
105 
106     /**
107      * Adds all request paramaters as request attributes.
108      *
109      * @param request HttpServletRequest
110      */
111     private static MultipartForm parseParameters(HttpServletRequest request) throws IOException {
112         MultipartForm form = new MultipartForm();
113         String encoding = StringUtils.defaultString(request.getCharacterEncoding(), "UTF-8");
114         MultipartRequest multi = new MultipartRequest(
115                 request,
116                 Path.getTempDirectoryPath(),
117                 MAX_FILE_SIZE,
118                 encoding,
119                 null);
120         Enumeration params = multi.getParameterNames();
121         while (params.hasMoreElements()) {
122             String name = (String) params.nextElement();
123             String value = multi.getParameter(name);
124             form.addParameter(name, value);
125             String[] s = multi.getParameterValues(name);
126             if (s != null) {
127                 form.addparameterValues(name, s);
128             }
129         }
130         Enumeration files = multi.getFileNames();
131         while (files.hasMoreElements()) {
132             String name = (String) files.nextElement();
133             form.addDocument(name, multi.getFilesystemName(name), multi.getContentType(name), multi.getFile(name));
134         }
135         request.setAttribute(MultipartForm.REQUEST_ATTRIBUTE_NAME, form);
136         return form;
137     }
138 }