001/*
002 * Copyright 2002-2018 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 *      https://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
017package org.springframework.web.multipart.support;
018
019import java.util.Collections;
020import java.util.Enumeration;
021import java.util.Iterator;
022import java.util.List;
023import java.util.Map;
024import javax.servlet.http.HttpServletRequest;
025import javax.servlet.http.HttpServletRequestWrapper;
026
027import org.springframework.http.HttpHeaders;
028import org.springframework.http.HttpMethod;
029import org.springframework.util.LinkedMultiValueMap;
030import org.springframework.util.MultiValueMap;
031import org.springframework.web.multipart.MultipartFile;
032import org.springframework.web.multipart.MultipartHttpServletRequest;
033
034/**
035 * Abstract base implementation of the MultipartHttpServletRequest interface.
036 * Provides management of pre-generated MultipartFile instances.
037 *
038 * @author Juergen Hoeller
039 * @author Arjen Poutsma
040 * @since 06.10.2003
041 */
042public abstract class AbstractMultipartHttpServletRequest extends HttpServletRequestWrapper
043                implements MultipartHttpServletRequest {
044
045        private MultiValueMap<String, MultipartFile> multipartFiles;
046
047
048        /**
049         * Wrap the given HttpServletRequest in a MultipartHttpServletRequest.
050         * @param request the request to wrap
051         */
052        protected AbstractMultipartHttpServletRequest(HttpServletRequest request) {
053                super(request);
054        }
055
056
057        @Override
058        public HttpServletRequest getRequest() {
059                return (HttpServletRequest) super.getRequest();
060        }
061
062        @Override
063        public HttpMethod getRequestMethod() {
064                return HttpMethod.resolve(getRequest().getMethod());
065        }
066
067        @Override
068        public HttpHeaders getRequestHeaders() {
069                HttpHeaders headers = new HttpHeaders();
070                Enumeration<String> headerNames = getHeaderNames();
071                while (headerNames.hasMoreElements()) {
072                        String headerName = headerNames.nextElement();
073                        headers.put(headerName, Collections.list(getHeaders(headerName)));
074                }
075                return headers;
076        }
077
078        @Override
079        public Iterator<String> getFileNames() {
080                return getMultipartFiles().keySet().iterator();
081        }
082
083        @Override
084        public MultipartFile getFile(String name) {
085                return getMultipartFiles().getFirst(name);
086        }
087
088        @Override
089        public List<MultipartFile> getFiles(String name) {
090                List<MultipartFile> multipartFiles = getMultipartFiles().get(name);
091                if (multipartFiles != null) {
092                        return multipartFiles;
093                }
094                else {
095                        return Collections.emptyList();
096                }
097        }
098
099        @Override
100        public Map<String, MultipartFile> getFileMap() {
101                return getMultipartFiles().toSingleValueMap();
102        }
103
104        @Override
105        public MultiValueMap<String, MultipartFile> getMultiFileMap() {
106                return getMultipartFiles();
107        }
108
109        /**
110         * Determine whether the underlying multipart request has been resolved.
111         * @return {@code true} when eagerly initialized or lazily triggered,
112         * {@code false} in case of a lazy-resolution request that got aborted
113         * before any parameters or multipart files have been accessed
114         * @since 4.3.15
115         * @see #getMultipartFiles()
116         */
117        public boolean isResolved() {
118                return (this.multipartFiles != null);
119        }
120
121
122        /**
123         * Set a Map with parameter names as keys and list of MultipartFile objects as values.
124         * To be invoked by subclasses on initialization.
125         */
126        protected final void setMultipartFiles(MultiValueMap<String, MultipartFile> multipartFiles) {
127                this.multipartFiles =
128                                new LinkedMultiValueMap<String, MultipartFile>(Collections.unmodifiableMap(multipartFiles));
129        }
130
131        /**
132         * Obtain the MultipartFile Map for retrieval,
133         * lazily initializing it if necessary.
134         * @see #initializeMultipart()
135         */
136        protected MultiValueMap<String, MultipartFile> getMultipartFiles() {
137                if (this.multipartFiles == null) {
138                        initializeMultipart();
139                }
140                return this.multipartFiles;
141        }
142
143        /**
144         * Lazily initialize the multipart request, if possible.
145         * Only called if not already eagerly initialized.
146         */
147        protected void initializeMultipart() {
148                throw new IllegalStateException("Multipart request not initialized");
149        }
150
151}