package examples;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.auth.AuthProvider;
import io.vertx.ext.auth.KeyStoreOptions;
import io.vertx.ext.auth.User;
import io.vertx.ext.auth.jwt.JWTAuth;
import io.vertx.ext.auth.jwt.JWTAuthOptions;
import io.vertx.ext.jwt.JWTOptions;
import io.vertx.ext.auth.oauth2.OAuth2Auth;
import io.vertx.ext.auth.oauth2.OAuth2ClientOptions;
import io.vertx.ext.auth.oauth2.OAuth2FlowType;
import io.vertx.ext.auth.oauth2.providers.GithubAuth;
import io.vertx.ext.bridge.BridgeEventType;
import io.vertx.ext.bridge.PermittedOptions;
import io.vertx.ext.web.*;
import io.vertx.ext.web.handler.*;
import io.vertx.ext.web.handler.sockjs.BridgeOptions;
import io.vertx.ext.web.handler.sockjs.SockJSHandler;
import io.vertx.ext.web.handler.sockjs.SockJSHandlerOptions;
import io.vertx.ext.web.sstore.ClusteredSessionStore;
import io.vertx.ext.web.sstore.LocalSessionStore;
import io.vertx.ext.web.sstore.SessionStore;
import io.vertx.ext.web.templ.TemplateEngine;
import java.util.List;
import java.util.Set;
public class WebExamples {
public void example1(Vertx vertx) {
HttpServer server = vertx.createHttpServer();
server.requestHandler(request -> {
HttpServerResponse response = request.response();
response.putHeader("content-type", "text/plain");
response.end("Hello World!");
});
server.listen(8080);
}
public void example2(Vertx vertx) {
HttpServer server = vertx.createHttpServer();
Router router = Router.router(vertx);
router.route().handler(routingContext -> {
HttpServerResponse response = routingContext.response();
response.putHeader("content-type", "text/plain");
response.end("Hello World from Vert.x-Web!");
});
server.requestHandler(router).listen(8080);
}
public void example3(Router router) {
Route route = router.route().path("/some/path/");
route.handler(routingContext -> {
});
}
public void example3_1(Router router) {
Route route = router.route().path("/some/path/*");
route.handler(routingContext -> {
});
}
public void example4(Router router) {
Route route = router.route("/some/path/*");
route.handler(routingContext -> {
});
}
public void example4_1(Router router) {
Route route = router.route(HttpMethod.POST, "/catalogue/products/:producttype/:productid/");
route.handler(routingContext -> {
String productType = routingContext.request().getParam("producttype");
String productID = routingContext.request().getParam("productid");
});
}
public void example5(Router router) {
Route route = router.route().pathRegex(".*foo");
route.handler(routingContext -> {
});
}
public void example6(Router router) {
Route route = router.routeWithRegex(".*foo");
route.handler(routingContext -> {
});
}
public void example6_1(Router router) {
Route route = router.routeWithRegex(".*foo");
route.pathRegex("\\/([^\\/]+)\\/([^\\/]+)").handler(routingContext -> {
String productType = routingContext.request().getParam("param0");
String productID = routingContext.request().getParam("param1");
});
}
public void example6_2(Router router) {
Route route = router.routeWithRegex("\\/(?<productType>[^\\/]+)\\/(?<productId>[^\\/]+)").handler(routingContext -> {
String productType = routingContext.request().getParam("productType");
String productID = routingContext.request().getParam("productId");
});
}
public void example7(Router router) {
Route route = router.route().method(HttpMethod.POST);
route.handler(routingContext -> {
});
}
public void example8(Router router) {
Route route = router.route(HttpMethod.POST, "/some/path/");
route.handler(routingContext -> {
});
}
public void example8_1(Router router) {
router.get().handler(routingContext -> {
});
router.get("/some/path/").handler(routingContext -> {
});
router.getWithRegex(".*foo").handler(routingContext -> {
});
}
public void example9(Router router) {
Route route = router.route().method(HttpMethod.POST).method(HttpMethod.PUT);
route.handler(routingContext -> {
});
}
public void example10(Router router) {
Route route1 = router.route("/some/path/").handler(routingContext -> {
HttpServerResponse response = routingContext.response();
response.setChunked(true);
response.write("route1\n");
routingContext.next();
});
Route route2 = router.route("/some/path/").handler(routingContext -> {
HttpServerResponse response = routingContext.response();
response.write("route2\n");
routingContext.next();
});
Route route3 = router.route("/some/path/").handler(routingContext -> {
HttpServerResponse response = routingContext.response();
response.write("route3");
routingContext.response().end();
});
}
public void example11(Router router) {
Route route1 = router.route("/some/path/").order(1).handler(routingContext -> {
HttpServerResponse response = routingContext.response();
response.write("route1\n");
routingContext.next();
});
Route route2 = router.route("/some/path/").order(0).handler(routingContext -> {
HttpServerResponse response = routingContext.response();
response.setChunked(true);
response.write("route2\n");
routingContext.next();
});
Route route3 = router.route("/some/path/").order(2).handler(routingContext -> {
HttpServerResponse response = routingContext.response();
response.write("route3");
routingContext.response().end();
});
}
public void example12(Router router) {
router.route().consumes("text/html").handler(routingContext -> {
});
}
public void example13(Router router) {
router.route().consumes("text/html").consumes("text/plain").handler(routingContext -> {
});
}
public void example14(Router router) {
router.route().consumes("text/*").handler(routingContext -> {
});
}
public void example15(Router router) {
router.route().consumes("*/json").handler(routingContext -> {
});
}
public void example16(Router router, String someJSON) {
router.route().produces("application/json").handler(routingContext -> {
HttpServerResponse response = routingContext.response();
response.putHeader("content-type", "application/json");
response.write(someJSON).end();
});
}
public void example17(Router router, String whatever) {
router.route().produces("application/json").produces("text/html").handler(routingContext -> {
HttpServerResponse response = routingContext.response();
String acceptableContentType = routingContext.getAcceptableContentType();
response.putHeader("content-type", acceptableContentType);
response.write(whatever).end();
});
}
public void example18(Router router) {
Route route = router.route(HttpMethod.PUT, "myapi/orders")
.consumes("application/json")
.produces("application/json");
route.handler(routingContext -> {
});
}
public void example20(Router router) {
Route route = router.route("/some/path/");
route.handler(routingContext -> {
HttpServerResponse response = routingContext.response();
response.setChunked(true);
response.write("route1\n");
routingContext.vertx().setTimer(5000, tid -> routingContext.next());
});
route.handler(routingContext -> {
HttpServerResponse response = routingContext.response();
response.write("route2\n");
routingContext.vertx().setTimer(5000, tid -> routingContext.next());
});
route.handler(routingContext -> {
HttpServerResponse response = routingContext.response();
response.write("route3");
routingContext.response().end();
});
}
public void example20_1(Router router, SomeLegacyService service) {
router.route().blockingHandler(routingContext -> {
service.doSomethingThatBlocks();
routingContext.next();
});
}
public void example20_2(Router router) {
router.post("/some/endpoint").handler(ctx -> {
ctx.request().setExpectMultipart(true);
ctx.next();
}).blockingHandler(ctx -> {
});
}
interface SomeLegacyService {
void doSomethingThatBlocks();
}
public void example21(Router router) {
router.get("/some/path").handler(routingContext -> {
routingContext.put("foo", "bar");
routingContext.next();
});
router.get("/some/path/other").handler(routingContext -> {
String bar = routingContext.get("foo");
routingContext.response().end();
});
}
public void example22(Vertx vertx, String productJSON) {
Router restAPI = Router.router(vertx);
restAPI.get("/products/:productID").handler(rc -> {
rc.response().write(productJSON);
});
restAPI.put("/products/:productID").handler(rc -> {
rc.response().end();
});
restAPI.delete("/products/:productID").handler(rc -> {
rc.response().end();
});
}
public void example23(Vertx vertx, Handler<RoutingContext> myStaticHandler, Handler<RoutingContext> myTemplateHandler) {
Router mainRouter = Router.router(vertx);
mainRouter.route("/static/*").handler(myStaticHandler);
mainRouter.route(".*\\.templ").handler(myTemplateHandler);
}
public void example24(Router mainRouter, Router restAPI) {
mainRouter.mountSubRouter("/productsAPI", restAPI);
}
public void example25(Router router) {
Route route = router.get("/somepath/*");
route.failureHandler(frc -> {
});
}
public void example26(Router router) {
Route route1 = router.get("/somepath/path1/");
route1.handler(routingContext -> {
throw new RuntimeException("something happened!");
});
Route route2 = router.get("/somepath/path2");
route2.handler(routingContext -> {
routingContext.fail(403);
});
Route route3 = router.get("/somepath/*");
route3.failureHandler(failureRoutingContext -> {
int statusCode = failureRoutingContext.statusCode();
HttpServerResponse response = failureRoutingContext.response();
response.setStatusCode(statusCode).end("Sorry! Not today");
});
}
public void example27(Router router) {
router.route().handler(BodyHandler.create());
}
public void example28(Router router) {
router.route().handler(BodyHandler.create());
router.post("/some/path/uploads").handler(routingContext -> {
Set<FileUpload> uploads = routingContext.fileUploads();
});
}
public void example29(Router router) {
router.route().handler(CookieHandler.create());
}
public void example30(Router router) {
router.route().handler(CookieHandler.create());
router.route("some/path/").handler(routingContext -> {
Cookie someCookie = routingContext.getCookie("mycookie");
String cookieValue = someCookie.getValue();
routingContext.addCookie(Cookie.cookie("othercookie", "somevalue"));
});
}
public void example31(Vertx vertx) {
SessionStore store1 = LocalSessionStore.create(vertx);
SessionStore store2 = LocalSessionStore.create(vertx, "myapp3.sessionmap");
SessionStore store3 = LocalSessionStore.create(vertx, "myapp3.sessionmap", 10000);
}
public void example32() {
Vertx.clusteredVertx(new VertxOptions().setClustered(true), res -> {
Vertx vertx = res.result();
SessionStore store1 = ClusteredSessionStore.create(vertx);
SessionStore store2 = ClusteredSessionStore.create(vertx, "myclusteredapp3.sessionmap");
});
}
public void example33(Vertx vertx) {
Router router = Router.router(vertx);
router.route().handler(CookieHandler.create());
SessionStore store = ClusteredSessionStore.create(vertx);
SessionHandler sessionHandler = SessionHandler.create(store);
router.route().handler(sessionHandler);
router.route("/somepath/blah/").handler(routingContext -> {
Session session = routingContext.session();
session.put("foo", "bar");
});
}
public void example34(SessionHandler sessionHandler, Router router) {
router.route().handler(CookieHandler.create());
router.route().handler(sessionHandler);
router.route("/somepath/blah").handler(routingContext -> {
Session session = routingContext.session();
session.put("foo", "bar");
int age = session.get("age");
JsonObject obj = session.remove("myobj");
});
}
public void example37(Vertx vertx, AuthProvider authProvider, Router router) {
router.route().handler(CookieHandler.create());
router.route().handler(SessionHandler.create(LocalSessionStore.create(vertx)));
AuthHandler basicAuthHandler = BasicAuthHandler.create(authProvider);
}
public void example38(Vertx vertx, AuthProvider authProvider, Router router) {
router.route().handler(CookieHandler.create());
router.route().handler(SessionHandler.create(LocalSessionStore.create(vertx)).setAuthProvider(authProvider));
AuthHandler basicAuthHandler = BasicAuthHandler.create(authProvider);
router.route("/private/*").handler(basicAuthHandler);
router.route("/someotherpath").handler(routingContext -> {
});
router.route("/private/somepath").handler(routingContext -> {
boolean isAuthenticated = routingContext.user() != null;
});
}
public void example39(Vertx vertx, AuthProvider authProvider, Router router) {
router.route().handler(CookieHandler.create());
router.route().handler(SessionHandler.create(LocalSessionStore.create(vertx)).setAuthProvider(authProvider));
AuthHandler redirectAuthHandler = RedirectAuthHandler.create(authProvider);
router.route("/private/*").handler(redirectAuthHandler);
router.post("/login").handler(FormLoginHandler.create(authProvider));
router.route().handler(StaticHandler.create());
router.route("/someotherpath").handler(routingContext -> {
});
router.route("/private/somepath").handler(routingContext -> {
boolean isAuthenticated = routingContext.user() != null;
});
}
public void example40(AuthProvider authProvider, Router router) {
AuthHandler listProductsAuthHandler = RedirectAuthHandler.create(authProvider);
listProductsAuthHandler.addAuthority("list_products");
router.route("/listproducts/*").handler(listProductsAuthHandler);
AuthHandler settingsAuthHandler = RedirectAuthHandler.create(authProvider);
settingsAuthHandler.addAuthority("role:admin");
router.route("/private/settings/*").handler(settingsAuthHandler);
}
public void example41(Router router) {
router.route("/static/*").handler(StaticHandler.create());
}
public void example41_0_1(Router router) {
router.route().handler(CorsHandler.create("vertx\\.io").allowedMethod(HttpMethod.GET));
router.route().handler(routingContext -> {
});
}
public void example41_2(Router router, TemplateEngine engine) {
TemplateHandler handler = TemplateHandler.create(engine);
router.get("/dynamic").handler(routingContext -> {
routingContext.put("request_path", routingContext.request().path());
routingContext.put("session_data", routingContext.session().data());
routingContext.next();
});
router.get("/dynamic/").handler(handler);
}
public void example41_3(Router router) {
router.route("/somepath/").failureHandler(ErrorHandler.create());
}
public void example42(Router router) {
router.route("/foo/").handler(TimeoutHandler.create(5000));
}
public void example43(Vertx vertx) {
Router router = Router.router(vertx);
SockJSHandlerOptions options = new SockJSHandlerOptions().setHeartbeatInterval(2000);
SockJSHandler sockJSHandler = SockJSHandler.create(vertx, options);
router.route("/myapp/*").handler(sockJSHandler);
}
public void example44(Vertx vertx) {
Router router = Router.router(vertx);
SockJSHandlerOptions options = new SockJSHandlerOptions().setHeartbeatInterval(2000);
SockJSHandler sockJSHandler = SockJSHandler.create(vertx, options);
sockJSHandler.socketHandler(sockJSSocket -> {
sockJSSocket.handler(sockJSSocket::write);
});
router.route("/myapp/*").handler(sockJSHandler);
}
public void example45(Vertx vertx) {
Router router = Router.router(vertx);
SockJSHandler sockJSHandler = SockJSHandler.create(vertx);
BridgeOptions options = new BridgeOptions();
sockJSHandler.bridge(options);
router.route("/eventbus/*").handler(sockJSHandler);
}
public void example46(Vertx vertx) {
Router router = Router.router(vertx);
SockJSHandler sockJSHandler = SockJSHandler.create(vertx);
PermittedOptions inboundPermitted1 = new PermittedOptions().setAddress("demo.orderMgr");
PermittedOptions inboundPermitted2 = new PermittedOptions().setAddress("demo.persistor")
.setMatch(new JsonObject().put("action", "find")
.put("collection", "albums"));
PermittedOptions inboundPermitted3 = new PermittedOptions().setMatch(new JsonObject().put("wibble", "foo"));
PermittedOptions outboundPermitted1 = new PermittedOptions().setAddress("ticker.mystock");
PermittedOptions outboundPermitted2 = new PermittedOptions().setAddressRegex("news\\..+");
BridgeOptions options = new BridgeOptions().
addInboundPermitted(inboundPermitted1).
addInboundPermitted(inboundPermitted1).
addInboundPermitted(inboundPermitted3).
addOutboundPermitted(outboundPermitted1).
addOutboundPermitted(outboundPermitted2);
sockJSHandler.bridge(options);
router.route("/eventbus/*").handler(sockJSHandler);
}
public void example47() {
PermittedOptions inboundPermitted = new PermittedOptions().setAddress("demo.orderService");
inboundPermitted.setRequiredAuthority("place_orders");
BridgeOptions options = new BridgeOptions().addInboundPermitted(inboundPermitted);
}
public void example48(Vertx vertx, AuthProvider authProvider) {
Router router = Router.router(vertx);
PermittedOptions inboundPermitted = new PermittedOptions().setAddress("demo.orderService");
inboundPermitted.setRequiredAuthority("place_orders");
SockJSHandler sockJSHandler = SockJSHandler.create(vertx);
sockJSHandler.bridge(new BridgeOptions().
addInboundPermitted(inboundPermitted));
router.route().handler(CookieHandler.create());
router.route().handler(SessionHandler.create(LocalSessionStore.create(vertx)));
AuthHandler basicAuthHandler = BasicAuthHandler.create(authProvider);
router.route("/eventbus/*").handler(basicAuthHandler);
router.route("/eventbus/*").handler(sockJSHandler);
}
public void example48_1(Vertx vertx) {
Router router = Router.router(vertx);
PermittedOptions inboundPermitted = new PermittedOptions().setAddress("demo.orderService");
SockJSHandler sockJSHandler = SockJSHandler.create(vertx);
BridgeOptions options = new BridgeOptions().addInboundPermitted(inboundPermitted);
sockJSHandler.bridge(options, be -> {
if (be.type() == BridgeEventType.PUBLISH || be.type() == BridgeEventType.SEND) {
JsonObject headers = new JsonObject().put("header1", "val").put("header2", "val2");
JsonObject rawMessage = be.getRawMessage();
rawMessage.put("headers", headers);
be.setRawMessage(rawMessage);
}
be.complete(true);
});
router.route("/eventbus/*").handler(sockJSHandler);
}
public void example49(Vertx vertx) {
Router router = Router.router(vertx);
PermittedOptions inboundPermitted = new PermittedOptions().setAddress("demo.someService");
SockJSHandler sockJSHandler = SockJSHandler.create(vertx);
BridgeOptions options = new BridgeOptions().addInboundPermitted(inboundPermitted);
sockJSHandler.bridge(options, be -> {
if (be.type() == BridgeEventType.PUBLISH || be.type() == BridgeEventType.RECEIVE) {
if (be.getRawMessage().getString("body").equals("armadillos")) {
be.complete(false);
return;
}
}
be.complete(true);
});
router.route("/eventbus/*").handler(sockJSHandler);
}
public void handleSocketIdle(Vertx vertx, PermittedOptions inboundPermitted) {
Router router = Router.router(vertx);
SockJSHandler sockJSHandler = SockJSHandler.create(vertx);
BridgeOptions options = new BridgeOptions().addInboundPermitted(inboundPermitted).setPingTimeout(5000);
sockJSHandler.bridge(options, be -> {
if (be.type() == BridgeEventType.SOCKET_IDLE) {
}
be.complete(true);
});
router.route("/eventbus/*").handler(sockJSHandler);
}
public void example50(Vertx vertx) {
Router router = Router.router(vertx);
JWTAuthOptions authConfig = new JWTAuthOptions()
.setKeyStore(new KeyStoreOptions()
.setType("jceks")
.setPath("keystore.jceks")
.setPassword("secret"));
JWTAuth authProvider = JWTAuth.create(vertx, authConfig);
router.route("/login").handler(ctx -> {
if ("paulo".equals(ctx.request().getParam("username")) && "secret".equals(ctx.request().getParam("password"))) {
ctx.response().end(authProvider.generateToken(new JsonObject().put("sub", "paulo"), new JWTOptions()));
} else {
ctx.fail(401);
}
});
}
public void example51(Vertx vertx) {
Router router = Router.router(vertx);
JWTAuthOptions authConfig = new JWTAuthOptions()
.setKeyStore(new KeyStoreOptions()
.setType("jceks")
.setPath("keystore.jceks")
.setPassword("secret"));
JWTAuth authProvider = JWTAuth.create(vertx, authConfig);
router.route("/protected/*").handler(JWTAuthHandler.create(authProvider));
router.route("/protected/somepage").handler(ctx -> {
});
}
public void example52(Vertx vertx) {
JWTAuthOptions authConfig = new JWTAuthOptions()
.setKeyStore(new KeyStoreOptions()
.setType("jceks")
.setPath("keystore.jceks")
.setPassword("secret"));
JWTAuth authProvider = JWTAuth.create(vertx, authConfig);
authProvider.generateToken(new JsonObject().put("sub", "paulo").put("someKey", "some value"), new JWTOptions());
}
public void example53(Vertx vertx) {
Handler<RoutingContext> handler = rc -> {
String theSubject = rc.user().principal().getString("sub");
String someKey = rc.user().principal().getString("someKey");
};
}
public void example54(Router router) {
router.route().handler(CookieHandler.create());
router.route().handler(CSRFHandler.create("abracadabra"));
router.route().handler(rc -> {
});
}
public void example55(Router router) {
router.get("/some/path").handler(routingContext -> {
routingContext.put("foo", "bar");
routingContext.next();
});
router.get("/some/path/B").handler(routingContext -> routingContext.response().end());
router.get("/some/path").handler(routingContext -> routingContext.reroute("/some/path/B"));
}
public void example55b(Router router) {
router.get("/my-pretty-notfound-handler").handler(ctx -> ctx.response()
.setStatusCode(404)
.end("NOT FOUND fancy html here!!!"));
router.get().failureHandler(ctx -> {
if (ctx.statusCode() == 404) {
ctx.reroute("/my-pretty-notfound-handler");
} else {
ctx.next();
}
});
}
public void example55c(Router router) {
router.get("/final-target").handler(ctx -> {
});
router.get().handler(ctx -> ctx.reroute("/final-target?variable=value"));
router.get().handler(ctx -> ctx
.put("variable", "value")
.reroute("/final-target"));
}
public void example56(Router router) {
router.route().handler(VirtualHostHandler.create("*.vertx.io", routingContext -> {
}));
}
public void example57(Router router) {
Route route = router.get("/localized").handler(rc -> {
for (LanguageHeader language : rc.acceptableLanguages()) {
switch (language.tag()) {
case "en":
rc.response().end("Hello!");
return;
case "fr":
rc.response().end("Bonjour!");
return;
case "pt":
rc.response().end("Olá!");
return;
case "es":
rc.response().end("Hola!");
return;
}
}
rc.response().end("Sorry we don't speak: " + rc.preferredLanguage());
});
}
public void example58(Vertx vertx, Router router) {
OAuth2Auth authProvider = GithubAuth.create(vertx, "CLIENT_ID", "CLIENT_SECRET");
OAuth2AuthHandler oauth2 = OAuth2AuthHandler.create(authProvider, "https://myserver.com/callback");
oauth2.setupCallback(router.route());
router.route("/protected/*").handler(oauth2);
router.route("/protected/somepage").handler(rc -> rc.response().end("Welcome to the protected resource!"));
router.get("/").handler(ctx -> ctx.response().putHeader("content-type", "text/html").end("Hello<br><a href=\"/protected/somepage\">Protected by Github</a>"));
}
public void example59(Vertx vertx, Router router) {
OAuth2Auth authProvider = OAuth2Auth.create(vertx, OAuth2FlowType.AUTH_CODE, new OAuth2ClientOptions()
.setClientID("CLIENT_ID")
.setClientSecret("CLIENT_SECRET")
.setSite("https://accounts.google.com")
.setTokenPath("https://www.googleapis.com/oauth2/v3/token")
.setAuthorizationPath("/o/oauth2/auth"));
OAuth2AuthHandler oauth2 = OAuth2AuthHandler.create(authProvider, "http://localhost:8080");
oauth2.addAuthority("profile");
oauth2.setupCallback(router.get("/callback"));
router.route("/protected/*").handler(oauth2);
router.route("/protected/somepage").handler(rc -> rc.response().end("Welcome to the protected resource!"));
router.get("/").handler(ctx -> ctx.response().putHeader("content-type", "text/html").end("Hello<br><a href=\"/protected/somepage\">Protected by Google</a>"));
}
public void example61(Vertx vertx, Router router, OAuth2Auth provider) {
OAuth2AuthHandler oauth2 = OAuth2AuthHandler.create(provider, "https://myserver.com:8447/callback");
oauth2.setupCallback(router.route());
}
public void example62(Vertx vertx, Router router) {
router.route()
.handler(CookieHandler.create());
OAuth2Auth authProvider =
GithubAuth.create(vertx, "YOUR PROVIDER CLIENTID", "YOUR PROVIDER CLIENT SECRET");
router.route()
.handler(SessionHandler.create(LocalSessionStore.create(vertx)).setAuthProvider(authProvider));
router.route("/protected").handler(
OAuth2AuthHandler.create(authProvider)
.setupCallback(router.route("/callback"))
.addAuthority("user:email")
);
router.get("/").handler(ctx -> ctx.response()
.putHeader("Content-Type", "text/html")
.end(
"<html>\n" +
" <body>\n" +
" <p>\n" +
" Well, hello there!\n" +
" </p>\n" +
" <p>\n" +
" We're going to the protected resource, if there is no\n" +
" user in the session we will talk to the GitHub API. Ready?\n" +
" <a href=\"/protected\">Click here</a> to begin!</a>\n" +
" </p>\n" +
" <p>\n" +
" <b>If that link doesn't work</b>, remember to provide\n" +
" your own <a href=\"https://github.com/settings/applications/new\">\n" +
" Client ID</a>!\n" +
" </p>\n" +
" </body>\n" +
"</html>"));
router.get("/protected").handler(ctx -> {
User user = ctx.user();
ctx.response().end(user.toString());
});
}
public void manualContentType(Router router) {
router.get("/api/books").produces("application/json").handler(rc -> findBooks(ar -> {
if (ar.succeeded()) {
rc.response().putHeader("Content-Type", "application/json").end(toJson(ar.result()));
} else {
rc.fail(ar.cause());
}
}));
}
public void contentTypeHandler(Router router) {
router.route("/api/*").handler(ResponseContentTypeHandler.create());
router.get("/api/books").produces("application/json").handler(rc -> findBooks(ar -> {
if (ar.succeeded()) {
rc.response().end(toJson(ar.result()));
} else {
rc.fail(ar.cause());
}
}));
}
private void findBooks(Handler<AsyncResult<List<Book>>> handler) {
throw new UnsupportedOperationException();
}
class Book {
}
Buffer toJson(List<Book> books) {
throw new UnsupportedOperationException();
}
Buffer toXML(List<Book> books) {
throw new UnsupportedOperationException();
}
public void mostAcceptableContentTypeHandler(Router router) {
router.route("/api/*").handler(ResponseContentTypeHandler.create());
router.get("/api/books").produces("text/xml").produces("application/json").handler(rc -> findBooks(ar -> {
if (ar.succeeded()) {
if (rc.getAcceptableContentType().equals("text/xml")) {
rc.response().end(toXML(ar.result()));
} else {
rc.response().end(toJson(ar.result()));
}
} else {
rc.fail(ar.cause());
}
}));
}
public void example63(Router router, AuthProvider provider) {
ChainAuthHandler chain = ChainAuthHandler.create();
chain.append(BasicAuthHandler.create(provider));
chain.append(RedirectAuthHandler.create(provider));
router.route("/secure/resource").handler(chain);
router.route("/secure/resource").handler(ctx -> {
});
}
}