1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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.FileSystemHelper;
39 import info.magnolia.context.MgnlContext;
40 import info.magnolia.objectfactory.Components;
41
42 import java.io.IOException;
43 import java.nio.file.Files;
44 import java.nio.file.Path;
45 import java.util.Enumeration;
46 import java.util.Iterator;
47 import java.util.Locale;
48
49 import javax.inject.Inject;
50 import javax.servlet.FilterChain;
51 import javax.servlet.ServletException;
52 import javax.servlet.http.HttpServletRequest;
53 import javax.servlet.http.HttpServletResponse;
54
55 import org.apache.commons.io.FileUtils;
56 import org.apache.commons.lang3.StringUtils;
57
58 import com.oreilly.servlet.MultipartRequest;
59
60
61
62
63 public class CosMultipartRequestFilter extends OncePerRequestAbstractMgnlFilter {
64
65
66
67
68 private static final int MAX_FILE_SIZE = 2000000000;
69 private static final String TEMP_SUBDIR_PREFIX = "multipart";
70
71 private final FileSystemHelper fileSystemHelper;
72
73 @Inject
74 public CosMultipartRequestFilter(FileSystemHelper fileSystemHelper) {
75 this.fileSystemHelper = fileSystemHelper;
76 }
77
78
79
80
81 @Deprecated
82 public CosMultipartRequestFilter() {
83 this(Components.getComponent(FileSystemHelper.class));
84 }
85
86 @Override
87 public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
88 throws IOException, ServletException {
89
90 String type = null;
91 String type1 = request.getHeader("Content-Type");
92 String type2 = request.getContentType();
93 if (type1 == null && type2 != null) {
94 type = type2;
95 } else if (type2 == null && type1 != null) {
96 type = type1;
97 } else if (type1 != null) {
98 type = (type1.length() > type2.length() ? type1 : type2);
99 }
100 boolean isMultipart = (type != null) && type.toLowerCase(Locale.ROOT).startsWith("multipart/form-data");
101 MultipartForm mpf = null;
102 Path tempDirPath = null;
103 try {
104 if (isMultipart) {
105 tempDirPath = Files.createTempDirectory(fileSystemHelper.getTempDirectory().toPath(), TEMP_SUBDIR_PREFIX).toAbsolutePath();
106 mpf = parseParameters(request, tempDirPath.toString());
107 request = new MultipartRequestWrapper(request, mpf);
108 MgnlContext.push(request, response);
109 }
110 try {
111 chain.doFilter(request, response);
112 } finally {
113 if (isMultipart) {
114 MgnlContext.pop();
115 }
116 }
117 } finally {
118 if (mpf != null) {
119 Iterator<Document> keys = mpf.getDocuments().values().iterator();
120 while (keys.hasNext()) {
121 Document doc = keys.next();
122 if (doc != null && doc.getFile() != null && doc.getFile().exists()) {
123 doc.delete();
124 }
125 }
126 }
127 if (tempDirPath != null) {
128 FileUtils.deleteQuietly(tempDirPath.toFile());
129 }
130 }
131 }
132
133
134
135
136
137
138 private MultipartForm parseParameters(HttpServletRequest request, String tempDirPath) throws IOException {
139 MultipartForm form = new MultipartForm();
140 String encoding = StringUtils.defaultString(request.getCharacterEncoding(), "UTF-8");
141 MultipartRequest multi = new MultipartRequest(
142 request,
143 tempDirPath,
144 MAX_FILE_SIZE,
145 encoding,
146 null);
147 Enumeration params = multi.getParameterNames();
148 while (params.hasMoreElements()) {
149 String name = (String) params.nextElement();
150 String value = multi.getParameter(name);
151 form.addParameter(name, value);
152 String[] s = multi.getParameterValues(name);
153 if (s != null) {
154 form.addparameterValues(name, s);
155 }
156 }
157 Enumeration files = multi.getFileNames();
158 while (files.hasMoreElements()) {
159 String name = (String) files.nextElement();
160 form.addDocument(name, multi.getFilesystemName(name), multi.getContentType(name), multi.getFile(name));
161 }
162 request.setAttribute(MultipartForm.REQUEST_ATTRIBUTE_NAME, form);
163 return form;
164 }
165 }