Search in sources :

Example 1 with RequestDumpingHandler

use of io.undertow.server.handlers.RequestDumpingHandler in project openremote by openremote.

the class WebService method build.

protected Undertow.Builder build(Container container, Undertow.Builder builder) {
    LOG.info("Building web routing with custom routes: " + getPrefixRoutes().keySet());
    IdentityService identityService = container.hasService(IdentityService.class) ? container.getService(IdentityService.class) : null;
    ResteasyDeployment resteasyDeployment = createResteasyDeployment(container);
    HttpHandler apiHandler = createApiHandler(identityService, resteasyDeployment);
    HttpHandler jsApiHandler = createJsApiHandler(identityService, resteasyDeployment);
    requestPathHandler = new PathHandler(apiHandler);
    HttpHandler handler = exchange -> {
        String requestPath = exchange.getRequestPath();
        LOG.fine("Handling request: " + exchange.getRequestMethod() + " " + exchange.getRequestPath());
        // Other services can register routes here with a prefix patch match
        boolean handled = false;
        for (Map.Entry<String, HttpHandler> entry : getPrefixRoutes().entrySet()) {
            if (requestPath.startsWith(entry.getKey())) {
                LOG.fine("Handling with '" + entry.getValue().getClass().getName() + "' path prefix: " + entry.getKey());
                entry.getValue().handleRequest(exchange);
                handled = true;
                break;
            }
        }
        if (handled)
            return;
        // Redirect / to default realm
        if (requestPath.equals("/")) {
            LOG.fine("Handling root request, redirecting client to default realm: " + requestPath);
            new RedirectHandler(fromUri(exchange.getRequestURL()).replacePath(getDefaultRealm()).build().toString()).handleRequest(exchange);
            return;
        }
        // Serve JavaScript API with path /jsapi/*
        if (jsApiHandler != null && requestPath.startsWith(JSAPI_PATH)) {
            LOG.fine("Serving JS API call: " + requestPath);
            jsApiHandler.handleRequest(exchange);
            return;
        }
        // Serve /<realm>/index.html
        Matcher realmRootMatcher = PATTERN_REALM_ROOT.matcher(requestPath);
        if (getRealmIndexHandler() != null && realmRootMatcher.matches()) {
            LOG.fine("Serving index document of realm: " + requestPath);
            exchange.setRelativePath("/index.html");
            getRealmIndexHandler().handleRequest(exchange);
            return;
        }
        Matcher realmSubMatcher = PATTERN_REALM_SUB.matcher(requestPath);
        if (!realmSubMatcher.matches()) {
            exchange.setStatusCode(NOT_FOUND.getStatusCode());
            throw new WebApplicationException(NOT_FOUND);
        }
        // Extract realm from path and push it into REQUEST_HEADER_REALM header
        String realm = realmSubMatcher.group(1);
        // Move the realm from path segment to header
        exchange.getRequestHeaders().put(HttpString.tryFromString(REQUEST_HEADER_REALM), realm);
        // Rewrite path, remove realm segment
        URI url = fromUri(exchange.getRequestURL()).replacePath(realmSubMatcher.group(2)).build();
        exchange.setRequestURI(url.toString(), true);
        exchange.setRequestPath(url.getPath());
        exchange.setRelativePath(url.getPath());
        // Look for registered path handlers and fallback to API handler
        LOG.fine("Serving HTTP call: " + url.getPath());
        requestPathHandler.handleRequest(exchange);
    };
    handler = new WebServiceExceptions.RootUndertowExceptionHandler(devMode, handler);
    if (getBoolean(container.getConfig(), WEBSERVER_DUMP_REQUESTS, WEBSERVER_DUMP_REQUESTS_DEFAULT)) {
        handler = new RequestDumpingHandler(handler);
    }
    builder.setHandler(handler);
    return builder;
}
Also used : java.util(java.util) UriBuilder.fromUri(javax.ws.rs.core.UriBuilder.fromUri) JSAPIServlet(org.openremote.container.web.jsapi.JSAPIServlet) ServletInfo(io.undertow.servlet.api.ServletInfo) Undertow(io.undertow.Undertow) HttpString(io.undertow.util.HttpString) RedirectHandler(io.undertow.server.handlers.RedirectHandler) Servlets(io.undertow.servlet.Servlets) Container(org.openremote.container.Container) PathHandler(io.undertow.server.handlers.PathHandler) Matcher(java.util.regex.Matcher) ResteasyDeployment(org.jboss.resteasy.spi.ResteasyDeployment) ContainerService(org.openremote.container.ContainerService) UriBuilder(javax.ws.rs.core.UriBuilder) URI(java.net.URI) RequestDumpingHandler(io.undertow.server.handlers.RequestDumpingHandler) NOT_FOUND(javax.ws.rs.core.Response.Status.NOT_FOUND) ModelValueMessageBodyConverter(org.openremote.container.json.ModelValueMessageBodyConverter) Logger(java.util.logging.Logger) DeploymentManager(io.undertow.servlet.api.DeploymentManager) Inet4Address(java.net.Inet4Address) HttpServlet30Dispatcher(org.jboss.resteasy.plugins.server.servlet.HttpServlet30Dispatcher) CORSFilter(org.openremote.container.security.CORSFilter) HttpHandler(io.undertow.server.HttpHandler) Options(org.xnio.Options) IdentityService(org.openremote.container.security.IdentityService) MapAccess(org.openremote.container.util.MapAccess) ResteasyContextParameters(org.jboss.resteasy.plugins.server.servlet.ResteasyContextParameters) JacksonConfig(org.openremote.container.json.JacksonConfig) WebApplicationException(javax.ws.rs.WebApplicationException) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) Pattern(java.util.regex.Pattern) HttpHandler(io.undertow.server.HttpHandler) WebApplicationException(javax.ws.rs.WebApplicationException) Matcher(java.util.regex.Matcher) RedirectHandler(io.undertow.server.handlers.RedirectHandler) PathHandler(io.undertow.server.handlers.PathHandler) HttpString(io.undertow.util.HttpString) URI(java.net.URI) IdentityService(org.openremote.container.security.IdentityService) ResteasyDeployment(org.jboss.resteasy.spi.ResteasyDeployment) RequestDumpingHandler(io.undertow.server.handlers.RequestDumpingHandler)

Example 2 with RequestDumpingHandler

use of io.undertow.server.handlers.RequestDumpingHandler in project undertow by undertow-io.

the class BinaryEndpointTest method setup.

@BeforeClass
public static void setup() throws Exception {
    bytes = new byte[256 * 1024];
    new Random().nextBytes(bytes);
    final ServletContainer container = ServletContainer.Factory.newInstance();
    DeploymentInfo builder = new DeploymentInfo().setClassLoader(BinaryEndpointTest.class.getClassLoader()).setContextPath("/").setClassIntrospecter(TestClassIntrospector.INSTANCE).addServlet(Servlets.servlet("bin", BinaryEndpointServlet.class).setLoadOnStartup(100)).addServletContextAttribute(WebSocketDeploymentInfo.ATTRIBUTE_NAME, new WebSocketDeploymentInfo().setBuffers(DefaultServer.getBufferPool()).setWorker(DefaultServer.getWorkerSupplier()).addListener(serverContainer -> deployment = serverContainer)).setDeploymentName("servletContext.war");
    deploymentManager = container.addDeployment(builder);
    deploymentManager.deploy();
    DefaultServer.setRootHandler(new RequestDumpingHandler(deploymentManager.start()));
    DefaultServer.startSSLServer();
}
Also used : Random(java.util.Random) ServletContainer(io.undertow.servlet.api.ServletContainer) RequestDumpingHandler(io.undertow.server.handlers.RequestDumpingHandler) WebSocketDeploymentInfo(io.undertow.websockets.jsr.WebSocketDeploymentInfo) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) WebSocketDeploymentInfo(io.undertow.websockets.jsr.WebSocketDeploymentInfo) BeforeClass(org.junit.BeforeClass)

Example 3 with RequestDumpingHandler

use of io.undertow.server.handlers.RequestDumpingHandler in project undertow by undertow-io.

the class PredicatedHandlersParserTestCase method testParsedHandler1.

@Test
public void testParsedHandler1() {
    String value = "dump-request";
    List<PredicatedHandler> ret = PredicatedHandlersParser.parse(value, getClass().getClassLoader());
    Assert.assertEquals(1, ret.size());
    HttpHandler handler = ret.get(0).getHandler().wrap(ResponseCodeHandler.HANDLE_200);
    Assert.assertTrue(handler instanceof RequestDumpingHandler);
}
Also used : HttpHandler(io.undertow.server.HttpHandler) RequestDumpingHandler(io.undertow.server.handlers.RequestDumpingHandler) HttpString(io.undertow.util.HttpString) UnitTest(io.undertow.testutils.category.UnitTest) Test(org.junit.Test)

Example 4 with RequestDumpingHandler

use of io.undertow.server.handlers.RequestDumpingHandler in project undertow by undertow-io.

the class PredicatedHandlersParserTestCase method testParsedPredicatedHandler1.

@Test
public void testParsedPredicatedHandler1() {
    String value = "contains(value='a', search=b) -> dump-request";
    List<PredicatedHandler> ret = PredicatedHandlersParser.parse(value, getClass().getClassLoader());
    Assert.assertEquals(1, ret.size());
    HttpHandler handler = ret.get(0).getHandler().wrap(ResponseCodeHandler.HANDLE_200);
    Assert.assertTrue(handler instanceof RequestDumpingHandler);
    ContainsPredicate predicate = (ContainsPredicate) ret.get(0).getPredicate();
    Assert.assertEquals("a", predicate.getAttribute().readAttribute(null));
    Assert.assertArrayEquals(new String[] { "b" }, predicate.getValues());
    value = "contains(value='a', search={b}) -> dump-request";
    ret = PredicatedHandlersParser.parse(value, getClass().getClassLoader());
    Assert.assertEquals(1, ret.size());
    handler = ret.get(0).getHandler().wrap(ResponseCodeHandler.HANDLE_200);
    Assert.assertTrue(handler instanceof RequestDumpingHandler);
    predicate = (ContainsPredicate) ret.get(0).getPredicate();
    Assert.assertEquals("a", predicate.getAttribute().readAttribute(null));
    Assert.assertArrayEquals(new String[] { "b" }, predicate.getValues());
    value = "contains[value='a', search={b, c}] -> dump-request";
    ret = PredicatedHandlersParser.parse(value, getClass().getClassLoader());
    Assert.assertEquals(1, ret.size());
    handler = ret.get(0).getHandler().wrap(ResponseCodeHandler.HANDLE_200);
    Assert.assertTrue(handler instanceof RequestDumpingHandler);
    predicate = (ContainsPredicate) ret.get(0).getPredicate();
    Assert.assertEquals("a", predicate.getAttribute().readAttribute(null));
    Assert.assertArrayEquals(new String[] { "b", "c" }, predicate.getValues());
}
Also used : HttpHandler(io.undertow.server.HttpHandler) ContainsPredicate(io.undertow.predicate.ContainsPredicate) RequestDumpingHandler(io.undertow.server.handlers.RequestDumpingHandler) HttpString(io.undertow.util.HttpString) UnitTest(io.undertow.testutils.category.UnitTest) Test(org.junit.Test)

Aggregations

RequestDumpingHandler (io.undertow.server.handlers.RequestDumpingHandler)4 HttpHandler (io.undertow.server.HttpHandler)3 HttpString (io.undertow.util.HttpString)3 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)2 UnitTest (io.undertow.testutils.category.UnitTest)2 Test (org.junit.Test)2 Undertow (io.undertow.Undertow)1 ContainsPredicate (io.undertow.predicate.ContainsPredicate)1 PathHandler (io.undertow.server.handlers.PathHandler)1 RedirectHandler (io.undertow.server.handlers.RedirectHandler)1 Servlets (io.undertow.servlet.Servlets)1 DeploymentManager (io.undertow.servlet.api.DeploymentManager)1 ServletContainer (io.undertow.servlet.api.ServletContainer)1 ServletInfo (io.undertow.servlet.api.ServletInfo)1 WebSocketDeploymentInfo (io.undertow.websockets.jsr.WebSocketDeploymentInfo)1 Inet4Address (java.net.Inet4Address)1 URI (java.net.URI)1 java.util (java.util)1 Random (java.util.Random)1 Logger (java.util.logging.Logger)1