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")));
}
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;
}
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;
}
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());
}
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"));
}
Aggregations