Example usage for javax.servlet ServletRegistration.Dynamic addMapping

List of usage examples for javax.servlet ServletRegistration.Dynamic addMapping

Introduction

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

Prototype

public Set<String> addMapping(String... urlPatterns);

Source Link

Document

Adds a servlet mapping with the given URL patterns for the Servlet represented by this ServletRegistration.

Usage

From source file:org.lightadmin.core.config.LightAdminWebApplicationInitializer.java

private void registerCusomResourceServlet(final ServletContext servletContext) {
    final ResourceServlet resourceServlet = new ResourceServlet();
    resourceServlet.setAllowedResources("/WEB-INF/admin/**/*.jsp");
    resourceServlet.setApplyLastModified(true);
    resourceServlet.setContentType("text/html");

    ServletRegistration.Dynamic customResourceServletRegistration = servletContext
            .addServlet(LIGHT_ADMIN_CUSTOM_RESOURCE_SERVLET_NAME, resourceServlet);
    customResourceServletRegistration.setLoadOnStartup(2);
    customResourceServletRegistration
            .addMapping(resourceServletMapping(servletContext, LIGHT_ADMIN_CUSTOM_FRAGMENT_SERVLET_URL));
}

From source file:org.pidster.tomcat.websocket.jmx.WebSocketJMXInitializer.java

@Override
public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException {

    // Allows the path of the Servlet & Filter to be configurable
    // relative to the web application's own path
    String path = System.getProperty("org.pidster.tomcat.websocket.jmx.path", "/jmx");

    log.info("Registering " + WebSocketJMXResourceFilter.class.getName() + ", with paths: " + path);
    FilterRegistration.Dynamic freg = ctx.addFilter(WebSocketJMXResourceFilter.class.getSimpleName(),
            WebSocketJMXResourceFilter.class);
    freg.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, path + "/*");
    freg.setInitParameter("path", path);

    String wspath = path + "/connect";

    log.info("Registering " + WebSocketJMXServlet.class.getName() + ", with path: " + wspath);
    ServletRegistration.Dynamic wreg = ctx.addServlet(WebSocketJMXServlet.class.getSimpleName(),
            WebSocketJMXServlet.class);
    wreg.addMapping(wspath);
    wreg.setInitParameter("path", path);

}

From source file:org.springframework.boot.context.embedded.ServletRegistrationBean.java

/**
 * Configure registration settings. Subclasses can override this method to perform
 * additional configuration if required.
 * @param registration the registration//from w w w.  ja v  a  2 s.c  om
 */
protected void configure(ServletRegistration.Dynamic registration) {
    super.configure(registration);
    String[] urlMapping = this.urlMappings.toArray(new String[this.urlMappings.size()]);
    if (urlMapping.length == 0 && this.alwaysMapUrl) {
        urlMapping = DEFAULT_MAPPINGS;
    }
    if (!ObjectUtils.isEmpty(urlMapping)) {
        registration.addMapping(urlMapping);
    }
    registration.setLoadOnStartup(this.loadOnStartup);
    if (this.multipartConfig != null) {
        registration.setMultipartConfig(this.multipartConfig);
    }
}

From source file:org.wso2.carbon.protobuf.listener.ProtobufServletContainerInitializer.java

@Override
public void onStartup(Set<Class<?>> classes, ServletContext servletContext) throws ServletException {

    if (classes == null || classes.size() == 0) {
        return;/*  www . j ava 2s .com*/
    }
    // adding a listener to remove services when wars are undeployed
    servletContext.addListener(new ProtobufServletContextListener());
    // keeps track of PB services in a PB war
    // Please note that, a PB war can contain many PB services
    List<ProtobufServiceData> serviceList = new ArrayList<ProtobufServiceData>();
    // servlet to display proto files (like WSDL files)
    ServletRegistration.Dynamic dynamic = servletContext.addServlet("ProtoBufServlet", ProtobufServlet.class);

    for (Class<?> clazz : classes) {
        // Getting binary service registry
        ProtobufRegistry binaryServiceRegistry = (ProtobufRegistry) PrivilegedCarbonContext
                .getThreadLocalCarbonContext().getOSGiService(ProtobufRegistry.class);
        // Is it a blocking service or not
        boolean blocking = clazz.getAnnotation(ProtobufService.class).blocking();
        Method reflectiveMethod = null;
        Object serviceObj = null;
        String serviceName;
        String serviceType;
        try {
            if (blocking) {
                // getting newReflectiveBlocking method which will return a
                // blocking service
                reflectiveMethod = clazz.getInterfaces()[0].getDeclaringClass()
                        .getMethod("newReflectiveBlockingService", clazz.getInterfaces()[0]);
                // Since it is a static method, we pass null
                serviceObj = reflectiveMethod.invoke(null, clazz.newInstance());
                BlockingService blockingService = (BlockingService) serviceObj;
                // register service into Binary Service Registry
                serviceName = binaryServiceRegistry.registerBlockingService(blockingService);
                serviceType = "BlockingService";
                // keeps PB service information in a bean
                // we need these when removing the services from Binary
                // Service Registry
                // we are using these beans instances inside our destroyer
                serviceList.add(new ProtobufServiceData(serviceName, serviceType));
                servletContext.setAttribute("services", serviceList);
                dynamic.addMapping("/");
            } else {
                // getting newReflectiveService which will return a non
                // blocking service
                reflectiveMethod = clazz.getInterfaces()[0].getDeclaringClass()
                        .getMethod("newReflectiveService", clazz.getInterfaces()[0]);
                // Since it is a static method, we pass null
                serviceObj = reflectiveMethod.invoke(null, clazz.newInstance());
                Service service = (Service) serviceObj;
                // register service into Binary Service Registry
                serviceName = binaryServiceRegistry.registerService(service);
                serviceType = "NonBlockingService";
                // keeps PB service information in a bean
                // we need these information to remove the service from
                // Binary Service Registry later
                // we are using these bean instances in our destroyer
                serviceList.add(new ProtobufServiceData(serviceName, serviceType));
                servletContext.setAttribute("services", serviceList);
                dynamic.addMapping("/");
            }
        } catch (InvocationTargetException e) {
            String msg = "InvocationTargetException" + e.getLocalizedMessage();
            log.error(msg, e);
        } catch (NoSuchMethodException e) {
            String msg = "NoSuchMethodException" + e.getLocalizedMessage();
            log.error(msg, e);
        } catch (InstantiationException e) {
            String msg = "InstantiationException" + e.getLocalizedMessage();
            log.error(msg, e);
        } catch (IllegalAccessException e) {
            String msg = "IllegalAccessException" + e.getLocalizedMessage();
            log.error(msg, e);
        }
    }
}