Example usage for org.springframework.web.context.request RequestContextHolder resetRequestAttributes

List of usage examples for org.springframework.web.context.request RequestContextHolder resetRequestAttributes

Introduction

In this page you can find the example usage for org.springframework.web.context.request RequestContextHolder resetRequestAttributes.

Prototype

public static void resetRequestAttributes() 

Source Link

Document

Reset the RequestAttributes for the current thread.

Usage

From source file:br.com.caelum.vraptor.ioc.spring.SpringBasedContainerTest.java

@After
public void destroyContainer() {
    container.stop();
    container = null;
    RequestContextHolder.resetRequestAttributes();
    VRaptorRequestHolder.resetRequestForCurrentThread();
}

From source file:com.github.peholmst.springsecuritydemo.servlet.SpringApplicationServlet.java

@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    /*//from www  .  jav a  2  s  .c  o m
     * Resolve the locale from the request
     */
    final Locale locale = localeResolver.resolveLocale(request);
    if (logger.isDebugEnabled()) {
        logger.debug("Resolved locale [" + locale + "]");
    }

    /*
     * Store the locale in the LocaleContextHolder, making it available to
     * Spring.
     */
    LocaleContextHolder.setLocale(locale);
    ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
    RequestContextHolder.setRequestAttributes(requestAttributes);
    try {
        /*
         * We need to override the request to return the locale resolved by
         * Spring.
         */
        super.service(new HttpServletRequestWrapper(request) {
            @Override
            public Locale getLocale() {
                return locale;
            }
        }, response);
    } finally {
        if (!locale.equals(LocaleContextHolder.getLocale())) {
            /*
             * The locale in LocaleContextHolder was changed during the
             * request, so we have to update the resolver.
             */
            if (logger.isDebugEnabled()) {
                logger.debug("Locale changed, updating locale resolver");
            }
            localeResolver.setLocale(request, response, LocaleContextHolder.getLocale());
        }
        LocaleContextHolder.resetLocaleContext();
        RequestContextHolder.resetRequestAttributes();
    }
}

From source file:org.cloudfoundry.identity.uaa.provider.oauth.XOAuthAuthenticationManagerIT.java

@Test
public void invitedUser_becomesVerifiedOnAccept() {
    setUpInvitedUser();/*from www. j  ava2  s.c o m*/

    claims.remove("preferred_username");
    claims.put("preferred_username", "marissa@bloggs.com");
    mockToken();

    xoAuthAuthenticationManager.authenticate(xCodeToken);
    mockUaaServer.verify();

    ArgumentCaptor<ApplicationEvent> userArgumentCaptor = ArgumentCaptor.forClass(ApplicationEvent.class);
    verify(publisher, times(3)).publishEvent(userArgumentCaptor.capture());
    assertEquals(3, userArgumentCaptor.getAllValues().size());
    assertThat(userArgumentCaptor.getAllValues().get(0), instanceOf(InvitedUserAuthenticatedEvent.class));

    RequestContextHolder.resetRequestAttributes();
}

From source file:org.cloudfoundry.identity.uaa.provider.oauth.XOAuthAuthenticationManagerTest.java

@Test
public void invitedUser_becomesVerifiedOnAccept() throws Exception {
    getInvitedUser();//  w w  w . j  a  va2 s  .co m

    claims.remove("preferred_username");
    claims.put("preferred_username", "marissa@bloggs.com");
    mockToken();

    xoAuthAuthenticationManager.authenticate(xCodeToken);
    mockUaaServer.verify();

    ArgumentCaptor<ApplicationEvent> userArgumentCaptor = ArgumentCaptor.forClass(ApplicationEvent.class);
    verify(publisher, times(3)).publishEvent(userArgumentCaptor.capture());
    assertEquals(3, userArgumentCaptor.getAllValues().size());
    assertThat(userArgumentCaptor.getAllValues().get(0), instanceOf(InvitedUserAuthenticatedEvent.class));

    RequestContextHolder.resetRequestAttributes();
}

From source file:org.codehaus.groovy.grails.web.util.WebUtils.java

/**
 * Removes any GrailsWebRequest instance from the current request.
 *//*from   w w  w. j  ava2  s. c o m*/
public static void clearGrailsWebRequest() {
    RequestAttributes reqAttrs = RequestContextHolder.getRequestAttributes();
    if (reqAttrs != null) {
        // First remove the web request from the HTTP request attributes.
        GrailsWebRequest webRequest = (GrailsWebRequest) reqAttrs;
        webRequest.getRequest().removeAttribute(GrailsApplicationAttributes.WEB_REQUEST);

        // Now remove it from RequestContextHolder.
        RequestContextHolder.resetRequestAttributes();
    }
}

From source file:org.geoserver.importer.Importer.java

public Long runAsync(final ImportContext context, final ImportFilter filter, final boolean init) {
    // we store the current request spring context
    final RequestAttributes parentRequestAttributes = RequestContextHolder.getRequestAttributes();
    final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    Thread parentThread = Thread.currentThread();
    // creating an asynchronous importer job
    return jobs.submit(new Job<ImportContext>() {

        @Override//from   w w  w  . ja  v a  2s  .  co  m
        protected ImportContext call(ProgressMonitor monitor) throws Exception {
            final Authentication oldAuth = SecurityContextHolder.getContext().getAuthentication();
            try {
                // set the parent request spring context, some interceptors like the security ones
                // for example may need to have access to the original request attributes
                RequestContextHolder.setRequestAttributes(parentRequestAttributes);
                SecurityContextHolder.getContext().setAuthentication(auth);
                if (init) {
                    init(context, true);
                }
                run(context, filter, monitor);
                return context;
            } finally {
                if (Thread.currentThread() != parentThread) {
                    // cleaning request spring context for the current thread
                    RequestContextHolder.resetRequestAttributes();
                    SecurityContextHolder.getContext().setAuthentication(oldAuth);
                }
            }
        }

        @Override
        public String toString() {
            return "Processing import " + context.getId();
        }
    });
}

From source file:org.springframework.test.context.web.ServletTestExecutionListener.java

/**
 * If the {@link #RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE} in the supplied
 * {@code TestContext} has a value of {@link Boolean#TRUE}, this method will
 * (1) clean up thread-local state after each test method by {@linkplain
 * RequestContextHolder#resetRequestAttributes() resetting} Spring Web's
 * {@code RequestContextHolder} and (2) ensure that new mocks are injected
 * into the test instance for subsequent tests by setting the
 * {@link DependencyInjectionTestExecutionListener#REINJECT_DEPENDENCIES_ATTRIBUTE}
 * in the test context to {@code true}.// w  w w . jav  a  2  s .c o m
 * <p>The {@link #RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE} and
 * {@link #POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE} will be subsequently
 * removed from the test context, regardless of their values.
 * @see TestExecutionListener#afterTestMethod(TestContext)
 */
@Override
public void afterTestMethod(TestContext testContext) throws Exception {
    if (Boolean.TRUE.equals(testContext.getAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE))) {
        if (logger.isDebugEnabled()) {
            logger.debug(String.format("Resetting RequestContextHolder for test context %s.", testContext));
        }
        RequestContextHolder.resetRequestAttributes();
        testContext.setAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE,
                Boolean.TRUE);
    }
    testContext.removeAttribute(POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE);
    testContext.removeAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE);
}

From source file:org.springframework.test.context.web.WebTestExecutionListener.java

/**
 * TODO [SPR-9864] Document overridden afterTestMethod().
 *
 * @see org.springframework.test.context.support.AbstractTestExecutionListener#afterTestMethod(org.springframework.test.context.TestContext)
 *//* w w w.  jav a  2s.  co m*/
@Override
public void afterTestMethod(TestContext testContext) throws Exception {
    if (logger.isDebugEnabled()) {
        logger.debug(String.format("Resetting RequestContextHolder for test context %s.", testContext));
    }
    RequestContextHolder.resetRequestAttributes();
}