use of org.springframework.web.context.request.ServletRequestAttributes in project spring-framework by spring-projects.
the class ServletUriComponentsBuilder method getCurrentRequest.
/**
* Obtain current request through {@link RequestContextHolder}.
*/
protected static HttpServletRequest getCurrentRequest() {
RequestAttributes attrs = RequestContextHolder.getRequestAttributes();
Assert.state(attrs instanceof ServletRequestAttributes, "No current ServletRequestAttributes");
return ((ServletRequestAttributes) attrs).getRequest();
}
use of org.springframework.web.context.request.ServletRequestAttributes in project spring-framework by spring-projects.
the class MvcUriComponentsBuilder method getWebApplicationContext.
@Nullable
private static WebApplicationContext getWebApplicationContext() {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
if (requestAttributes == null) {
return null;
}
HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
String attributeName = DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE;
WebApplicationContext wac = (WebApplicationContext) request.getAttribute(attributeName);
if (wac == null) {
return null;
}
return wac;
}
use of org.springframework.web.context.request.ServletRequestAttributes in project midpoint by Evolveum.
the class AbstractRestController method controllerBasePath.
/**
* Returns base path (without servlet context) reflecting currently used request.
* This solves the problem of base path being one of multiple possible mappings.
*/
protected String controllerBasePath() {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
if (requestAttributes instanceof ServletRequestAttributes) {
HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
String servletPath = request.getServletPath();
for (String requestMappingPath : requestMappingPaths) {
if (servletPath.startsWith(requestMappingPath)) {
return requestMappingPath;
}
}
}
throw new NullPointerException("Base controller URL could not be determined.");
}
use of org.springframework.web.context.request.ServletRequestAttributes in project spring-framework by spring-projects.
the class CustomRequestAttributesRequestContextHolderTests method verifyCustomRequestAttributesAreRestored.
@AfterEach
public void verifyCustomRequestAttributesAreRestored() {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
assertThat(requestAttributes).isInstanceOf(ServletRequestAttributes.class);
HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
assertThat(request.getAttribute(FROM_CUSTOM_MOCK)).isEqualTo(FROM_CUSTOM_MOCK);
assertThat(request.getAttribute(FROM_MVC_TEST_DEFAULT)).isNull();
assertThat(request.getAttribute(FROM_MVC_TEST_MOCK)).isNull();
RequestContextHolder.resetRequestAttributes();
this.wac.close();
}
use of org.springframework.web.context.request.ServletRequestAttributes in project spring-framework by spring-projects.
the class CustomRequestAttributesRequestContextHolderTests method assertRequestAttributes.
private static void assertRequestAttributes() {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
assertThat(requestAttributes).isInstanceOf(ServletRequestAttributes.class);
assertRequestAttributes(((ServletRequestAttributes) requestAttributes).getRequest());
}
Aggregations