001/*
002 * Copyright 2002-2017 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 org.springframework.util.Assert;
020import org.springframework.web.bind.support.WebArgumentResolver;
021import org.springframework.web.context.request.NativeWebRequest;
022import org.springframework.web.context.request.RequestAttributes;
023import org.springframework.web.context.request.RequestContextHolder;
024import org.springframework.web.context.request.ServletRequestAttributes;
025import org.springframework.web.context.request.ServletWebRequest;
026import org.springframework.web.method.annotation.AbstractWebArgumentResolverAdapter;
027
028/**
029 * A Servlet-specific
030 * {@link org.springframework.web.method.annotation.AbstractWebArgumentResolverAdapter}
031 * that creates a {@link NativeWebRequest} from {@link ServletRequestAttributes}.
032 *
033 * <p><strong>Note:</strong> This class is provided for backwards compatibility.
034 * However it is recommended to re-write a {@code WebArgumentResolver} as
035 * {@code HandlerMethodArgumentResolver}. For more details see javadoc of
036 * {@link org.springframework.web.method.annotation.AbstractWebArgumentResolverAdapter}.
037 *
038 * @author Rossen Stoyanchev
039 * @since 3.1
040 */
041public class ServletWebArgumentResolverAdapter extends AbstractWebArgumentResolverAdapter {
042
043        public ServletWebArgumentResolverAdapter(WebArgumentResolver adaptee) {
044                super(adaptee);
045        }
046
047        @Override
048        protected NativeWebRequest getWebRequest() {
049                RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
050                Assert.state(requestAttributes instanceof ServletRequestAttributes, "No ServletRequestAttributes");
051                ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
052                return new ServletWebRequest(servletRequestAttributes.getRequest());
053        }
054}