1 16 17 package org.springframework.web.portlet.multipart; 18 19 import java.util.List ; 20 21 import javax.portlet.ActionRequest; 22 import javax.portlet.PortletContext; 23 24 import org.apache.commons.fileupload.FileItemFactory; 25 import org.apache.commons.fileupload.FileUpload; 26 import org.apache.commons.fileupload.FileUploadBase; 27 import org.apache.commons.fileupload.FileUploadException; 28 import org.apache.commons.fileupload.portlet.PortletFileUpload; 29 import org.apache.commons.fileupload.portlet.PortletRequestContext; 30 31 import org.springframework.web.multipart.MaxUploadSizeExceededException; 32 import org.springframework.web.multipart.MultipartException; 33 import org.springframework.web.multipart.commons.CommonsFileUploadSupport; 34 import org.springframework.web.portlet.context.PortletContextAware; 35 import org.springframework.web.portlet.util.PortletUtils; 36 37 59 public class CommonsPortletMultipartResolver extends CommonsFileUploadSupport 60 implements PortletMultipartResolver, PortletContextAware { 61 62 69 public CommonsPortletMultipartResolver() { 70 super(); 71 } 72 73 78 public CommonsPortletMultipartResolver(PortletContext portletContext) { 79 this(); 80 setPortletContext(portletContext); 81 } 82 83 88 protected FileUpload newFileUpload(FileItemFactory fileItemFactory) { 89 return new PortletFileUpload(fileItemFactory); 90 } 91 92 public void setPortletContext(PortletContext portletContext) { 93 if (!isUploadTempDirSpecified()) { 94 getFileItemFactory().setRepository(PortletUtils.getTempDir(portletContext)); 95 } 96 } 97 98 99 public boolean isMultipart(ActionRequest request) { 100 return PortletFileUpload.isMultipartContent(new PortletRequestContext(request)); 101 } 102 103 public MultipartActionRequest resolveMultipart(ActionRequest request) throws MultipartException { 104 String encoding = determineEncoding(request); 105 FileUpload fileUpload = prepareFileUpload(encoding); 106 try { 107 List fileItems = ((PortletFileUpload) fileUpload).parseRequest(request); 108 MultipartParsingResult parsingResult = parseFileItems(fileItems, encoding); 109 return new DefaultMultipartActionRequest( 110 request, parsingResult.getMultipartFiles(), parsingResult.getMultipartParameters()); 111 } 112 catch (FileUploadBase.SizeLimitExceededException ex) { 113 throw new MaxUploadSizeExceededException(fileUpload.getSizeMax(), ex); 114 } 115 catch (FileUploadException ex) { 116 throw new MultipartException("Could not parse multipart portlet request", ex); 117 } 118 } 119 120 130 protected String determineEncoding(ActionRequest request) { 131 String encoding = request.getCharacterEncoding(); 132 if (encoding == null) { 133 encoding = getDefaultEncoding(); 134 } 135 return encoding; 136 } 137 138 public void cleanupMultipart(MultipartActionRequest request) { 139 cleanupFileItems(request.getFileMap().values()); 140 } 141 142 } 143 | Popular Tags |