use of io.undertow.server.handlers.PathHandler 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.PathHandler in project undertow by undertow-io.
the class DeploymentUtils method setupServlet.
/**
* Sets up a simple servlet deployment with the provided servlets.
*
* This is just a convenience method for simple deployments
*
* @param servlets The servlets to add
*/
public static Deployment setupServlet(final ServletExtension servletExtension, final ServletInfo... servlets) {
final PathHandler pathHandler = new PathHandler();
final ServletContainer container = ServletContainer.Factory.newInstance();
DeploymentInfo builder = new DeploymentInfo().setClassLoader(SimpleServletTestCase.class.getClassLoader()).setContextPath("/servletContext").setClassIntrospecter(TestClassIntrospector.INSTANCE).setDeploymentName("servletContext.war").addServlets(servlets);
if (servletExtension != null) {
builder.addServletExtension(servletExtension);
}
DeploymentManager manager = container.addDeployment(builder);
manager.deploy();
try {
pathHandler.addPrefixPath(builder.getContextPath(), manager.start());
} catch (ServletException e) {
throw new RuntimeException(e);
}
DefaultServer.setRootHandler(pathHandler);
return manager.getDeployment();
}
use of io.undertow.server.handlers.PathHandler in project undertow by undertow-io.
the class AbstractResponseWrapperTestCase method setup.
@Before
public void setup() throws ServletException {
DeploymentInfo builder = new DeploymentInfo();
builder.setExceptionHandler(LoggingExceptionHandler.builder().add(IllegalArgumentException.class, "io.undertow", Logger.Level.DEBUG).build());
final PathHandler root = new PathHandler();
final ServletContainer container = ServletContainer.Factory.newInstance();
builder.addServlet(new ServletInfo("wrapperServlet", WrapperServlet.class).addMapping("/*"));
builder.addFilter(new FilterInfo("standard", StandardRequestWrappingFilter.class));
builder.addFilterUrlMapping("standard", "/standard", DispatcherType.REQUEST);
builder.addFilter(new FilterInfo("nonstandard", NonStandardRequestWrappingFilter.class));
builder.addFilterUrlMapping("nonstandard", "/nonstandard", DispatcherType.REQUEST);
builder.setClassIntrospecter(TestClassIntrospector.INSTANCE).setClassLoader(AbstractResponseWrapperTestCase.class.getClassLoader()).setContextPath("/servletContext").setDeploymentName("servletContext.war").setAllowNonStandardWrappers(isNonStandardAllowed());
final DeploymentManager manager = container.addDeployment(builder);
manager.deploy();
root.addPrefixPath(builder.getContextPath(), manager.start());
DefaultServer.setRootHandler(root);
}
use of io.undertow.server.handlers.PathHandler in project undertow by undertow-io.
the class CrossContextServletSessionTestCase method setup.
@BeforeClass
public static void setup() throws ServletException {
final ServletContainer container = ServletContainer.Factory.newInstance();
final PathHandler path = new PathHandler();
DefaultServer.setRootHandler(path);
createDeployment("1", container, path);
createDeployment("2", container, path);
}
use of io.undertow.server.handlers.PathHandler in project undertow by undertow-io.
the class ServletSessionPersistenceTestCase method testSimpleSessionUsage.
@Test
public void testSimpleSessionUsage() throws IOException, ServletException {
final PathHandler pathHandler = new PathHandler();
final ServletContainer container = ServletContainer.Factory.newInstance();
DeploymentInfo builder = new DeploymentInfo().setClassLoader(SimpleServletTestCase.class.getClassLoader()).setContextPath("/servletContext").setClassIntrospecter(TestClassIntrospector.INSTANCE).setDeploymentName("servletContext.war").setSessionPersistenceManager(new InMemorySessionPersistence()).setServletSessionConfig(new ServletSessionConfig().setPath("/servletContext/aa")).addServlets(new ServletInfo("servlet", SessionServlet.class).addMapping("/aa/b"));
DeploymentManager manager = container.addDeployment(builder);
manager.deploy();
try {
pathHandler.addPrefixPath(builder.getContextPath(), manager.start());
} catch (ServletException e) {
throw new RuntimeException(e);
}
DefaultServer.setRootHandler(pathHandler);
TestHttpClient client = new TestHttpClient();
try {
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/aa/b");
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
String response = HttpClientUtils.readResponse(result);
Assert.assertEquals("1", response);
String cookieValue = result.getHeaders("Set-Cookie")[0].getValue();
Assert.assertTrue(cookieValue, cookieValue.contains("JSESSIONID"));
Assert.assertTrue(cookieValue, cookieValue.contains("/servletContext/aa"));
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
response = HttpClientUtils.readResponse(result);
Assert.assertEquals("2", response);
manager.stop();
manager.undeploy();
manager.deploy();
pathHandler.addPrefixPath(builder.getContextPath(), manager.start());
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
response = HttpClientUtils.readResponse(result);
Assert.assertEquals("3", response);
} finally {
client.getConnectionManager().shutdown();
}
}
Aggregations