/** * Create the target URL and, if necessary, pre-pend the contextPath, expand * URI template variables, append the current request query, and apply the * configured {@link #getRequestDataValueProcessor() * RequestDataValueProcessor}. */ protected final String createTargetUrl(Map<String, Object> model, ServerWebExchange exchange) { String url = getUrl(); Assert.state(url != null, "'url' not set"); ServerHttpRequest request = exchange.getRequest(); StringBuilder targetUrl = new StringBuilder(); if (isContextRelative() && url.startsWith("/")) { targetUrl.append(request.getPath().contextPath().value()); } targetUrl.append(url); if (StringUtils.hasText(targetUrl)) { Map<String, String> uriVars = getCurrentUriVariables(exchange); targetUrl = expandTargetUrlTemplate(targetUrl.toString(), model, uriVars); } if (isPropagateQuery()) { targetUrl = appendCurrentRequestQuery(targetUrl.toString(), request); } String result = targetUrl.toString(); RequestDataValueProcessor processor = getRequestDataValueProcessor(); return (processor != null ? processor.processUrl(exchange, result) : result); }
@Test public void defaultValuesForRedirect() throws Exception { Rendering rendering = Rendering.redirectTo("abc").build(); Object view = rendering.view(); assertEquals(RedirectView.class, view.getClass()); assertEquals("abc", ((RedirectView) view).getUrl()); assertTrue(((RedirectView) view).isContextRelative()); assertFalse(((RedirectView) view).isPropagateQuery()); }
@Test public void redirectView() throws Exception { Mono<View> mono = this.resolver.resolveViewName("redirect:foo", Locale.US); StepVerifier.create(mono) .consumeNextWith(view -> { assertEquals(RedirectView.class, view.getClass()); RedirectView redirectView = (RedirectView) view; assertEquals("foo", redirectView.getUrl()); assertEquals(HttpStatus.SEE_OTHER, redirectView.getStatusCode()); }) .expectComplete() .verify(Duration.ZERO); }
@Test public void customizedRedirectView() throws Exception { this.resolver.setRedirectViewProvider(url -> new RedirectView(url, HttpStatus.FOUND)); Mono<View> mono = this.resolver.resolveViewName("redirect:foo", Locale.US); StepVerifier.create(mono) .consumeNextWith(view -> { assertEquals(RedirectView.class, view.getClass()); RedirectView redirectView = (RedirectView) view; assertEquals("foo", redirectView.getUrl()); assertEquals(HttpStatus.FOUND, redirectView.getStatusCode()); }) .expectComplete() .verify(Duration.ZERO); }
/** * Create the target URL and, if necessary, pre-pend the contextPath, expand * URI template variables, append the current request query, and apply the * configured {@link #getRequestDataValueProcessor() * RequestDataValueProcessor}. */ protected final String createTargetUrl(Map<String, Object> model, ServerWebExchange exchange) { StringBuilder targetUrl = new StringBuilder(); if (isContextRelative() && getUrl().startsWith("/")) { targetUrl.append(exchange.getRequest().getContextPath()); } targetUrl.append(getUrl()); if (StringUtils.hasText(targetUrl)) { Map<String, String> uriVars = getCurrentUriVariables(exchange); targetUrl = expandTargetUrlTemplate(targetUrl.toString(), model, uriVars); } if (isPropagateQuery()) { targetUrl = appendCurrentRequestQuery(targetUrl.toString(), exchange.getRequest()); } String result = targetUrl.toString(); RequestDataValueProcessor processor = getRequestDataValueProcessor(); return (processor != null ? processor.processUrl(exchange, result) : result); }