use of org.springframework.web.context.request.ServletRequestAttributes in project spring-framework by spring-projects.
the class ContentNegotiatingViewResolverTests method createViewResolver.
@Before
public void createViewResolver() {
StaticWebApplicationContext wac = new StaticWebApplicationContext();
wac.setServletContext(new MockServletContext());
wac.refresh();
viewResolver = new ContentNegotiatingViewResolver();
viewResolver.setApplicationContext(wac);
request = new MockHttpServletRequest("GET", "/test");
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
}
use of org.springframework.web.context.request.ServletRequestAttributes in project spring-framework by spring-projects.
the class ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests method setUp.
@Before
public void setUp() {
this.oldRequestAttributes = new ServletRequestAttributes(new MockHttpServletRequest());
this.newRequestAttributes = new ServletRequestAttributes(new MockHttpServletRequest());
MockHttpServletRequest oldRequestWithSession = new MockHttpServletRequest();
oldRequestWithSession.setSession(new MockHttpSession());
this.oldRequestAttributesWithSession = new ServletRequestAttributes(oldRequestWithSession);
MockHttpServletRequest newRequestWithSession = new MockHttpServletRequest();
newRequestWithSession.setSession(new MockHttpSession());
this.newRequestAttributesWithSession = new ServletRequestAttributes(newRequestWithSession);
}
use of org.springframework.web.context.request.ServletRequestAttributes in project spring-framework by spring-projects.
the class AopNamespaceHandlerScopeIntegrationTests method testRequestScoping.
@Test
public void testRequestScoping() throws Exception {
MockHttpServletRequest oldRequest = new MockHttpServletRequest();
MockHttpServletRequest newRequest = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(oldRequest));
ITestBean scoped = (ITestBean) this.context.getBean("requestScoped");
assertTrue("Should be AOP proxy", AopUtils.isAopProxy(scoped));
assertTrue("Should be target class proxy", scoped instanceof TestBean);
ITestBean testBean = (ITestBean) this.context.getBean("testBean");
assertTrue("Should be AOP proxy", AopUtils.isAopProxy(testBean));
assertFalse("Regular bean should be JDK proxy", testBean instanceof TestBean);
String rob = "Rob Harrop";
String bram = "Bram Smeets";
assertEquals(rob, scoped.getName());
scoped.setName(bram);
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(newRequest));
assertEquals(rob, scoped.getName());
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(oldRequest));
assertEquals(bram, scoped.getName());
assertTrue("Should have advisors", ((Advised) scoped).getAdvisors().length > 0);
}
use of org.springframework.web.context.request.ServletRequestAttributes in project spring-framework by spring-projects.
the class TilesView method checkResource.
@Override
public boolean checkResource(final Locale locale) throws Exception {
HttpServletRequest servletRequest = null;
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
if (requestAttributes instanceof ServletRequestAttributes) {
servletRequest = ((ServletRequestAttributes) requestAttributes).getRequest();
}
Request request = new ServletRequest(this.applicationContext, servletRequest, null) {
@Override
public Locale getRequestLocale() {
return locale;
}
};
return this.renderer.isRenderable(getUrl(), request);
}
use of org.springframework.web.context.request.ServletRequestAttributes in project spring-framework by spring-projects.
the class ContentNegotiatingViewResolver method resolveViewName.
@Override
public View resolveViewName(String viewName, Locale locale) throws Exception {
RequestAttributes attrs = RequestContextHolder.getRequestAttributes();
Assert.state(attrs instanceof ServletRequestAttributes, "No current ServletRequestAttributes");
List<MediaType> requestedMediaTypes = getMediaTypes(((ServletRequestAttributes) attrs).getRequest());
if (requestedMediaTypes != null) {
List<View> candidateViews = getCandidateViews(viewName, locale, requestedMediaTypes);
View bestView = getBestView(candidateViews, requestedMediaTypes, attrs);
if (bestView != null) {
return bestView;
}
}
if (this.useNotAcceptableStatusCode) {
if (logger.isDebugEnabled()) {
logger.debug("No acceptable view found; returning 406 (Not Acceptable) status code");
}
return NOT_ACCEPTABLE_VIEW;
} else {
logger.debug("No acceptable view found; returning null");
return null;
}
}
Aggregations