KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > portlet > multipart > CommonsPortletMultipartResolver


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.portlet.multipart;
18
19 import java.util.List JavaDoc;
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 /**
38  * PortletMultipartResolver implementation for
39  * <a HREF="http://jakarta.apache.org/commons/fileupload">Jakarta Commons FileUpload</a>
40  * 1.1 or higher.
41  *
42  * <p>Provides maxUploadSize, maxInMemorySize, and defaultEncoding settings as
43  * bean properties (inherited from CommonsFileUploadSupport). See respective
44  * PortletFileUpload / DiskFileItemFactory properties (sizeMax, sizeThreshold,
45  * headerEncoding) for details in terms of defaults and accepted values.
46  *
47  * <p>Saves temporary files to the portlet container's temporary directory.
48  * Needs to be initialized <i>either</i> by an application context <i>or</i>
49  * via the constructor that takes a PortletContext (for standalone usage).
50  *
51  * @author Juergen Hoeller
52  * @since 2.0
53  * @see #CommonsPortletMultipartResolver(javax.portlet.PortletContext)
54  * @see org.springframework.web.multipart.commons.CommonsMultipartFile
55  * @see org.springframework.web.multipart.commons.CommonsMultipartResolver
56  * @see org.apache.commons.fileupload.portlet.PortletFileUpload
57  * @see org.apache.commons.fileupload.disk.DiskFileItemFactory
58  */

59 public class CommonsPortletMultipartResolver extends CommonsFileUploadSupport
60         implements PortletMultipartResolver, PortletContextAware {
61
62     /**
63      * Constructor for use as bean. Determines the portlet container's
64      * temporary directory via the PortletContext passed in as through the
65      * PortletContextAware interface (typically by an ApplicationContext).
66      * @see #setPortletContext
67      * @see org.springframework.web.portlet.context.PortletContextAware
68      */

69     public CommonsPortletMultipartResolver() {
70         super();
71     }
72
73     /**
74      * Constructor for standalone usage. Determines the portlet container's
75      * temporary directory via the given PortletContext.
76      * @param portletContext the PortletContext to use
77      */

78     public CommonsPortletMultipartResolver(PortletContext portletContext) {
79         this();
80         setPortletContext(portletContext);
81     }
82
83     /**
84      * Initialize the underlying <code>org.apache.commons.fileupload.portlet.PortletFileUpload</code>
85      * instance. Can be overridden to use a custom subclass, e.g. for testing purposes.
86      * @return the new PortletFileUpload instance
87      */

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 JavaDoc encoding = determineEncoding(request);
105         FileUpload fileUpload = prepareFileUpload(encoding);
106         try {
107             List JavaDoc 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     /**
121      * Determine the encoding for the given request.
122      * Can be overridden in subclasses.
123      * <p>The default implementation checks the request encoding,
124      * falling back to the default encoding specified for this resolver.
125      * @param request current portlet request
126      * @return the encoding for the request (never <code>null</code>)
127      * @see javax.portlet.ActionRequest#getCharacterEncoding
128      * @see #setDefaultEncoding
129      */

130     protected String JavaDoc determineEncoding(ActionRequest request) {
131         String JavaDoc 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