1 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 ; 19 import java.util.Enumeration ; 20 21 import javax.servlet.Filter ; 22 import javax.servlet.FilterChain ; 23 import javax.servlet.FilterConfig ; 24 import javax.servlet.ServletException ; 25 import javax.servlet.ServletRequest ; 26 import javax.servlet.ServletResponse ; 27 import javax.servlet.http.HttpServletRequest ; 28 29 import org.apache.commons.lang.StringUtils; 30 31 import com.oreilly.servlet.MultipartRequest; 32 33 34 38 public class MultipartRequestFilter implements Filter { 39 40 43 private static final int MAX_FILE_SIZE = 2000000000; 45 48 public void init(FilterConfig filterConfig) throws ServletException { 49 } 51 52 55 public void destroy() { 56 } 58 59 62 public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException , 63 ServletException { 64 HttpServletRequest request = (HttpServletRequest ) req; 65 String type = null; 66 String type1 = request.getHeader("Content-Type"); String 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")) { parseParameters(request); 79 } 80 filterChain.doFilter(req, res); 81 } 82 83 87 private static void parseParameters(HttpServletRequest request) throws IOException { 88 MultipartForm form = new MultipartForm(); 89 String encoding = StringUtils.defaultString(request.getCharacterEncoding(), "UTF-8"); MultipartRequest multi = new MultipartRequest( 91 request, 92 Path.getTempDirectoryPath(), 93 MAX_FILE_SIZE, 94 encoding, 95 null); 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 String [] s = multi.getParameterValues(name); 102 if (s != null) { 103 form.addparameterValues(name, s); 104 } 105 } 106 Enumeration files = multi.getFileNames(); 107 while (files.hasMoreElements()) { 108 String name = (String ) 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 |