Search in sources :

Example 66 with RedirectView

use of org.springframework.web.servlet.view.RedirectView in project spring-security-oauth by spring-projects.

the class AuthorizationEndpointTests method testAuthorizationCodeWithQueryParams.

@Test
public void testAuthorizationCodeWithQueryParams() throws Exception {
    endpoint.setAuthorizationCodeServices(new StubAuthorizationCodeServices());
    model.put("authorizationRequest", getAuthorizationRequest("foo", "http://anywhere.com?foo=bar", null, null, Collections.singleton("code")));
    View result = endpoint.approveOrDeny(Collections.singletonMap(OAuth2Utils.USER_OAUTH_APPROVAL, "true"), model, sessionStatus, principal);
    assertEquals("http://anywhere.com?foo=bar&code=thecode", ((RedirectView) result).getUrl());
}
Also used : RedirectView(org.springframework.web.servlet.view.RedirectView) ModelAndView(org.springframework.web.servlet.ModelAndView) View(org.springframework.web.servlet.View) Test(org.junit.Test)

Example 67 with RedirectView

use of org.springframework.web.servlet.view.RedirectView in project spring-framework by spring-projects.

the class FreeMarkerViewTests method freeMarkerViewResolver.

@Test
public void freeMarkerViewResolver() throws Exception {
    FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
    configurer.setConfiguration(new TestConfiguration());
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(new MockServletContext());
    wac.getBeanFactory().registerSingleton("configurer", configurer);
    wac.refresh();
    FreeMarkerViewResolver vr = new FreeMarkerViewResolver("prefix_", "_suffix");
    vr.setApplicationContext(wac);
    View view = vr.resolveViewName("test", Locale.CANADA);
    assertEquals("Correct view class", FreeMarkerView.class, view.getClass());
    assertEquals("Correct URL", "prefix_test_suffix", ((FreeMarkerView) view).getUrl());
    view = vr.resolveViewName("non-existing", Locale.CANADA);
    assertNull(view);
    view = vr.resolveViewName("redirect:myUrl", Locale.getDefault());
    assertEquals("Correct view class", RedirectView.class, view.getClass());
    assertEquals("Correct URL", "myUrl", ((RedirectView) view).getUrl());
    view = vr.resolveViewName("forward:myUrl", Locale.getDefault());
    assertEquals("Correct view class", InternalResourceView.class, view.getClass());
    assertEquals("Correct URL", "myUrl", ((InternalResourceView) view).getUrl());
}
Also used : StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) AbstractView(org.springframework.web.servlet.view.AbstractView) InternalResourceView(org.springframework.web.servlet.view.InternalResourceView) RedirectView(org.springframework.web.servlet.view.RedirectView) View(org.springframework.web.servlet.View) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 68 with RedirectView

use of org.springframework.web.servlet.view.RedirectView in project spring-framework by spring-projects.

the class MvcNamespaceTests method testViewControllers.

@Test
public void testViewControllers() throws Exception {
    loadBeanDefinitions("mvc-config-view-controllers.xml", 19);
    RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
    assertNotNull(mapping);
    mapping.setDefaultHandler(handlerMethod);
    BeanNameUrlHandlerMapping beanNameMapping = appContext.getBean(BeanNameUrlHandlerMapping.class);
    assertNotNull(beanNameMapping);
    assertEquals(2, beanNameMapping.getOrder());
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("GET");
    HandlerExecutionChain chain = mapping.getHandler(request);
    assertEquals(3, chain.getInterceptors().length);
    assertTrue(chain.getInterceptors()[0] instanceof ConversionServiceExposingInterceptor);
    assertTrue(chain.getInterceptors()[1] instanceof LocaleChangeInterceptor);
    assertTrue(chain.getInterceptors()[2] instanceof ThemeChangeInterceptor);
    SimpleUrlHandlerMapping mapping2 = appContext.getBean(SimpleUrlHandlerMapping.class);
    assertNotNull(mapping2);
    SimpleControllerHandlerAdapter adapter = appContext.getBean(SimpleControllerHandlerAdapter.class);
    assertNotNull(adapter);
    request = new MockHttpServletRequest("GET", "/foo");
    chain = mapping2.getHandler(request);
    assertEquals(4, chain.getInterceptors().length);
    assertTrue(chain.getInterceptors()[1] instanceof ConversionServiceExposingInterceptor);
    assertTrue(chain.getInterceptors()[2] instanceof LocaleChangeInterceptor);
    assertTrue(chain.getInterceptors()[3] instanceof ThemeChangeInterceptor);
    ModelAndView mv = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
    assertNull(mv.getViewName());
    request = new MockHttpServletRequest("GET", "/myapp/app/bar");
    request.setContextPath("/myapp");
    request.setServletPath("/app");
    chain = mapping2.getHandler(request);
    assertEquals(4, chain.getInterceptors().length);
    assertTrue(chain.getInterceptors()[1] instanceof ConversionServiceExposingInterceptor);
    assertTrue(chain.getInterceptors()[2] instanceof LocaleChangeInterceptor);
    assertTrue(chain.getInterceptors()[3] instanceof ThemeChangeInterceptor);
    mv = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
    assertEquals("baz", mv.getViewName());
    request = new MockHttpServletRequest("GET", "/myapp/app/");
    request.setContextPath("/myapp");
    request.setServletPath("/app");
    chain = mapping2.getHandler(request);
    assertEquals(4, chain.getInterceptors().length);
    assertTrue(chain.getInterceptors()[1] instanceof ConversionServiceExposingInterceptor);
    assertTrue(chain.getInterceptors()[2] instanceof LocaleChangeInterceptor);
    assertTrue(chain.getInterceptors()[3] instanceof ThemeChangeInterceptor);
    mv = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
    assertEquals("root", mv.getViewName());
    request = new MockHttpServletRequest("GET", "/myapp/app/old");
    request.setContextPath("/myapp");
    request.setServletPath("/app");
    request.setQueryString("a=b");
    chain = mapping2.getHandler(request);
    mv = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
    assertNotNull(mv.getView());
    assertEquals(RedirectView.class, mv.getView().getClass());
    RedirectView redirectView = (RedirectView) mv.getView();
    MockHttpServletResponse response = new MockHttpServletResponse();
    redirectView.render(Collections.emptyMap(), request, response);
    assertEquals("/new?a=b", response.getRedirectedUrl());
    assertEquals(308, response.getStatus());
    request = new MockHttpServletRequest("GET", "/bad");
    chain = mapping2.getHandler(request);
    response = new MockHttpServletResponse();
    mv = adapter.handle(request, response, chain.getHandler());
    assertNull(mv);
    assertEquals(404, response.getStatus());
}
Also used : LocaleChangeInterceptor(org.springframework.web.servlet.i18n.LocaleChangeInterceptor) BeanNameUrlHandlerMapping(org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) ConversionServiceExposingInterceptor(org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) SimpleControllerHandlerAdapter(org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter) ModelAndView(org.springframework.web.servlet.ModelAndView) RedirectView(org.springframework.web.servlet.view.RedirectView) ThemeChangeInterceptor(org.springframework.web.servlet.theme.ThemeChangeInterceptor) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 69 with RedirectView

use of org.springframework.web.servlet.view.RedirectView in project spring-framework by spring-projects.

the class ModelAndViewMethodReturnValueHandlerTests method handleRedirectAttributesWithViewReference.

@Test
public void handleRedirectAttributesWithViewReference() throws Exception {
    RedirectAttributesModelMap redirectAttributes = new RedirectAttributesModelMap();
    mavContainer.setRedirectModel(redirectAttributes);
    ModelAndView mav = new ModelAndView(new RedirectView(), "attrName", "attrValue");
    handler.handleReturnValue(mav, returnParamModelAndView, mavContainer, webRequest);
    assertEquals(RedirectView.class, mavContainer.getView().getClass());
    assertEquals("attrValue", mavContainer.getModel().get("attrName"));
    assertSame("RedirectAttributes should be used if controller redirects", redirectAttributes, mavContainer.getModel());
}
Also used : ModelAndView(org.springframework.web.servlet.ModelAndView) RedirectView(org.springframework.web.servlet.view.RedirectView) RedirectAttributesModelMap(org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap) Test(org.junit.Test)

Example 70 with RedirectView

use of org.springframework.web.servlet.view.RedirectView in project spring-framework by spring-projects.

the class ModelAndViewMethodReturnValueHandlerTests method handleRedirectWithIgnoreDefaultModel.

// SPR-14045
@Test
public void handleRedirectWithIgnoreDefaultModel() throws Exception {
    mavContainer.setIgnoreDefaultModelOnRedirect(true);
    RedirectView redirectView = new RedirectView();
    ModelAndView mav = new ModelAndView(redirectView, "name", "value");
    handler.handleReturnValue(mav, returnParamModelAndView, mavContainer, webRequest);
    ModelMap model = mavContainer.getModel();
    assertSame(redirectView, mavContainer.getView());
    assertEquals(1, model.size());
    assertEquals("value", model.get("name"));
}
Also used : RedirectAttributesModelMap(org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap) ModelMap(org.springframework.ui.ModelMap) RedirectView(org.springframework.web.servlet.view.RedirectView) ModelAndView(org.springframework.web.servlet.ModelAndView) Test(org.junit.Test)

Aggregations

RedirectView (org.springframework.web.servlet.view.RedirectView)86 ModelAndView (org.springframework.web.servlet.ModelAndView)70 Test (org.junit.Test)34 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)19 HashMap (java.util.HashMap)18 View (org.springframework.web.servlet.View)17 Authentication (org.springframework.security.core.Authentication)14 AuthorizationRequest (org.springframework.security.oauth2.provider.AuthorizationRequest)13 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)9 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)8 TokenRequest (org.springframework.security.oauth2.provider.TokenRequest)8 DefaultUserApprovalHandler (org.springframework.security.oauth2.provider.approval.DefaultUserApprovalHandler)8 ServletException (javax.servlet.ServletException)7 RequestInfoForm (org.orcid.pojo.ajaxForm.RequestInfoForm)7 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)7 TokenGranter (org.springframework.security.oauth2.provider.TokenGranter)7 Principal (org.apereo.cas.authentication.principal.Principal)6 OAuthRegisteredService (org.apereo.cas.support.oauth.services.OAuthRegisteredService)6 CasProfile (org.pac4j.cas.profile.CasProfile)6 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)6