Search in sources :

Example 31 with RequestAttributes

use of org.springframework.web.context.request.RequestAttributes 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 32 with RequestAttributes

use of org.springframework.web.context.request.RequestAttributes in project spring-framework by spring-projects.

the class ServletTestExecutionListenerTests method assertRequestAttributesExist.

private RequestAttributes assertRequestAttributesExist() {
    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    assertThat(requestAttributes).as("request attributes should exist").isNotNull();
    return requestAttributes;
}
Also used : RequestAttributes(org.springframework.web.context.request.RequestAttributes)

Example 33 with RequestAttributes

use of org.springframework.web.context.request.RequestAttributes 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 34 with RequestAttributes

use of org.springframework.web.context.request.RequestAttributes 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)

Example 35 with RequestAttributes

use of org.springframework.web.context.request.RequestAttributes in project spring-framework by spring-projects.

the class RequestContextHolderTests method assertRequestAttributes.

private static void assertRequestAttributes(boolean withinMockMvc) {
    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    assertThat(requestAttributes).isInstanceOf(ServletRequestAttributes.class);
    assertRequestAttributes(((ServletRequestAttributes) requestAttributes).getRequest(), withinMockMvc);
}
Also used : RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes)

Aggregations

RequestAttributes (org.springframework.web.context.request.RequestAttributes)76 ServletRequestAttributes (org.springframework.web.context.request.ServletRequestAttributes)46 HttpServletRequest (javax.servlet.http.HttpServletRequest)15 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)15 Test (org.junit.Test)11 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)3 DefaultPrettyPrinter (com.fasterxml.jackson.core.util.DefaultPrettyPrinter)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Response (javax.ws.rs.core.Response)3 XWorkRequestAttributes (org.onebusaway.presentation.impl.users.XWorkRequestAttributes)3 DBUnitTest (org.orcid.test.DBUnitTest)3 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)3 ProjectService (ca.corefacility.bioinformatics.irida.service.ProjectService)2 UserService (ca.corefacility.bioinformatics.irida.service.user.UserService)2 UserSession (com.haulmont.cuba.security.global.UserSession)2 ActionContext (com.opensymphony.xwork2.ActionContext)2 RecordDefinition (com.revolsys.record.schema.RecordDefinition)2 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)2