Example usage for javax.servlet ServletContext getServletRegistrations

List of usage examples for javax.servlet ServletContext getServletRegistrations

Introduction

In this page you can find the example usage for javax.servlet ServletContext getServletRegistrations.

Prototype

public Map<String, ? extends ServletRegistration> getServletRegistrations();

Source Link

Document

Gets a (possibly empty) Map of the ServletRegistration objects (keyed by servlet name) corresponding to all servlets registered with this ServletContext.

Usage

From source file:org.apache.tomee.security.cdi.TomEESecurityServletAuthenticationMechanismMapper.java

public void init(@Observes @Initialized(ApplicationScoped.class) final ServletContext context) {
    final Map<String, ? extends ServletRegistration> servletRegistrations = context.getServletRegistrations();
    servletRegistrations.forEach((servletName, servletRegistration) -> {
        try {//from  w w w.j a  va 2s  .  c  o m
            final Class<?> servletClass = Thread.currentThread().getContextClassLoader().loadClass(servletName);
            if (servletClass.isAnnotationPresent(BasicAuthenticationMechanismDefinition.class)) {
                servletAuthenticationMapper.put(servletName,
                        CDI.current().select(BasicAuthenticationMechanism.class).get());
            }

            if (servletClass.isAnnotationPresent(FormAuthenticationMechanismDefinition.class)) {
                servletAuthenticationMapper.put(servletName,
                        CDI.current().select(FormAuthenticationMechanism.class).get());
            }

        } catch (final ClassNotFoundException e) {
            // Ignore
        }
    });

    final Set<HttpAuthenticationMechanism> availableBeans = authenticationMechanisms.stream()
            .collect(Collectors.toSet());
    availableBeans.removeAll(servletAuthenticationMapper.values());
    availableBeans.remove(defaultAuthenticationMechanism);

    if (availableBeans.size() == 1) {
        defaultAuthenticationMechanism.setDelegate(availableBeans.iterator().next());
    } else if (availableBeans.size() > 1) {
        throw new IllegalStateException("Multiple HttpAuthenticationMechanism found "
                + availableBeans.stream().map(b -> substringBefore(b.getClass().getSimpleName(), "$$"))
                        .collect(toList())
                + " " + "without a @WebServlet association. "
                + "Deploy a single one for the application, or associate it with a @WebServlet.");
    }
}

From source file:org.dmb.trueprice.utils.internal.InitContextListener.java

@Override
public void contextInitialized(ServletContextEvent sce) {

    log.warn("\n\t >>> \t INITIALIZING APPLICATION ....");

    //        sce.getServletConfig().gets

    ServletContext sctxt = sce.getServletContext();

    log.debug("\t >>>\t SHOW servlets names list ");
    //            log.debug("\t >>>\t SERVLET NAME IS > " + sctxt.getServletContextName());
    Map<String, ? extends ServletRegistration> eParam = sctxt.getServletRegistrations();
    for (String paramName : eParam.keySet()) {
        //                String paramName = eParam.nextElement();
        //                String paramValue = sctxt.getInitParameter(paramName).toString();

        if (paramName != null) {
            String paramValue = eParam.get(paramName)
                    //on ajoute .getName car l'obejt dans la Map extends ServletRegistration
                    .getName();//from  w  w  w.  j ava2 s . c o m
            log.debug("\t >> New servlet [" + ("".equals(paramName) ? "???" : paramName)
            //                    log.debug("\t >> New servlet [" + ("".equals(paramName) ? "???" : paramName) 
                    + "] > " + ("".equals(paramValue) ? "???" : paramValue));
            servletsContext.put(paramName, null);
        }

    }

    //            log.debug("\t >>>\t SHOW INIT PARAMETERS of SERVLET CONTEXT");
    //            log.debug("\t >>>\t SERVLET NAME IS > " + sctxt.getServletContextName());
    //            Enumeration<String> eParam = sctxt.getInitParameterNames();
    //            while (eParam.hasMoreElements()) {
    //            String paramName = eParam.nextElement();
    //            String paramValue = sctxt.getInitParameter(paramName).toString();
    //            log.debug("New init param [" + (paramName == null ? "???" : paramName) 
    //                    + "] > " + (paramValue == null ? "???" : paramValue)
    //            );
    //            }
    //        
    //            log.debug("\t >>>\t SHOW ATTRIBUTES of SERVLET CONTEXT");
    //            Enumeration<String> e = sctxt.getAttributeNames();
    //            while (e.hasMoreElements()) {
    //            String attName = e.nextElement();
    //            String attValue = sctxt.getAttribute(attName).toString();
    //            log.debug("New att [" + (attName == null ? "???" : attName) 
    //                    + "] > " + (attValue == null ? "???" : attValue)
    //            );
    //              }

    //    servletsContext = sctxt;

}