001: /*
002: * Copyright 2002-2007 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.support;
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.Map;
024: import java.util.Set;
025:
026: import javax.servlet.http.HttpServletRequest;
027:
028: /**
029: * Default implementation of the
030: * {@link org.springframework.web.multipart.MultipartHttpServletRequest}
031: * interface. Provides management of pre-generated parameter values.
032: *
033: * @author Trevor D. Cook
034: * @author Juergen Hoeller
035: * @since 29.09.2003
036: * @see org.springframework.web.multipart.MultipartResolver
037: */
038: public class DefaultMultipartHttpServletRequest extends
039: AbstractMultipartHttpServletRequest {
040:
041: private Map multipartParameters;
042:
043: /**
044: * Wrap the given HttpServletRequest in a MultipartHttpServletRequest.
045: * @param request the servlet request to wrap
046: * @param multipartFiles a map of the multipart files
047: * @param multipartParameters a map of the parameters to expose,
048: * with Strings as keys and String arrays as values
049: */
050: public DefaultMultipartHttpServletRequest(
051: HttpServletRequest request, Map multipartFiles,
052: Map multipartParameters) {
053:
054: super (request);
055: setMultipartFiles(multipartFiles);
056: setMultipartParameters(multipartParameters);
057: }
058:
059: /**
060: * Wrap the given HttpServletRequest in a MultipartHttpServletRequest.
061: * @param request the servlet request to wrap
062: */
063: public DefaultMultipartHttpServletRequest(HttpServletRequest request) {
064: super (request);
065: }
066:
067: public Enumeration getParameterNames() {
068: Set paramNames = new HashSet();
069: Enumeration paramEnum = super .getParameterNames();
070: while (paramEnum.hasMoreElements()) {
071: paramNames.add(paramEnum.nextElement());
072: }
073: paramNames.addAll(getMultipartParameters().keySet());
074: return Collections.enumeration(paramNames);
075: }
076:
077: public String getParameter(String name) {
078: String[] values = (String[]) getMultipartParameters().get(name);
079: if (values != null) {
080: return (values.length > 0 ? values[0] : null);
081: }
082: return super .getParameter(name);
083: }
084:
085: public String[] getParameterValues(String name) {
086: String[] values = (String[]) getMultipartParameters().get(name);
087: if (values != null) {
088: return values;
089: }
090: return super .getParameterValues(name);
091: }
092:
093: public Map getParameterMap() {
094: Map paramMap = new HashMap();
095: paramMap.putAll(super .getParameterMap());
096: paramMap.putAll(getMultipartParameters());
097: return paramMap;
098: }
099:
100: /**
101: * Set a Map with parameter names as keys and String array objects as values.
102: * To be invoked by subclasses on initialization.
103: */
104: protected final void setMultipartParameters(Map multipartParameters) {
105: this .multipartParameters = multipartParameters;
106: }
107:
108: /**
109: * Obtain the multipart parameter Map for retrieval,
110: * lazily initializing it if necessary.
111: * @see #initializeMultipart()
112: */
113: protected Map getMultipartParameters() {
114: if (this.multipartParameters == null) {
115: initializeMultipart();
116: }
117: return this.multipartParameters;
118: }
119:
120: }
|