Search in sources :

Example 1 with HttpHandler

use of io.undertow.server.HttpHandler in project camel by apache.

the class CamelRootHandler method add.

public synchronized void add(String path, String[] methods, boolean prefixMatch, HttpHandler handler) {
    String basePath = getBasePath(path);
    HttpHandler basePathHandler = pathHandler.getHandler(basePath);
    CamelMethodHandler targetHandler;
    if (path.contains("{")) {
        // Adding a handler for the template path
        String relativePath = path.substring(basePath.length());
        if (basePathHandler instanceof CamelPathTemplateHandler) {
            CamelPathTemplateHandler templateHandler = (CamelPathTemplateHandler) basePathHandler;
            targetHandler = templateHandler.get(relativePath);
            if (targetHandler == null) {
                targetHandler = new CamelMethodHandler();
                templateHandler.add(relativePath, targetHandler);
            }
        } else {
            CamelPathTemplateHandler templateHandler;
            if (basePathHandler instanceof CamelMethodHandler) {
                // A static path handler is already set for the base path. Use it as a default handler
                templateHandler = new CamelPathTemplateHandler((CamelMethodHandler) basePathHandler);
            } else if (basePathHandler == null) {
                templateHandler = new CamelPathTemplateHandler(new CamelMethodHandler());
            } else {
                throw new IllegalArgumentException(String.format("Unsupported handler '%s' was found", basePathHandler));
            }
            targetHandler = new CamelMethodHandler();
            templateHandler.add(relativePath, targetHandler);
            pathHandler.addPrefixPath(basePath, templateHandler);
        }
    } else {
        // Adding a handler for the static path
        if (basePathHandler instanceof CamelPathTemplateHandler) {
            CamelPathTemplateHandler templateHandler = (CamelPathTemplateHandler) basePathHandler;
            if (prefixMatch) {
                targetHandler = templateHandler.getDefault();
            } else {
                throw new IllegalArgumentException(String.format("Duplicate handlers on a path '%s'", path));
            }
        } else {
            if (basePathHandler instanceof CamelMethodHandler) {
                targetHandler = (CamelMethodHandler) basePathHandler;
            } else if (basePathHandler == null) {
                targetHandler = new CamelMethodHandler();
                if (prefixMatch) {
                    pathHandler.addPrefixPath(basePath, targetHandler);
                } else {
                    pathHandler.addExactPath(basePath, targetHandler);
                }
            } else {
                throw new IllegalArgumentException(String.format("Unsupported handler '%s' was found", basePathHandler));
            }
        }
    }
    if (methods != null && methods.length != 0) {
        targetHandler.add(methods, handler);
    } else {
        targetHandler.addDefault(handler);
    }
}
Also used : HttpHandler(io.undertow.server.HttpHandler)

Example 2 with HttpHandler

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

the class WebServer method initServer.

private void initServer(DaemonConfigTO daemonConfigTO) throws Exception {
    WebServerTO webServerConfig = daemonConfigTO.getWebServer();
    // Bind address and port
    String bindAddress = webServerConfig.getBindAddress();
    int bindPort = webServerConfig.getBindPort();
    // Users (incl. CLI user!)
    List<UserTO> users = readWebServerUsers(daemonConfigTO);
    IdentityManager identityManager = new MapIdentityManager(users);
    // (Re-)generate keypair/certificate (if requested)
    boolean certificateAutoGenerate = webServerConfig.isCertificateAutoGenerate();
    String certificateCommonName = webServerConfig.getCertificateCommonName();
    if (certificateAutoGenerate && certificateCommonNameChanged(certificateCommonName)) {
        generateNewKeyPairAndCertificate(certificateCommonName);
    }
    // Set up the handlers for WebSocket, REST and the web interface
    HttpHandler pathHttpHandler = path().addPrefixPath(API_ENDPOINT_WS_XML, websocket(new InternalWebSocketHandler(this, certificateCommonName, RequestFormatType.XML))).addPrefixPath(API_ENDPOINT_WS_JSON, websocket(new InternalWebSocketHandler(this, certificateCommonName, RequestFormatType.JSON))).addPrefixPath(API_ENDPOINT_REST_XML, new InternalRestHandler(this, RequestFormatType.XML)).addPrefixPath(API_ENDPOINT_REST_JSON, new InternalRestHandler(this, RequestFormatType.JSON)).addPrefixPath("/", new InternalWebInterfaceHandler());
    // Add some security spices
    HttpHandler securityPathHttpHandler = addSecurity(pathHttpHandler, identityManager);
    SSLContext sslContext = UserConfig.createUserSSLContext();
    // And go for it!
    webServer = Undertow.builder().addHttpsListener(bindPort, bindAddress, sslContext).setHandler(securityPathHttpHandler).build();
    logger.log(Level.INFO, "Initialized web server.");
}
Also used : WebServerTO(org.syncany.config.to.WebServerTO) HttpHandler(io.undertow.server.HttpHandler) IdentityManager(io.undertow.security.idm.IdentityManager) InternalWebSocketHandler(org.syncany.operations.daemon.handlers.InternalWebSocketHandler) SSLContext(javax.net.ssl.SSLContext) InternalRestHandler(org.syncany.operations.daemon.handlers.InternalRestHandler) UserTO(org.syncany.config.to.UserTO) InternalWebInterfaceHandler(org.syncany.operations.daemon.handlers.InternalWebInterfaceHandler)

Example 3 with HttpHandler

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

the class ModClusterUndertowDeploymentProcessor method deploy.

@Override
public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
    DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
    // Add mod_cluster-undertow integration service (jboss.modcluster.undertow) as a web deployment dependency
    deploymentUnit.addToAttachmentList(Attachments.WEB_DEPENDENCIES, UndertowEventHandlerAdapterBuilder.SERVICE_NAME);
    // Request count wrapping
    if (isMetricEnabled(RequestCountLoadMetric.class)) {
        deploymentUnit.addToAttachmentList(UndertowAttachments.UNDERTOW_INITIAL_HANDLER_CHAIN_WRAPPERS, new HandlerWrapper() {

            @Override
            public HttpHandler wrap(final HttpHandler handler) {
                return new RequestCountHttpHandler(handler);
            }
        });
    }
    // Bytes Sent wrapping
    if (isMetricEnabled(SendTrafficLoadMetric.class)) {
        deploymentUnit.addToAttachmentList(UndertowAttachments.UNDERTOW_INITIAL_HANDLER_CHAIN_WRAPPERS, new HandlerWrapper() {

            @Override
            public HttpHandler wrap(final HttpHandler handler) {
                return new BytesSentHttpHandler(handler);
            }
        });
    }
    // Bytes Received wrapping
    if (isMetricEnabled(ReceiveTrafficLoadMetric.class)) {
        deploymentUnit.addToAttachmentList(UndertowAttachments.UNDERTOW_INITIAL_HANDLER_CHAIN_WRAPPERS, new HandlerWrapper() {

            @Override
            public HttpHandler wrap(final HttpHandler handler) {
                return new BytesReceivedHttpHandler(handler);
            }
        });
    }
    // Busyness thread setup actions
    if (isMetricEnabled(BusyConnectorsLoadMetric.class)) {
        deploymentUnit.addToAttachmentList(UndertowAttachments.UNDERTOW_OUTER_HANDLER_CHAIN_WRAPPERS, new HandlerWrapper() {

            @Override
            public HttpHandler wrap(final HttpHandler handler) {
                return new RunningRequestsHttpHandler(handler);
            }
        });
    }
}
Also used : RequestCountHttpHandler(org.wildfly.mod_cluster.undertow.metric.RequestCountHttpHandler) HttpHandler(io.undertow.server.HttpHandler) BytesReceivedHttpHandler(org.wildfly.mod_cluster.undertow.metric.BytesReceivedHttpHandler) BytesSentHttpHandler(org.wildfly.mod_cluster.undertow.metric.BytesSentHttpHandler) RunningRequestsHttpHandler(org.wildfly.mod_cluster.undertow.metric.RunningRequestsHttpHandler) RequestCountHttpHandler(org.wildfly.mod_cluster.undertow.metric.RequestCountHttpHandler) BytesSentHttpHandler(org.wildfly.mod_cluster.undertow.metric.BytesSentHttpHandler) BytesReceivedHttpHandler(org.wildfly.mod_cluster.undertow.metric.BytesReceivedHttpHandler) RunningRequestsHttpHandler(org.wildfly.mod_cluster.undertow.metric.RunningRequestsHttpHandler) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit) HandlerWrapper(io.undertow.server.HandlerWrapper)

Example 4 with HttpHandler

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

the class UndertowHostTestCase method findContext.

@Test
public void findContext() {
    Deployment deployment = mock(Deployment.class);
    DeploymentInfo info = new DeploymentInfo();
    String expectedPath = "";
    info.setContextPath(expectedPath);
    HttpHandler handler = mock(HttpHandler.class);
    when(deployment.getDeploymentInfo()).thenReturn(info);
    this.undertowHost.registerDeployment(deployment, handler);
    Context result = this.host.findContext(expectedPath);
    assertSame(this.host, result.getHost());
    assertSame(expectedPath, result.getPath());
    result = this.host.findContext("unknown");
    assertNull(result);
}
Also used : Context(org.jboss.modcluster.container.Context) HttpHandler(io.undertow.server.HttpHandler) Deployment(io.undertow.servlet.api.Deployment) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) Test(org.junit.Test)

Example 5 with HttpHandler

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

the class UndertowHostTestCase method getContexts.

@Test
public void getContexts() {
    Deployment deployment = mock(Deployment.class);
    DeploymentInfo info = new DeploymentInfo();
    String expectedPath = "";
    info.setContextPath(expectedPath);
    HttpHandler handler = mock(HttpHandler.class);
    when(deployment.getDeploymentInfo()).thenReturn(info);
    this.undertowHost.registerDeployment(deployment, handler);
    Iterator<Context> result = this.host.getContexts().iterator();
    assertTrue(result.hasNext());
    Context context = result.next();
    assertSame(this.host, context.getHost());
    assertSame(expectedPath, context.getPath());
    assertFalse(result.hasNext());
}
Also used : Context(org.jboss.modcluster.container.Context) HttpHandler(io.undertow.server.HttpHandler) Deployment(io.undertow.servlet.api.Deployment) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) Test(org.junit.Test)

Aggregations

HttpHandler (io.undertow.server.HttpHandler)117 HttpServerExchange (io.undertow.server.HttpServerExchange)71 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)16 HttpString (io.undertow.util.HttpString)14 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)11 ArrayList (java.util.ArrayList)11 Undertow (io.undertow.Undertow)10 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 OutputStream (java.io.OutputStream)7 ServiceController (org.jboss.msc.service.ServiceController)7