Search in sources :

Example 6 with ServletRequestEvent

use of javax.servlet.ServletRequestEvent in project opennms by OpenNMS.

the class DispatcherFilter method doFilter.

@Override
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
    final ServletRequestEvent sre = new ServletRequestEvent(m_filterConfig.getServletContext(), request);
    m_controller.getRequestListener().requestInitialized(sre);
    try {
        if (request instanceof HttpServletRequest && response instanceof HttpServletResponse) {
            final HttpServletRequest req = new AttributeEventRequest(m_filterConfig.getServletContext(), m_controller.getRequestAttributeListener(), (HttpServletRequest) request);
            m_controller.getDispatcher().dispatch(req, (HttpServletResponse) response, chain);
        } else {
            chain.doFilter(request, response);
        }
    } finally {
        m_controller.getRequestListener().requestDestroyed(sre);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequestEvent(javax.servlet.ServletRequestEvent) HttpServletResponse(javax.servlet.http.HttpServletResponse)

Example 7 with ServletRequestEvent

use of javax.servlet.ServletRequestEvent in project wildfly by wildfly.

the class UndertowContextTestCase method addRequestListener.

@Test
public void addRequestListener() throws ServletException {
    ServletRequestListener listener = mock(ServletRequestListener.class);
    ServletContext context = mock(ServletContext.class);
    ServletRequest request = mock(ServletRequest.class);
    ApplicationListeners listeners = new ApplicationListeners(Collections.<ManagedListener>emptyList(), context);
    ArgumentCaptor<ServletRequestEvent> event = ArgumentCaptor.forClass(ServletRequestEvent.class);
    when(this.deployment.getApplicationListeners()).thenReturn(listeners);
    this.context.addRequestListener(listener);
    listeners.start();
    listeners.requestInitialized(request);
    verify(listener).requestInitialized(event.capture());
    assertSame(request, event.getValue().getServletRequest());
    assertSame(context, event.getValue().getServletContext());
    event = ArgumentCaptor.forClass(ServletRequestEvent.class);
    listeners.requestDestroyed(request);
    verify(listener).requestDestroyed(event.capture());
    assertSame(request, event.getValue().getServletRequest());
    assertSame(context, event.getValue().getServletContext());
}
Also used : ServletRequest(javax.servlet.ServletRequest) ServletRequestListener(javax.servlet.ServletRequestListener) ApplicationListeners(io.undertow.servlet.core.ApplicationListeners) ServletContext(javax.servlet.ServletContext) ServletRequestEvent(javax.servlet.ServletRequestEvent) Test(org.junit.Test)

Example 8 with ServletRequestEvent

use of javax.servlet.ServletRequestEvent in project tomee by apache.

the class HttpRequestImpl method destroy.

public void destroy() {
    final boolean openejbRequestDestroyed = getAttribute("openejb_requestDestroyed") == null;
    if (listeners != null && !listeners.isEmpty()) {
        if (begin != null && end != null && openejbRequestDestroyed) {
            end.requestDestroyed(new ServletRequestEvent(getServletContext(), this));
        }
        final ServletRequestEvent event = new ServletRequestEvent(getServletContext(), this);
        for (final ServletRequestListener listener : listeners) {
            listener.requestDestroyed(event);
        }
    }
    if (begin != null && openejbRequestDestroyed) {
        setAttribute("openejb_requestDestroyed", "ok");
        begin.requestDestroyed(new ServletRequestEvent(getServletContext(), this));
    }
}
Also used : ServletRequestListener(javax.servlet.ServletRequestListener) ServletRequestEvent(javax.servlet.ServletRequestEvent)

Example 9 with ServletRequestEvent

use of javax.servlet.ServletRequestEvent in project tomcat by apache.

the class StandardContext method fireRequestDestroyEvent.

@Override
public boolean fireRequestDestroyEvent(ServletRequest request) {
    Object[] instances = getApplicationEventListeners();
    if ((instances != null) && (instances.length > 0)) {
        ServletRequestEvent event = new ServletRequestEvent(getServletContext(), request);
        for (int i = 0; i < instances.length; i++) {
            int j = (instances.length - 1) - i;
            if (instances[j] == null)
                continue;
            if (!(instances[j] instanceof ServletRequestListener))
                continue;
            ServletRequestListener listener = (ServletRequestListener) instances[j];
            try {
                listener.requestDestroyed(event);
            } catch (Throwable t) {
                ExceptionUtils.handleThrowable(t);
                getLogger().error(sm.getString("standardContext.requestListener.requestInit", instances[j].getClass().getName()), t);
                request.setAttribute(RequestDispatcher.ERROR_EXCEPTION, t);
                return false;
            }
        }
    }
    return true;
}
Also used : ServletRequestListener(javax.servlet.ServletRequestListener) ServletRequestEvent(javax.servlet.ServletRequestEvent) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint)

Example 10 with ServletRequestEvent

use of javax.servlet.ServletRequestEvent in project jetty.project by eclipse.

the class RequestTest method testBadMultiPart.

@Test
public void testBadMultiPart() throws Exception {
    //a bad multipart where one of the fields has no name
    final File testTmpDir = File.createTempFile("badmptest", null);
    if (testTmpDir.exists())
        testTmpDir.delete();
    testTmpDir.mkdir();
    testTmpDir.deleteOnExit();
    assertTrue(testTmpDir.list().length == 0);
    ContextHandler contextHandler = new ContextHandler();
    contextHandler.setContextPath("/foo");
    contextHandler.setResourceBase(".");
    contextHandler.setHandler(new BadMultiPartRequestHandler(testTmpDir));
    contextHandler.addEventListener(new MultiPartCleanerListener() {

        @Override
        public void requestDestroyed(ServletRequestEvent sre) {
            MultiPartInputStreamParser m = (MultiPartInputStreamParser) sre.getServletRequest().getAttribute(Request.__MULTIPART_INPUT_STREAM);
            ContextHandler.Context c = (ContextHandler.Context) sre.getServletRequest().getAttribute(Request.__MULTIPART_CONTEXT);
            assertNotNull(m);
            assertNotNull(c);
            assertTrue(c == sre.getServletContext());
            super.requestDestroyed(sre);
            String[] files = testTmpDir.list();
            assertTrue(files.length == 0);
        }
    });
    _server.stop();
    _server.setHandler(contextHandler);
    _server.start();
    String multipart = "--AaB03x\r\n" + "content-disposition: form-data; name=\"xxx\"\r\n" + "\r\n" + "Joe Blow\r\n" + "--AaB03x\r\n" + "content-disposition: form-data;  filename=\"foo.upload\"\r\n" + "Content-Type: text/plain;charset=ISO-8859-1\r\n" + "\r\n" + "000000000000000000000000000000000000000000000000000\r\n" + "--AaB03x--\r\n";
    String request = "GET /foo/x.html HTTP/1.1\r\n" + "Host: whatever\r\n" + "Content-Type: multipart/form-data; boundary=\"AaB03x\"\r\n" + "Content-Length: " + multipart.getBytes().length + "\r\n" + "Connection: close\r\n" + "\r\n" + multipart;
    try (StacklessLogging stackless = new StacklessLogging(HttpChannel.class)) {
        String responses = _connector.getResponse(request);
        //System.err.println(responses);
        assertTrue(responses.startsWith("HTTP/1.1 500"));
    }
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) ServletRequestEvent(javax.servlet.ServletRequestEvent) MultiPartInputStreamParser(org.eclipse.jetty.util.MultiPartInputStreamParser) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) File(java.io.File) Test(org.junit.Test)

Aggregations

ServletRequestEvent (javax.servlet.ServletRequestEvent)16 Test (org.junit.Test)9 ServletRequestListener (javax.servlet.ServletRequestListener)6 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 MockRunnable (org.springframework.core.task.MockRunnable)3 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)3 MockServletContext (org.springframework.mock.web.test.MockServletContext)3 File (java.io.File)2 HttpSession (javax.servlet.http.HttpSession)2 HttpSessionBindingEvent (javax.servlet.http.HttpSessionBindingEvent)2 ServletsMockitoUtil.createHttpSessionBindingEvent (jodd.petite.ServletsMockitoUtil.createHttpSessionBindingEvent)2 ServletsMockitoUtil.createServletRequestEvent (jodd.petite.ServletsMockitoUtil.createServletRequestEvent)2 Ses (jodd.petite.tst.Ses)2 RequestContextListener (jodd.servlet.RequestContextListener)2 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)2 MultiPartInputStreamParser (org.eclipse.jetty.util.MultiPartInputStreamParser)2 ApplicationListeners (io.undertow.servlet.core.ApplicationListeners)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 ServletContext (javax.servlet.ServletContext)1