KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > multipart > commons > CommonsMultipartResolver


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.multipart.commons;
18
19 import java.util.List JavaDoc;
20
21 import javax.servlet.ServletContext JavaDoc;
22 import javax.servlet.http.HttpServletRequest JavaDoc;
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.servlet.ServletFileUpload;
29 import org.apache.commons.fileupload.servlet.ServletRequestContext;
30
31 import org.springframework.web.context.ServletContextAware;
32 import org.springframework.web.multipart.MaxUploadSizeExceededException;
33 import org.springframework.web.multipart.MultipartException;
34 import org.springframework.web.multipart.MultipartHttpServletRequest;
35 import org.springframework.web.multipart.MultipartResolver;
36 import org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest;
37 import org.springframework.web.util.WebUtils;
38
39 /**
40  * Servlet-based MultipartResolver implementation for
41  * <a HREF="http://jakarta.apache.org/commons/fileupload">Jakarta Commons FileUpload</a>
42  * 1.1 or higher.
43  *
44  * <p>Provides maxUploadSize, maxInMemorySize, and defaultEncoding settings as
45  * bean properties (inherited from CommonsFileUploadSupport). See respective
46  * ServletFileUpload / DiskFileItemFactory properties (sizeMax, sizeThreshold,
47  * headerEncoding) for details in terms of defaults and accepted values.
48  *
49  * <p>Saves temporary files to the servlet container's temporary directory.
50  * Needs to be initialized <i>either</i> by an application context <i>or</i>
51  * via the constructor that takes a ServletContext (for standalone usage).
52  *
53  * <p><b>NOTE:</b> As of Spring 2.0, this multipart resolver requires
54  * Commons FileUpload 1.1 or higher. The implementation does not use
55  * any deprecated FileUpload 1.0 API anymore, to be compatible with future
56  * Commons FileUpload releases.
57  *
58  * @author Trevor D. Cook
59  * @author Juergen Hoeller
60  * @since 29.09.2003
61  * @see #CommonsMultipartResolver(ServletContext)
62  * @see CommonsMultipartFile
63  * @see org.springframework.web.portlet.multipart.PortletMultipartResolver
64  * @see org.apache.commons.fileupload.servlet.ServletFileUpload
65  * @see org.apache.commons.fileupload.disk.DiskFileItemFactory
66  */

67 public class CommonsMultipartResolver extends CommonsFileUploadSupport
68         implements MultipartResolver, ServletContextAware {
69
70     /**
71      * Constructor for use as bean. Determines the servlet container's
72      * temporary directory via the ServletContext passed in as through the
73      * ServletContextAware interface (typically by a WebApplicationContext).
74      * @see #setServletContext
75      * @see org.springframework.web.context.ServletContextAware
76      * @see org.springframework.web.context.WebApplicationContext
77      */

78     public CommonsMultipartResolver() {
79         super();
80     }
81
82     /**
83      * Constructor for standalone usage. Determines the servlet container's
84      * temporary directory via the given ServletContext.
85      * @param servletContext the ServletContext to use
86      */

87     public CommonsMultipartResolver(ServletContext JavaDoc servletContext) {
88         this();
89         setServletContext(servletContext);
90     }
91
92     /**
93      * Initialize the underlying <code>org.apache.commons.fileupload.servlet.ServletFileUpload</code>
94      * instance. Can be overridden to use a custom subclass, e.g. for testing purposes.
95      * @param fileItemFactory the Commons FileItemFactory to use
96      * @return the new ServletFileUpload instance
97      */

98     protected FileUpload newFileUpload(FileItemFactory fileItemFactory) {
99         return new ServletFileUpload(fileItemFactory);
100     }
101
102     public void setServletContext(ServletContext JavaDoc servletContext) {
103         if (!isUploadTempDirSpecified()) {
104             getFileItemFactory().setRepository(WebUtils.getTempDir(servletContext));
105         }
106     }
107
108
109     public boolean isMultipart(HttpServletRequest JavaDoc request) {
110         return ServletFileUpload.isMultipartContent(new ServletRequestContext(request));
111     }
112
113     public MultipartHttpServletRequest resolveMultipart(HttpServletRequest JavaDoc request) throws MultipartException {
114         String JavaDoc encoding = determineEncoding(request);
115         FileUpload fileUpload = prepareFileUpload(encoding);
116         try {
117             List JavaDoc fileItems = ((ServletFileUpload) fileUpload).parseRequest(request);
118             MultipartParsingResult parsingResult = parseFileItems(fileItems, encoding);
119             return new DefaultMultipartHttpServletRequest(
120                     request, parsingResult.getMultipartFiles(), parsingResult.getMultipartParameters());
121         }
122         catch (FileUploadBase.SizeLimitExceededException ex) {
123             throw new MaxUploadSizeExceededException(fileUpload.getSizeMax(), ex);
124         }
125         catch (FileUploadException ex) {
126             throw new MultipartException("Could not parse multipart servlet request", ex);
127         }
128     }
129
130     /**
131      * Determine the encoding for the given request.
132      * Can be overridden in subclasses.
133      * <p>The default implementation checks the request encoding,
134      * falling back to the default encoding specified for this resolver.
135      * @param request current HTTP request
136      * @return the encoding for the request (never <code>null</code>)
137      * @see javax.servlet.ServletRequest#getCharacterEncoding
138      * @see #setDefaultEncoding
139      */

140     protected String JavaDoc determineEncoding(HttpServletRequest JavaDoc request) {
141         String JavaDoc encoding = request.getCharacterEncoding();
142         if (encoding == null) {
143             encoding = getDefaultEncoding();
144         }
145         return encoding;
146     }
147
148     public void cleanupMultipart(MultipartHttpServletRequest request) {
149         cleanupFileItems(request.getFileMap().values());
150     }
151
152 }
153
Popular Tags