Java Code Examples for javax.servlet.ServletRequestEvent

The following examples show how to use javax.servlet.ServletRequestEvent. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: spring-analysis-note   Source File: RequestContextListener.java    License: MIT License 6 votes vote down vote up
@Override
public void requestDestroyed(ServletRequestEvent requestEvent) {
	ServletRequestAttributes attributes = null;
	Object reqAttr = requestEvent.getServletRequest().getAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE);
	if (reqAttr instanceof ServletRequestAttributes) {
		attributes = (ServletRequestAttributes) reqAttr;
	}
	RequestAttributes threadAttributes = RequestContextHolder.getRequestAttributes();
	if (threadAttributes != null) {
		// We're assumably within the original request thread...
		LocaleContextHolder.resetLocaleContext();
		RequestContextHolder.resetRequestAttributes();
		if (attributes == null && threadAttributes instanceof ServletRequestAttributes) {
			attributes = (ServletRequestAttributes) threadAttributes;
		}
	}
	if (attributes != null) {
		attributes.requestCompleted();
	}
}
 
Example 2
Source Project: spring-analysis-note   Source File: RequestContextListenerTests.java    License: MIT License 6 votes vote down vote up
@Test
public void requestContextListenerWithSameThread() {
	RequestContextListener listener = new RequestContextListener();
	MockServletContext context = new MockServletContext();
	MockHttpServletRequest request = new MockHttpServletRequest(context);
	request.setAttribute("test", "value");

	assertNull(RequestContextHolder.getRequestAttributes());
	listener.requestInitialized(new ServletRequestEvent(context, request));
	assertNotNull(RequestContextHolder.getRequestAttributes());
	assertEquals("value",
			RequestContextHolder.getRequestAttributes().getAttribute("test", RequestAttributes.SCOPE_REQUEST));
	MockRunnable runnable = new MockRunnable();
	RequestContextHolder.getRequestAttributes().registerDestructionCallback(
			"test", runnable, RequestAttributes.SCOPE_REQUEST);

	listener.requestDestroyed(new ServletRequestEvent(context, request));
	assertNull(RequestContextHolder.getRequestAttributes());
	assertTrue(runnable.wasExecuted());
}
 
Example 3
Source Project: quarkus-http   Source File: ApplicationListeners.java    License: Apache License 2.0 6 votes vote down vote up
public void requestDestroyed(final ServletRequest request) {
    if(!started) {
        return;
    }
    if(servletRequestListeners.length > 0) {
        final ServletRequestEvent sre = new ServletRequestEvent(servletContext, request);
        for (int i = servletRequestListeners.length - 1; i >= 0; --i) {
            ManagedListener listener = servletRequestListeners[i];
            try {
                this.<ServletRequestListener>get(listener).requestDestroyed(sre);
            } catch (Exception e) {
                UndertowServletLogger.REQUEST_LOGGER.errorInvokingListener("requestDestroyed", listener.getListenerInfo().getListenerClass(), e);
            }
        }
    }
}
 
Example 4
Source Project: java-technology-stack   Source File: RequestContextListener.java    License: MIT License 6 votes vote down vote up
@Override
public void requestDestroyed(ServletRequestEvent requestEvent) {
	ServletRequestAttributes attributes = null;
	Object reqAttr = requestEvent.getServletRequest().getAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE);
	if (reqAttr instanceof ServletRequestAttributes) {
		attributes = (ServletRequestAttributes) reqAttr;
	}
	RequestAttributes threadAttributes = RequestContextHolder.getRequestAttributes();
	if (threadAttributes != null) {
		// We're assumably within the original request thread...
		LocaleContextHolder.resetLocaleContext();
		RequestContextHolder.resetRequestAttributes();
		if (attributes == null && threadAttributes instanceof ServletRequestAttributes) {
			attributes = (ServletRequestAttributes) threadAttributes;
		}
	}
	if (attributes != null) {
		attributes.requestCompleted();
	}
}
 
Example 5
@Test
public void requestContextListenerWithSameThread() {
	RequestContextListener listener = new RequestContextListener();
	MockServletContext context = new MockServletContext();
	MockHttpServletRequest request = new MockHttpServletRequest(context);
	request.setAttribute("test", "value");

	assertNull(RequestContextHolder.getRequestAttributes());
	listener.requestInitialized(new ServletRequestEvent(context, request));
	assertNotNull(RequestContextHolder.getRequestAttributes());
	assertEquals("value",
			RequestContextHolder.getRequestAttributes().getAttribute("test", RequestAttributes.SCOPE_REQUEST));
	MockRunnable runnable = new MockRunnable();
	RequestContextHolder.getRequestAttributes().registerDestructionCallback(
			"test", runnable, RequestAttributes.SCOPE_REQUEST);

	listener.requestDestroyed(new ServletRequestEvent(context, request));
	assertNull(RequestContextHolder.getRequestAttributes());
	assertTrue(runnable.wasExecuted());
}
 
Example 6
@Test
public void requestContextListenerWithSameThreadAndAttributesGone() {
	RequestContextListener listener = new RequestContextListener();
	MockServletContext context = new MockServletContext();
	MockHttpServletRequest request = new MockHttpServletRequest(context);
	request.setAttribute("test", "value");

	assertNull(RequestContextHolder.getRequestAttributes());
	listener.requestInitialized(new ServletRequestEvent(context, request));
	assertNotNull(RequestContextHolder.getRequestAttributes());
	assertEquals("value",
			RequestContextHolder.getRequestAttributes().getAttribute("test", RequestAttributes.SCOPE_REQUEST));
	MockRunnable runnable = new MockRunnable();
	RequestContextHolder.getRequestAttributes().registerDestructionCallback(
			"test", runnable, RequestAttributes.SCOPE_REQUEST);

	request.clearAttributes();
	listener.requestDestroyed(new ServletRequestEvent(context, request));
	assertNull(RequestContextHolder.getRequestAttributes());
	assertTrue(runnable.wasExecuted());
}
 
Example 7
Source Project: lams   Source File: RequestContextListener.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public void requestDestroyed(ServletRequestEvent requestEvent) {
	ServletRequestAttributes attributes = null;
	Object reqAttr = requestEvent.getServletRequest().getAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE);
	if (reqAttr instanceof ServletRequestAttributes) {
		attributes = (ServletRequestAttributes) reqAttr;
	}
	RequestAttributes threadAttributes = RequestContextHolder.getRequestAttributes();
	if (threadAttributes != null) {
		// We're assumably within the original request thread...
		LocaleContextHolder.resetLocaleContext();
		RequestContextHolder.resetRequestAttributes();
		if (attributes == null && threadAttributes instanceof ServletRequestAttributes) {
			attributes = (ServletRequestAttributes) threadAttributes;
		}
	}
	if (attributes != null) {
		attributes.requestCompleted();
	}
}
 
Example 8
@Test
public void testWithUnitializedRequest() {
    // destroy a wrapper of the root request
    SlingHttpServletRequest destroyedRequest = new SlingHttpServletRequestWrapper(request);

    // but adapt from a wrapper of a wrapper of that wrapper
    SlingHttpServletRequest adaptableRequest = new SlingHttpServletRequestWrapper(new SlingHttpServletRequestWrapper(destroyedRequest));

    TestModel model = factory.getAdapter(adaptableRequest, TestModel.class);
    assertEquals("teststring", model.testString);

    assertNoneDisposed();

    factory.requestDestroyed(new ServletRequestEvent(servletContext, destroyedRequest));

    assertNoneDisposed();
}
 
Example 9
@Override
public void requestDestroyed(ServletRequestEvent requestEvent) {
	ServletRequestAttributes attributes = null;
	Object reqAttr = requestEvent.getServletRequest().getAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE);
	if (reqAttr instanceof ServletRequestAttributes) {
		attributes = (ServletRequestAttributes) reqAttr;
	}
	RequestAttributes threadAttributes = RequestContextHolder.getRequestAttributes();
	if (threadAttributes != null) {
		// We're assumably within the original request thread...
		LocaleContextHolder.resetLocaleContext();
		RequestContextHolder.resetRequestAttributes();
		if (attributes == null && threadAttributes instanceof ServletRequestAttributes) {
			attributes = (ServletRequestAttributes) threadAttributes;
		}
	}
	if (attributes != null) {
		attributes.requestCompleted();
	}
}
 
Example 10
@Test
public void requestContextListenerWithSameThread() {
	RequestContextListener listener = new RequestContextListener();
	MockServletContext context = new MockServletContext();
	MockHttpServletRequest request = new MockHttpServletRequest(context);
	request.setAttribute("test", "value");

	assertNull(RequestContextHolder.getRequestAttributes());
	listener.requestInitialized(new ServletRequestEvent(context, request));
	assertNotNull(RequestContextHolder.getRequestAttributes());
	assertEquals("value",
			RequestContextHolder.getRequestAttributes().getAttribute("test", RequestAttributes.SCOPE_REQUEST));
	MockRunnable runnable = new MockRunnable();
	RequestContextHolder.getRequestAttributes().registerDestructionCallback(
			"test", runnable, RequestAttributes.SCOPE_REQUEST);

	listener.requestDestroyed(new ServletRequestEvent(context, request));
	assertNull(RequestContextHolder.getRequestAttributes());
	assertTrue(runnable.wasExecuted());
}
 
Example 11
/**
 * Test of getPage method, of class fr.paris.lutece.portal.web.search.SearchApp.
 * 
 * @throws SiteMessageException
 */
public void testGetPage( ) throws SiteMessageException
{
    System.out.println( "getPage" );

    MockHttpServletRequest request = new MockHttpServletRequest( );
    request.addParameter( "query", "lutece" );
    request.addParameter( "items_per_page", "5" );

    RequestContextListener listener = new RequestContextListener( );
    ServletContext context = new MockServletContext( );
    listener.requestInitialized( new ServletRequestEvent( context, request ) );

    int nMode = 0;
    Plugin plugin = null;
    SearchApp instance = SpringContextService.getBean( "core.xpage.search" );

    assertNotNull( instance.getPage( request, nMode, plugin ) );

    listener.requestDestroyed( new ServletRequestEvent( context, request ) );
}
 
Example 12
public void requestDestroyed(ServletRequestEvent servletRequestEvent) {
	ServletRequestAttributes attributes = (ServletRequestAttributes) servletRequestEvent.getServletRequest()
			.getAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE);
	ServletRequestAttributes threadAttributes =
			(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
	if (threadAttributes != null) {
		if (attributes == null) {
			attributes = threadAttributes;
		}
		RequestContextHolder.setRequestAttributes(null);
		LocaleContextHolder.setLocale(null);
	}
	if (attributes != null) {
		attributes.requestCompleted();
	}
}
 
Example 13
Source Project: deltaspike   Source File: RequestResponseHolderListener.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void requestDestroyed(ServletRequestEvent sre)
{
    if (activated)
    {
        RequestResponseHolder.REQUEST.release();
    }
}
 
Example 14
Source Project: syncope   Source File: ThreadLocalCleanupListener.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void requestDestroyed(final ServletRequestEvent sre) {
    FormatUtils.clear();

    ThreadClassLoaderManager.clearInstance();
    CurrentLocale.clear();
}
 
Example 15
public void requestInitialized(ServletRequestEvent servletRequestEvent) {
	if (servletRequestEvent.getServletRequest() instanceof HttpServletRequest) {
		HttpServletRequest request = (HttpServletRequest) servletRequestEvent.getServletRequest();
		ServletRequestAttributes attributes = new ServletRequestAttributes(request);
		request.setAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE, attributes);
		LocaleContextHolder.setLocale(request.getLocale());
		RequestContextHolder.setRequestAttributes(attributes);
	}
}
 
Example 16
Source Project: spring-analysis-note   Source File: RequestContextListenerTests.java    License: MIT License 5 votes vote down vote up
@Test
public void requestContextListenerWithDifferentThread() {
	final RequestContextListener listener = new RequestContextListener();
	final MockServletContext context = new MockServletContext();
	final MockHttpServletRequest request = new MockHttpServletRequest(context);
	request.setAttribute("test", "value");

	assertNull(RequestContextHolder.getRequestAttributes());
	listener.requestInitialized(new ServletRequestEvent(context, request));
	assertNotNull(RequestContextHolder.getRequestAttributes());
	assertEquals("value",
			RequestContextHolder.getRequestAttributes().getAttribute("test", RequestAttributes.SCOPE_REQUEST));
	MockRunnable runnable = new MockRunnable();
	RequestContextHolder.getRequestAttributes().registerDestructionCallback(
			"test", runnable, RequestAttributes.SCOPE_REQUEST);

	// Execute requestDestroyed callback in different thread.
	Thread thread = new Thread() {
		@Override
		public void run() {
			listener.requestDestroyed(new ServletRequestEvent(context, request));
		}
	};
	thread.start();
	try {
		thread.join();
	}
	catch (InterruptedException ex) {
	}
	// Still bound to original thread, but at least completed.
	assertNotNull(RequestContextHolder.getRequestAttributes());
	assertTrue(runnable.wasExecuted());

	// Check that a repeated execution in the same thread works and performs cleanup.
	listener.requestInitialized(new ServletRequestEvent(context, request));
	listener.requestDestroyed(new ServletRequestEvent(context, request));
	assertNull(RequestContextHolder.getRequestAttributes());
}
 
Example 17
Source Project: quarkus-http   Source File: ApplicationListeners.java    License: Apache License 2.0 5 votes vote down vote up
public void requestInitialized(final ServletRequest request) {
    if(!started) {
        return;
    }
    if(servletRequestListeners.length > 0) {
        final ServletRequestEvent sre = new ServletRequestEvent(servletContext, request);
        for (int i = 0; i < servletRequestListeners.length; ++i) {
            this.<ServletRequestListener>get(servletRequestListeners[i]).requestInitialized(sre);
        }
    }
}
 
Example 18
Source Project: quarkus-http   Source File: SimpleRequestListener.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void requestInitialized(ServletRequestEvent sre) {
    if (IN_REQUEST.get() != null) {
        nestedInvocationOccured = true;
    }
    IN_REQUEST.set(Boolean.TRUE);
}
 
Example 19
Source Project: quarkus-http   Source File: SimpleRequestListener.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void requestDestroyed(ServletRequestEvent sre) {
    if (IN_REQUEST.get() == null) {
        nestedInvocationOccured = true;
    }
    IN_REQUEST.remove();
}
 
Example 20
Source Project: deltaspike   Source File: CdiServletRequestListener.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void requestDestroyed(ServletRequestEvent servletRequestEvent)
{
    LOG.log(Level.FINER,"Request done.");
    ContextControl contextControl = (ContextControl)servletRequestEvent.getServletRequest()
            .getAttribute(CDI_REQ_CONTEXT);
    contextControl.stopContext(RequestScoped.class);
}
 
Example 21
Source Project: kardio   Source File: SHDRequestListener.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void requestInitialized(ServletRequestEvent requestEvent) {
    HttpServletRequest request = (HttpServletRequest) requestEvent.getServletRequest();
    String endPointName = request.getRequestURI();
    endPointName = endPointName.substring(1);
    endPointName = endPointName.replaceAll("/", "_");
    prometheusMetricService.initializeMetricData(request, endPointName);
}
 
Example 22
Source Project: java-technology-stack   Source File: RequestContextListener.java    License: MIT License 5 votes vote down vote up
@Override
public void requestInitialized(ServletRequestEvent requestEvent) {
	if (!(requestEvent.getServletRequest() instanceof HttpServletRequest)) {
		throw new IllegalArgumentException(
				"Request is not an HttpServletRequest: " + requestEvent.getServletRequest());
	}
	HttpServletRequest request = (HttpServletRequest) requestEvent.getServletRequest();
	ServletRequestAttributes attributes = new ServletRequestAttributes(request);
	request.setAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE, attributes);
	LocaleContextHolder.setLocale(request.getLocale());
	RequestContextHolder.setRequestAttributes(attributes);
}
 
Example 23
@Test
public void requestContextListenerWithDifferentThread() {
	final RequestContextListener listener = new RequestContextListener();
	final MockServletContext context = new MockServletContext();
	final MockHttpServletRequest request = new MockHttpServletRequest(context);
	request.setAttribute("test", "value");

	assertNull(RequestContextHolder.getRequestAttributes());
	listener.requestInitialized(new ServletRequestEvent(context, request));
	assertNotNull(RequestContextHolder.getRequestAttributes());
	assertEquals("value",
			RequestContextHolder.getRequestAttributes().getAttribute("test", RequestAttributes.SCOPE_REQUEST));
	MockRunnable runnable = new MockRunnable();
	RequestContextHolder.getRequestAttributes().registerDestructionCallback(
			"test", runnable, RequestAttributes.SCOPE_REQUEST);

	// Execute requestDestroyed callback in different thread.
	Thread thread = new Thread() {
		@Override
		public void run() {
			listener.requestDestroyed(new ServletRequestEvent(context, request));
		}
	};
	thread.start();
	try {
		thread.join();
	}
	catch (InterruptedException ex) {
	}
	// Still bound to original thread, but at least completed.
	assertNotNull(RequestContextHolder.getRequestAttributes());
	assertTrue(runnable.wasExecuted());

	// Check that a repeated execution in the same thread works and performs cleanup.
	listener.requestInitialized(new ServletRequestEvent(context, request));
	listener.requestDestroyed(new ServletRequestEvent(context, request));
	assertNull(RequestContextHolder.getRequestAttributes());
}
 
Example 24
/**
 * Fire the request initialized event
 *
 * @param request the request
 */
private void requestInitialized(ServletRequest request) {
    if (!requestListeners.isEmpty()) {
        requestListeners.stream().forEach((servletRequestListener) -> {
            servletRequestListener.requestInitialized(new ServletRequestEvent(this, request));
        });
    }
}
 
Example 25
Source Project: tomee   Source File: HttpRequestImpl.java    License: Apache License 2.0 5 votes vote down vote up
public void init() {
    if (begin != null && getAttribute("openejb_requestInitialized") == null) {
        setAttribute("openejb_requestInitialized", "ok"); // if called again we loose the request scope
        begin.requestInitialized(new ServletRequestEvent(getServletContext(), this));
    }

    listeners = LightweightWebAppBuilderListenerExtractor.findByTypeForContext(contextPath, ServletRequestListener.class);
    if (!listeners.isEmpty()) {
        final ServletRequestEvent event = new ServletRequestEvent(getServletContext(), this);
        for (final ServletRequestListener listener : listeners) {
            listener.requestInitialized(event);
        }
    }
}
 
Example 26
Source Project: lams   Source File: RequestContextListener.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public void requestInitialized(ServletRequestEvent requestEvent) {
	if (!(requestEvent.getServletRequest() instanceof HttpServletRequest)) {
		throw new IllegalArgumentException(
				"Request is not an HttpServletRequest: " + requestEvent.getServletRequest());
	}
	HttpServletRequest request = (HttpServletRequest) requestEvent.getServletRequest();
	ServletRequestAttributes attributes = new ServletRequestAttributes(request);
	request.setAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE, attributes);
	LocaleContextHolder.setLocale(request.getLocale());
	RequestContextHolder.setRequestAttributes(attributes);
}
 
Example 27
Source Project: lams   Source File: ApplicationListeners.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void requestInitialized(final ServletRequest request) {
    if(!started) {
        return;
    }
    if(servletRequestListeners.length > 0) {
        final ServletRequestEvent sre = new ServletRequestEvent(servletContext, request);
        for (int i = 0; i < servletRequestListeners.length; ++i) {
            this.<ServletRequestListener>get(servletRequestListeners[i]).requestInitialized(sre);
        }
    }
}
 
Example 28
Source Project: trimou   Source File: RequestListener.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void requestInitialized(ServletRequestEvent event) {
    if (event.getServletRequest() instanceof HttpServletRequest) {
        RequestHolder.requestInitialized((HttpServletRequest) event
                .getServletRequest());
    } else {
        throw new IllegalStateException(
                "This extension only supports HTTP Servlet lifecycle");
    }
}
 
Example 29
Source Project: tomee   Source File: BeginWebBeansListener.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void requestDestroyed(final ServletRequestEvent event) {
    if (webBeansContext == null) {
        return;
    }

    if (logger.isDebugEnabled()) {
        logger.debug("Destroying a request : [{0}]", event == null ? "null" : event.getServletRequest().getRemoteAddr());
    }

    final Object oldContext;
    if (event != null) {
        oldContext = event.getServletRequest().getAttribute(contextKey);
    } else {
        oldContext = null;
    }

    try {
        // clean up the EL caches after each request
        final ELContextStore elStore = ELContextStore.getInstance(false);
        if (elStore != null) {
            elStore.destroyELContextStore();
        }

        contextsService.endContext(RequestScoped.class, event);
        if (webBeansContext instanceof WebappWebBeansContext) { // end after child
            ((WebappWebBeansContext) webBeansContext).getParent().getContextsService().endContext(RequestScoped.class, event);
        }
    } finally {
        contextsService.removeThreadLocals();
        ThreadSingletonServiceImpl.enter((WebBeansContext) oldContext);
    }
}
 
Example 30
@Test
public void test_work(){
	
	MockServletContext context = new MockServletContext();
	MockHttpServletRequest req1 = new MockHttpServletRequest();
	MockHttpServletRequest req2 = new MockHttpServletRequest();
	
	SessionsCounterService service = new SessionsCounterService();
	
	assertFalse(service.isInited());
	service.init(context);
	assertTrue(service.isInited());
	assertEquals(0, service.getSessionsCount(req1));
	assertEquals(0, service.getSessionsCount(req2));
	
	service.requestInitialized(new ServletRequestEvent(context, req1));
	service.sessionCreated(new HttpSessionEvent(req1.getSession()));
	service.requestDestroyed(new ServletRequestEvent(context, req1));
	assertEquals(1, service.getSessionsCount(req1));
	assertEquals(1, service.getSessionsCount(req2));
	
	
	service.requestInitialized(new ServletRequestEvent(context, req2));
	service.sessionCreated(new HttpSessionEvent(req2.getSession()));
	assertEquals(2, service.getSessionsCount(req1));
	assertEquals(2, service.getSessionsCount(req2));
	
	service.sessionDestroyed(new HttpSessionEvent(req1.getSession()));
	assertEquals(1, service.getSessionsCount(req1));
	assertEquals(1, service.getSessionsCount(req2));
	
	service.sessionDestroyed(new HttpSessionEvent(req2.getSession()));
	assertEquals(0, service.getSessionsCount(req1));
	assertEquals(0, service.getSessionsCount(req2));
	
}