01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of 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,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.springframework.web.multipart.support;
18:
19: import java.util.Collections;
20: import java.util.Iterator;
21: import java.util.Map;
22:
23: import javax.servlet.http.HttpServletRequest;
24: import javax.servlet.http.HttpServletRequestWrapper;
25:
26: import org.springframework.web.multipart.MultipartFile;
27: import org.springframework.web.multipart.MultipartHttpServletRequest;
28:
29: /**
30: * Abstract base implementation of the MultipartHttpServletRequest interface.
31: * Provides management of pre-generated MultipartFile instances.
32: *
33: * @author Juergen Hoeller
34: * @since 06.10.2003
35: */
36: public abstract class AbstractMultipartHttpServletRequest extends
37: HttpServletRequestWrapper implements
38: MultipartHttpServletRequest {
39:
40: private Map multipartFiles;
41:
42: /**
43: * Wrap the given HttpServletRequest in a MultipartHttpServletRequest.
44: * @param request the request to wrap
45: */
46: protected AbstractMultipartHttpServletRequest(
47: HttpServletRequest request) {
48: super (request);
49: }
50:
51: public Iterator getFileNames() {
52: return getMultipartFiles().keySet().iterator();
53: }
54:
55: public MultipartFile getFile(String name) {
56: return (MultipartFile) getMultipartFiles().get(name);
57: }
58:
59: public Map getFileMap() {
60: return getMultipartFiles();
61: }
62:
63: /**
64: * Set a Map with parameter names as keys and MultipartFile objects as values.
65: * To be invoked by subclasses on initialization.
66: */
67: protected final void setMultipartFiles(Map multipartFiles) {
68: this .multipartFiles = Collections
69: .unmodifiableMap(multipartFiles);
70: }
71:
72: /**
73: * Obtain the MultipartFile Map for retrieval,
74: * lazily initializing it if necessary.
75: * @see #initializeMultipart()
76: */
77: protected Map getMultipartFiles() {
78: if (this .multipartFiles == null) {
79: initializeMultipart();
80: }
81: return this .multipartFiles;
82: }
83:
84: /**
85: * Lazily initialize the multipart request, if possible.
86: * Only called if not already eagerly initialized.
87: */
88: protected void initializeMultipart() {
89: throw new IllegalStateException(
90: "Multipart request not initialized");
91: }
92:
93: }
|