Search in sources :

Example 1 with ServletRequestListener

use of javax.servlet.ServletRequestListener in project felix by apache.

the class EventListenerTest method testServletRequestListener.

/**
 * Tests {@link ServletRequestListener}s
 */
@Test
public void testServletRequestListener() throws Exception {
    final List<String> list = new ArrayList<>();
    final ServletRequestListener listener = new ServletRequestListener() {

        @Override
        public void requestDestroyed(ServletRequestEvent arg0) {
            list.add("DESTROY");
        }

        @Override
        public void requestInitialized(ServletRequestEvent arg0) {
            list.add("INIT");
        }
    };
    // register with default context
    final ServiceRegistration<ServletRequestListener> reg1 = m_context.registerService(ServletRequestListener.class, listener, getListenerProps());
    // register proprietary listener
    final ServiceRegistration<ServletRequestListener> reg2 = m_context.registerService(ServletRequestListener.class, listener, null);
    // register test servlet with default context
    ServiceRegistration<Servlet> regS = m_context.registerService(Servlet.class, new TestServlet() {

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.setStatus(SC_OK);
            resp.flushBuffer();
        }
    }, getServletProps("/test"));
    try {
        assertEquals(0, list.size());
        assertContent(SC_OK, null, createURL("/test"));
        assertEquals(2, list.size());
        assertEquals("INIT", list.get(0));
        assertEquals("DESTROY", list.get(1));
    } finally {
        reg1.unregister();
        reg2.unregister();
        regS.unregister();
    }
}
Also used : ServletRequestListener(javax.servlet.ServletRequestListener) ArrayList(java.util.ArrayList) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServletRequestEvent(javax.servlet.ServletRequestEvent) Servlet(javax.servlet.Servlet) Test(org.junit.Test)

Example 2 with ServletRequestListener

use of javax.servlet.ServletRequestListener in project nutzboot by nutzam.

the class TomcatStarter method addNutzSupport.

private void addNutzSupport() {
    List<WebFilterFace> filters = appContext.getBeans(WebFilterFace.class);
    Collections.sort(filters, Comparator.comparing(WebFilterFace::getOrder));
    filters.forEach((face) -> addFilter(face));
    appContext.getBeans(WebServletFace.class).forEach((face) -> {
        if (face.getServlet() == null) {
            return;
        }
        addServlet(face);
    });
    appContext.getBeans(WebEventListenerFace.class).forEach((face) -> {
        EventListener listener = face.getEventListener();
        if (listener != null) {
            if ((listener instanceof ServletContextAttributeListener) || (listener instanceof ServletRequestAttributeListener) || (listener instanceof ServletRequestListener) || (listener instanceof HttpSessionIdListener) || (listener instanceof HttpSessionAttributeListener)) {
                this.tomcatContext.addApplicationEventListener(listener);
            }
            if ((listener instanceof ServletContextListener) || (listener instanceof HttpSessionListener)) {
                this.tomcatContext.addApplicationLifecycleListener(listener);
            }
        }
    });
}
Also used : ServletContextAttributeListener(javax.servlet.ServletContextAttributeListener) ServletRequestAttributeListener(javax.servlet.ServletRequestAttributeListener) HttpSessionListener(javax.servlet.http.HttpSessionListener) WebEventListenerFace(org.nutz.boot.starter.WebEventListenerFace) ServletContextListener(javax.servlet.ServletContextListener) ServletRequestListener(javax.servlet.ServletRequestListener) HttpSessionAttributeListener(javax.servlet.http.HttpSessionAttributeListener) WebFilterFace(org.nutz.boot.starter.WebFilterFace) WebServletFace(org.nutz.boot.starter.WebServletFace) EventListener(java.util.EventListener) HttpSessionIdListener(javax.servlet.http.HttpSessionIdListener)

Example 3 with ServletRequestListener

use of javax.servlet.ServletRequestListener in project tomcat70 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.catalina.deploy.SecurityConstraint)

Example 4 with ServletRequestListener

use of javax.servlet.ServletRequestListener in project tomcat70 by apache.

the class TestStandardHostValve method testSRLAfterError.

@Test
public void testSRLAfterError() throws Exception {
    // Set up a container
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    File docBase = new File(System.getProperty("java.io.tmpdir"));
    Context ctx = tomcat.addContext("", docBase.getAbsolutePath());
    // Add the error page
    Tomcat.addServlet(ctx, "error", new ErrorServlet());
    ctx.addServletMapping("/error", "error");
    final List<String> result = new ArrayList<String>();
    // Add the request listener
    ServletRequestListener servletRequestListener = new ServletRequestListener() {

        @Override
        public void requestDestroyed(ServletRequestEvent sre) {
            result.add("Visit requestDestroyed");
        }

        @Override
        public void requestInitialized(ServletRequestEvent sre) {
            result.add("Visit requestInitialized");
        }
    };
    ((StandardContext) ctx).addApplicationEventListener(servletRequestListener);
    tomcat.start();
    // Request a page that triggers an error
    ByteChunk bc = new ByteChunk();
    int rc = getUrl("http://localhost:" + getPort() + "/error?errorCode=400", bc, null);
    Assert.assertEquals(400, rc);
    Assert.assertTrue(result.contains("Visit requestInitialized"));
    Assert.assertTrue(result.contains("Visit requestDestroyed"));
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) ServletRequestListener(javax.servlet.ServletRequestListener) ArrayList(java.util.ArrayList) ServletRequestEvent(javax.servlet.ServletRequestEvent) File(java.io.File) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 5 with ServletRequestListener

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

the class UndertowContext method addRequestListener.

@Override
public void addRequestListener(org.jboss.modcluster.container.listeners.ServletRequestListener requestListener) {
    ServletRequestListener listener = new ServletRequestListener() {

        @Override
        public void requestInitialized(ServletRequestEvent sre) {
            requestListener.requestInitialized();
        }

        @Override
        public void requestDestroyed(ServletRequestEvent sre) {
            requestListener.requestDestroyed();
        }
    };
    ManagedListener ml = new ManagedListener(new ListenerInfo(ServletRequestListener.class, new ImmediateInstanceFactory<>(listener)), true);
    try {
        ml.start();
    } catch (ServletException e) {
        throw new RuntimeException(e);
    }
    this.deployment.getApplicationListeners().addListener(ml);
}
Also used : ServletException(javax.servlet.ServletException) ListenerInfo(io.undertow.servlet.api.ListenerInfo) ServletRequestListener(javax.servlet.ServletRequestListener) ManagedListener(io.undertow.servlet.core.ManagedListener) ServletRequestEvent(javax.servlet.ServletRequestEvent) ImmediateInstanceFactory(io.undertow.servlet.util.ImmediateInstanceFactory)

Aggregations

ServletRequestListener (javax.servlet.ServletRequestListener)15 ServletRequestEvent (javax.servlet.ServletRequestEvent)11 ArrayList (java.util.ArrayList)4 ServletException (javax.servlet.ServletException)4 EventListener (java.util.EventListener)3 ServletContextAttributeListener (javax.servlet.ServletContextAttributeListener)3 ServletContextListener (javax.servlet.ServletContextListener)3 ServletRequestAttributeListener (javax.servlet.ServletRequestAttributeListener)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 HttpSessionAttributeListener (javax.servlet.http.HttpSessionAttributeListener)3 HttpSessionListener (javax.servlet.http.HttpSessionListener)3 SecurityConstraint (org.apache.catalina.deploy.SecurityConstraint)3 Test (org.junit.Test)3 ListenerInfo (io.undertow.servlet.api.ListenerInfo)2 ManagedListener (io.undertow.servlet.core.ManagedListener)2 ImmediateInstanceFactory (io.undertow.servlet.util.ImmediateInstanceFactory)2 IOException (java.io.IOException)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 HttpSessionIdListener (javax.servlet.http.HttpSessionIdListener)2 File (java.io.File)1