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

The following examples show how to use io.undertow.servlet.api.DeploymentInfo#setContextPath() . 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: Demo.java    From resteasy-examples with Apache License 2.0 6 votes vote down vote up
public static UndertowJaxrsServer buildServer() {
    UndertowJaxrsServer server;
    System.setProperty("java.util.logging.manager", "org.jboss.logmanager.LogManager");
    try {
        LogManager.getLogManager().readConfiguration(Main.class.getClassLoader().getResourceAsStream("logging.jboss.properties"));
    } catch (IOException e) {
        e.printStackTrace();
    }
    server = new UndertowJaxrsServer().start();

    ResteasyDeployment deployment = new ResteasyDeploymentImpl();

    deployment.setApplicationClass(TracingApp.class.getName());

    DeploymentInfo di = server.undertowDeployment(deployment);
    di.setClassLoader(TracingApp.class.getClassLoader());
    di.setContextPath("");
    di.setDeploymentName("Resteasy");
    di.getServlets().get("ResteasyServlet").addInitParam(ResteasyContextParameters.RESTEASY_TRACING_TYPE, ResteasyContextParameters.RESTEASY_TRACING_TYPE_ALL)
            .addInitParam(ResteasyContextParameters.RESTEASY_TRACING_THRESHOLD, ResteasyContextParameters.RESTEASY_TRACING_LEVEL_VERBOSE);
    server.deploy(di);
    return server;
}
 
Example 2
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 3
Source File: Main.java    From resteasy-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    UndertowJaxrsSpringServer server = new UndertowJaxrsSpringServer();
    server.start();

    DeploymentInfo deployment = server.undertowDeployment("classpath:resteasy-spring-mvc-servlet.xml", null);
    deployment.setDeploymentName(Main.class.getName());
    deployment.setContextPath("/");
    deployment.setClassLoader(Main.class.getClassLoader());
    server.deploy(deployment);

    System.out.println("UNDERTOW SERVER STARTED");

    Thread.currentThread().join();

}
 
Example 4
Source File: UndertowHTTPServerEngine.java    From cxf with Apache License 2.0 5 votes vote down vote up
private ServletContext buildServletContext(String contextName)
    throws ServletException {
    ServletContainer servletContainer = new ServletContainerImpl();
    DeploymentInfo deploymentInfo = new DeploymentInfo();
    deploymentInfo.setClassLoader(Thread.currentThread().getContextClassLoader());
    deploymentInfo.setDeploymentName("cxf-undertow");
    deploymentInfo.setContextPath(contextName);
    ServletInfo asyncServlet = new ServletInfo(ServletPathMatches.DEFAULT_SERVLET_NAME, CxfUndertowServlet.class);
    deploymentInfo.addServlet(asyncServlet);
    servletContainer.addDeployment(deploymentInfo);
    DeploymentManager deploymentManager = servletContainer.getDeployment(deploymentInfo.getDeploymentName());
    deploymentManager.deploy();
    deploymentManager.start();
    return deploymentManager.getDeployment().getServletContext();
}
 
Example 5
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 6
Source File: KeycloakOnUndertow.java    From keycloak with Apache License 2.0 5 votes vote down vote up
private DeploymentInfo createAuthServerDeploymentInfo() {
    ResteasyDeployment deployment = new ResteasyDeployment();
    deployment.setApplicationClass(KeycloakApplication.class.getName());

    // RESTEASY-2034
    deployment.setProperty(ResteasyContextParameters.RESTEASY_DISABLE_HTML_SANITIZER, true);

    DeploymentInfo di = undertow.undertowDeployment(deployment);
    di.setClassLoader(getClass().getClassLoader());
    di.setContextPath("/auth");
    di.setDeploymentName("Keycloak");
    if (configuration.getKeycloakConfigPropertyOverridesMap() != null) {
        try {
            di.addInitParameter(JsonConfigProviderFactory.SERVER_CONTEXT_CONFIG_PROPERTY_OVERRIDES,
              JsonSerialization.writeValueAsString(configuration.getKeycloakConfigPropertyOverridesMap()));
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }

    di.setDefaultServletConfig(new DefaultServletConfig(true));
    di.addWelcomePage("theme/keycloak/welcome/resources/index.html");

    FilterInfo filter = Servlets.filter("SessionFilter", TestKeycloakSessionServletFilter.class);
    di.addFilter(filter);
    di.addFilterUrlMapping("SessionFilter", "/*", DispatcherType.REQUEST);
    filter.setAsyncSupported(true);

    return di;
}
 
Example 7
Source File: UndertowAppServer.java    From keycloak with Apache License 2.0 4 votes vote down vote up
@Override
public ProtocolMetaData deploy(Archive<?> archive) throws DeploymentException {
    log.info("Deploying archive " + archive.getName());

    // Remove jsps
    String ioTMPDir = System.getProperty("java.io.tmpdir", ""); // My Intellij and Terminal stores tmp directory in this property
    if (!ioTMPDir.isEmpty()) {
        ioTMPDir = ioTMPDir.endsWith("/") ? ioTMPDir : ioTMPDir + "/";
        File tmpUndertowJSPDirectory = new File(ioTMPDir + "org/apache/jsp");
        if (tmpUndertowJSPDirectory.exists()) {
            try {
                FileUtils.deleteDirectory(tmpUndertowJSPDirectory);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    DeploymentInfo di;
    if (archive instanceof UndertowWebArchive) {
        di = ((UndertowWebArchive) archive).getDeploymentInfo();
    } else if (archive instanceof WebArchive) {
        WebArchive webArchive = (WebArchive)archive;

        Optional<Node> applicationClassNode = archive.getContent(archivePath ->
                archivePath.get().startsWith("/WEB-INF/classes/") && archivePath.get().endsWith("Application.class"))
                .values().stream().findFirst();

        if (isJaxrsApp(webArchive)) {
            di = new UndertowDeployerHelper().getDeploymentInfo(configuration, webArchive,
                    undertow.undertowDeployment(discoverPathAnnotatedClasses(webArchive)));
        } else if (applicationClassNode.isPresent()) {
            String applicationPath = applicationClassNode.get().getPath().get();

            ResteasyDeployment deployment = new ResteasyDeployment();
            deployment.setApplicationClass(extractClassName(applicationPath));
            di = new UndertowDeployerHelper().getDeploymentInfo(configuration, (WebArchive) archive, undertow.undertowDeployment(deployment));
        } else {
            di = new UndertowDeployerHelper().getDeploymentInfo(configuration, webArchive);
        }
    } else {
        throw new IllegalArgumentException("UndertowContainer only supports UndertowWebArchive or WebArchive.");
    }

    if ("ROOT.war".equals(archive.getName())) {
        di.setContextPath("/");
    }

    ClassLoader parentCl = Thread.currentThread().getContextClassLoader();
    UndertowWarClassLoader classLoader = new UndertowWarClassLoader(parentCl, archive);
    Thread.currentThread().setContextClassLoader(classLoader);

    try {
        undertow.deploy(di);
    } finally {
        Thread.currentThread().setContextClassLoader(parentCl);
    }

    deployedArchivesToContextPath.put(archive.getName(), di.getContextPath());

    return new ProtocolMetaData().addContext(
            createHttpContextForDeploymentInfo(di));
}