001/*
002 * Copyright 2002-2014 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.servlet.mvc.method.annotation;
018
019import java.io.OutputStream;
020import java.io.Writer;
021import java.lang.reflect.Method;
022import javax.servlet.ServletResponse;
023import javax.servlet.http.HttpServletResponse;
024
025import org.springframework.core.MethodParameter;
026import org.springframework.web.bind.support.WebDataBinderFactory;
027import org.springframework.web.context.request.NativeWebRequest;
028import org.springframework.web.method.support.HandlerMethodArgumentResolver;
029import org.springframework.web.method.support.ModelAndViewContainer;
030
031/**
032 * Resolves response-related method argument values of types:
033 * <ul>
034 * <li>{@link ServletResponse}
035 * <li>{@link OutputStream}
036 * <li>{@link Writer}
037 * </ul>
038 *
039 * @author Arjen Poutsma
040 * @author Rossen Stoyanchev
041 * @since 3.1
042 */
043public class ServletResponseMethodArgumentResolver implements HandlerMethodArgumentResolver {
044
045        @Override
046        public boolean supportsParameter(MethodParameter parameter) {
047                Class<?> paramType = parameter.getParameterType();
048                return (ServletResponse.class.isAssignableFrom(paramType) ||
049                                OutputStream.class.isAssignableFrom(paramType) ||
050                                Writer.class.isAssignableFrom(paramType));
051        }
052
053        /**
054         * Set {@link ModelAndViewContainer#setRequestHandled(boolean)} to
055         * {@code false} to indicate that the method signature provides access
056         * to the response. If subsequently the underlying method returns
057         * {@code null}, the request is considered directly handled.
058         */
059        @Override
060        public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
061                        NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
062
063                if (mavContainer != null) {
064                        mavContainer.setRequestHandled(true);
065                }
066
067                HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class);
068                Class<?> paramType = parameter.getParameterType();
069
070                if (ServletResponse.class.isAssignableFrom(paramType)) {
071                        Object nativeResponse = webRequest.getNativeResponse(paramType);
072                        if (nativeResponse == null) {
073                                throw new IllegalStateException(
074                                                "Current response is not of type [" + paramType.getName() + "]: " + response);
075                        }
076                        return nativeResponse;
077                }
078                else if (OutputStream.class.isAssignableFrom(paramType)) {
079                        return response.getOutputStream();
080                }
081                else if (Writer.class.isAssignableFrom(paramType)) {
082                        return response.getWriter();
083                }
084                else {
085                        // should not happen
086                        Method method = parameter.getMethod();
087                        throw new UnsupportedOperationException("Unknown parameter type: " + paramType + " in method: " + method);
088                }
089        }
090
091}