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