001/*
002 * Copyright 2002-2015 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;
018
019import javax.servlet.http.HttpServletRequest;
020
021/**
022 * A strategy interface for multipart file upload resolution in accordance
023 * with <a href="https://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>.
024 * Implementations are typically usable both within an application context
025 * and standalone.
026 *
027 * <p>There are two concrete implementations included in Spring, as of Spring 3.1:
028 * <ul>
029 * <li>{@link org.springframework.web.multipart.commons.CommonsMultipartResolver}
030 * for Apache Commons FileUpload
031 * <li>{@link org.springframework.web.multipart.support.StandardServletMultipartResolver}
032 * for the Servlet 3.0+ Part API
033 * </ul>
034 *
035 * <p>There is no default resolver implementation used for Spring
036 * {@link org.springframework.web.servlet.DispatcherServlet DispatcherServlets},
037 * as an application might choose to parse its multipart requests itself. To define
038 * an implementation, create a bean with the id "multipartResolver" in a
039 * {@link org.springframework.web.servlet.DispatcherServlet DispatcherServlet's}
040 * application context. Such a resolver gets applied to all requests handled
041 * by that {@link org.springframework.web.servlet.DispatcherServlet}.
042 *
043 * <p>If a {@link org.springframework.web.servlet.DispatcherServlet} detects a
044 * multipart request, it will resolve it via the configured {@link MultipartResolver}
045 * and pass on a wrapped {@link javax.servlet.http.HttpServletRequest}. Controllers
046 * can then cast their given request to the {@link MultipartHttpServletRequest}
047 * interface, which allows for access to any {@link MultipartFile MultipartFiles}.
048 * Note that this cast is only supported in case of an actual multipart request.
049 *
050 * <pre class="code">
051 * public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {
052 *   MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
053 *   MultipartFile multipartFile = multipartRequest.getFile("image");
054 *   ...
055 * }</pre>
056 *
057 * Instead of direct access, command or form controllers can register a
058 * {@link org.springframework.web.multipart.support.ByteArrayMultipartFileEditor}
059 * or {@link org.springframework.web.multipart.support.StringMultipartFileEditor}
060 * with their data binder, to automatically apply multipart content to form
061 * bean properties.
062 *
063 * <p>As an alternative to using a {@link MultipartResolver} with a
064 * {@link org.springframework.web.servlet.DispatcherServlet},
065 * a {@link org.springframework.web.multipart.support.MultipartFilter} can be
066 * registered in {@code web.xml}. It will delegate to a corresponding
067 * {@link MultipartResolver} bean in the root application context. This is mainly
068 * intended for applications that do not use Spring's own web MVC framework.
069 *
070 * <p>Note: There is hardly ever a need to access the {@link MultipartResolver}
071 * itself from application code. It will simply do its work behind the scenes,
072 * making {@link MultipartHttpServletRequest MultipartHttpServletRequests}
073 * available to controllers.
074 *
075 * @author Juergen Hoeller
076 * @author Trevor D. Cook
077 * @since 29.09.2003
078 * @see MultipartHttpServletRequest
079 * @see MultipartFile
080 * @see org.springframework.web.multipart.commons.CommonsMultipartResolver
081 * @see org.springframework.web.multipart.support.ByteArrayMultipartFileEditor
082 * @see org.springframework.web.multipart.support.StringMultipartFileEditor
083 * @see org.springframework.web.servlet.DispatcherServlet
084 */
085public interface MultipartResolver {
086
087        /**
088         * Determine if the given request contains multipart content.
089         * <p>Will typically check for content type "multipart/form-data", but the actually
090         * accepted requests might depend on the capabilities of the resolver implementation.
091         * @param request the servlet request to be evaluated
092         * @return whether the request contains multipart content
093         */
094        boolean isMultipart(HttpServletRequest request);
095
096        /**
097         * Parse the given HTTP request into multipart files and parameters,
098         * and wrap the request inside a
099         * {@link org.springframework.web.multipart.MultipartHttpServletRequest}
100         * object that provides access to file descriptors and makes contained
101         * parameters accessible via the standard ServletRequest methods.
102         * @param request the servlet request to wrap (must be of a multipart content type)
103         * @return the wrapped servlet request
104         * @throws MultipartException if the servlet request is not multipart, or if
105         * implementation-specific problems are encountered (such as exceeding file size limits)
106         * @see MultipartHttpServletRequest#getFile
107         * @see MultipartHttpServletRequest#getFileNames
108         * @see MultipartHttpServletRequest#getFileMap
109         * @see javax.servlet.http.HttpServletRequest#getParameter
110         * @see javax.servlet.http.HttpServletRequest#getParameterNames
111         * @see javax.servlet.http.HttpServletRequest#getParameterMap
112         */
113        MultipartHttpServletRequest resolveMultipart(HttpServletRequest request) throws MultipartException;
114
115        /**
116         * Cleanup any resources used for the multipart handling,
117         * like a storage for the uploaded files.
118         * @param request the request to cleanup resources for
119         */
120        void cleanupMultipart(MultipartHttpServletRequest request);
121
122}