Search in sources :

Example 46 with HttpHandler

use of io.undertow.server.HttpHandler in project undertow by undertow-io.

the class DeploymentManagerImpl method deploy.

@Override
public void deploy() {
    final DeploymentInfo deploymentInfo = originalDeployment.clone();
    if (deploymentInfo.getServletStackTraces() == ServletStackTraces.ALL) {
        UndertowServletLogger.REQUEST_LOGGER.servletStackTracesAll(deploymentInfo.getDeploymentName());
    }
    deploymentInfo.validate();
    final DeploymentImpl deployment = new DeploymentImpl(this, deploymentInfo, servletContainer);
    this.deployment = deployment;
    final ServletContextImpl servletContext = new ServletContextImpl(servletContainer, deployment);
    deployment.setServletContext(servletContext);
    handleExtensions(deploymentInfo, servletContext);
    final List<ThreadSetupHandler> setup = new ArrayList<>();
    setup.add(ServletRequestContextThreadSetupAction.INSTANCE);
    setup.add(new ContextClassLoaderSetupAction(deploymentInfo.getClassLoader()));
    setup.addAll(deploymentInfo.getThreadSetupActions());
    deployment.setThreadSetupActions(setup);
    deployment.getServletPaths().setWelcomePages(deploymentInfo.getWelcomePages());
    if (deploymentInfo.getDefaultEncoding() != null) {
        deployment.setDefaultCharset(Charset.forName(deploymentInfo.getDefaultEncoding()));
    }
    handleDeploymentSessionConfig(deploymentInfo, servletContext);
    deployment.setSessionManager(deploymentInfo.getSessionManagerFactory().createSessionManager(deployment));
    deployment.getSessionManager().setDefaultSessionTimeout(deploymentInfo.getDefaultSessionTimeout());
    try {
        deployment.createThreadSetupAction(new ThreadSetupHandler.Action<Void, Object>() {

            @Override
            public Void call(HttpServerExchange exchange, Object ignore) throws Exception {
                final ApplicationListeners listeners = createListeners();
                listeners.start();
                deployment.setApplicationListeners(listeners);
                //now create the servlets and filters that we know about. We can still get more later
                createServletsAndFilters(deployment, deploymentInfo);
                //first initialize the temp dir
                initializeTempDir(servletContext, deploymentInfo);
                //then run the SCI's
                for (final ServletContainerInitializerInfo sci : deploymentInfo.getServletContainerInitializers()) {
                    final InstanceHandle<? extends ServletContainerInitializer> instance = sci.getInstanceFactory().createInstance();
                    try {
                        instance.getInstance().onStartup(sci.getHandlesTypes(), servletContext);
                    } finally {
                        instance.release();
                    }
                }
                deployment.getSessionManager().registerSessionListener(new SessionListenerBridge(deployment, listeners, servletContext));
                for (SessionListener listener : deploymentInfo.getSessionListeners()) {
                    deployment.getSessionManager().registerSessionListener(listener);
                }
                initializeErrorPages(deployment, deploymentInfo);
                initializeMimeMappings(deployment, deploymentInfo);
                listeners.contextInitialized();
                //run
                HttpHandler wrappedHandlers = ServletDispatchingHandler.INSTANCE;
                wrappedHandlers = wrapHandlers(wrappedHandlers, deploymentInfo.getInnerHandlerChainWrappers());
                if (!deploymentInfo.isSecurityDisabled()) {
                    HttpHandler securityHandler = setupSecurityHandlers(wrappedHandlers);
                    wrappedHandlers = new PredicateHandler(DispatcherTypePredicate.REQUEST, securityHandler, wrappedHandlers);
                }
                HttpHandler outerHandlers = wrapHandlers(wrappedHandlers, deploymentInfo.getOuterHandlerChainWrappers());
                wrappedHandlers = new PredicateHandler(DispatcherTypePredicate.REQUEST, outerHandlers, wrappedHandlers);
                wrappedHandlers = handleDevelopmentModePersistentSessions(wrappedHandlers, deploymentInfo, deployment.getSessionManager(), servletContext);
                MetricsCollector metrics = deploymentInfo.getMetricsCollector();
                if (metrics != null) {
                    wrappedHandlers = new MetricsChainHandler(wrappedHandlers, metrics, deployment);
                }
                if (deploymentInfo.getCrawlerSessionManagerConfig() != null) {
                    wrappedHandlers = new CrawlerSessionManagerHandler(deploymentInfo.getCrawlerSessionManagerConfig(), wrappedHandlers);
                }
                final ServletInitialHandler servletInitialHandler = SecurityActions.createServletInitialHandler(deployment.getServletPaths(), wrappedHandlers, deployment, servletContext);
                HttpHandler initialHandler = wrapHandlers(servletInitialHandler, deployment.getDeploymentInfo().getInitialHandlerChainWrappers());
                initialHandler = new HttpContinueReadHandler(initialHandler);
                if (deploymentInfo.getUrlEncoding() != null) {
                    initialHandler = Handlers.urlDecodingHandler(deploymentInfo.getUrlEncoding(), initialHandler);
                }
                deployment.setInitialHandler(initialHandler);
                deployment.setServletHandler(servletInitialHandler);
                //make sure we have a fresh set of servlet paths
                deployment.getServletPaths().invalidate();
                servletContext.initDone();
                return null;
            }
        }).call(null, null);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    //any problems with the paths won't get detected until the data is initialize
    //so we force initialization here
    deployment.getServletPaths().initData();
    state = State.DEPLOYED;
}
Also used : MetricsCollector(io.undertow.servlet.api.MetricsCollector) HttpHandler(io.undertow.server.HttpHandler) ServletInitialHandler(io.undertow.servlet.handlers.ServletInitialHandler) ServletContainerInitializerInfo(io.undertow.servlet.api.ServletContainerInitializerInfo) ServletContextImpl(io.undertow.servlet.spec.ServletContextImpl) ArrayList(java.util.ArrayList) PredicateHandler(io.undertow.server.handlers.PredicateHandler) ServletException(javax.servlet.ServletException) HttpServerExchange(io.undertow.server.HttpServerExchange) ThreadSetupHandler(io.undertow.servlet.api.ThreadSetupHandler) HttpContinueReadHandler(io.undertow.server.handlers.HttpContinueReadHandler) CrawlerSessionManagerHandler(io.undertow.servlet.handlers.CrawlerSessionManagerHandler) SessionListener(io.undertow.server.session.SessionListener) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo)

Example 47 with HttpHandler

use of io.undertow.server.HttpHandler in project undertow by undertow-io.

the class ServletPathMatches method servletChain.

private static ServletChain servletChain(HttpHandler next, final ManagedServlet managedServlet, final String servletPath, final DeploymentInfo deploymentInfo, boolean defaultServlet, MappingMatch mappingMatch, String pattern) {
    HttpHandler servletHandler = new ServletSecurityRoleHandler(next, deploymentInfo.getAuthorizationManager());
    servletHandler = wrapHandlers(servletHandler, managedServlet.getServletInfo().getHandlerChainWrappers());
    return new ServletChain(servletHandler, managedServlet, servletPath, defaultServlet, mappingMatch, pattern);
}
Also used : HttpHandler(io.undertow.server.HttpHandler) ServletSecurityRoleHandler(io.undertow.servlet.handlers.security.ServletSecurityRoleHandler)

Example 48 with HttpHandler

use of io.undertow.server.HttpHandler in project undertow by undertow-io.

the class ModClusterProxyServer method main.

public static void main(final String[] args) throws IOException {
    final XnioWorker worker = Xnio.getInstance().createWorker(OptionMap.EMPTY);
    final Undertow server;
    final ModCluster modCluster = ModCluster.builder(worker).build();
    try {
        if (chost == null) {
            // We are going to guess it.
            chost = java.net.InetAddress.getLocalHost().getHostName();
            System.out.println("Using: " + chost + ":" + cport);
        }
        modCluster.start();
        // Create the proxy and mgmt handler
        final HttpHandler proxy = modCluster.createProxyHandler();
        final MCMPConfig config = MCMPConfig.webBuilder().setManagementHost(chost).setManagementPort(cport).enableAdvertise().getParent().build();
        final HttpHandler mcmp = config.create(modCluster, proxy);
        server = Undertow.builder().addHttpListener(cport, chost).addHttpListener(pport, phost).setHandler(mcmp).build();
        server.start();
        // Start advertising the mcmp handler
        modCluster.advertise(config);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : HttpHandler(io.undertow.server.HttpHandler) ModCluster(io.undertow.server.handlers.proxy.mod_cluster.ModCluster) XnioWorker(org.xnio.XnioWorker) MCMPConfig(io.undertow.server.handlers.proxy.mod_cluster.MCMPConfig) Undertow(io.undertow.Undertow) IOException(java.io.IOException)

Example 49 with HttpHandler

use of io.undertow.server.HttpHandler in project undertow by undertow-io.

the class ReverseProxyServer method main.

public static void main(final String[] args) {
    try {
        final Undertow server1 = Undertow.builder().addHttpListener(8081, "localhost").setHandler(new HttpHandler() {

            @Override
            public void handleRequest(HttpServerExchange exchange) throws Exception {
                exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
                exchange.getResponseSender().send("Server1");
            }
        }).build();
        server1.start();
        final Undertow server2 = Undertow.builder().addHttpListener(8082, "localhost").setHandler(new HttpHandler() {

            @Override
            public void handleRequest(HttpServerExchange exchange) throws Exception {
                exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
                exchange.getResponseSender().send("Server2");
            }
        }).build();
        server2.start();
        final Undertow server3 = Undertow.builder().addHttpListener(8083, "localhost").setHandler(new HttpHandler() {

            @Override
            public void handleRequest(HttpServerExchange exchange) throws Exception {
                exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
                exchange.getResponseSender().send("Server3");
            }
        }).build();
        server3.start();
        LoadBalancingProxyClient loadBalancer = new LoadBalancingProxyClient().addHost(new URI("http://localhost:8081")).addHost(new URI("http://localhost:8082")).addHost(new URI("http://localhost:8083")).setConnectionsPerThread(20);
        Undertow reverseProxy = Undertow.builder().addHttpListener(8080, "localhost").setIoThreads(4).setHandler(new ProxyHandler(loadBalancer, 30000, ResponseCodeHandler.HANDLE_404)).build();
        reverseProxy.start();
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) ProxyHandler(io.undertow.server.handlers.proxy.ProxyHandler) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) Undertow(io.undertow.Undertow) LoadBalancingProxyClient(io.undertow.server.handlers.proxy.LoadBalancingProxyClient)

Example 50 with HttpHandler

use of io.undertow.server.HttpHandler in project undertow by undertow-io.

the class HelloWorldServer method main.

public static void main(final String[] args) {
    Undertow server = Undertow.builder().addHttpListener(8080, "localhost").setHandler(new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) throws Exception {
            exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
            exchange.getResponseSender().send("Hello World");
        }
    }).build();
    server.start();
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) Undertow(io.undertow.Undertow)

Aggregations

HttpHandler (io.undertow.server.HttpHandler)126 HttpServerExchange (io.undertow.server.HttpServerExchange)72 IOException (java.io.IOException)54 BeforeClass (org.junit.BeforeClass)35 Test (org.junit.Test)25 TestHttpClient (io.undertow.testutils.TestHttpClient)20 HttpResponse (org.apache.http.HttpResponse)20 HttpGet (org.apache.http.client.methods.HttpGet)19 PathHandler (io.undertow.server.handlers.PathHandler)17 HttpString (io.undertow.util.HttpString)15 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)13 Undertow (io.undertow.Undertow)12 ArrayList (java.util.ArrayList)11 HandlerWrapper (io.undertow.server.HandlerWrapper)9 InMemorySessionManager (io.undertow.server.session.InMemorySessionManager)9 SessionAttachmentHandler (io.undertow.server.session.SessionAttachmentHandler)9 Header (org.apache.http.Header)9 SessionCookieConfig (io.undertow.server.session.SessionCookieConfig)8 AuthenticationMechanism (io.undertow.security.api.AuthenticationMechanism)7 AuthenticationCallHandler (io.undertow.security.handlers.AuthenticationCallHandler)7