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