Beispiel #1
0
 @ExceptionHandler
 public RedirectView handle(PostMovedException moved) {
   RedirectView redirect = new RedirectView();
   redirect.setStatusCode(HttpStatus.MOVED_PERMANENTLY);
   redirect.setUrl("/blog/" + moved.getPublicSlug());
   return redirect;
 }
 @Test
 public void explicitStatusCodeHttp10() throws Exception {
   RedirectView rv = new RedirectView();
   rv.setUrl("http://url.somewhere.com");
   rv.setStatusCode(HttpStatus.MOVED_PERMANENTLY);
   MockHttpServletRequest request = createRequest();
   MockHttpServletResponse response = new MockHttpServletResponse();
   rv.render(new HashMap<String, Object>(), request, response);
   assertEquals(301, response.getStatus());
   assertEquals("http://url.somewhere.com", response.getHeader("Location"));
 }
  /**
   * Resolves the view from the advanced search form, from search form under the menu in start page,
   * from categories or from tags AND from links to next/previous page and so on.
   *
   * @param advancedSearchData
   * @param bindingResult
   * @param model
   * @return
   * @throws UnsupportedEncodingException
   */
  @RequestMapping(value = "/advanced_search/results", method = RequestMethod.GET)
  public ModelAndView getAdvancedSearchResults(
      @ModelAttribute("advancedSearchData") SearchData advancedSearchData,
      BindingResult bindingResult,
      Model model,
      HttpServletRequest httpRequest)
      throws UnsupportedEncodingException {

    LOG.debug(
        "------ getAdvancedSearchResults : get SEARCH RESULTS based on advanced search form -----");

    if (advancedSearchData.getSearchTarget() == null)
      advancedSearchData.setSearchTarget("episodes");
    SearchResult searchResult = searchService.getResultsForSearchCriteria(advancedSearchData);

    String redirectUrl = null;
    String tilesDef = null;
    ModelAndView mv;

    // no results found
    if (searchResult.getResults().isEmpty()) {
      bindingResult.rejectValue("queryText", "notFound", "not found");
      redirectUrl = "/search/advanced_search?noResultsFound=true";
    } else if (searchResult.getResults().size() > 1) {
      String query = httpRequest.getQueryString();
      query = query.substring(0, query.lastIndexOf("&currentPage="));

      model.addAttribute("queryString", query.replaceAll("&", "&amp;"));
      model.addAttribute("advancedSearchResult", searchResult);

      tilesDef = "search_results_def";

    } else {
      // exactly one result found (either podcast or episode), redirect to it
      redirectUrl = searchResult.getResults().get(0).getRelativeLink();
    }

    if (tilesDef != null) {
      mv = new ModelAndView();
      mv.setViewName(tilesDef);
    } else {
      // must be a redirect
      RedirectView rv = new RedirectView();
      rv.setStatusCode(HttpStatus.MOVED_PERMANENTLY);
      rv.setUrl(redirectUrl);
      mv = new ModelAndView(rv);
    }

    return mv;
  }
  @RequestMapping(value = "/check-user", method = RequestMethod.POST)
  public ModelAndView checkUser(
      Locale locale,
      @Valid @ModelAttribute("user") User user,
      BindingResult bindingResult,
      ModelMap modelMap,
      RedirectAttributes redirectAttributes) {

    ModelAndView modelAndView = new ModelAndView();
    if (!bindingResult.hasErrors()) {
      RedirectView redirectView = new RedirectView("mainpage");
      redirectView.setStatusCode(HttpStatus.MOVED_PERMANENTLY);
      modelAndView.setView(redirectView);

      redirectAttributes.addFlashAttribute(
          "locale",
          messageSource.getMessage("locale", new String[] {locale.getDisplayName(locale)}, locale));
    } else {
      modelAndView.setViewName("login");
    }

    return modelAndView;
  }