Skip to content

Instantly share code, notes, and snippets.

@Jotschi
Last active March 7, 2017 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jotschi/39fc0d3fcd45122eafe68d61bea4c120 to your computer and use it in GitHub Desktop.
Save Jotschi/39fc0d3fcd45122eafe68d61bea4c120 to your computer and use it in GitHub Desktop.
Mesh Vert.x Example
public static void main(String[] args) {
VertxOptions options = new VertxOptions();
Vertx vertx = Vertx.vertx(options);
vertx.deployVerticle(new Server());
}
private void handlePath(String path, RoutingContext rc) {
// Resolve the node using the webroot API.
resolvePath(path).subscribe(response -> {
// The response contains binary data which means that the
// webroot API returned binary data for the specified path. We
// pass the binary data along and return it to the client.
if (response.isBinary()) {
String contentType = response.getDownloadResponse().getContentType();
rc.response().putHeader(CONTENT_TYPE, contentType);
rc.response().end(response.getDownloadResponse().getBuffer());
// Note that we are not calling rc.next() which
// will prevent the execution of the template route handler.
return;
}
// Alternatively the response contains node JSON data which can
// be used to render a template.
JsonObject jsonObject = toJsonNode(response.getNodeResponse());
// We render different templates for each schema type. Category
// nodes show products and thus the productList template is
// utilized for those nodes.
String schemaName = response.getNodeResponse().getSchema().getName();
switch(schemaName) {
case "category":
Single<JsonObject> childrenObs = loadCategoryChildren(response.getNodeResponse());
Single.zip(childrenObs, loadBreadcrumb(),
(children, breadcrumb) -> {
rc.put("tmplName", "productList.hbs");
rc.put("category", jsonObject);
rc.put("products", children);
rc.put("breadcrumb", breadcrumb);
rc.response().putHeader(CONTENT_TYPE, "text/html");
rc.next();
return null;
}).subscribe((e) -> {
}, error -> {
rc.fail(error);
});
return;
// Show the productDetail page for nodes of type vehicle
case "vehicle":
loadBreadcrumb().subscribe(nav -> {
rc.put("tmplName", "productDetail.hbs");
rc.put("product", jsonObject);
rc.put("breadcrumb", nav);
rc.response().putHeader(CONTENT_TYPE, "text/html");
rc.next();
});
return;
}
// Return a 404 response for all other cases
rc.response().setStatusCode(404).end("Not found");
});
}
public void routeHandler(RoutingContext rc) {
String path = rc.pathParam("param0");
if (path.equals("favicon.ico")) {
rc.response().setStatusCode(404).end("Not found");
return;
}
// Render the welcome page for root page requests
if (path.isEmpty()) {
loadBreadcrumb().subscribe(breadcrumb -> {
rc.put("breadcrumb", breadcrumb);
rc.put("tmplName", "welcome.hbs");
rc.next();
});
return;
}
if (log.isDebugEnabled()) {
log.debug("Handling request for path {" + path + "}");
}
handlePath(path, rc);
}
@Override
public void start() throws Exception {
// Login to mesh on http://localhost:8080
log.info("Login into mesh..");
client = MeshRestClient.create("localhost", 8080, vertx, BASIC_AUTH);
client.setLogin("webclient", "webclient");
client.login().doOnCompleted(() -> log.info("Login successful")).subscribe();
engine = HandlebarsTemplateEngine.create();
Router router = Router.router(vertx);
router.routeWithRegex("/(.*)").handler(this::routeHandler).failureHandler(rc -> {
log.error("Error handling request {" + rc.request().absoluteURI() + "}", rc.failure());
rc.response().setStatusCode(500).end();
});
// Finally use the previously set context data to render the templates
router.route().handler(this::templateHandler).failureHandler(rc -> {
log.error("Error while rendering template {" + rc.get("tmplName") + "}", rc.failure());
rc.response().setStatusCode(500).end();
});
vertx.createHttpServer().requestHandler(router::accept).listen(3000);
}
<img style="width: 100%;" class="img-thumbnail" src="{{product.fields.vehicleImage.path }}"">
private void templateHandler(RoutingContext rc) {
String file = DEFAULT_TEMPLATE_DIRECTORY + File.separator + rc.get("tmplName");
engine.render(rc, file, res -> {
if (res.succeeded()) {
rc.response().putHeader(CONTENT_TYPE, DEFAULT_CONTENT_TYPE)
.end(res.result());
} else {
rc.fail(res.cause());
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment