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.config.annotation;
018
019import org.springframework.context.ApplicationContext;
020import org.springframework.http.HttpStatus;
021import org.springframework.lang.Nullable;
022import org.springframework.util.Assert;
023import org.springframework.web.servlet.mvc.ParameterizableViewController;
024import org.springframework.web.servlet.view.RedirectView;
025
026/**
027 * Assist with the registration of a single redirect view controller.
028 *
029 * @author Rossen Stoyanchev
030 * @since 4.1
031 */
032public class RedirectViewControllerRegistration {
033
034        private final String urlPath;
035
036        private final RedirectView redirectView;
037
038        private final ParameterizableViewController controller = new ParameterizableViewController();
039
040
041        public RedirectViewControllerRegistration(String urlPath, String redirectUrl) {
042                Assert.notNull(urlPath, "'urlPath' is required.");
043                Assert.notNull(redirectUrl, "'redirectUrl' is required.");
044                this.urlPath = urlPath;
045                this.redirectView = new RedirectView(redirectUrl);
046                this.redirectView.setContextRelative(true);
047                this.controller.setView(this.redirectView);
048        }
049
050
051        /**
052         * Set the specific redirect 3xx status code to use.
053         * <p>If not set, {@link org.springframework.web.servlet.view.RedirectView}
054         * will select {@code HttpStatus.MOVED_TEMPORARILY (302)} by default.
055         */
056        public RedirectViewControllerRegistration setStatusCode(HttpStatus statusCode) {
057                Assert.isTrue(statusCode.is3xxRedirection(), "Not a redirect status code");
058                this.redirectView.setStatusCode(statusCode);
059                return this;
060        }
061
062        /**
063         * Whether to interpret a given redirect URL that starts with a slash ("/")
064         * as relative to the current ServletContext, i.e. as relative to the web
065         * application root.
066         * <p>Default is {@code true}.
067         */
068        public RedirectViewControllerRegistration setContextRelative(boolean contextRelative) {
069                this.redirectView.setContextRelative(contextRelative);
070                return this;
071        }
072
073        /**
074         * Whether to propagate the query parameters of the current request through
075         * to the target redirect URL.
076         * <p>Default is {@code false}.
077         */
078        public RedirectViewControllerRegistration setKeepQueryParams(boolean propagate) {
079                this.redirectView.setPropagateQueryParams(propagate);
080                return this;
081        }
082
083        protected void setApplicationContext(@Nullable ApplicationContext applicationContext) {
084                this.controller.setApplicationContext(applicationContext);
085                this.redirectView.setApplicationContext(applicationContext);
086        }
087
088        protected String getUrlPath() {
089                return this.urlPath;
090        }
091
092        protected ParameterizableViewController getViewController() {
093                return this.controller;
094        }
095
096}