001: /*
002: * $Id: MultipartRequestWrapper.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts.upload;
022:
023: import javax.servlet.http.HttpServletRequest;
024: import javax.servlet.http.HttpServletRequestWrapper;
025:
026: import java.util.Collection;
027: import java.util.Collections;
028: import java.util.Enumeration;
029: import java.util.HashMap;
030: import java.util.Iterator;
031: import java.util.Map;
032: import java.util.Vector;
033:
034: /**
035: * <p> This class functions as a wrapper around HttpServletRequest to provide
036: * working getParameter methods for multipart requests. </p>
037: */
038: public class MultipartRequestWrapper extends HttpServletRequestWrapper {
039: /**
040: * <p> The parameters for this multipart request </p>
041: */
042: protected Map parameters;
043:
044: public MultipartRequestWrapper(HttpServletRequest request) {
045: super (request);
046: this .parameters = new HashMap();
047: }
048:
049: /**
050: * <p> Sets a parameter for this request. The parameter is actually
051: * separate from the request parameters, but calling on the getParameter()
052: * methods of this class will work as if they weren't. </p>
053: */
054: public void setParameter(String name, String value) {
055: String[] mValue = (String[]) parameters.get(name);
056:
057: if (mValue == null) {
058: mValue = new String[0];
059: }
060:
061: String[] newValue = new String[mValue.length + 1];
062:
063: System.arraycopy(mValue, 0, newValue, 0, mValue.length);
064: newValue[mValue.length] = value;
065:
066: parameters.put(name, newValue);
067: }
068:
069: /**
070: * <p> Attempts to get a parameter for this request. It first looks in
071: * the underlying HttpServletRequest object for the parameter, and if that
072: * doesn't exist it looks for the parameters retrieved from the multipart
073: * request </p>
074: */
075: public String getParameter(String name) {
076: String value = getRequest().getParameter(name);
077:
078: if (value == null) {
079: String[] mValue = (String[]) parameters.get(name);
080:
081: if ((mValue != null) && (mValue.length > 0)) {
082: value = mValue[0];
083: }
084: }
085:
086: return value;
087: }
088:
089: /**
090: * <p> Returns the names of the parameters for this request. The
091: * enumeration consists of the normal request parameter names plus the
092: * parameters read from the multipart request </p>
093: */
094: public Enumeration getParameterNames() {
095: Enumeration baseParams = getRequest().getParameterNames();
096: Vector list = new Vector();
097:
098: while (baseParams.hasMoreElements()) {
099: list.add(baseParams.nextElement());
100: }
101:
102: Collection multipartParams = parameters.keySet();
103: Iterator iterator = multipartParams.iterator();
104:
105: while (iterator.hasNext()) {
106: list.add(iterator.next());
107: }
108:
109: return Collections.enumeration(list);
110: }
111:
112: /**
113: * <p> Returns the values of a parameter in this request. It first looks
114: * in the underlying HttpServletRequest object for the parameter, and if
115: * that doesn't exist it looks for the parameter retrieved from the
116: * multipart request. </p>
117: */
118: public String[] getParameterValues(String name) {
119: String[] value = getRequest().getParameterValues(name);
120:
121: if (value == null) {
122: value = (String[]) parameters.get(name);
123: }
124:
125: return value;
126: }
127:
128: /**
129: * <p> Combines the parameters stored here with those in the underlying
130: * request. If paramater values in the underlying request take precedence
131: * over those stored here. </p>
132: */
133: public Map getParameterMap() {
134: Map map = new HashMap(parameters);
135:
136: map.putAll(getRequest().getParameterMap());
137:
138: return map;
139: }
140: }
|