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.portlet.multipart;
018:
019: import java.util.List;
020:
021: import javax.portlet.ActionRequest;
022: import javax.portlet.PortletContext;
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.portlet.PortletFileUpload;
029: import org.apache.commons.fileupload.portlet.PortletRequestContext;
030:
031: import org.springframework.web.multipart.MaxUploadSizeExceededException;
032: import org.springframework.web.multipart.MultipartException;
033: import org.springframework.web.multipart.commons.CommonsFileUploadSupport;
034: import org.springframework.web.portlet.context.PortletContextAware;
035: import org.springframework.web.portlet.util.PortletUtils;
036:
037: /**
038: * PortletMultipartResolver implementation for
039: * <a href="http://jakarta.apache.org/commons/fileupload">Jakarta Commons FileUpload</a>
040: * 1.1 or higher.
041: *
042: * <p>Provides maxUploadSize, maxInMemorySize, and defaultEncoding settings as
043: * bean properties (inherited from CommonsFileUploadSupport). See respective
044: * PortletFileUpload / DiskFileItemFactory properties (sizeMax, sizeThreshold,
045: * headerEncoding) for details in terms of defaults and accepted values.
046: *
047: * <p>Saves temporary files to the portlet container's temporary directory.
048: * Needs to be initialized <i>either</i> by an application context <i>or</i>
049: * via the constructor that takes a PortletContext (for standalone usage).
050: *
051: * @author Juergen Hoeller
052: * @since 2.0
053: * @see #CommonsPortletMultipartResolver(javax.portlet.PortletContext)
054: * @see org.springframework.web.multipart.commons.CommonsMultipartFile
055: * @see org.springframework.web.multipart.commons.CommonsMultipartResolver
056: * @see org.apache.commons.fileupload.portlet.PortletFileUpload
057: * @see org.apache.commons.fileupload.disk.DiskFileItemFactory
058: */
059: public class CommonsPortletMultipartResolver extends
060: CommonsFileUploadSupport implements PortletMultipartResolver,
061: PortletContextAware {
062:
063: /**
064: * Constructor for use as bean. Determines the portlet container's
065: * temporary directory via the PortletContext passed in as through the
066: * PortletContextAware interface (typically by an ApplicationContext).
067: * @see #setPortletContext
068: * @see org.springframework.web.portlet.context.PortletContextAware
069: */
070: public CommonsPortletMultipartResolver() {
071: super ();
072: }
073:
074: /**
075: * Constructor for standalone usage. Determines the portlet container's
076: * temporary directory via the given PortletContext.
077: * @param portletContext the PortletContext to use
078: */
079: public CommonsPortletMultipartResolver(PortletContext portletContext) {
080: this ();
081: setPortletContext(portletContext);
082: }
083:
084: /**
085: * Initialize the underlying <code>org.apache.commons.fileupload.portlet.PortletFileUpload</code>
086: * instance. Can be overridden to use a custom subclass, e.g. for testing purposes.
087: * @return the new PortletFileUpload instance
088: */
089: protected FileUpload newFileUpload(FileItemFactory fileItemFactory) {
090: return new PortletFileUpload(fileItemFactory);
091: }
092:
093: public void setPortletContext(PortletContext portletContext) {
094: if (!isUploadTempDirSpecified()) {
095: getFileItemFactory().setRepository(
096: PortletUtils.getTempDir(portletContext));
097: }
098: }
099:
100: public boolean isMultipart(ActionRequest request) {
101: return PortletFileUpload
102: .isMultipartContent(new PortletRequestContext(request));
103: }
104:
105: public MultipartActionRequest resolveMultipart(ActionRequest request)
106: throws MultipartException {
107: String encoding = determineEncoding(request);
108: FileUpload fileUpload = prepareFileUpload(encoding);
109: try {
110: List fileItems = ((PortletFileUpload) fileUpload)
111: .parseRequest(request);
112: MultipartParsingResult parsingResult = parseFileItems(
113: fileItems, encoding);
114: return new DefaultMultipartActionRequest(request,
115: parsingResult.getMultipartFiles(), parsingResult
116: .getMultipartParameters());
117: } catch (FileUploadBase.SizeLimitExceededException ex) {
118: throw new MaxUploadSizeExceededException(fileUpload
119: .getSizeMax(), ex);
120: } catch (FileUploadException ex) {
121: throw new MultipartException(
122: "Could not parse multipart portlet request", ex);
123: }
124: }
125:
126: /**
127: * Determine the encoding for the given request.
128: * Can be overridden in subclasses.
129: * <p>The default implementation checks the request encoding,
130: * falling back to the default encoding specified for this resolver.
131: * @param request current portlet request
132: * @return the encoding for the request (never <code>null</code>)
133: * @see javax.portlet.ActionRequest#getCharacterEncoding
134: * @see #setDefaultEncoding
135: */
136: protected String determineEncoding(ActionRequest request) {
137: String encoding = request.getCharacterEncoding();
138: if (encoding == null) {
139: encoding = getDefaultEncoding();
140: }
141: return encoding;
142: }
143:
144: public void cleanupMultipart(MultipartActionRequest request) {
145: cleanupFileItems(request.getFileMap().values());
146: }
147:
148: }
|