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.Collections;
020: import java.util.Enumeration;
021: import java.util.HashMap;
022: import java.util.HashSet;
023: import java.util.Iterator;
024: import java.util.Map;
025: import java.util.Set;
026:
027: import javax.portlet.ActionRequest;
028:
029: import org.springframework.web.multipart.MultipartFile;
030: import org.springframework.web.portlet.util.ActionRequestWrapper;
031:
032: /**
033: * Default implementation of the MultipartActionRequest interface.
034: * Provides management of pre-generated parameter values.
035: *
036: * @author Juergen Hoeller
037: * @since 2.0
038: * @see PortletMultipartResolver
039: */
040: public class DefaultMultipartActionRequest extends ActionRequestWrapper
041: implements MultipartActionRequest {
042:
043: private Map multipartFiles;
044:
045: private final Map multipartParameters;
046:
047: /**
048: * Wrap the given Portlet ActionRequest in a MultipartActionRequest.
049: * @param request the request to wrap
050: * @param multipartFiles a map of the multipart files
051: * @param multipartParameters a map of the parameters to expose,
052: * with Strings as keys and String arrays as values
053: */
054: public DefaultMultipartActionRequest(ActionRequest request,
055: Map multipartFiles, Map multipartParameters) {
056: super (request);
057: this .multipartFiles = Collections
058: .unmodifiableMap(multipartFiles);
059: this .multipartParameters = multipartParameters;
060: }
061:
062: public Iterator getFileNames() {
063: return this .multipartFiles.keySet().iterator();
064: }
065:
066: public MultipartFile getFile(String name) {
067: return (MultipartFile) this .multipartFiles.get(name);
068: }
069:
070: public Map getFileMap() {
071: return this .multipartFiles;
072: }
073:
074: public Enumeration getParameterNames() {
075: Set paramNames = new HashSet();
076: Enumeration paramEnum = super .getParameterNames();
077: while (paramEnum.hasMoreElements()) {
078: paramNames.add(paramEnum.nextElement());
079: }
080: paramNames.addAll(this .multipartParameters.keySet());
081: return Collections.enumeration(paramNames);
082: }
083:
084: public String getParameter(String name) {
085: String[] values = (String[]) this .multipartParameters.get(name);
086: if (values != null) {
087: return (values.length > 0 ? values[0] : null);
088: }
089: return super .getParameter(name);
090: }
091:
092: public String[] getParameterValues(String name) {
093: String[] values = (String[]) this .multipartParameters.get(name);
094: if (values != null) {
095: return values;
096: }
097: return super .getParameterValues(name);
098: }
099:
100: public Map getParameterMap() {
101: Map paramMap = new HashMap();
102: paramMap.putAll(super.getParameterMap());
103: paramMap.putAll(this.multipartParameters);
104: return paramMap;
105: }
106:
107: }
|