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;
}
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();
}
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);
}
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());
}
Aggregations