KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > filters > MultipartRequestFilter


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.filters;
14
15 import info.magnolia.cms.beans.runtime.MultipartForm;
16 import info.magnolia.cms.core.Path;
17
18 import java.io.IOException JavaDoc;
19 import java.util.Enumeration JavaDoc;
20
21 import javax.servlet.Filter JavaDoc;
22 import javax.servlet.FilterChain JavaDoc;
23 import javax.servlet.FilterConfig JavaDoc;
24 import javax.servlet.ServletException JavaDoc;
25 import javax.servlet.ServletRequest JavaDoc;
26 import javax.servlet.ServletResponse JavaDoc;
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28
29 import org.apache.commons.lang.StringUtils;
30
31 import com.oreilly.servlet.MultipartRequest;
32
33
34 /**
35  * @author Sameer Charles
36  * @version $Id: MultipartRequestFilter.java 6341 2006-09-12 09:18:27Z philipp $
37  */

38 public class MultipartRequestFilter implements Filter JavaDoc {
39
40     /**
41      * Max file upload size.
42      */

43     private static final int MAX_FILE_SIZE = 2000000000; // 2GB
44

45     /**
46      * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
47      */

48     public void init(FilterConfig JavaDoc filterConfig) throws ServletException JavaDoc {
49         // unused
50
}
51
52     /**
53      * @see javax.servlet.Filter#destroy()
54      */

55     public void destroy() {
56         // unused
57
}
58
59     /**
60      * @see javax.servlet.Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
61      */

62     public void doFilter(ServletRequest JavaDoc req, ServletResponse JavaDoc res, FilterChain JavaDoc filterChain) throws IOException JavaDoc,
63         ServletException JavaDoc {
64         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) req;
65         String JavaDoc type = null;
66         String JavaDoc type1 = request.getHeader("Content-Type"); //$NON-NLS-1$
67
String JavaDoc type2 = request.getContentType();
68         if (type1 == null && type2 != null) {
69             type = type2;
70         }
71         else if (type2 == null && type1 != null) {
72             type = type1;
73         }
74         else if (type1 != null) {
75             type = (type1.length() > type2.length() ? type1 : type2);
76         }
77         if ((type != null) && type.toLowerCase().startsWith("multipart/form-data")) { //$NON-NLS-1$
78
parseParameters(request);
79         }
80         filterChain.doFilter(req, res);
81     }
82
83     /**
84      * Adds all request paramaters as request attributes.
85      * @param request HttpServletRequest
86      */

87     private static void parseParameters(HttpServletRequest JavaDoc request) throws IOException JavaDoc {
88         MultipartForm form = new MultipartForm();
89         String JavaDoc encoding = StringUtils.defaultString(request.getCharacterEncoding(), "UTF-8"); //$NON-NLS-1$
90
MultipartRequest multi = new MultipartRequest(
91             request,
92             Path.getTempDirectoryPath(),
93             MAX_FILE_SIZE,
94             encoding,
95             null);
96         Enumeration JavaDoc params = multi.getParameterNames();
97         while (params.hasMoreElements()) {
98             String JavaDoc name = (String JavaDoc) params.nextElement();
99             String JavaDoc value = multi.getParameter(name);
100             form.addParameter(name, value);
101             String JavaDoc[] s = multi.getParameterValues(name);
102             if (s != null) {
103                 form.addparameterValues(name, s);
104             }
105         }
106         Enumeration JavaDoc files = multi.getFileNames();
107         while (files.hasMoreElements()) {
108             String JavaDoc name = (String JavaDoc) files.nextElement();
109             form.addDocument(name, multi.getFilesystemName(name), multi.getContentType(name), multi.getFile(name));
110         }
111         request.setAttribute(MultipartForm.REQUEST_ATTRIBUTE_NAME, form);
112     }
113 }
114
Popular Tags