Example usage for javax.servlet ServletRegistration getMappings

List of usage examples for javax.servlet ServletRegistration getMappings

Introduction

In this page you can find the example usage for javax.servlet ServletRegistration getMappings.

Prototype

public Collection<String> getMappings();

Source Link

Document

Gets the currently available mappings of the Servlet represented by this ServletRegistration.

Usage

From source file:org.springframework.http.server.reactive.ServletHttpHandlerAdapter.java

private String getServletPath(ServletConfig config) {
    String name = config.getServletName();
    ServletRegistration registration = config.getServletContext().getServletRegistration(name);
    if (registration == null) {
        throw new IllegalStateException("ServletRegistration not found for Servlet '" + name + "'");
    }/*from w w w . j av a2s  .  co m*/

    Collection<String> mappings = registration.getMappings();
    if (mappings.size() == 1) {
        String mapping = mappings.iterator().next();
        if (mapping.equals("/")) {
            return "";
        }
        if (mapping.endsWith("/*")) {
            String path = mapping.substring(0, mapping.length() - 2);
            if (!path.isEmpty()) {
                logger.info("Found Servlet mapping '" + path + "' for Servlet '" + name + "'");
            }
            return path;
        }
    }

    throw new IllegalArgumentException(
            "Expected a single Servlet mapping: " + "either the default Servlet mapping (i.e. '/'), "
                    + "or a path based mapping (e.g. '/*', '/foo/*'). " + "Actual mappings: " + mappings
                    + " for Servlet '" + name + "'");
}

From source file:org.wso2.carbon.discovery.cxf.listeners.TomcatCxfDiscoveryListener.java

public void lifecycleEvent(LifecycleEvent lifecycleEvent) {
    try {/*from   w  ww  .  j av  a  2s  .c o  m*/
        String type = lifecycleEvent.getType();
        if (Lifecycle.AFTER_START_EVENT.equals(type) || Lifecycle.BEFORE_STOP_EVENT.equals(type)) {
            StandardContext context = (StandardContext) lifecycleEvent.getLifecycle();
            String jaxServletMapping = null;

            boolean isJaxWebapp = false;
            Map<String, ? extends ServletRegistration> servletRegs = context.getServletContext()
                    .getServletRegistrations();
            for (ServletRegistration servletReg : servletRegs.values()) {
                if (cxfServletClass.equals(servletReg.getClassName())) {
                    Object[] mappings = servletReg.getMappings().toArray();
                    jaxServletMapping = mappings.length > 0 ? getServletContextPath((String) mappings[0])
                            : null;
                    isJaxWebapp = true;
                    break;
                }
            }

            if (isJaxWebapp) {
                CXFServiceInfo serviceBean = getServiceInfo(context, jaxServletMapping);
                if (serviceBean == null) {
                    return;
                }

                if (Lifecycle.AFTER_START_EVENT.equals(type)) {
                    cxfMessageSender.sendHello(serviceBean, null);

                } else if (Lifecycle.BEFORE_STOP_EVENT.equals(type)) {
                    cxfMessageSender.sendBye(serviceBean, null);
                }
            }
        }

    } catch (DiscoveryException e) {
        log.warn("Error while publishing the services to the discovery service ", e);
    } catch (Throwable e) {
        //Catching throwable since this listener's state shouldn't affect the webapp deployment.
        log.warn("Error while publishing the services to the discovery service ", e);
    }
}