Search in sources :

Example 61 with ServletRequestAttributes

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();
}
Also used : ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes)

Example 62 with ServletRequestAttributes

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;
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) WebApplicationContext(org.springframework.web.context.WebApplicationContext) Nullable(org.springframework.lang.Nullable)

Example 63 with ServletRequestAttributes

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.");
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes)

Example 64 with ServletRequestAttributes

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();
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) AfterEach(org.junit.jupiter.api.AfterEach)

Example 65 with ServletRequestAttributes

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());
}
Also used : RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes)

Aggregations

ServletRequestAttributes (org.springframework.web.context.request.ServletRequestAttributes)204 HttpServletRequest (javax.servlet.http.HttpServletRequest)92 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)62 RequestAttributes (org.springframework.web.context.request.RequestAttributes)50 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)32 Test (org.junit.jupiter.api.Test)28 lombok.val (lombok.val)25 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)22 Test (org.junit.Test)18 AuthChecker (com.code.server.login.anotation.AuthChecker)17 HttpSession (javax.servlet.http.HttpSession)12 Before (org.junit.Before)12 HashMap (java.util.HashMap)11 Method (java.lang.reflect.Method)8 Date (java.util.Date)7 AbstractTracingSpan (org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan)7 TraceSegment (org.apache.skywalking.apm.agent.core.context.trace.TraceSegment)7 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)6 Map (java.util.Map)6 BeforeEach (org.junit.jupiter.api.BeforeEach)6