/** * Set the specific redirect 3xx status code to use. * <p>If not set, {@link org.springframework.web.servlet.view.RedirectView} * will select {@code HttpStatus.MOVED_TEMPORARILY (302)} by default. */ public RedirectViewControllerRegistration setStatusCode(HttpStatus statusCode) { Assert.isTrue(statusCode.is3xxRedirection(), "Not a redirect status code"); this.redirectView.setStatusCode(statusCode); return this; }
/** * Set the specific redirect 3xx status code to use. * <p>If not set, {@link org.springframework.web.servlet.view.RedirectView} * will select {@code HttpStatus.MOVED_TEMPORARILY (302)} by default. */ public RedirectViewControllerRegistration setStatusCode(HttpStatus statusCode) { Assert.isTrue(statusCode.is3xxRedirection(), "Not a redirect status code"); this.redirectView.setStatusCode(statusCode); return this; }
@Test public void explicitStatusCodeHttp10() throws Exception { RedirectView rv = new RedirectView(); rv.setUrl("http://url.somewhere.com"); rv.setStatusCode(HttpStatus.MOVED_PERMANENTLY); rv.render(new HashMap<>(), request, response); assertEquals(301, response.getStatus()); assertEquals("http://url.somewhere.com", response.getHeader("Location")); }
@Test public void explicitStatusCodeHttp11() throws Exception { RedirectView rv = new RedirectView(); rv.setUrl("http://url.somewhere.com"); rv.setHttp10Compatible(false); rv.setStatusCode(HttpStatus.MOVED_PERMANENTLY); rv.render(new HashMap<>(), request, response); assertEquals(301, response.getStatus()); assertEquals("http://url.somewhere.com", response.getHeader("Location")); }
@RequestMapping(value="/oldPath") public ModelAndView doProductOld( @PathVariable(value = "oldProductId") Long oldProductId, Model model ) throws Exception { Product product = productDao.findByOldId(oldProductId); RedirectView red = new RedirectView(product.getUrlNewPath(),true); red.setStatusCode(HttpStatus.MOVED_PERMANENTLY); return new ModelAndView(red); }
/** * Set the specific redirect 3xx status code to use. * <p>If not set, {@link org.springframework.web.servlet.view.RedirectView} * will select {@code HttpStatus.MOVED_TEMPORARILY (302)} by default. */ public RedirectViewControllerRegistration setStatusCode(HttpStatus statusCode) { Assert.isTrue(statusCode.is3xxRedirection(), "Not a redirect status code"); this.redirectView.setStatusCode(statusCode); return this; }
@RequestMapping("") public RedirectView root() { RedirectView redirectView = new RedirectView("/developer/doc"); redirectView.setStatusCode(HttpStatus.TEMPORARY_REDIRECT); return redirectView; } }
/** * Backward compatibility for url indexed by google * /resource/{id:.+}/tab/jContent/ * witch after paging leed to 404 page */ @RequestMapping(value = "/resource/{id:.+}/tab/jContent/jContent") @Profiled(tag = "resource.tab.{$1}", el = true) @Deprecated public SmartView resourceTabCompatibility(@PathVariable("id") String id, Model model, HttpServletRequest request, Locale locale) { RedirectView view = new RedirectView("/resource/" + id + "/tab/jContent", true, false, true); view.getAttributesMap().putAll(request.getParameterMap()); view.setStatusCode(HttpStatus.MOVED_PERMANENTLY); return view; }
private RedirectView redirectViewWithQuery(String redirectUri, String state, String query) { try { if (!Strings.isNullOrEmpty(state)) { query += "&state=" + state; } String encoded = UriUtils.encodeQuery(query, Charsets.UTF_8.name()); String locationUri = redirectUri; if (redirectUri.contains("?")) { locationUri += "&" + encoded; } else { locationUri += "?" + encoded; } RedirectView redirectView = new RedirectView(locationUri); redirectView.setStatusCode(HttpStatus.FOUND); redirectView.setExposeModelAttributes(false); redirectView.setPropagateQueryParams(false); return redirectView; } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } }
@RequestMapping("/u/{username}") public Object userProfile(@PathVariable("username") String username) { Account account = accountService.loadAccount(username); if (!account.getUsername().equals(username)) { RedirectView redirectView = new RedirectView("/u/" + account.getUsername()); redirectView.setStatusCode(HttpStatus.MOVED_PERMANENTLY); return redirectView; } AccountStats accountStats = accountService.loadAccountStats(account.getUsername()); List<ZoneInfo> zones = zoneService.listAdministerZones(account.getUsername()); return new ModelAndView("account/user-profile")// .addObject("account", account) .addObject("accountStats", accountStats) .addObject("administerZones", zones); }
@RequestMapping("/{flakeId}") public View redirectDebateOrArticle(@PathVariable("flakeId") FlakeId flakeId) { return articleService.findArticle(flakeId).map(article -> { RedirectView redirectView = new RedirectView(String.format("/z/%s/debates/%s", article.getZone().value(), article.getArticleId())); redirectView.setStatusCode(HttpStatus.PERMANENT_REDIRECT); return redirectView; }).orElseGet(() -> { // if not found will throw exception and go to 404 Debate debate = articleService.loadDebateWithCache(flakeId); RedirectView redirectView = new RedirectView(String.format("/z/%s/debates/%s/%s", debate.getZone().value(), debate.getArticleId(), debate.getDebateId())); redirectView.setStatusCode(HttpStatus.PERMANENT_REDIRECT); return redirectView; }); } }
private Object resolveZone(HttpServletRequest request, String decodedRawZone, Function<ZoneInfo, ModelAndView> onZoneInfo) { // note that decodedRawZone already do http url decode, and PathVariable already trim() // space of value return Zone.tryFallback(decodedRawZone).map(zone -> { if (!zone.value().equals(decodedRawZone)) { String orgUrl = request.getRequestURL().toString(); // replace pattern is combine of fallback pattern and valid pattern // TODO refactor replace rule to Zone String location = orgUrl.replaceFirst("/z/[a-zA-Z0-9_\\-]+", "/z/" + zone); //check if fallback success, this prevent infinite redirect loop if (!location.equals(orgUrl)) { RedirectView redirectView = new RedirectView(location); redirectView.setPropagateQueryParams(true); redirectView.setExpandUriTemplateVariables(false); redirectView.setExposeModelAttributes(false); redirectView.setExposeContextBeansAsAttributes(false); redirectView.setExposePathVariables(false); redirectView.setContextRelative(true); redirectView.setStatusCode(HttpStatus.PERMANENT_REDIRECT); return redirectView; } } return onZoneInfo.apply(zoneService.loadZone(zone)); }).orElseThrow(() -> new EmptyResultDataAccessException("no such zone: " + decodedRawZone, 1)); }