Search in sources :

Example 71 with RequestAttributes

use of org.springframework.web.context.request.RequestAttributes in project onebusaway-application-modules by camsys.

the class DefaultSearchLocationServiceImpl method setDefaultLocationForCurrentUser.

@Override
public void setDefaultLocationForCurrentUser(String locationName, double lat, double lon) {
    _currentUserService.setDefaultLocation(locationName, lat, lon);
    DefaultSearchLocation location = new DefaultSearchLocation(locationName, lat, lon, true);
    RequestAttributes attributes = RequestContextHolder.currentRequestAttributes();
    attributes.setAttribute(KEY_DEFAULT_SEARCH_LOCATION_FOR_SESSSION, location, RequestAttributes.SCOPE_SESSION);
}
Also used : DefaultSearchLocation(org.onebusaway.presentation.model.DefaultSearchLocation) RequestAttributes(org.springframework.web.context.request.RequestAttributes)

Example 72 with RequestAttributes

use of org.springframework.web.context.request.RequestAttributes in project onebusaway-application-modules by camsys.

the class DefaultSearchLocationServiceImpl method clearDefaultLocationForCurrentUser.

@Override
public void clearDefaultLocationForCurrentUser() {
    _currentUserService.clearDefaultLocation();
    RequestAttributes attributes = RequestContextHolder.currentRequestAttributes();
    attributes.removeAttribute(KEY_DEFAULT_SEARCH_LOCATION_FOR_SESSSION, RequestAttributes.SCOPE_SESSION);
}
Also used : RequestAttributes(org.springframework.web.context.request.RequestAttributes)

Example 73 with RequestAttributes

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

the class ServletWebArgumentResolverAdapter method getWebRequest.

@Override
protected NativeWebRequest getWebRequest() {
    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    Assert.state(requestAttributes instanceof ServletRequestAttributes, "No ServletRequestAttributes");
    ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
    return new ServletWebRequest(servletRequestAttributes.getRequest());
}
Also used : ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest)

Example 74 with RequestAttributes

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

the class MockMvc method perform.

/**
 * Perform a request and return a type that allows chaining further
 * actions, such as asserting expectations, on the result.
 * @param requestBuilder used to prepare the request to execute;
 * see static factory methods in
 * {@link org.springframework.test.web.servlet.request.MockMvcRequestBuilders}
 * @return an instance of {@link ResultActions} (never {@code null})
 * @see org.springframework.test.web.servlet.request.MockMvcRequestBuilders
 * @see org.springframework.test.web.servlet.result.MockMvcResultMatchers
 */
public ResultActions perform(RequestBuilder requestBuilder) throws Exception {
    if (this.defaultRequestBuilder != null && requestBuilder instanceof Mergeable) {
        requestBuilder = (RequestBuilder) ((Mergeable) requestBuilder).merge(this.defaultRequestBuilder);
    }
    MockHttpServletRequest request = requestBuilder.buildRequest(this.servletContext);
    AsyncContext asyncContext = request.getAsyncContext();
    MockHttpServletResponse mockResponse;
    HttpServletResponse servletResponse;
    if (asyncContext != null) {
        servletResponse = (HttpServletResponse) asyncContext.getResponse();
        mockResponse = unwrapResponseIfNecessary(servletResponse);
    } else {
        mockResponse = new MockHttpServletResponse();
        servletResponse = mockResponse;
    }
    if (this.defaultResponseCharacterEncoding != null) {
        mockResponse.setDefaultCharacterEncoding(this.defaultResponseCharacterEncoding.name());
    }
    if (requestBuilder instanceof SmartRequestBuilder) {
        request = ((SmartRequestBuilder) requestBuilder).postProcessRequest(request);
    }
    MvcResult mvcResult = new DefaultMvcResult(request, mockResponse);
    request.setAttribute(MVC_RESULT_ATTRIBUTE, mvcResult);
    RequestAttributes previousAttributes = RequestContextHolder.getRequestAttributes();
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request, servletResponse));
    MockFilterChain filterChain = new MockFilterChain(this.servlet, this.filters);
    filterChain.doFilter(request, servletResponse);
    if (DispatcherType.ASYNC.equals(request.getDispatcherType()) && asyncContext != null && !request.isAsyncStarted()) {
        asyncContext.complete();
    }
    applyDefaultResultActions(mvcResult);
    RequestContextHolder.setRequestAttributes(previousAttributes);
    return new ResultActions() {

        @Override
        public ResultActions andExpect(ResultMatcher matcher) throws Exception {
            matcher.match(mvcResult);
            return this;
        }

        @Override
        public ResultActions andDo(ResultHandler handler) throws Exception {
            handler.handle(mvcResult);
            return this;
        }

        @Override
        public MvcResult andReturn() {
            return mvcResult;
        }
    };
}
Also used : Mergeable(org.springframework.beans.Mergeable) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) AsyncContext(jakarta.servlet.AsyncContext) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 75 with RequestAttributes

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

the class ServletTestExecutionListenerTests method assertSetUpOutsideOfStelAttributeExists.

private void assertSetUpOutsideOfStelAttributeExists() {
    RequestAttributes requestAttributes = assertRequestAttributesExist();
    Object setUpOutsideOfStel = requestAttributes.getAttribute(SET_UP_OUTSIDE_OF_STEL, RequestAttributes.SCOPE_REQUEST);
    assertThat(setUpOutsideOfStel).as(SET_UP_OUTSIDE_OF_STEL + " should exist as a request attribute").isNotNull();
}
Also used : RequestAttributes(org.springframework.web.context.request.RequestAttributes)

Aggregations

RequestAttributes (org.springframework.web.context.request.RequestAttributes)81 ServletRequestAttributes (org.springframework.web.context.request.ServletRequestAttributes)51 HttpServletRequest (javax.servlet.http.HttpServletRequest)20 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