01: /*
02: * Copyright 2004-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License. You may obtain a copy of
06: * the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13: * License for the specific language governing permissions and limitations under
14: * the License.
15: */
16: package org.springframework.webflow.context.servlet;
17:
18: import java.util.Iterator;
19:
20: import javax.servlet.http.HttpServletRequest;
21:
22: import org.springframework.binding.collection.CompositeIterator;
23: import org.springframework.binding.collection.StringKeyedMapAdapter;
24: import org.springframework.web.multipart.MultipartHttpServletRequest;
25: import org.springframework.webflow.core.collection.CollectionUtils;
26:
27: /**
28: * Map backed by the Servlet HTTP request parameter map for accessing request
29: * parameters. Also provides support for multi-part requests, providing
30: * transparent access to the request "fileMap" as a request parameter entry.
31: *
32: * @author Keith Donald
33: */
34: public class HttpServletRequestParameterMap extends
35: StringKeyedMapAdapter {
36:
37: /**
38: * The wrapped HTTP request.
39: */
40: private HttpServletRequest request;
41:
42: /**
43: * Create a new map wrapping the parameters of given request.
44: */
45: public HttpServletRequestParameterMap(HttpServletRequest request) {
46: this .request = request;
47: }
48:
49: protected Object getAttribute(String key) {
50: if (request instanceof MultipartHttpServletRequest) {
51: MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
52: Object data = multipartRequest.getFileMap().get(key);
53: if (data != null) {
54: return data;
55: }
56: }
57: String[] parameters = request.getParameterValues(key);
58: if (parameters == null) {
59: return null;
60: } else if (parameters.length == 1) {
61: return parameters[0];
62: } else {
63: return parameters;
64: }
65: }
66:
67: protected void setAttribute(String key, Object value) {
68: throw new UnsupportedOperationException(
69: "HttpServletRequest parameter maps are immutable");
70: }
71:
72: protected void removeAttribute(String key) {
73: throw new UnsupportedOperationException(
74: "HttpServletRequest parameter maps are immutable");
75: }
76:
77: protected Iterator getAttributeNames() {
78: if (request instanceof MultipartHttpServletRequest) {
79: MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
80: CompositeIterator iterator = new CompositeIterator();
81: iterator.add(multipartRequest.getFileMap().keySet()
82: .iterator());
83: iterator.add(CollectionUtils.toIterator(request
84: .getParameterNames()));
85: return iterator;
86: } else {
87: return CollectionUtils.toIterator(request
88: .getParameterNames());
89: }
90: }
91: }
|