Java Code Examples for io.undertow.servlet.api.DeploymentInfo#addListener()

The following examples show how to use io.undertow.servlet.api.DeploymentInfo#addListener() . 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: WebServer.java    From metamodel-membrane with Apache License 2.0 5 votes vote down vote up
public static void startServer(int port, boolean enableCors) throws Exception {
    final DeploymentInfo deployment = Servlets.deployment().setClassLoader(WebServer.class.getClassLoader());
    deployment.setContextPath("");
    deployment.setDeploymentName("membrane");
    deployment.addInitParameter("contextConfigLocation", "classpath:context/application-context.xml");
    deployment.setResourceManager(new FileResourceManager(new File("."), 0));
    deployment.addListener(Servlets.listener(ContextLoaderListener.class));
    deployment.addListener(Servlets.listener(RequestContextListener.class));
    deployment.addServlet(Servlets.servlet("dispatcher", DispatcherServlet.class).addMapping("/*")
            .addInitParam("contextConfigLocation", "classpath:context/dispatcher-servlet.xml"));
    deployment.addFilter(Servlets.filter(CharacterEncodingFilter.class).addInitParam("forceEncoding", "true")
            .addInitParam("encoding", "UTF-8"));

    final DeploymentManager manager = Servlets.defaultContainer().addDeployment(deployment);
    manager.deploy();

    final HttpHandler handler;
    if (enableCors) {
        CorsHandlers corsHandlers = new CorsHandlers();
        handler = corsHandlers.allowOrigin(manager.start());
    } else {
        handler = manager.start();
    }

    final Undertow server = Undertow.builder().addHttpListener(port, "0.0.0.0").setHandler(handler).build();
    server.start();

    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            // graceful shutdown of everything
            server.stop();
            try {
                manager.stop();
            } catch (ServletException e) {
            }
            manager.undeploy();
        }
    });
}
 
Example 2
Source File: UndertowServer.java    From pippo with Apache License 2.0 5 votes vote down vote up
protected DeploymentManager createPippoDeploymentManager() {
    DeploymentInfo info = Servlets.deployment();
    info.setDeploymentName("Pippo");
    info.setClassLoader(this.getClass().getClassLoader());
    info.setContextPath(getSettings().getContextPath());
    info.setIgnoreFlush(true);

    // inject application as context attribute
    info.addServletContextAttribute(PIPPO_APPLICATION, getApplication());

    // add pippo filter
    addPippoFilter(info);

    // add initializers
    info.addListener(new ListenerInfo(PippoServletContextListener.class));

    // add listeners
    listeners.forEach(listener -> info.addListener(new ListenerInfo(listener)));

    ServletInfo defaultServlet = new ServletInfo("DefaultServlet", DefaultServlet.class);
    defaultServlet.addMapping("/");

    MultipartConfigElement multipartConfig = createMultipartConfigElement();
    defaultServlet.setMultipartConfig(multipartConfig);
    info.addServlets(defaultServlet);

    DeploymentManager deploymentManager = Servlets.defaultContainer().addDeployment(info);
    deploymentManager.deploy();

    return deploymentManager;
}
 
Example 3
Source File: OpenshiftAuthServletExtension.java    From hawkular-metrics with Apache License 2.0 4 votes vote down vote up
@Override
public void handleDeployment(DeploymentInfo deploymentInfo, ServletContext servletContext) {

    String componentName = "";
    String resourceName = "";
    Pattern insecureEndpointPattern = null;
    Pattern postQueryPattern = null;
    // file to contain configurations options for this Servlet Extension
    InputStream is = servletContext.getResourceAsStream(PROPERTY_FILE);
    if (is != null) {
        try {
            Properties props = new Properties();
            props.load(is);
            componentName = props.getProperty("component");
            resourceName = props.getProperty("resource-name");
            String insecurePatternString = props.getProperty("unsecure-endpoints");
            String postQueryPatternString = props.getProperty("post-query");

            if (insecurePatternString != null) {
                insecureEndpointPattern = Pattern.compile(insecurePatternString);
            }

            if (postQueryPatternString != null) {
                postQueryPattern = Pattern.compile(postQueryPatternString);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    if (componentName == null || componentName.isEmpty()) {
        throw new RuntimeException("Missing or empty 'component' key from the " + PROPERTY_FILE + " configuration file.");
    }

    if (resourceName == null || resourceName.isEmpty()) {
        throw new RuntimeException("Missing or empty 'resource-name' key from the " + PROPERTY_FILE + " configuratin file.");
    }

    final String cName = componentName;
    final String rName = resourceName;
    final Pattern insecurePattern = insecureEndpointPattern;
    final Pattern postQuery = postQueryPattern;

    if (DISABLE_NAMESPACE_FILTER.equalsIgnoreCase("true")) {
        log.info("The OpenShift Namespace Filter has been disabled via the 'DISABLE_NAMESPACE_FILTER' system property.");
    }
    else {
        log.info("Enabling the OpenShift Namespace Filter.");
        deploymentInfo.addInitialHandlerChainWrapper(containerHandler -> {
            namespaceHandler = new NamespaceHandler(containerHandler);
            return namespaceHandler;
        });
    }

    ImmediateInstanceFactory<EventListener> instanceFactory = new ImmediateInstanceFactory<>(new SCListener());
    deploymentInfo.addListener(new ListenerInfo(SCListener.class, instanceFactory));
    deploymentInfo.addInitialHandlerChainWrapper(containerHandler -> {
        openshiftAuthHandler = new OpenshiftAuthHandler(containerHandler, cName, rName, insecurePattern, postQuery);
        return openshiftAuthHandler;
    });

}