Source code: com/obinary/cms/admin/Filters/MultipartRequestFilter.java
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-2003 obinary Ltd. (http://www.obinary.com) All rights reserved.
11 *
12 * */
13
14
15
16
17 package com.obinary.cms.admin.Filters;
18
19
20 import com.oreilly.servlet.MultipartRequest;
21 import com.obinary.cms.beans.MultipartForm;
22 import com.obinary.cms.core.PathUtil;
23
24 import javax.servlet.*;
25 import javax.servlet.http.HttpServletRequest;
26 import java.io.IOException;
27 import java.util.Enumeration;
28
29
30 /**
31 * User: sameercharles
32 * Date: Apr 28, 2003
33 * Time: 11:20:59 AM
34 * @author Sameer Charles
35 * @version 1.0
36 */
37
38
39 public class MultipartRequestFilter extends BaseEncodeFilter {
40
41
42 private static final int MAX_FILE_SIZE = 50 * 1024 * 1024;
43 private static final String TMP_DIRECTORY = PathUtil.getTempDirectoryPath();
44
45
46
47 /**
48 * default constructor
49 */
50 public MultipartRequestFilter() {}
51
52
53
54 /**
55 *
56 *
57 */
58 public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain)
59 throws IOException, javax.servlet.ServletException {
60 HttpServletRequest request = (HttpServletRequest)req;
61 String type = null;
62 String type1 = request.getHeader("Content-Type");
63 String type2 = request.getContentType();
64 if (type1 == null && type2 != null) {
65 type = type2;
66 }
67 else if (type2 == null && type1 != null) {
68 type = type1;
69 }
70 else if (type1 != null && type2 != null) {
71 type = (type1.length() > type2.length() ? type1 : type2);
72 }
73
74 if ((type!=null) && type.toLowerCase().startsWith("multipart/form-data")) {
75 parseParameters(request);
76 }
77 filterChain.doFilter(req,res);
78 }
79
80
81
82 /**
83 * <p>adds all request paramaters as request attribultes</p>
84 *
85 *
86 * @param req , HttpServletRequest
87 */
88 private static void parseParameters(ServletRequest req) throws IOException {
89
90 MultipartForm form = new MultipartForm();
91
92 MultipartRequest multi = new
93 MultipartRequest(req,
94 TMP_DIRECTORY,
95 MAX_FILE_SIZE);
96 Enumeration params = multi.getParameterNames();
97 while (params.hasMoreElements()) {
98 String name = (String)params.nextElement();
99 String value = multi.getParameter(name);
100 form.addParameter(name, value);
101 }
102
103 Enumeration files = multi.getFileNames();
104 while (files.hasMoreElements()) {
105 String name = (String)files.nextElement();
106 form.addDocument(name,
107 multi.getFilesystemName(name),
108 multi.getContentType(name),
109 multi.getFile(name));
110 }
111
112 req.setAttribute("multipartform",form);
113
114 }
115
116
117
118
119
120
121 }