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.File ; 19 import java.io.IOException ; 20 import java.io.UnsupportedEncodingException ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 24 import javax.servlet.Filter ; 25 import javax.servlet.FilterChain ; 26 import javax.servlet.FilterConfig ; 27 import javax.servlet.ServletException ; 28 import javax.servlet.ServletRequest ; 29 import javax.servlet.ServletResponse ; 30 import javax.servlet.http.HttpServletRequest ; 31 32 import org.apache.commons.fileupload.FileItem; 33 import org.apache.commons.fileupload.FileUploadBase; 34 import org.apache.commons.fileupload.disk.DiskFileItemFactory; 35 import org.apache.commons.fileupload.servlet.ServletFileUpload; 36 import org.apache.commons.fileupload.servlet.ServletRequestContext; 37 import org.apache.commons.lang.StringUtils; 38 39 40 46 public class CommonsFileUploadMultipartRequestFilter implements Filter { 47 48 51 private static final int DEFAULT_MAX_FILE_SIZE = 209715200; 52 53 56 private static final String PARAM_MAX_FILE_SIZE = "maxFileSize"; 57 58 61 private int maxFileSize = DEFAULT_MAX_FILE_SIZE; 62 63 66 private File tempDir; 67 68 71 public void init(FilterConfig config) throws ServletException { 72 String maxFileSize = config.getInitParameter(PARAM_MAX_FILE_SIZE); 73 if (maxFileSize != null) { 74 this.maxFileSize = Integer.parseInt(maxFileSize); 75 } 76 77 this.tempDir = new File (Path.getTempDirectoryPath()); 78 } 79 80 83 public void destroy() { 84 } 86 87 93 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException , 94 ServletException { 95 if (request instanceof HttpServletRequest ) { 96 97 boolean isMultipartContent = FileUploadBase.isMultipartContent(new ServletRequestContext( 98 (HttpServletRequest ) request)); 99 if (isMultipartContent) { 100 try { 101 parseRequest((HttpServletRequest ) request); 102 } 103 catch (IOException e) { 104 throw e; 105 } 106 catch (Exception e) { 107 throw new ServletException (e); 108 } 109 } 110 } 111 112 chain.doFilter(request, response); 113 } 114 115 118 private void parseRequest(HttpServletRequest request) throws Exception { 119 MultipartForm form = new MultipartForm(); 120 121 ServletFileUpload upload = newServletFileUpload(); 122 123 List fileItems = upload.parseRequest(request); 124 125 for (Iterator fileItemIterator = fileItems.iterator(); fileItemIterator.hasNext();) { 126 FileItem item = (FileItem) fileItemIterator.next(); 127 if (item.isFormField()) { 128 addField(request, item, form); 129 } 130 else { 131 addFile(item, form); 132 } 133 } 134 135 request.setAttribute(MultipartForm.REQUEST_ATTRIBUTE_NAME, form); 136 } 137 138 141 private ServletFileUpload newServletFileUpload() { 142 ServletFileUpload upload = new ServletFileUpload(); 143 upload.setSizeMax(this.maxFileSize); 144 DiskFileItemFactory fif = new DiskFileItemFactory(); 145 fif.setRepository(Path.getTempDirectory()); 146 upload.setFileItemFactory(fif); 147 148 return upload; 149 } 150 151 154 private void addField(HttpServletRequest request, FileItem item, MultipartForm form) { 155 String name = item.getFieldName(); 156 157 String value; 158 try { 159 String encoding = StringUtils.defaultString(request.getCharacterEncoding(), "UTF-8"); 160 value = item.getString(encoding); 161 } 162 catch (UnsupportedEncodingException ex) { 163 value = item.getString(); 164 } 165 form.addParameter(name, value); 166 167 String [] values = request.getParameterValues(name); 168 if (values != null) { 169 form.addparameterValues(name, values); 170 } 171 } 172 173 176 private void addFile(FileItem item, MultipartForm form) throws Exception { 177 String atomName = item.getFieldName(); 178 String fileName = item.getName(); 179 String type = item.getContentType(); 180 File file = File.createTempFile(atomName, null, this.tempDir); 181 item.write(file); 182 183 form.addDocument(atomName, fileName, type, file); 184 } 185 } 186 | Popular Tags |