001: /*
002: * Copyright 2002-2005 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.web.multipart.commons;
018:
019: import java.util.List;
020:
021: import javax.servlet.ServletContext;
022: import javax.servlet.http.HttpServletRequest;
023:
024: import org.apache.commons.fileupload.FileItemFactory;
025: import org.apache.commons.fileupload.FileUpload;
026: import org.apache.commons.fileupload.FileUploadBase;
027: import org.apache.commons.fileupload.FileUploadException;
028: import org.apache.commons.fileupload.servlet.ServletFileUpload;
029: import org.apache.commons.fileupload.servlet.ServletRequestContext;
030:
031: import org.springframework.web.context.ServletContextAware;
032: import org.springframework.web.multipart.MaxUploadSizeExceededException;
033: import org.springframework.web.multipart.MultipartException;
034: import org.springframework.web.multipart.MultipartHttpServletRequest;
035: import org.springframework.web.multipart.MultipartResolver;
036: import org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest;
037: import org.springframework.web.util.WebUtils;
038:
039: /**
040: * Servlet-based MultipartResolver implementation for
041: * <a href="http://jakarta.apache.org/commons/fileupload">Jakarta Commons FileUpload</a>
042: * 1.1 or higher.
043: *
044: * <p>Provides maxUploadSize, maxInMemorySize, and defaultEncoding settings as
045: * bean properties (inherited from CommonsFileUploadSupport). See respective
046: * ServletFileUpload / DiskFileItemFactory properties (sizeMax, sizeThreshold,
047: * headerEncoding) for details in terms of defaults and accepted values.
048: *
049: * <p>Saves temporary files to the servlet container's temporary directory.
050: * Needs to be initialized <i>either</i> by an application context <i>or</i>
051: * via the constructor that takes a ServletContext (for standalone usage).
052: *
053: * <p><b>NOTE:</b> As of Spring 2.0, this multipart resolver requires
054: * Commons FileUpload 1.1 or higher. The implementation does not use
055: * any deprecated FileUpload 1.0 API anymore, to be compatible with future
056: * Commons FileUpload releases.
057: *
058: * @author Trevor D. Cook
059: * @author Juergen Hoeller
060: * @since 29.09.2003
061: * @see #CommonsMultipartResolver(ServletContext)
062: * @see CommonsMultipartFile
063: * @see org.springframework.web.portlet.multipart.PortletMultipartResolver
064: * @see org.apache.commons.fileupload.servlet.ServletFileUpload
065: * @see org.apache.commons.fileupload.disk.DiskFileItemFactory
066: */
067: public class CommonsMultipartResolver extends CommonsFileUploadSupport
068: implements MultipartResolver, ServletContextAware {
069:
070: /**
071: * Constructor for use as bean. Determines the servlet container's
072: * temporary directory via the ServletContext passed in as through the
073: * ServletContextAware interface (typically by a WebApplicationContext).
074: * @see #setServletContext
075: * @see org.springframework.web.context.ServletContextAware
076: * @see org.springframework.web.context.WebApplicationContext
077: */
078: public CommonsMultipartResolver() {
079: super ();
080: }
081:
082: /**
083: * Constructor for standalone usage. Determines the servlet container's
084: * temporary directory via the given ServletContext.
085: * @param servletContext the ServletContext to use
086: */
087: public CommonsMultipartResolver(ServletContext servletContext) {
088: this ();
089: setServletContext(servletContext);
090: }
091:
092: /**
093: * Initialize the underlying <code>org.apache.commons.fileupload.servlet.ServletFileUpload</code>
094: * instance. Can be overridden to use a custom subclass, e.g. for testing purposes.
095: * @param fileItemFactory the Commons FileItemFactory to use
096: * @return the new ServletFileUpload instance
097: */
098: protected FileUpload newFileUpload(FileItemFactory fileItemFactory) {
099: return new ServletFileUpload(fileItemFactory);
100: }
101:
102: public void setServletContext(ServletContext servletContext) {
103: if (!isUploadTempDirSpecified()) {
104: getFileItemFactory().setRepository(
105: WebUtils.getTempDir(servletContext));
106: }
107: }
108:
109: public boolean isMultipart(HttpServletRequest request) {
110: return ServletFileUpload
111: .isMultipartContent(new ServletRequestContext(request));
112: }
113:
114: public MultipartHttpServletRequest resolveMultipart(
115: HttpServletRequest request) throws MultipartException {
116: String encoding = determineEncoding(request);
117: FileUpload fileUpload = prepareFileUpload(encoding);
118: try {
119: List fileItems = ((ServletFileUpload) fileUpload)
120: .parseRequest(request);
121: MultipartParsingResult parsingResult = parseFileItems(
122: fileItems, encoding);
123: return new DefaultMultipartHttpServletRequest(request,
124: parsingResult.getMultipartFiles(), parsingResult
125: .getMultipartParameters());
126: } catch (FileUploadBase.SizeLimitExceededException ex) {
127: throw new MaxUploadSizeExceededException(fileUpload
128: .getSizeMax(), ex);
129: } catch (FileUploadException ex) {
130: throw new MultipartException(
131: "Could not parse multipart servlet request", ex);
132: }
133: }
134:
135: /**
136: * Determine the encoding for the given request.
137: * Can be overridden in subclasses.
138: * <p>The default implementation checks the request encoding,
139: * falling back to the default encoding specified for this resolver.
140: * @param request current HTTP request
141: * @return the encoding for the request (never <code>null</code>)
142: * @see javax.servlet.ServletRequest#getCharacterEncoding
143: * @see #setDefaultEncoding
144: */
145: protected String determineEncoding(HttpServletRequest request) {
146: String encoding = request.getCharacterEncoding();
147: if (encoding == null) {
148: encoding = getDefaultEncoding();
149: }
150: return encoding;
151: }
152:
153: public void cleanupMultipart(MultipartHttpServletRequest request) {
154: cleanupFileItems(request.getFileMap().values());
155: }
156:
157: }
|