/** * Send a redirect back to the HTTP client. * @param targetUrl the target URL to redirect to * @param exchange current exchange */ protected Mono<Void> sendRedirect(String targetUrl, ServerWebExchange exchange) { String transformedUrl = (isRemoteHost(targetUrl) ? targetUrl : exchange.transformUrl(targetUrl)); ServerHttpResponse response = exchange.getResponse(); response.getHeaders().setLocation(URI.create(transformedUrl)); response.setStatusCode(getStatusCode()); return Mono.empty(); }
@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); }
@Override public void afterPropertiesSet() throws Exception { super.afterPropertiesSet(); if (getStatusCode() == null) { throw new IllegalArgumentException("Property 'statusCode' is required"); } }
/** * Send a redirect back to the HTTP client * @param targetUrl the target URL to redirect to * @param exchange current exchange */ protected Mono<Void> sendRedirect(String targetUrl, ServerWebExchange exchange) { ServerHttpResponse response = exchange.getResponse(); String encodedURL = (isRemoteHost(targetUrl) ? targetUrl : response.encodeUrl(targetUrl)); response.getHeaders().setLocation(URI.create(encodedURL)); response.setStatusCode(getStatusCode()); return Mono.empty(); }