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();
}
}
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);
}
}
});
}
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;
}
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"));
}
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);
}
Aggregations