Search in sources :

Example 1 with View

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

the class ViewResolutionTests method testContentNegotiation.

@Test
public void testContentNegotiation() throws Exception {
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
    marshaller.setClassesToBeBound(Person.class);
    List<View> viewList = new ArrayList<>();
    viewList.add(new MappingJackson2JsonView());
    viewList.add(new MarshallingView(marshaller));
    ContentNegotiationManager manager = new ContentNegotiationManager(new HeaderContentNegotiationStrategy(), new FixedContentNegotiationStrategy(MediaType.TEXT_HTML));
    ContentNegotiatingViewResolver cnViewResolver = new ContentNegotiatingViewResolver();
    cnViewResolver.setDefaultViews(viewList);
    cnViewResolver.setContentNegotiationManager(manager);
    cnViewResolver.afterPropertiesSet();
    MockMvc mockMvc = standaloneSetup(new PersonController()).setViewResolvers(cnViewResolver, new InternalResourceViewResolver()).build();
    mockMvc.perform(get("/person/Corea")).andExpect(status().isOk()).andExpect(model().size(1)).andExpect(model().attributeExists("person")).andExpect(forwardedUrl("person/show"));
    mockMvc.perform(get("/person/Corea").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_JSON)).andExpect(jsonPath("$.person.name").value("Corea"));
    mockMvc.perform(get("/person/Corea").accept(MediaType.APPLICATION_XML)).andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_XML)).andExpect(xpath("/person/name/text()").string(equalTo("Corea")));
}
Also used : ArrayList(java.util.ArrayList) Jaxb2Marshaller(org.springframework.oxm.jaxb.Jaxb2Marshaller) FixedContentNegotiationStrategy(org.springframework.web.accept.FixedContentNegotiationStrategy) View(org.springframework.web.servlet.View) MappingJackson2JsonView(org.springframework.web.servlet.view.json.MappingJackson2JsonView) MarshallingView(org.springframework.web.servlet.view.xml.MarshallingView) MappingJackson2JsonView(org.springframework.web.servlet.view.json.MappingJackson2JsonView) ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) MarshallingView(org.springframework.web.servlet.view.xml.MarshallingView) ContentNegotiatingViewResolver(org.springframework.web.servlet.view.ContentNegotiatingViewResolver) HeaderContentNegotiationStrategy(org.springframework.web.accept.HeaderContentNegotiationStrategy) MockMvc(org.springframework.test.web.servlet.MockMvc) InternalResourceViewResolver(org.springframework.web.servlet.view.InternalResourceViewResolver) Test(org.junit.Test)

Example 2 with View

use of org.springframework.web.servlet.View in project grails-core by grails.

the class GroovyPageViewResolver method loadView.

@Override
protected View loadView(String viewName, Locale locale) throws Exception {
    Assert.notNull(templateEngine, "Property [templateEngine] cannot be null");
    if (viewName.endsWith(GSP_SUFFIX)) {
        viewName = viewName.substring(0, viewName.length() - GSP_SUFFIX.length());
    }
    if (!allowGrailsViewCaching) {
        return createGrailsView(viewName);
    }
    String viewCacheKey = groovyPageLocator.resolveViewFormat(viewName);
    String currentControllerKeyPrefix = resolveCurrentControllerKeyPrefixes();
    if (currentControllerKeyPrefix != null) {
        viewCacheKey = currentControllerKeyPrefix + ':' + viewCacheKey;
    }
    CacheEntry<View> entry = viewCache.get(viewCacheKey);
    final String lookupViewName = viewName;
    Callable<View> updater = new Callable<View>() {

        public View call() throws Exception {
            try {
                return createGrailsView(lookupViewName);
            } catch (Exception e) {
                throw new WrappedInitializationException(e);
            }
        }
    };
    View view = null;
    if (entry == null) {
        try {
            return CacheEntry.getValue(viewCache, viewCacheKey, cacheTimeout, updater);
        } catch (CacheEntry.UpdateException e) {
            e.rethrowCause();
            // make compiler happy
            return null;
        }
    }
    try {
        view = entry.getValue(cacheTimeout, updater, true, null);
    } catch (WrappedInitializationException e) {
        e.rethrowCause();
    }
    return view;
}
Also used : CacheEntry(grails.util.CacheEntry) View(org.springframework.web.servlet.View) AbstractUrlBasedView(org.springframework.web.servlet.view.AbstractUrlBasedView) Callable(java.util.concurrent.Callable)

Example 3 with View

use of org.springframework.web.servlet.View in project grails-core by grails.

the class GroovyPageLayoutFinder method getNamedDecorator.

public Decorator getNamedDecorator(HttpServletRequest request, String name, boolean viewMustExist) {
    if (GrailsStringUtils.isBlank(name) || NONE_LAYOUT.equals(name)) {
        return null;
    }
    if (cacheEnabled) {
        DecoratorCacheValue cacheValue = decoratorCache.get(name);
        if (cacheValue != null && (!gspReloadEnabled || !cacheValue.isExpired())) {
            return cacheValue.getDecorator();
        }
    }
    View view;
    try {
        view = viewResolver.resolveViewName(GrailsResourceUtils.cleanPath(GrailsResourceUtils.appendPiecesForUri(LAYOUTS_PATH, name)), request.getLocale());
        // it's only possible to check that GroovyPageView exists
        if (viewMustExist && !(view instanceof AbstractGrailsView)) {
            view = null;
        }
    } catch (Exception e) {
        throw new RuntimeException("Unable to resolve view", e);
    }
    Decorator d = null;
    if (view != null) {
        d = createDecorator(name, view);
    }
    if (cacheEnabled) {
        decoratorCache.put(name, new DecoratorCacheValue(d));
    }
    return d;
}
Also used : Decorator(com.opensymphony.module.sitemesh.Decorator) AbstractGrailsView(org.grails.web.servlet.view.AbstractGrailsView) AbstractGrailsView(org.grails.web.servlet.view.AbstractGrailsView) View(org.springframework.web.servlet.View)

Example 4 with View

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

the class AuthorizationEndpointTests method testAuthorizationCodeWithTrickyEncodedQueryParams.

@Test
public void testAuthorizationCodeWithTrickyEncodedQueryParams() throws Exception {
    endpoint.setAuthorizationCodeServices(new StubAuthorizationCodeServices());
    model.put("authorizationRequest", getAuthorizationRequest("foo", "http://anywhere.com/path?foo=b%20%3D&bar=f%20$", null, null, Collections.singleton("code")));
    View result = endpoint.approveOrDeny(Collections.singletonMap(OAuth2Utils.USER_OAUTH_APPROVAL, "true"), model, sessionStatus, principal);
    assertEquals("http://anywhere.com/path?foo=b%20%3D&bar=f%20$&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 5 with View

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

the class AuthorizationEndpointTests method testApproveOrDeny.

@Test
public void testApproveOrDeny() throws Exception {
    AuthorizationRequest request = getAuthorizationRequest("foo", "http://anywhere.com", null, null, Collections.singleton("code"));
    request.setApproved(true);
    Map<String, String> approvalParameters = new HashMap<String, String>();
    approvalParameters.put("user_oauth_approval", "true");
    model.put("authorizationRequest", request);
    View result = endpoint.approveOrDeny(approvalParameters, model, sessionStatus, principal);
    assertTrue("Wrong view: " + result, ((RedirectView) result).getUrl().startsWith("http://anywhere.com"));
}
Also used : AuthorizationRequest(org.springframework.security.oauth2.provider.AuthorizationRequest) HashMap(java.util.HashMap) RedirectView(org.springframework.web.servlet.view.RedirectView) RedirectView(org.springframework.web.servlet.view.RedirectView) ModelAndView(org.springframework.web.servlet.ModelAndView) View(org.springframework.web.servlet.View) Test(org.junit.Test)

Aggregations

View (org.springframework.web.servlet.View)114 Test (org.junit.jupiter.api.Test)43 Test (org.junit.Test)37 HashMap (java.util.HashMap)29 ModelAndView (org.springframework.web.servlet.ModelAndView)27 Locale (java.util.Locale)20 RedirectView (org.springframework.web.servlet.view.RedirectView)20 ViewResolver (org.springframework.web.servlet.ViewResolver)19 Map (java.util.Map)13 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)13 HttpServletResponse (javax.servlet.http.HttpServletResponse)11 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)11 ContentNegotiationManager (org.springframework.web.accept.ContentNegotiationManager)11 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)11 HttpServletRequest (javax.servlet.http.HttpServletRequest)9 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)9 ArrayList (java.util.ArrayList)8 MediaType (org.springframework.http.MediaType)8 MockServletContext (org.springframework.mock.web.test.MockServletContext)8 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)7