Search in sources :

Example 6 with ClassPathResourceManager

use of io.undertow.server.handlers.resource.ClassPathResourceManager in project symja_android_library by axkr.

the class ServletServer method runServer.

/**
 * @param deploymentName the <code>*.war</code> deployment name.
 * @param classLoader the current class loader
 * @param ajaxServlet the name of the AJAX servlet class
 * @param port typical port <code>8080</code>
 * @param welcomeFile TODO
 */
protected static void runServer(String deploymentName, ClassLoader classLoader, Class<? extends Servlet> ajaxServlet, int port, String welcomeFile) {
    try {
        // https://stackoverflow.com/a/41652378/24819
        String host = LOCALHOST_STRING ? "localhost" : InetAddress.getLocalHost().getHostAddress();
        DeploymentInfo servletBuilder = deployment().setClassLoader(classLoader).setContextPath(MMAServletServer.MYAPP).setDeploymentName(deploymentName).addServlets(servlet("query", ajaxServlet).setLoadOnStartup(1).addMapping("/query/"), servlet("doc", AJAXDocServlet.class).addMapping("/doc/*"), servlet("search", AJAXSearchServlet.class).addMapping("/doc/search/"));
        DeploymentManager manager = defaultContainer().addDeployment(servletBuilder);
        manager.deploy();
        HttpHandler servletHandler = manager.start();
        PathHandler path = // Handlers.redirect(MYAPP)
        Handlers.path().addPrefixPath("/ajax", servletHandler).addPrefixPath("/", resource(new ClassPathResourceManager(classLoader, "public/")).addWelcomeFiles(welcomeFile));
        Undertow server = Undertow.builder().addHttpListener(port, host).setHandler(path).build();
        server.start();
        URI uri = new URI("http://" + host + ":" + port + "/" + welcomeFile);
        if (Desktop.isDesktopSupported()) {
            Desktop.getDesktop().browse(uri);
        }
        LOGGER.info("Open browser URL: {}", uri);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Also used : HttpHandler(io.undertow.server.HttpHandler) DeploymentManager(io.undertow.servlet.api.DeploymentManager) PathHandler(io.undertow.server.handlers.PathHandler) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) ClassPathResourceManager(io.undertow.server.handlers.resource.ClassPathResourceManager) URI(java.net.URI) Undertow(io.undertow.Undertow) ReturnException(org.matheclipse.core.eval.exception.ReturnException)

Example 7 with ClassPathResourceManager

use of io.undertow.server.handlers.resource.ClassPathResourceManager in project undertow by undertow-io.

the class WebSocketServer method main.

public static void main(final String[] args) {
    Undertow server = Undertow.builder().addHttpListener(8080, "localhost").setHandler(path().addPrefixPath("/myapp", websocket(new WebSocketConnectionCallback() {

        @Override
        public void onConnect(WebSocketHttpExchange exchange, WebSocketChannel channel) {
            channel.getReceiveSetter().set(new AbstractReceiveListener() {

                @Override
                protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) {
                    WebSockets.sendText(message.getData(), channel, null);
                }
            });
            channel.resumeReceives();
        }
    })).addPrefixPath("/", resource(new ClassPathResourceManager(WebSocketServer.class.getClassLoader(), WebSocketServer.class.getPackage())).addWelcomeFiles("index.html"))).build();
    server.start();
}
Also used : WebSocketHttpExchange(io.undertow.websockets.spi.WebSocketHttpExchange) WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) AbstractReceiveListener(io.undertow.websockets.core.AbstractReceiveListener) ClassPathResourceManager(io.undertow.server.handlers.resource.ClassPathResourceManager) WebSocketConnectionCallback(io.undertow.websockets.WebSocketConnectionCallback) Undertow(io.undertow.Undertow) BufferedTextMessage(io.undertow.websockets.core.BufferedTextMessage)

Example 8 with ClassPathResourceManager

use of io.undertow.server.handlers.resource.ClassPathResourceManager in project undertow by undertow-io.

the class JSRWebSocketServer method main.

public static void main(final String[] args) {
    PathHandler path = Handlers.path();
    Undertow server = Undertow.builder().addHttpListener(8080, "localhost").setHandler(path).build();
    server.start();
    final ServletContainer container = ServletContainer.Factory.newInstance();
    DeploymentInfo builder = new DeploymentInfo().setClassLoader(JSRWebSocketServer.class.getClassLoader()).setContextPath("/").addWelcomePage("index.html").setResourceManager(new ClassPathResourceManager(JSRWebSocketServer.class.getClassLoader(), JSRWebSocketServer.class.getPackage())).addServletContextAttribute(WebSocketDeploymentInfo.ATTRIBUTE_NAME, new WebSocketDeploymentInfo().setBuffers(new DefaultByteBufferPool(true, 100)).addEndpoint(JsrChatWebSocketEndpoint.class)).setDeploymentName("chat.war");
    DeploymentManager manager = container.addDeployment(builder);
    manager.deploy();
    try {
        path.addPrefixPath("/", manager.start());
    } catch (ServletException e) {
        throw new RuntimeException(e);
    }
}
Also used : ServletException(javax.servlet.ServletException) DefaultByteBufferPool(io.undertow.server.DefaultByteBufferPool) DeploymentManager(io.undertow.servlet.api.DeploymentManager) ServletContainer(io.undertow.servlet.api.ServletContainer) PathHandler(io.undertow.server.handlers.PathHandler) WebSocketDeploymentInfo(io.undertow.websockets.jsr.WebSocketDeploymentInfo) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) ClassPathResourceManager(io.undertow.server.handlers.resource.ClassPathResourceManager) WebSocketDeploymentInfo(io.undertow.websockets.jsr.WebSocketDeploymentInfo) Undertow(io.undertow.Undertow)

Example 9 with ClassPathResourceManager

use of io.undertow.server.handlers.resource.ClassPathResourceManager in project core by weld.

the class UndertowSmokeTest method testUndertow.

@Test
public void testUndertow() throws ServletException, InterruptedException {
    DeploymentInfo servletBuilder = Servlets.deployment().setClassLoader(UndertowSmokeTest.class.getClassLoader()).setResourceManager(new ClassPathResourceManager(UndertowSmokeTest.class.getClassLoader())).setContextPath("/").setDeploymentName("test.war").addListener(Servlets.listener(Listener.class)).addServlet(Servlets.servlet(InjectedServlet.class).addMapping("/*").setLoadOnStartup(1)).addListener(Servlets.listener(InjectedListener.class)).addFilter(Servlets.filter(InjectedFilter.class)).setEagerFilterInit(true).addInitParameter(Container.CONTEXT_PARAM_CONTAINER_CLASS, UndertowContainer.class.getName());
    DeploymentManager manager = Servlets.defaultContainer().addDeployment(servletBuilder);
    manager.deploy();
    HttpHandler servletHandler = manager.start();
    PathHandler path = Handlers.path(Handlers.redirect("/")).addPrefixPath("/", servletHandler);
    Undertow server = Undertow.builder().addHttpListener(8080, "localhost").setHandler(path).build();
    server.start();
    try {
        assertTrue(SYNC.await(5, TimeUnit.SECONDS));
    } finally {
        server.stop();
    }
}
Also used : HttpHandler(io.undertow.server.HttpHandler) DeploymentManager(io.undertow.servlet.api.DeploymentManager) PathHandler(io.undertow.server.handlers.PathHandler) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) ClassPathResourceManager(io.undertow.server.handlers.resource.ClassPathResourceManager) Undertow(io.undertow.Undertow) UndertowContainer(org.jboss.weld.environment.undertow.UndertowContainer) Test(org.junit.Test)

Example 10 with ClassPathResourceManager

use of io.undertow.server.handlers.resource.ClassPathResourceManager in project joinfaces by joinfaces.

the class UndertowAutoConfiguration method jsfUndertowFactoryCustomizer.

@Bean
public WebServerFactoryCustomizer<UndertowServletWebServerFactory> jsfUndertowFactoryCustomizer() {
    return factory -> factory.addDeploymentInfoCustomizers(deploymentInfo -> {
        AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
            ClassLoader jsfClassLoader = new URLClassLoader(new URL[0], this.getClass().getClassLoader());
            deploymentInfo.setClassLoader(jsfClassLoader);
            deploymentInfo.setResourceManager(new ClassPathResourceManager(jsfClassLoader, this.undertowProperties.getClassPathResource()));
            return null;
        });
        log.info("Setting Undertow classLoader to {} directory", this.undertowProperties.getClassPathResource());
    });
}
Also used : ClassPathResourceManager(io.undertow.server.handlers.resource.ClassPathResourceManager) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) URL(java.net.URL) RequiredArgsConstructor(lombok.RequiredArgsConstructor) WebServerFactoryCustomizer(org.springframework.boot.web.server.WebServerFactoryCustomizer) ConfigureListener(com.sun.faces.config.ConfigureListener) PrivilegedAction(java.security.PrivilegedAction) Undertow(io.undertow.Undertow) Configuration(org.springframework.context.annotation.Configuration) URLClassLoader(java.net.URLClassLoader) Slf4j(lombok.extern.slf4j.Slf4j) EnableConfigurationProperties(org.springframework.boot.context.properties.EnableConfigurationProperties) UndertowServletWebServerFactory(org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory) Bean(org.springframework.context.annotation.Bean) AccessController(java.security.AccessController) ConditionalOnWebApplication(org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) ClassPathResourceManager(io.undertow.server.handlers.resource.ClassPathResourceManager) URL(java.net.URL) Bean(org.springframework.context.annotation.Bean)

Aggregations

ClassPathResourceManager (io.undertow.server.handlers.resource.ClassPathResourceManager)13 Undertow (io.undertow.Undertow)10 PathHandler (io.undertow.server.handlers.PathHandler)5 HttpHandler (io.undertow.server.HttpHandler)4 DeploymentManager (io.undertow.servlet.api.DeploymentManager)4 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)3 WebSocketConnectionCallback (io.undertow.websockets.WebSocketConnectionCallback)3 AbstractReceiveListener (io.undertow.websockets.core.AbstractReceiveListener)3 BufferedTextMessage (io.undertow.websockets.core.BufferedTextMessage)3 WebSocketChannel (io.undertow.websockets.core.WebSocketChannel)3 WebSocketHttpExchange (io.undertow.websockets.spi.WebSocketHttpExchange)3 URI (java.net.URI)2 ReturnException (org.matheclipse.core.eval.exception.ReturnException)2 ConfigureListener (com.sun.faces.config.ConfigureListener)1 Config (com.typesafe.config.Config)1 Predicate (io.undertow.predicate.Predicate)1 DefaultByteBufferPool (io.undertow.server.DefaultByteBufferPool)1 HttpServerExchange (io.undertow.server.HttpServerExchange)1 DirectBufferCache (io.undertow.server.handlers.cache.DirectBufferCache)1 CachingResourceManager (io.undertow.server.handlers.resource.CachingResourceManager)1