javax.servlet.ServletRequestListener Java Examples

The following examples show how to use javax.servlet.ServletRequestListener. 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 check out the related API usage on the sidebar.
Example #1
Source File: WebContext.java    From tomee with 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 #2
Source File: ApplicationListeners.java    From quarkus-http with 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 #3
Source File: DefaultWebApplication.java    From piranha with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * 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 #4
Source File: DefaultWebApplication.java    From piranha with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * 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 #5
Source File: HttpRequestImpl.java    From tomee with 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 #6
Source File: ApplicationListeners.java    From lams with 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 #7
Source File: BootFrontEndApplicationConfigurator.java    From micro-server with Apache License 2.0 6 votes vote down vote up
@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 #8
Source File: BootFrontEndApplicationConfigurator.java    From micro-server with Apache License 2.0 6 votes vote down vote up
@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 #9
Source File: ServletContextListenerConfigurer.java    From micro-server with Apache License 2.0 6 votes vote down vote up
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 #10
Source File: Module.java    From micro-server with 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 #11
Source File: SpringBootConfiguration.java    From bugsnag-java with 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 #12
Source File: HttpRequestImpl.java    From tomee with 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 #13
Source File: WebBeansListenerHelper.java    From tomee with 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 #14
Source File: WebBeansListenerHelper.java    From tomee with 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 #15
Source File: AllData.java    From micro-server with 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 #16
Source File: AllData.java    From micro-server with 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 #17
Source File: ConfigurableModule.java    From micro-server with 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 #18
Source File: ApplicationContext.java    From Tomcat8-Source-Read with 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 #19
Source File: ApplicationContext.java    From tomcatsrc with 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 #20
Source File: ApplicationListeners.java    From lams with 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 #21
Source File: ApplicationContext.java    From Tomcat7.0.67 with 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 #22
Source File: ApplicationListeners.java    From quarkus-http with 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 #23
Source File: ServletContextListenerConfigurer.java    From micro-server with Apache License 2.0 4 votes vote down vote up
public ServletContextListenerConfigurer(ServerData serverData,
                                           PersistentList<ServletContextListener> listenerData, PersistentList<ServletRequestListener> listenerRequestData) {
	this.serverData = serverData;
	this.listenerData = ListX.fromIterable(listenerData);
	this.listenerRequestData = ListX.fromIterable(listenerRequestData);
}
 
Example #24
Source File: RESTCXFContext.java    From syncope with 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 #25
Source File: Plugin.java    From micro-server with 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 #26
Source File: SpringWebConfig.java    From we-cmdb with 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 #27
Source File: ServletContextListenerConfigurer.java    From micro-server with Apache License 2.0 2 votes vote down vote up
private void logListener(ServletRequestListener listener) {
	logger.info("Registering servlet request listener {}",listener.getClass().getName());

}