Java Code Examples for javax.servlet.ServletRequestListener

The following examples show how to use javax.servlet.ServletRequestListener. 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: 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 2
/**
 * Add the listener.
 *
 * @param <T> the type.
 * @param listener the listener
 */
@Override
public <T extends EventListener> void addListener(T listener) {
    if (listener instanceof ServletContextListener) {
        contextListeners.add((ServletContextListener) listener);
    }
    if (listener instanceof ServletContextAttributeListener) {
        contextAttributeListeners.add((ServletContextAttributeListener) listener);
    }
    if (listener instanceof ServletRequestListener) {
        requestListeners.add((ServletRequestListener) listener);
    }
    if (listener instanceof ServletRequestAttributeListener) {
        httpRequestManager.addListener((ServletRequestAttributeListener) listener);
    }
    if (listener instanceof HttpSessionAttributeListener) {
        httpSessionManager.addListener(listener);
    }
    if (listener instanceof HttpSessionIdListener) {
        httpSessionManager.addListener(listener);
    }
    if (listener instanceof HttpSessionListener) {
        httpSessionManager.addListener(listener);
    }
}
 
Example 3
/**
 * Create the listener.
 *
 * @param <T> the type.
 * @param clazz the class of the listener to create.
 * @return the listener.
 * @throws ServletException when it fails to create the listener.
 */
@Override
public <T extends EventListener> T createListener(Class<T> clazz) throws ServletException {
    T result = objectInstanceManager.createListener(clazz);
    boolean ok = false;
    if (result instanceof ServletContextListener || result instanceof ServletContextAttributeListener || result instanceof ServletRequestListener
            || result instanceof ServletRequestAttributeListener || result instanceof HttpSessionAttributeListener
            || result instanceof HttpSessionIdListener || result instanceof HttpSessionListener) {
        ok = true;
    }

    if (!ok) {
        LOGGER.log(WARNING, "Unable to create listener: {0}", clazz);
        throw new IllegalArgumentException("Invalid type");
    }

    return result;
}
 
Example 4
Source Project: lams   Source File: ApplicationListeners.java    License: GNU General Public License v2.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 5
@Override
public void onStartup(ServletContext webappContext) throws ServletException {
	
	ModuleDataExtractor extractor = new ModuleDataExtractor(module);
	microserverEnvironment.assureModule(module);
	String fullRestResource = "/" + module.getContext() + "/*";

	ServerData serverData = new ServerData(microserverEnvironment.getModuleBean(module).getPort(),
			Arrays.asList(),
			rootContext, fullRestResource, module);
	List<FilterData> filterDataList = extractor.createFilteredDataList(serverData);
	List<ServletData> servletDataList = extractor.createServletDataList(serverData);
	new ServletConfigurer(serverData, LinkedListX.fromIterable(servletDataList)).addServlets(webappContext);

	new FilterConfigurer(serverData, LinkedListX.fromIterable(filterDataList)).addFilters(webappContext);
	PersistentList<ServletContextListener> servletContextListenerData = LinkedListX.fromIterable(module.getListeners(serverData)).filter(i->!(i instanceof ContextLoader));
    PersistentList<ServletRequestListener> servletRequestListenerData =	LinkedListX.fromIterable(module.getRequestListeners(serverData));
	
	new ServletContextListenerConfigurer(serverData, servletContextListenerData, servletRequestListenerData).addListeners(webappContext);
	
}
 
Example 6
@Override
public void onStartup(ServletContext webappContext) throws ServletException {
	
	ModuleDataExtractor extractor = new ModuleDataExtractor(module);
	microserverEnvironment.assureModule(module);
	String fullRestResource = "/" + module.getContext() + "/*";

	ServerData serverData = new ServerData(microserverEnvironment.getModuleBean(module).getPort(),
			Arrays.asList(),
			rootContext, fullRestResource, module);
	List<FilterData> filterDataList = extractor.createFilteredDataList(serverData);
	List<ServletData> servletDataList = extractor.createServletDataList(serverData);
	new ServletConfigurer(serverData, LinkedListX.fromIterable(servletDataList)).addServlets(webappContext);

	new FilterConfigurer(serverData, LinkedListX.fromIterable(filterDataList)).addFilters(webappContext);
	PersistentList<ServletContextListener> servletContextListenerData = LinkedListX.fromIterable(module.getListeners(serverData)).filter(i->!(i instanceof ContextLoader));
    PersistentList<ServletRequestListener> servletRequestListenerData =	LinkedListX.fromIterable(module.getRequestListeners(serverData));
	
	new ServletContextListenerConfigurer(serverData, servletContextListenerData, servletRequestListenerData).addListeners(webappContext);
	
}
 
Example 7
public void addListeners(ServletContext webappContext) {

	serverData.getRootContext()
			.getBeansOfType(ServletContextListener.class)
			.values()
			
			.stream()
			
			.peek(this::logListener)
			.forEach(listener -> webappContext.addListener(listener));
	listenerData.forEach(it -> webappContext.addListener(it));

	serverData.getRootContext()
			.getBeansOfType(ServletRequestListener.class)
			.values()
			.stream()
			.peek(this::logListener)
			.forEach(listener -> webappContext.addListener(listener));
	listenerRequestData.forEach(it -> webappContext.addListener(it));

}
 
Example 8
Source Project: tomee   Source File: HttpRequestImpl.java    License: Apache License 2.0 6 votes vote down vote up
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));
    }
}
 
Example 9
Source Project: tomee   Source File: WebContext.java    License: Apache License 2.0 6 votes vote down vote up
private static boolean isWeb(final Class<?> beanClass) {
    if (Servlet.class.isAssignableFrom(beanClass)
        || Filter.class.isAssignableFrom(beanClass)) {
        return true;
    }
    if (EventListener.class.isAssignableFrom(beanClass)) {
        return HttpSessionAttributeListener.class.isAssignableFrom(beanClass)
               || ServletContextListener.class.isAssignableFrom(beanClass)
               || ServletRequestListener.class.isAssignableFrom(beanClass)
               || ServletContextAttributeListener.class.isAssignableFrom(beanClass)
               || HttpSessionListener.class.isAssignableFrom(beanClass)
               || HttpSessionBindingListener.class.isAssignableFrom(beanClass)
               || HttpSessionActivationListener.class.isAssignableFrom(beanClass)
               || HttpSessionIdListener.class.isAssignableFrom(beanClass)
               || ServletRequestAttributeListener.class.isAssignableFrom(beanClass);
    }

    return false;
}
 
Example 10
Source Project: Tomcat8-Source-Read   Source File: ApplicationContext.java    License: MIT License 5 votes vote down vote up
@Override
public <T extends EventListener> void addListener(T t) {
    if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
        throw new IllegalStateException(
                sm.getString("applicationContext.addListener.ise",
                        getContextPath()));
    }

    boolean match = false;
    if (t instanceof ServletContextAttributeListener ||
            t instanceof ServletRequestListener ||
            t instanceof ServletRequestAttributeListener ||
            t instanceof HttpSessionIdListener ||
            t instanceof HttpSessionAttributeListener) {
        context.addApplicationEventListener(t);
        match = true;
    }

    if (t instanceof HttpSessionListener ||
            (t instanceof ServletContextListener && newServletContextListenerAllowed)) {
        // Add listener directly to the list of instances rather than to
        // the list of class names.
        context.addApplicationLifecycleListener(t);
        match = true;
    }

    if (match) return;

    if (t instanceof ServletContextListener) {
        throw new IllegalArgumentException(sm.getString(
                "applicationContext.addListener.iae.sclNotAllowed",
                t.getClass().getName()));
    } else {
        throw new IllegalArgumentException(sm.getString(
                "applicationContext.addListener.iae.wrongType",
                t.getClass().getName()));
    }
}
 
Example 11
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 12
Source Project: Tomcat7.0.67   Source File: ApplicationContext.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public <T extends EventListener> void addListener(T t) {
    if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
        throw new IllegalStateException(
                sm.getString("applicationContext.addListener.ise",
                        getContextPath()));
    }

    boolean match = false;
    if (t instanceof ServletContextAttributeListener ||
            t instanceof ServletRequestListener ||
            t instanceof ServletRequestAttributeListener ||
            t instanceof HttpSessionAttributeListener) {
        context.addApplicationEventListener(t);
        match = true;
    }

    if (t instanceof HttpSessionListener
            || (t instanceof ServletContextListener &&
                    newServletContextListenerAllowed)) {
        // Add listener directly to the list of instances rather than to
        // the list of class names.
        context.addApplicationLifecycleListener(t);
        match = true;
    }

    if (match) return;

    if (t instanceof ServletContextListener) {
        throw new IllegalArgumentException(sm.getString(
                "applicationContext.addListener.iae.sclNotAllowed",
                t.getClass().getName()));
    } else {
        throw new IllegalArgumentException(sm.getString(
                "applicationContext.addListener.iae.wrongType",
                t.getClass().getName()));
    }
}
 
Example 13
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 14
Source Project: tomcatsrc   Source File: ApplicationContext.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public <T extends EventListener> void addListener(T t) {
    if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
        throw new IllegalStateException(
                sm.getString("applicationContext.addListener.ise",
                        getContextPath()));
    }

    boolean match = false;
    if (t instanceof ServletContextAttributeListener ||
            t instanceof ServletRequestListener ||
            t instanceof ServletRequestAttributeListener ||
            t instanceof HttpSessionAttributeListener) {
        context.addApplicationEventListener(t);
        match = true;
    }

    if (t instanceof HttpSessionListener
            || (t instanceof ServletContextListener &&
                    newServletContextListenerAllowed)) {
        // Add listener directly to the list of instances rather than to
        // the list of class names.
        context.addApplicationLifecycleListener(t);
        match = true;
    }

    if (match) return;

    if (t instanceof ServletContextListener) {
        throw new IllegalArgumentException(sm.getString(
                "applicationContext.addListener.iae.sclNotAllowed",
                t.getClass().getName()));
    } else {
        throw new IllegalArgumentException(sm.getString(
                "applicationContext.addListener.iae.wrongType",
                t.getClass().getName()));
    }
}
 
Example 15
Source Project: micro-server   Source File: Module.java    License: Apache License 2.0 5 votes vote down vote up
default List<ServletRequestListener> getRequestListeners(ServerData data) {

        return PluginLoader.INSTANCE.plugins.get()
                                            .stream()
                                            .filter(module -> module.servletRequestListeners() != null)
                                            .concatMap(Plugin::servletRequestListeners)
                                            .map(fn -> fn.apply(data))
                                             .to(ListX::fromIterable);

    }
 
Example 16
Source Project: micro-server   Source File: ConfigurableModule.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public List<ServletRequestListener> getRequestListeners(ServerData data) {
    if (requestListeners != null)
        return ListX.fromIterable(concat(this.requestListeners,
                extract(() -> Module.super.getRequestListeners(data))));

    return Module.super.getRequestListeners(data);
}
 
Example 17
Source Project: micro-server   Source File: AllData.java    License: Apache License 2.0 5 votes vote down vote up
public AllData(ServerData serverData, List<FilterData> filterDataList,
			List<ServletData> servletDataList,
			List<ServletContextListener> servletContextListeners,
			List<ServletRequestListener> servletRequestListeners  ) {

	this.servletContextListeners = Seq.fromIterable(UsefulStaticMethods.either(servletContextListeners, new ArrayList<ServletContextListener>()));

	this.servletRequestListeners = Seq.fromIterable(UsefulStaticMethods.either(servletRequestListeners, new ArrayList<ServletRequestListener>()));

	this.filterDataList = Seq.fromIterable(UsefulStaticMethods.either(filterDataList, new ArrayList<FilterData>()));
	this.servletDataList = Seq.fromIterable(UsefulStaticMethods.either(servletDataList, new ArrayList<ServletData>()));
	this.serverData = serverData;
}
 
Example 18
Source Project: micro-server   Source File: AllData.java    License: Apache License 2.0 5 votes vote down vote up
public AllData(ServerData serverData, PersistentList<FilterData> filterDataList,
                  PersistentList<ServletData> servletDataList,
                  PersistentList<ServletContextListener> servletContextListeners,
                  PersistentList<ServletRequestListener> servletRequestListeners  ) {

	this.servletContextListeners = Seq.fromIterable(UsefulStaticMethods.either(servletContextListeners, new ArrayList<ServletContextListener>()));

	this.servletRequestListeners = Seq.fromIterable(UsefulStaticMethods.either(servletRequestListeners, new ArrayList<ServletRequestListener>()));

	this.filterDataList = Seq.fromIterable(UsefulStaticMethods.either(filterDataList, new ArrayList<FilterData>()));
	this.servletDataList = Seq.fromIterable(UsefulStaticMethods.either(servletDataList, new ArrayList<ServletData>()));
	this.serverData = serverData;
}
 
Example 19
Source Project: tomee   Source File: WebBeansListenerHelper.java    License: Apache License 2.0 5 votes vote down vote up
public static void destroyFakedRequest(final ServletRequestListener listener) {
    final Boolean faked = FAKE_REQUEST.get();
    try {
        if (faked != null && faked) {
            listener.requestDestroyed(null);
        }
    } finally {
        FAKE_REQUEST.remove();
    }
}
 
Example 20
Source Project: tomee   Source File: WebBeansListenerHelper.java    License: Apache License 2.0 5 votes vote down vote up
public static void ensureRequestScope(final ContextsService cs, final ServletRequestListener listener) {
    final Context reqCtx = cs.getCurrentContext(RequestScoped.class);
    if (reqCtx == null || !cs.getCurrentContext(RequestScoped.class).isActive()) {
        listener.requestInitialized(null);
        FAKE_REQUEST.set(true);
    }
}
 
Example 21
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 22
Source Project: bugsnag-java   Source File: SpringBootConfiguration.java    License: MIT License 5 votes vote down vote up
/**
 * The {@link com.bugsnag.servlet.BugsnagServletContainerInitializer} does not work for Spring Boot, need to
 * register the {@link BugsnagServletRequestListener} using a Spring Boot
 * {@link ServletListenerRegistrationBean} instead. This adds session tracking and
 * automatic servlet request metadata collection.
 */
@Bean
@Conditional(SpringWebMvcLoadedCondition.class)
ServletListenerRegistrationBean<ServletRequestListener> listenerRegistrationBean() {
    ServletListenerRegistrationBean<ServletRequestListener> srb =
            new ServletListenerRegistrationBean<ServletRequestListener>();
    srb.setListener(new BugsnagServletRequestListener());
    return srb;
}
 
Example 23
Source Project: we-cmdb   Source File: SpringWebConfig.java    License: Apache License 2.0 4 votes vote down vote up
@Bean
public ServletListenerRegistrationBean<ServletRequestListener> registerRequestListener() {
    ServletListenerRegistrationBean<ServletRequestListener> servletListenerRegistrationBean = new ServletListenerRegistrationBean<>();
    servletListenerRegistrationBean.setListener(new RequestContextListener());
    return servletListenerRegistrationBean;
}
 
Example 24
Source Project: micro-server   Source File: Plugin.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * @return Servlet Request Listeners for this plugin
 */
default Set<Function<ServerData,ServletRequestListener>> servletRequestListeners(){
	return SetX.empty();
}
 
Example 25
public ServletContextListenerConfigurer(ServerData serverData,
                                           PersistentList<ServletContextListener> listenerData, PersistentList<ServletRequestListener> listenerRequestData) {
	this.serverData = serverData;
	this.listenerData = ListX.fromIterable(listenerData);
	this.listenerRequestData = ListX.fromIterable(listenerRequestData);
}
 
Example 26
Source Project: syncope   Source File: RESTCXFContext.java    License: Apache License 2.0 4 votes vote down vote up
@Bean
public ServletListenerRegistrationBean<ServletRequestListener> listenerRegistrationBean() {
    ServletListenerRegistrationBean<ServletRequestListener> bean = new ServletListenerRegistrationBean<>();
    bean.setListener(new ThreadLocalCleanupListener());
    return bean;
}
 
Example 27
private void logListener(ServletRequestListener listener) {
	logger.info("Registering servlet request listener {}",listener.getClass().getName());

}