Search in sources :

Example 66 with RequestAttributes

use of org.springframework.web.context.request.RequestAttributes in project irida by phac-nml.

the class CartControllerTest method testGetCartMap.

@Test
public void testGetCartMap() {
    RequestAttributes ra = new ServletRequestAttributes(new MockHttpServletRequest());
    RequestContextHolder.setRequestAttributes(ra);
    Map<Project, Set<Sample>> selected = new HashMap<>();
    selected.put(project, samples);
    controller.setSelected(selected);
    Map<String, Object> cartMap = controller.getCartMap();
    assertTrue(cartMap.containsKey("projects"));
    @SuppressWarnings("unchecked") List<Map<String, Object>> pList = (List<Map<String, Object>>) cartMap.get("projects");
    Map<String, Object> projectMap = pList.iterator().next();
    assertTrue(projectMap.containsKey("samples"));
    @SuppressWarnings("unchecked") List<Map<String, Object>> sList = (List<Map<String, Object>>) projectMap.get("samples");
    for (Map<String, Object> map : sList) {
        assertTrue(map.containsKey("id"));
        assertTrue(map.containsKey("label"));
        assertTrue(map.containsKey("href"));
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) 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) Project(ca.corefacility.bioinformatics.irida.model.project.Project) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 67 with RequestAttributes

use of org.springframework.web.context.request.RequestAttributes in project irida by phac-nml.

the class UserControllerTest method setUp.

@Before
public void setUp() {
    userService = mock(UserService.class);
    projectService = mock(ProjectService.class);
    controller = new RESTUsersController(userService, projectService);
    // fake out the servlet response so that the URI builder will work.
    RequestAttributes ra = new ServletRequestAttributes(new MockHttpServletRequest());
    RequestContextHolder.setRequestAttributes(ra);
}
Also used : UserService(ca.corefacility.bioinformatics.irida.service.user.UserService) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) ProjectService(ca.corefacility.bioinformatics.irida.service.ProjectService) RESTUsersController(ca.corefacility.bioinformatics.irida.web.controller.api.RESTUsersController) RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) Before(org.junit.Before)

Example 68 with RequestAttributes

use of org.springframework.web.context.request.RequestAttributes in project spring-boot-jpa by ssherwood.

the class BootJpaApplication method errorAttributes.

/**
 * Customized ErrorAttribute bean.
 * We really need to find a cleaner way of handling these error messages.
 *
 * @return customized ErrorAttributes
 */
@Bean
public ErrorAttributes errorAttributes() {
    return new DefaultErrorAttributes() {

        @Override
        public Map<String, Object> getErrorAttributes(final RequestAttributes requestAttributes, final boolean includeStackTrace) {
            Map<String, Object> attributes = super.getErrorAttributes(requestAttributes, includeStackTrace);
            Throwable error = getError(requestAttributes);
            if (error instanceof MethodArgumentNotValidException) {
                MethodArgumentNotValidException ex = ((MethodArgumentNotValidException) error);
                attributes.put("errors", ex.getMessage());
            }
            return attributes;
        }
    };
}
Also used : RequestAttributes(org.springframework.web.context.request.RequestAttributes) DefaultErrorAttributes(org.springframework.boot.autoconfigure.web.DefaultErrorAttributes) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) Bean(org.springframework.context.annotation.Bean)

Example 69 with RequestAttributes

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

the class SecurityReactorContextConfigurationTests method createSubscriberIfNecessaryWhenNotServletRequestAttributesThenStillCreate.

@Test
public void createSubscriberIfNecessaryWhenNotServletRequestAttributesThenStillCreate() {
    RequestContextHolder.setRequestAttributes(new RequestAttributes() {

        @Override
        public Object getAttribute(String name, int scope) {
            return null;
        }

        @Override
        public void setAttribute(String name, Object value, int scope) {
        }

        @Override
        public void removeAttribute(String name, int scope) {
        }

        @Override
        public String[] getAttributeNames(int scope) {
            return new String[0];
        }

        @Override
        public void registerDestructionCallback(String name, Runnable callback, int scope) {
        }

        @Override
        public Object resolveReference(String key) {
            return null;
        }

        @Override
        public String getSessionId() {
            return null;
        }

        @Override
        public Object getSessionMutex() {
            return null;
        }
    });
    CoreSubscriber<Object> subscriber = this.subscriberRegistrar.createSubscriberIfNecessary(Operators.emptySubscriber());
    assertThat(subscriber).isInstanceOf(SecurityReactorContextConfiguration.SecurityReactorContextSubscriber.class);
}
Also used : RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) Test(org.junit.jupiter.api.Test)

Example 70 with RequestAttributes

use of org.springframework.web.context.request.RequestAttributes in project pinpoint by naver.

the class RequestContextPropagatingTaskDecoratorTest method requestContextShouldBePropagated.

@Test
public void requestContextShouldBePropagated() throws InterruptedException {
    // Given
    final int testCount = 100;
    final CountDownLatch completeLatch = new CountDownLatch(testCount);
    final AtomicBoolean verifiedFlag = new AtomicBoolean(true);
    final TestWorker.Callback workerCallback = new TestWorker.Callback() {

        @Override
        public void onRun() {
            RequestAttributes actualRequestAttributes = RequestContextHolder.getRequestAttributes();
            boolean verified = requestAttributes == actualRequestAttributes;
            verifiedFlag.compareAndSet(true, verified);
        }

        @Override
        public void onError() {
        // do nothing
        }
    };
    // When
    RequestContextHolder.setRequestAttributes(requestAttributes);
    for (int i = 0; i < testCount; i++) {
        executor.execute(new TestWorker(completeLatch, workerCallback));
    }
    completeLatch.await(5, TimeUnit.SECONDS);
    // Then
    boolean testVerified = verifiedFlag.get();
    Assert.assertTrue("RequestContext has not been propagated", testVerified);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RequestAttributes(org.springframework.web.context.request.RequestAttributes) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

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