01: // Copyright 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // Unless required by applicable law or agreed to in writing, software
10: // distributed under the License is distributed on an "AS IS" BASIS,
11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: // See the License for the specific language governing permissions and
13: // limitations under the License.
14:
15: package org.apache.tapestry.upload.services;
16:
17: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newMap;
18:
19: import java.io.UnsupportedEncodingException;
20: import java.util.Collections;
21: import java.util.Enumeration;
22: import java.util.Map;
23:
24: import javax.servlet.http.HttpServletRequest;
25: import javax.servlet.http.HttpServletRequestWrapper;
26:
27: import org.apache.tapestry.services.Dispatcher;
28:
29: /**
30: * Wrapper for HttpServletRequest that overrides the parameter methods of the wrapped request. i.e.
31: * parameters are retreived from the wrapper rather than the real request.
32: */
33: public class ParametersServletRequestWrapper extends
34: HttpServletRequestWrapper {
35: private final Map<String, ParameterValue> _parameters = newMap();
36:
37: public ParametersServletRequestWrapper(
38: HttpServletRequest httpServletRequest) {
39: super (httpServletRequest);
40: }
41:
42: public String getParameter(String name) {
43: return getValueFor(name).single();
44: }
45:
46: public Map<String, Object> getParameterMap() {
47: Map<String, Object> paramMap = newMap();
48:
49: for (Map.Entry<String, ParameterValue> e : _parameters
50: .entrySet()) {
51: ParameterValue value = e.getValue();
52:
53: paramMap.put(e.getKey(), value.isMulti() ? value.multi()
54: : value.single());
55: }
56:
57: return paramMap;
58: }
59:
60: public Enumeration getParameterNames() {
61: return Collections.enumeration(_parameters.keySet());
62: }
63:
64: public String[] getParameterValues(String name) {
65: return getValueFor(name).multi();
66: }
67:
68: public void addParameter(String name, String value) {
69: ParameterValue pv = _parameters.get(name);
70: if (pv == null) {
71: pv = new ParameterValue(value);
72: _parameters.put(name, pv);
73: } else {
74: pv.add(value);
75: }
76: }
77:
78: ParameterValue getValueFor(String name) {
79: ParameterValue value = _parameters.get(name);
80:
81: return value == null ? ParameterValue.NULL : value;
82: }
83:
84: /**
85: * Ignores any attempt to set the character encoding. Tapestry attempts to set the encoding
86: * <em>after</em> the page name has been identified by the correct {@link Dispatcher}, and
87: * that's too late from the perspective of the Servlet API as HttpServlet.getInputStream() will
88: * already have been called.
89: */
90: @Override
91: public void setCharacterEncoding(String enc)
92: throws UnsupportedEncodingException {
93:
94: }
95: }
|