View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 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.MultipartForm;
37  import info.magnolia.cms.core.Path;
38  import info.magnolia.context.MgnlContext;
39  
40  import java.io.IOException;
41  import java.util.Enumeration;
42  import java.util.Map;
43  
44  import javax.servlet.FilterChain;
45  import javax.servlet.ServletException;
46  import javax.servlet.http.HttpServletRequest;
47  import javax.servlet.http.HttpServletRequestWrapper;
48  import javax.servlet.http.HttpServletResponse;
49  
50  import org.apache.commons.lang.StringUtils;
51  
52  import com.oreilly.servlet.MultipartRequest;
53  
54  
55  /**
56   * @author Sameer Charles
57   * @version $Id: CosMultipartRequestFilter.java 32667 2010-03-13 00:37:06Z gjoseph $
58   */
59  public class CosMultipartRequestFilter extends AbstractMgnlFilter {
60  
61      /**
62       * Max file upload size.
63       */
64      private static final int MAX_FILE_SIZE = 2000000000; // 2GB
65  
66      public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
67          throws IOException, ServletException {
68  
69          String type = null;
70          String type1 = request.getHeader("Content-Type"); //$NON-NLS-1$
71          String type2 = request.getContentType();
72          if (type1 == null && type2 != null) {
73              type = type2;
74          }
75          else if (type2 == null && type1 != null) {
76              type = type1;
77          }
78          else if (type1 != null) {
79              type = (type1.length() > type2.length() ? type1 : type2);
80          }
81          boolean isMultipart = (type != null) && type.toLowerCase().startsWith("multipart/form-data"); 
82          if (isMultipart) { 
83              MultipartForm mpf = parseParameters(request);
84              request = new MultipartRequestWrapper(request, mpf);
85              MgnlContext.push(request, response);
86          }
87          chain.doFilter(request, response);
88          if (isMultipart) { 
89              MgnlContext.pop();
90          }
91      }
92  
93      /**
94       * Adds all request paramaters as request attributes.
95       * @param request HttpServletRequest
96       */
97      private static MultipartForm parseParameters(HttpServletRequest request) throws IOException {
98          MultipartForm form = new MultipartForm();
99          String encoding = StringUtils.defaultString(request.getCharacterEncoding(), "UTF-8"); //$NON-NLS-1$
100         MultipartRequest multi = new MultipartRequest(
101             request,
102             Path.getTempDirectoryPath(),
103             MAX_FILE_SIZE,
104             encoding,
105             null);
106         Enumeration params = multi.getParameterNames();
107         while (params.hasMoreElements()) {
108             String name = (String) params.nextElement();
109             String value = multi.getParameter(name);
110             form.addParameter(name, value);
111             String[] s = multi.getParameterValues(name);
112             if (s != null) {
113                 form.addparameterValues(name, s);
114             }
115         }
116         Enumeration files = multi.getFileNames();
117         while (files.hasMoreElements()) {
118             String name = (String) files.nextElement();
119             form.addDocument(name, multi.getFilesystemName(name), multi.getContentType(name), multi.getFile(name));
120         }
121         request.setAttribute(MultipartForm.REQUEST_ATTRIBUTE_NAME, form);
122         return form;
123     }
124 
125     static class MultipartRequestWrapper extends HttpServletRequestWrapper {
126 
127         private MultipartForm form;
128 
129         /**
130          * @param request
131          */
132         public MultipartRequestWrapper(HttpServletRequest request, MultipartForm form) {
133             super(request);
134             this.form = form;
135         }
136 
137         /**
138          * {@inheritDoc}
139          */
140 
141         public String getParameter(String name) {
142             String value = form.getParameter(name);
143             return value;
144         }
145 
146         /**
147          * {@inheritDoc}
148          */
149 
150         public Map getParameterMap() {
151             return form.getParameters();
152         }
153 
154         /**
155          * {@inheritDoc}
156          */
157 
158         public Enumeration getParameterNames() {
159             return form.getParameterNames();
160         }
161 
162         /**
163          * {@inheritDoc}
164          */
165 
166         public String[] getParameterValues(String name) {
167             String[] value = form.getParameterValues(name);
168             return value;
169         }
170     }
171 }